Skip to content

ENH: add support for array-likes in polynomial evaluation #18662

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/release/upcoming_changes/18662.new_feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Add support for array-likes in polynomial evaluation
----------------------------------------------------------------------------

Polynomial evaluation now accepts array-likes (e.g. lists) as its
input argument, in addition to NumPy arrays.
1 change: 1 addition & 0 deletions numpy/polynomial/_polybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def __setstate__(self, dict):

def __call__(self, arg):
off, scl = pu.mapparms(self.domain, self.window)
arg = np.asarray(arg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, previously working calls using masked arrays would stop working - e.g.,

np.polynomial.Polynomial([1, 2])(np.ma.MaskedArray([3, 4], mask=[True, False]))

That particular part could be solved by making this np.asanyarray - though I guess it is possible the polynomials are also used by, e.g., dask, in which case this would still be a problem.

arg = off + scl*arg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler way to do the same thing might be to make sure scl is an array scalar rather than a regular scalar, since array * list already has the right behaviour of interpreting this list as an array. One can do it here by writing this as off + scl[...] * arg - though it might make more sense to ensure scl is an array scalar inside mapparms.

return self._val(arg, self.coef)

Expand Down