python中的Laguerre多项式使用scipy,缺乏收敛性?

时间:2014-10-21 03:22:25

标签: python scipy arbitrary-precision

laguerre多项式似乎没有在某些阶上收敛,这可以通过运行以下代码来证明。

import numpy as np
from sympy import mpmath as mp
from scipy.special import genlaguerre as genlag
from sympy.mpmath import laguerre as genlag2
from matplotlib import pyplot as plt

def laguerre(x, r_ord, phi_ord, useArbitraryPrecision=False):
    if (r_ord < 30 and phi_ord < 30) and not useArbitraryPrecision:
        polyCoef = genlag(r_ord, phi_ord)
        out = np.polyval(polyCoef, x)
    else:
        fun = lambda arg: genlag2(r_ord, phi_ord, arg)
        fun2 = np.frompyfunc(genlag2, 3, 1)
        # fun2 = np.vectorize(fun)
        out = fun2(r_ord, phi_ord, x)
    return out

r_ord = 29
phi_ord = 29
f = lambda x, useArb : mp.log10(laguerre(x, 29, 29, useArb))
mp.plot(lambda y : f(y, True) - f(y, False), [0, 200], points = 1e3)
plt.show()

我想知道是否有人知道发生了什么或scipy功能有任何准确性限制?您是否建议我只使用mpmath功能?起初我认为可能是在某个订单之后它不起作用但是(100,100)似乎工作得很好。

运行

mp.plot([lambda y : f(y, True), lambda y: f(y, False)], [0, 200], points = 1e3)

你会得到以下图像,其中差异变得非常明显。

enter image description here

任何帮助表示感谢。

如果有任何需要澄清,请告诉我。

2 个答案:

答案 0 :(得分:1)

polyval与高阶多项式(约n > 20)一起使用通常是一个坏主意,因为使用系数(以功率为基础)评估多项式将开始在浮点高时产生大误差命令。 Scipy文档中的warning试图告诉你。

您应该使用scipy.special.eval_genlaguerre(r_ord, phi_ord, float(x))代替genlaguerre + polyval;它使用更稳定的数值算法来评估多项式。

答案 1 :(得分:0)

您可以按照NumPy documentation中的说明使用scipy.special.eval_genlaguerre,而不是使用numpy.polynomial.Laguerre来评估高等项多项式。

不幸的是,它似乎没有为广义 Laguerre多项式提供函数。

import numpy as np
from numpy.polynomial import Laguerre
p = Laguerre([1, -2, 1])
x = np.arange(5)
p(x)

NumPy输出:0,0.5,2,4.5,8