Difference between implementation of Chebyshev polynomial in scipy and numpy
1 answer
Polynomial functions scipy.special
use np.poly1d
, which is deprecated and error prone - in particular, it stores the index x0
inpoly.coeffs[-1]
numpy.polynomial.Chebyshev
not only store the coefficients in a more reasonable order, but also store them in terms of their basis, which improves accuracy. You can convert using the method cast
:
>>> from numpy.polynomial import Chebyshev, Polynomial
# note loss of precision
>>> sc_che = scipy.special.chebyt(4); sc_che
poly1d([ 8.000000e+00, 0.000000e+00, -8.000000e+00, 8.881784e-16, 1.000000e+00])
# using the numpy functions - note that the result is just in terms of basis 4
>>> np_che = Chebyshev.basis(4); np_che
Chebyshev([ 0., 0., 0., 0., 1.], [-1., 1.], [-1., 1.])
# converting to a standard polynomial - note that these store the
# coefficient of x^i in .coeffs[i] - so are reversed when compared to above
>>> Polynomial.cast(np_che)
Polynomial([ 1., 0., -8., 0., 8.], [-1., 1.], [-1., 1.])
+4
source to share