简体   繁体   English

在python中找到切比雪夫多项式的根

[英]Finding the Roots of Chebyshev's polynomials in python

I want to find the roots of the Chebysev polynomial of any order using Python.我想使用 Python 找到任意阶的 Chebysev 多项式的根。 I have seen similar threads for Legendre polynomials.我见过类似的勒让德多项式线程 However, I have constructed my polynomials using the method defined here as但是,我使用此处定义的方法构建了多项式

import numpy as np 
import sympy as sp 

f0 = lambda x: chebyt(0,x)
f1 = lambda x: chebyt(1,x)
f2 = lambda x: chebyt(2,x)
f3 = lambda x: chebyt(3,x)
f4 = lambda x: chebyt(4,x)
plot([f0,f1,f2,f3,f4],[-1,1])

I have tried to use np.roots(f4) , but I receive the following error: TypeError: float() argument must be a string or a number, not 'function' .我尝试使用np.roots(f4) ,但收到以下错误: TypeError: float() argument must be a string or a number, not 'function' Additionally, it seems that even if I could, it wouldn't work for high order polynomials.此外, 似乎即使我可以,它也不适用于高阶多项式。

You can do this by finding the coefficients of the Chebyshev polynomials using the method under the heading "Basic Evaluation" here , and then using np.roots on the reversed list to generate the roots of the polynomial.您可以通过使用 此处“基本评估”标题下的方法找到切比雪夫多项式的系数,然后使用反向列表上的np.roots来生成多项式的根来完成此操作。

Using np.roots(f4) wasn't working because the roots function only accepts a list of polynomial coefficients, rather than a lambda function.使用np.roots(f4)不起作用,因为roots函数只接受多项式系数列表,而不是 lambda 函数。

Code:代码:

from mpmath import chebyt, chop, taylor
import numpy as np

for n in range(5):
    print(np.roots(chop(taylor(lambda x: chebyt(n, x), 0, n))[::-1]))

Output:输出:

[]
[0.]
[ 0.70710678 -0.70710678]
[ 0.8660254 -0.8660254  0.       ]
[-0.92387953  0.92387953 -0.38268343  0.38268343]

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM