python 导数 vector_Python。Scipy数值导数

不久前我发现了scipy.misc.derivative。在def derivative(func, x0, dx=1.0, n=1, args=(), order=3)

令人难以置信的是它的速度和精确度。

所以,我决定弄清楚它是怎么工作的。

但是我不完全理解这个函数的代码,它在derivative中使用:

^{pr2}$

据我所知,这个函数在高阶导数的展开式中找到系数,但是它是如何做到的呢?在

代码:def central_diff_weights(Np, ndiv=1):

"""

Return weights for an Np-point central derivative.

Assumes equally-spaced function points.

If weights are in the vector w, then

derivative is w[0] * f(x-ho*dx) + ... + w[-1] * f(x+h0*dx)

Parameters

----------

Np : int

Number of points for the central derivative.

ndiv : int, optional

Number of divisions. Default is 1.

Notes

-----

Can be inaccurate for large number of points.

"""

if Np < ndiv + 1:

raise ValueError("Number of points must be at least the derivative order

+ 1.")

if Np % 2 == 0:

raise ValueError("The number of points must be odd.")

from scipy import linalg

ho = Np >> 1

x = arange(-ho,ho+1.0)

x = x[:,newaxis]

X = x**0.0

for k in range(1,Np):

X = hstack([X,x**k])

w = product(arange(1,ndiv+1),axis=0)*linalg.inv(X)[ndiv]

return w

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值