BUG: Creating polynomials from existing polynomial instances incorrectly sets coefficients
See original GitHub issueIn the polynomial package (np.polynomial
), if one attempts to create a new polynomial instance from an existing instance, the entire original polynomial instance is stored in the coef
attribute of the new polynomial instance (in an object array) without any error or warning being raised:
Reproducing code example:
>>> p = np.polynomial.Polynomial([1, 2, 3])
>>> p # expected
Polynomial([1., 2., 3.], domain=[-1, 1], window=[-1, 1])
>>> p2 = np.polynomial.Polynomial(p)
>>> p2 # p2.coef is an object array containing p
Polynomial([Polynomial([1., 2., 3.], domain=[-1, 1], window=[-1, 1])],
dtype=object, domain=[-1, 1], window=[-1, 1])
Some potential solutions come to mind to prevent this from happening:
- Disallow creating new polynomial instances from existing polynomial instances. This may break backwards compatibility
- Construct the new polynomial instance from the state (i.e.
coef
,domain
andwindow
attributes) of the original polynomial.
Numpy/Python version information:
Python version: 3.8.2 NumPy version: 1.18.3
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Chapter 12 Polynomial Regression Models - IIT Kanpur
One possible approach is to successively fit the models in increasing order and test the significance of regression coefficients at each step of...
Read more >polynomials - Australian Mathematical Sciences Institute
An application of polynomials to error-correcting codes ... We can add, subtract and multiply two or more polynomials together to obtain another polynomial....
Read more >1.1. Example: Polynomial Curve Fitting
The values of the coefficients will be determined by fitting the polynomial to the training data. This can be done by minimizing an...
Read more >How to interpret coefficients from a polynomial model fit?
My detailed answer is below, but the general (i.e. real) answer to this kind of question is: 1) experiment, mess around, look at...
Read more >How to Find Polynomial Equation from Set of Coordinate ...
There is also a very important relation between the leading coefficient degree of polynomial and the finite difference: a n!
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks so much @WarrenWeckesser & @charris for your detailed insights. @WarrenWeckesser , you are indeed right that this (along with #16108 and the associated issues) are inspired by thinking about what is the best way to change attributes other than the
coef
on the polynomial instance (i.e. thedomain
,window
, or a proposedsymbol
for instance).The point about
cast
is well-taken - other methods such asconvert
are intended to handle changing these attributes as well. These methods are entirely sufficient, but IMO are a little tricky because of how overloaded (in the general sense) they are. For instance, theconvert
method can be used to change thedomain
orwindow
of a polynomial (and apply the corresponding transformation to thecoef
), linearly map thecoefs
from thedomain
->window
(if they differ), or to change the kind of polynomial, e.g. converting aChebyshev
instance to aHermite
instance (again, accounting for changes in thecoef
do to the change in basis). All that said, the point made above about being explicit (p2 = Polynomial(p.coef, window=p.window, domain=p.domain)
instead ofp2 = Polynomial(p)
) is a good one.The proposal in this PR also draws from the way
np.poly1d
, which hasisinstance
checks in__init__
in order to handle creation of new instances both fromarray-like
s and frompoly1d
instances; though perhaps the decision not to go that route with thepolynomial
package was intentional!I think @WarrenWeckesser makes a strong case above for not modifying the constructor as a means for creating polynomial instances with different
domain
andwindow
attributes, so I’m inclined to close this. Thanks again, all, for taking a close look at the proposal!Thanks for weighing in @anirudh2290 , those are good points. Re: multivariate polynomials, I agree that the docs for the polynomial package do no mention support for multivariable polynomials, though they don’t specifically discount them either (unlike
np.poly1d
from the “old” polynomial API, which has1d
in the name).One thing to be careful of: the
polyval
that is in the main numpy namespace (i.e.np.polyval
) is actually thepolyval
from the “old” polynomial modulenp.lib.polynomial
. There is apolyval
associated with the new polynomial package, i.e.np.polynomial.polynomial.polyval
. In fact, after careful inspection, there are also 2d and 3d functions defined, e.g.np.polynomial.polynomial.polyval[2,3]d
. However there is not currently any support for multivariable polynomials through the convenience classes, which are the preferred interface for thenp.polynomial
package. In other words, there is some support for evaluation of multivariable polynomial expressions, but it is not discoverable and has not been added to the preferred (class-based) interface.If there are plans in the future to support multivariable polynomials with the convenience classes, then indeed some additional thought will be needed to define how the
coefs
will be stored and what the constructor and evaluation will look like. I don’t know what the plans are for adding support for multivariable polynomial expressions to thenp.polynomial
package - maybe @charris has some insight on that front.