Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Polynomial.fit(...).window.dtype inconsistent with Polynomial(...).dtype #22742

Open
SV-97 opened this issue Dec 6, 2022 · 1 comment
Open
Labels

Comments

@SV-97
Copy link

SV-97 commented Dec 6, 2022

Describe the issue:

When using np.polynomial.Polynomial.fit to fit a polynomial to some data, the dtype of the window of the resulting np.polynomial.Polynomial is different than the one of an instance constructed directly via np.polynomial.Polynomial.

This leads to polynomials effectively having the same window but still being incompatible because of the windows equality comparison evaluating to False.

I locally tested modifying the dtype of polydomain here by setting polydomain = np.array([-1., 1.]) which fixed the issue for me - however I have no idea if this raises other problems in other places.

Reproduce the code example:

import numpy as np

xs1 = np.array([0.0]) 
ys1 = np.array([1.0])

# fit a constant polynomial to (xs1, ys1)
p1 = np.polynomial.Polynomial(np.mean(ys1))

xs2 = np.array([0.0, 1.0])
ys2 = np.array([0.0, 2.0])

# fit a linear polynomial to (xs2, ys2)
p2 = np.polynomial.Polynomial.fit(xs2, ys2, deg=1)

print(p1.window.dtype, p2.window.dtype)
assert(p1.window.dtype != p2.window.dtype) # this assertion should Error but it doesn't

Error message:

No response

NumPy/Python version information:

1.23.5 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:36:39) [GCC 10.4.0]

Context for the issue:

Since np.polynomial.Polynomial can't fit constant polynomials, a wrapping fitting function has to construct some polynomials via the default constructor and others via .fit. My work requires finding (poly_1 - poly_2)**2 which fails because of incompatible windows.

This can be worked around by manually setting the windows on either instance.

@SV-97 SV-97 added the 00 - Bug label Dec 6, 2022
@SV-97
Copy link
Author

SV-97 commented Dec 6, 2022

I just noticed that a similar problem affects the domains as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant