2
如何定義sympy中函數的導數的數值評估? 我有一些函數可以用函數樣條描述,它是使用scipy.interpolate的派生函數。 我想用這個函數處理一些表達式,然後用樣條線來評估表達式。定義sympy函數的導數的數字評估
我可以使用lambdify使sympy函數以數值形式評估爲樣條。 但是,我怎樣才能定義一個sympy函數的派生數值評估爲樣條?
E.g.
import sympy as sp
import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline
from sympy.ultilitis.lambdify import implemented_function, lambdify
r = sp.symbols('r')
B = sp.symbols('B', cls=sp.Function)
B_spline = InterpolatedUnivariateSpline([1,2,3,4],[1,4,9,16])
B_der_spline = InterpolatedUnivariateSpline([1,2,3,4],[2,4,6,8])
B = implemented_function(B, lambda r: B_spline(r))
class A(sp.Function):
nargs = 2
@classfunction
def eval(cls, r, B):
return r**2*B(r)
A_eval = lambdify(r, A(r,B))
A_eval(3)
>>> 81.0
A_diff_eval = lambdify(r, sp.diff(A(r,B)))
A_diff_eval(3)
>>> NameError: global name 'Derivative' is not defined