在Python中计算切比雪夫级数的根
要计算多项式的根,可以使用Python Numpy中的chebyshev.chebroots()方法。 该方法返回一组级数的根。如果所有的根都是实数,则输出也是实数,否则为复数。参数c是一个一维数组,表示系数。
根的估计值是通过伴随矩阵的特征值得到的。远离复平面原点的根可能会有较大的误差,这是由于对于这样的值,序列的数值不稳定性造成的。具有多重根的根也会显示出较大的误差,因为在这些点附近,序列的值对根的误差相对不敏感。靠近原点的孤立根可以通过几次牛顿迭代法进行改进。
步骤
首先,导入所需的库 –
from numpy.polynomial import chebyshev as C
在Python的Numpy中,要计算多项式的根,可以使用chebyshev.chebroots()方法。
print("Result (roots)...\n",C.chebroots((-1,0,1)))
获取数据类型 –
print("\nType...\n",C.chebroots((-1,0,1)).dtype)
获取形状 –
print("\nShape...\n",C.chebroots((-1,0,1)).shape)
示例
from numpy.polynomial import chebyshev as C
# To compute the roots of a polynomials, use the chebyshev.chebroots() method in Python Numpy.
# The method returns an array of the roots of the series. If all the roots are real, then out is also real, otherwise it is complex.
# The parameter, c is a 1-D array of coefficients.
print("Result (roots)...\n",C.chebroots((-1,0,1)))
# Get the datatype
print("\nType...\n",C.chebroots((-1,0,1)).dtype)
# Get the shape
print("\nShape...\n",C.chebroots((-1,0,1)).shape)
输出
Result (roots)...
[-1. 1.]
Type...
float64
Shape...
(2,)
请注意: 该脚本是通过 src
属性引入的外部 JavaScript 文件。 v=2.6
是用来指定版本号的参数。