3

I am trying to compute the coefficients of the kth Chebyshev polynomial. Let's just set k to 5 for this. So far, I have the following:

a = (0,0,0,0,0,1) #selects the 5th Chebyshev polynomial
p = numpy.polynomial.chebyshev.Chebyshev(a) #type here is Chebyshev
cpoly = numpy.polynomial.chebyshev.cheb2poly(p) #trying to convert to Poly
print cpoly.all_coeffs()

After the second line runs, I have an object of type Chebyshev, as expected. However, the third line fails to convert to a type Poly, and converts to type numpy.ndarray. Thus, I get an error saying that ndarray has no attribute all_coeffs.

Anyone know how to fix this?

ali_m
  • 71,714
  • 23
  • 223
  • 298
N. Mao
  • 499
  • 1
  • 10
  • 19
  • what is 'NP' package ? i guess it stands for umpy... and are you sure cpoly has 'all_coeffs' method ? – Zohar81 Aug 23 '15 at 05:42
  • 2
    Have you checked the docs? for `cheb2poly`: --> `Convert an array representing the coefficients of a Chebyshev series, ordered from lowest degree to highest, to an array of the coefficients of the equivalent polynomial` – cel Aug 23 '15 at 05:45
  • @Zohar yes, it stands for numpy..should've converted that before posting on here. cpoly does not have that method. But I'm not sure how to define cpoly so that it does. – N. Mao Aug 23 '15 at 06:37
  • @cel If cheb2poly is not the right method to call, do you know which is? – N. Mao Aug 23 '15 at 06:37
  • 2
    I think you are simply misusing the method. The docs explicitly state that you should pass coefficients and get coefficients. You actually pass a polynomial object and expect to get a polynomial object, which is simply not what the method does. – cel Aug 23 '15 at 06:50
  • try x=cpoly[0]; x.coef[] – Zohar81 Aug 23 '15 at 07:38

2 Answers2

5

@cel has the right idea in the comments - you need to pass the coefficients of the Chebyshev polynomial to cheb2poly, not the object itself:

import numpy as np

cheb = np.polynomial.chebyshev.Chebyshev((0,0,0,0,0,1))
coef = np.polynomial.chebyshev.cheb2poly(cheb.coef)

print(coef)
# [  0.,   5.,   0., -20.,   0.,  16.]

i.e. 16x5 - 20x3 + 5x. You can confirm that these are the correct coefficients here.

To turn these coefficients into a Polynomial object, you just need to pass the array to the Polynomial constructor:

poly = np.polynomial.Polynomial(coef)
ali_m
  • 71,714
  • 23
  • 223
  • 298
1
In [1]: import numpy.polynomial

In [2]: p = numpy.polynomial.Chebyshev.basis(5)

In [3]: p
Out[3]: Chebyshev([ 0.,  0.,  0.,  0.,  0.,  1.], [-1.,  1.], [-1.,  1.])

In [4]: p.convert(kind=numpy.polynomial.Polynomial)
Out[4]: Polynomial([  0.,   5.,   0., -20.,   0.,  16.], [-1.,  1.], [-1.,  1.])
Charles Harris
  • 934
  • 6
  • 4