python分段三次埃尔米特插值器的根 [英] roots of piecewise cubic hermite interpolator with python

查看:698
本文介绍了python分段三次埃尔米特插值器的根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些分段三次Hermite插值并获得多项式的 roots . (我以前在matlab中这样做,但现在想在python 3.4中实现).
我尝试使用时髦的PchipInterpolator.插值没问题,但是当我尝试检索根时,出现此错误...
我现在被困在这里,找不到任何解决方法.这是一个简单的代码,再现了我的工作以及确切的错误消息...

I would like to do some piecewise cubic hermite interpolation and get the roots of the polynomials. (I used to do this in matlab but would now like to implement this in python 3.4).
I tried to use the scipy PchipInterpolator. Interpolation is OK but when I tried to retrieve the roots I got this error...
I now am stuck here and could'nt find any workaround. Here is a simple code reproducing what I do and the exact error message...

import matplotlib.pyplot as plt
from scipy import interpolate, __version__
import numpy as np

print('numpy : ' + np.__version__)
print('scipy :' + __version__)

x = np.arange(10)
y = [1., 1., 3., 2., 1., 1., 1.5, 2., 8., 1.]
f = interpolate.PchipInterpolator(x, y, axis=0, extrapolate=None)
print(f.roots()) # this produces an error !

xnew = np.arange(0, 9, 0.1)
ynew = f(xnew)   # use interpolation function returned by `PchipInterpolator`
plt.plot(x, y, 'o', xnew, ynew, '-')
plt.show()

错误消息:

numpy : 1.10.1
scipy :0.17.1
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    print(f.roots()) # this produces an error !
  File "/usr/local/lib/python3.4/dist-packages/scipy/interpolate/_monotone.py", line 105, in roots
    return (PPoly.from_bernstein_basis(self._bpoly)).roots()
AttributeError: 'PchipInterpolator' object has no attribute '_bpoly'

Process finished with exit code 1

但是scipy文档说:"roots()返回插值函数的根."

However scipy docs say : "roots() Return the roots of the interpolated function."

我在这里想念什么?

推荐答案

这是scipy中的错误.

This is a bug in scipy.

(请在 https://github.com/scipy/scipy/issues 上进行报告. )

(Please report it on https://github.com/scipy/scipy/issues)

作为解决方法,您可以手动转换为功率基准:

As a workaround, you can convert to the power basis manually:

In [1]: from scipy.interpolate import pchip

In [2]: import numpy as np

In [3]: x = np.arange(10)

In [4]: y = [1., 1., 3., 2., 1., 1., 1.5, 2., 8., 1.]

In [5]: s = pchip(x, y)

In [6]: from scipy.interpolate import PPoly

In [7]: pp = PPoly.from
PPoly.from_bernstein_basis  PPoly.from_spline

In [7]: pp = PPoly.from_bernstein_basis(s)

In [8]: pp.roots()
Out[8]: array([  9.07179677,  22.92820323])

并且如果确实使用此替代方法,并且您具有非默认值axis,则最好在转换时检查是否已处理轴.如果不是,请也进行报告.

and if do use this workaround and you have non-default axis, best check that the axis is handled when converting. If it's not, please report it too.

这篇关于python分段三次埃尔米特插值器的根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆