define numerical evaluation of a derivative of a sympy function

一笑奈何 提交于 2019-12-01 00:46:23

SymPy doesn't know how to take the derivative of the spline function, since it only has the numeric version of it from scipy.

Also, A here could just be a Python function, since you never don't evaluate it. That also makes more sense in that passing a function as an argument to a SymPy function is a bit odd.

All implemented_function does is symfunc._imp_ = staticmethod(implementation) (here symfunc = B and implementation = lambda r: B_spline(r)). You will also need to add fdiff so that it returns a new SymPy Function for B_der_spline. Something like

class B_spline_sym(Function):
    _imp_ = staticmethod(B_spline)

    def fdiff(self, argindex=1):
        return B_der_spline_sym(self.args[0])

class B_der_spline_sym(Function):
    _imp_ = staticmethod(B_der_spline)

def A(r, B):
    return r**2*B(r)

Giving

In [87]: B = B_spline_sym

In [88]:  A_eval = lambdify(r, A(r,B))

In [89]:  A_eval(3)
Out[89]: 81.0

In [91]:  A_diff_eval = lambdify(r, sp.diff(A(r,B)))

In [92]:  A_diff_eval(3)
Out[92]: 108.0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!