我想使用Python查找任何顺序的Chebysev多项式的根。我已经看到similar threads用于Legendre多项式。但是,我使用here定义为
的方法构造了多项式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])
我尝试使用np.roots(f4)
,但收到以下错误:TypeError: float() argument must be a string or a number, not 'function'
。此外,it seems that即使可以,也不适用于高阶多项式。
答案 0 :(得分:2)
您可以通过使用“基本求值” here下的方法找到切比雪夫多项式的系数,然后在反向列表中使用np.roots
来生成多项式的根来实现
使用np.roots(f4)
无效,因为roots
函数仅接受多项式系数列表,而不接受lambda函数。
代码:
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]))
输出:
[]
[0.]
[ 0.70710678 -0.70710678]
[ 0.8660254 -0.8660254 0. ]
[-0.92387953 0.92387953 -0.38268343 0.38268343]
希望有帮助。