define numerical evaluation of a derivative of a sympy function

后端 未结 1 913
春和景丽
春和景丽 2021-01-07 04:36

How can I define the numerical evaluation of a derivative of a function in sympy? I have some functions I can describe with splines for the function and it\'s derivative usi

相关标签:
1条回答
  • 2021-01-07 05:11

    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
    
    0 讨论(0)
提交回复
热议问题