python中使用scipy的Laguerre多项式,缺乏收敛性吗? [英] Laguerre polynomials in python using scipy, lack of convergence?

查看:274
本文介绍了python中使用scipy的Laguerre多项式,缺乏收敛性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拉古尔多项式似乎没有收敛,可以通过运行以下代码来证明。

The laguerre polynomials don't seem to be converging at some orders as can be demonstrated by running the following code.

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)来说似乎还可以。

I was wondering if anyone knew what is going on or of any accuracy limitations of the scipy function? Do you recommend I simply use the mpmath function? At first I thought it might be that after a certain order it doesn't work but for (100, 100) it seems to be working just fine.

通过运行

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

您会看到以下图片,其中的差异变得非常明显。

you get the following image where the discrepancy becomes pretty clear.

任何帮助表示赞赏。

让我知道是否需要澄清。

Let me know if anything needs clarification.

推荐答案

使用 polyval 和高阶多项式(约 n> 20 )通常是个坏主意,因为使用系数(基于幂)评估多项式将开始在高阶浮点运算中产生较大的误差。 Scipy文档中的警告试图告诉您

Using polyval with high-order polynomials (about n > 20) is in general a bad idea, because evaluating polynomial using the coefficients (in power basis) will start giving large errors in floating point at high orders. The warning in the Scipy documentation tries to tell you that.

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

You should use scipy.special.eval_genlaguerre(r_ord, phi_ord, float(x)) instead of genlaguerre + polyval; it uses a stabler numerical algorithm for evaluating the polynomial.

这篇关于python中使用scipy的Laguerre多项式,缺乏收敛性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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