test_finite_diff.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:Python-iBeacon-Scan 作者: NikNitro 项目源码 文件源码
def test_differentiate_finite():
    x, y = symbols('x y')
    f = Function('f')
    res0 = differentiate_finite(f(x, y) + exp(42), x, y)
    xm, xp, ym, yp = [v + sign*S(1)/2 for v, sign in product([x, y], [-1, 1])]
    ref0 = f(xm, ym) + f(xp, yp) - f(xm, yp) - f(xp, ym)
    assert (res0 - ref0).simplify() == 0

    g = Function('g')
    res1 = differentiate_finite(f(x)*g(x) + 42, x)
    ref1 = (-f(x - S(1)/2) + f(x + S(1)/2))*g(x) + \
           (-g(x - S(1)/2) + g(x + S(1)/2))*f(x)
    assert (res1 - ref1).simplify() == 0

    res2 = differentiate_finite(f(x) + x**3 + 42, x, points=[x-1, x+1],
                                evaluate=False)
    ref2 = (f(x + 1) + (x + 1)**3 - f(x - 1) - (x - 1)**3)/2
    assert (res2 - ref2).simplify() == 0
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号