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?

rlchqrd
  • 519
  • 4
  • 11
  • 2
    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.) – Mark Dickinson Jun 24 '16 at 16:00
  • 1
    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 :-) – mgilson Jun 24 '16 at 16:02
  • 1
    http://docs.scipy.org/doc/numpy/reference/routines.polynomials.html is interesting, too: "Polynomial is recommended for new coding." – Mark Dickinson Jun 24 '16 at 16:05
  • 1
    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. – Charles Harris Jul 03 '16 at 05:41

1 Answers1

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.

obataku
  • 29,212
  • 3
  • 44
  • 57
  • 1
    http://docs.scipy.org/doc/numpy-1.10.1/reference/routines.polynomials.html#transition-notice transition notice acknowledges the reversed order. – hpaulj Jun 24 '16 at 19:44
  • +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... – Rmano Nov 30 '18 at 19:12
  • 1
    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. – mins Apr 10 '23 at 14:04
  • @mins: *np.flip(coeffs_)* to get reversed order – JeeyCi Jul 17 '23 at 03:09