Derivatives Discretization with Sympy

697 views Asked by At

Is there a way to discretize the derivative of an unknown function in sympy? I am trying to achieve the following:

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
1

There are 1 answers

0
Bjoern Dahlgren On

After reading this question I have implemented this functionality in Sympy and it is currently available in:

my branch: https://github.com/bjodah/sympy/tree/finite_difference

sympy master (https://github.com/sympy/sympy), and will be availble in 0.7.6

Here is an example:

>>> 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