Why is Numpy inconsistent in ordering polynomial coefficients by degree?
Asked Answered
L

1

5

numpy.polynomial.polynomial.Polynomial stores polynomial coefficients in order of increasing degree, while numpy.poly1d stores polynomial coefficients in order of decreasing degree.

Is there a reason for this difference? Is there an advantage to either approach?

Loopy answered 24/6, 2016 at 15:57 Comment(4)
There are clear advantages to both approaches: decreasing degree is more user-friendly, in that matches the way that most people write polynomials. Increasing degree means that the index matches the monomial exponent, which is convenient for calculations. (Personally, I much prefer the increasing degree convention.)Inhume
I think that the more surprising thing is that they have two classes for representing polynomials... It seems like they'd pick one and stick with it :-)Yurev
docs.scipy.org/doc/numpy/reference/routines.polynomials.html is interesting, too: "Polynomial is recommended for new coding."Inhume
The polynomial package deals with truncated series in different polynomial basis, so in that sense the increasing degree mimics how one writes series. Also, from a numerical standpoint, one hopes that the coefficients become smaller and less significant as the degree increases so it makes sense to put them at the end where they can be ignored ;) As said above, it is also convenient to have the index correspond to the degree.Stamp
D
8

According to the SciPy reference on NumPy:

Prior to NumPy 1.4, numpy.poly1d was the class of choice and it is still available in order to maintain backward compatibility. However, the newer Polynomial package is more complete than numpy.poly1d and its convenience classes are better behaved in the numpy environment. Therefore Polynomial is recommended for new coding.

Dor answered 24/6, 2016 at 16:5 Comment(4)
docs.scipy.org/doc/numpy-1.10.1/reference/… transition notice acknowledges the reversed order.Electrodynamics
+1, thanks, I was getting crazy over that. I suppose that the scipy.signals.bessel and co-functions are still working with the "old" order...Shayne
That doesn't explain why the order of coefficients is for increasing powers in Polynomial package while it is for decreasing powers in scipy.signal. E.g. when dealing with DSP, Scipy has functions like lfilter expecting coefficients for powers in decreasing order, z^0, z^-1, etc. When calling numpy polyval the coefficients must be passed in reversed order because numpy uses them for terms z^0, z^1, etc. In addition the result must be divided by z^n, the polynomial degree to compensate. So from this standpoint only, the new package is not more friendly to Scipy, it's worse.Pelagias
@mins: np.flip(coeffs_) to get reversed orderThriftless

© 2022 - 2024 — McMap. All rights reserved.