python求导函数的值_定义sympy函数导数的数值计算

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 using scipy.interpolate.

I want to manipulate some expressions with this function and then evaluate the expressions with the splines.

I can use lambdify to make a sympy function evaluate numerically as a spline.

But how can I define the derivative of a sympy function to evaluate numerically as a spline?

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

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值