<div dir="ltr">Here's my take on this, but it may not be an accurate summary of the history.<div><br></div><div>`np.poly<func>` is part of the original matlab-style API, built around `poly1d` objects. This isn't a great design, because they represent:<br><br><div>    p(x) = c[0] * x^2 + c[1] * x^1 + c[2] * x^0</div><div><br></div><div>For this reason, among others, the `np.polynomial` module was created, starting with a clean slate. The core of this is `np.polynomial.Polynomial`. There, everything uses the convention</div><br class="inbox-inbox-Apple-interchange-newline">    p(x) = c[0] * x^0 + c[1] * x^1 + c[2] * x^2</div><div><br></div><div>It sounds like we might need clearer docs explaining the difference, and pointing users to the more sensible `np.polynomial.Polynomial`</div><div><br></div><div>Eric<br><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, 29 Jun 2018 at 20:10 Charles R Harris <<a href="mailto:charlesr.harris@gmail.com">charlesr.harris@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jun 29, 2018 at 8:21 PM, Maxwell Aifer <span dir="ltr"><<a href="mailto:maifer@haverford.edu" target="_blank">maifer@haverford.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="m_2800686980799807020gmail-m_-6016144409142987216markdown-here-wrapper"><p style="margin:0px 0px 1.2em">Hi,<br>I noticed some frustrating inconsistencies in the various ways to evaluate polynomials using numpy. Numpy has three ways of evaluating polynomials (that I know of) and each of them has a different syntax:</p>
<ul style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px"><p style="margin:0.5em 0px"><a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.polynomial.polynomial.Polynomial.html#numpy.polynomial.polynomial.Polynomial" target="_blank">numpy.polynomial.polynomial.Polynomial</a>: You define a polynomial by a list of coefficients <strong>in order of increasing degree</strong>, and then use the class’s call() function.</p>
</li>
<li style="margin:0.5em 0px"><p style="margin:0.5em 0px"><a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.polyval.html" target="_blank">np.polyval</a>: Evaluates a polynomial at a point. <em>First</em> argument is the polynomial, or list of coefficients <strong>in order of decreasing degree</strong>, and the <em>second</em> argument is the point to evaluate at.</p>
</li>
<li style="margin:0.5em 0px"><p style="margin:0.5em 0px"><a href="https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.polynomial.polynomial.polyval.html" target="_blank">np.polynomial.polynomial.polyval</a>: Also evaluates a polynomial at a point, but has more support for vectorization. <em>First</em> argument is the point to evaluate at, and <em>second</em> argument the list of coefficients <strong>in order of increasing degree</strong>.</p>
</li>
</ul>
<p style="margin:0px 0px 1.2em">Not only the order of arguments is changed between different methods, but the order of the coefficients is reversed as well, leading to puzzling bugs (in my experience). What could be the reason for this madness? As polyval is a shameless ripoff of <a href="https://www.mathworks.com/help/matlab/ref/polyval.html" target="_blank">Matlab’s function of the same name</a> anyway, why not just use matlab’s syntax (<code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">polyval([c0, c1, c2...], x)</code>) across the board?</p>
<div title="MDH:SGksPGRpdj5JIG5vdGljZWQgc29tZSBmcnVzdHJhdGluZyBpbmNvbnNpc3RlbmNpZXMgaW4gdGhl
IHZhcmlvdXMgd2F5cyB0byBldmFsdWF0ZSBwb2x5bm9taWFscyB1c2luZyA8ZyBjbGFzcz0iZ3Jf
IGdyXzE5NiBnci1hbGVydCBncl9zcGVsbCBncl9pbmxpbmVfY2FyZHMgZ3JfZGlzYWJsZV9hbmlt
X2FwcGVhciBDb250ZXh0dWFsU3BlbGxpbmciIGlkPSIxOTYiIGRhdGEtZ3ItaWQ9IjE5NiI+bnVt
cHk8L2c+LiBOdW1weSBoYXMgdGhyZWUgd2F5cy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3d3dy5tYXRod29ya3MuY29tL2hlbHAvbWF0bGFiL3JlZi9wb2x5dmFs
Lmh0bWwpIGFueXdheSwgd2h5IG5vdCBqdXN0IHVzZSA8ZyBjbGFzcz0iZ3JfIGdyXzM0MTMgZ3It
YWxlcnQgZ3Jfc3BlbGwgZ3JfaW5saW5lX2NhcmRzIGdyX3J1bl9hbmltIENvbnRleHR1YWxTcGVs
bGluZyBpbnMtZGVsIG11bHRpUmVwbGFjZSIgaWQ9IjM0MTMiIGRhdGEtZ3ItaWQ9IjM0MTMiPm1h
dGxhYidzPC9nPiBzeW50YXggKDxnIGNsYXNzPSJncl8gZ3JfMzQxNSBnci1hbGVydCBncl9zcGVs
bCBncl9pbmxpbmVfY2FyZHMgZ3JfcnVuX2FuaW0gQ29udGV4dHVhbFNwZWxsaW5nIiBpZD0iMzQx
NSIgZGF0YS1nci1pZD0iMzQxNSI+YGBgcG9seXZhbDwvZz4oW2MwLCBjMSwgYzIuLi5dLCB4KWBg
YCkgYWNyb3NzIHRoZSBib2FyZD88L3NwYW4+PC9kaXY+PGRpdj48YnI+PC9kaXY+" style="height:0px;width:0px;max-height:0px;max-width:0px;overflow:hidden;font-size:0em;padding:0px;margin:0px">​</div></div></div>
<br></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>The polynomial package, with its various basis, deals with series, and especially with the truncated series approximations that are used in numerical work. Series are universally written in increasing order of the degree. The Polynomial class is efficient in a single variable, while the num<span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">py.polynomial.polynomial.polyval function is intended as a building block and can also deal with multivariate polynomials or multidimensional arrays of polynomials, or a mix. See the simple implementation of polyval3d for an example. If you are just dealing with a single variable, use Polynomial, which will also track scaling and offsets for numerical stability and is generally much superior to the simple polyval function from a numerical point of view.</span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">As to the ordering of the degrees, learning that the degree matches the index is pretty easy and is a more natural fit for the implementation code, especially as the number of variables increases. I note that Matlab has ones based indexing, so that was really not an option for them.</span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Chuck</span></div></div></div></div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>