Derivatives Discretization with Sympy



有没有办法离散sympy中未知函数的导数?我正在尝试实现以下目标:

from sympy import *
>>> f = Function('f')
>>> x = Symbol('x')
>>> dfdx = Derivative(f(x),x).somemethod()
>>> print dfdx
    (f(x+1) - f(x-1)) / 2
>>> eq = lambdify((f,x),dfdx)
>>> w = np.array([1,5,7,9])
>>> print eq(w,1)
    -3

阅读此问题后,我已经在 Sympy 中实现了此功能,目前可用于:

我的分支:https://github.com/bjodah/sympy/tree/finite_difference

Sympy Master (https://github.com/sympy/sympy),并将在 0.7.6 中可用

下面是一个示例:

>>> from sympy import symbols, Function, as_finite_diff
>>> x, h = symbols('x h')
>>> f = Function('f')
>>> print(as_finite_diff(f(x).diff(x), h))
-f(-h/2 + x)/h + f(h/2 + x)/h

最新更新