【问题标题】:What algorithm used in interp1d function in scipy.interpolatescipy.interpolate 中的 interp1d 函数使用什么算法
【发布时间】:2016-03-20 00:48:52
【问题描述】:

所以我正在为我的数值课程编写一个 python 程序,我不得不编写一个三次样条程序。所以我实现了Numerical methods by Chapra and canale和Numerical mathematics by chenny and kincaid等书中给出的三次样条公式。

所以我的数据是

x=[1.0,3.0,4.0,7.0]
y=[1.5,4.5,9.0,25.5]

使用这些数据并应用三次样条我得到 x=1.5 , y=1.79122340426

虽然使用相同的数据但使用 scipy 函数给出:

  >>> scipy.interpolate.interp1d(x, y, kind='cubic')(1.5)
array(1.265624999999932)

那么,为什么结果会有差异?很明显,他们没有使用相同的公式。 该 scipy 函数中使用的三次样条公式是什么?它是自然三次样条公式还是改进的东西? 注意:1.2656 的值更准确。

【问题讨论】:

    标签: python scipy cubic-spline


    【解决方案1】:

    编辑:此答案的 cmets 中的 @ev-br 为我的答案提供了重要的更正。实际上 interp1D spline 不是基于 FITPACK 的。检查 cmets 以获取 @ev-br 提供的链接。

    曲线拟合的 Scipy 函数基于 FITPACK。尝试查看有关您正在使用的函数的文档,您将能够看到“参考”一章,其中会出现类似的内容:

    Notes
    -----
    See splev for evaluation of the spline and its derivatives. Uses the
    FORTRAN routine curfit from FITPACK.
    If provided, knots `t` must satisfy the Schoenberg-Whitney conditions,
    i.e., there must be a subset of data points ``x[j]`` such that
    ``t[j] < x[j] < t[j+k+1]``, for ``j=0, 1,...,n-k-2``.
    References
    ----------
    Based on algorithms described in [1]_, [2]_, [3]_, and [4]_:
    .. [1] P. Dierckx, "An algorithm for smoothing, differentiation and
       integration of experimental data using spline functions",
       J.Comp.Appl.Maths 1 (1975) 165-184.
    .. [2] P. Dierckx, "A fast algorithm for smoothing data on a rectangular
       grid while using spline functions", SIAM J.Numer.Anal. 19 (1982)
       1286-1304.
    .. [3] P. Dierckx, "An improved algorithm for curve fitting with spline
       functions", report tw54, Dept. Computer Science,K.U. Leuven, 1981.
    .. [4] P. Dierckx, "Curve and surface fitting with splines", Monographs on
       Numerical Analysis, Oxford University Press, 1993.
    

    这些引用特别是从函数“splrep”上的fitpack.py 的源中获取的。如果您需要在您的算法和 interp1D 的样条曲线之间进行非常彻底的比较,只需转到文档:

    scipy.interpolate.interp1d

    您会在函数名称的定义之后看到一个名为 [source] 的链接(例如:scipy.interpolate.interp1D [来源])。请记住,这些函数上有很多常规处理程序,因此在导航源代码时请耐心等待。

    【讨论】:

    • 一般正确,splrep/splev 和 UnivariateSpline 使用 FITPACK,但 interp1d 不是。
    • 如果不是,他们为子模块选择了一些奇怪的名称,因为在 interp1D 源代码中,您可以清楚地看到对“splmake”的调用(如果插值是样条曲线),而 B = _fitpack._bsplmat(订单,xk)。我会将此解释为使用 FITPACK。
    • 不,_bspldismat 没有使用 FITPACK Dierckx fortran 库:-)。
    • 是的,可悲的是,这有点乱。正如您所说的那样,其中大多数是(顺便说一句,我+1)。 interp1d 是个奇怪的人。
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 2018-02-07
    • 2015-10-28
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多