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

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

wants to merge 2 commits into from

Conversation

cval26
Copy link
Contributor

@cval26 cval26 commented Mar 21, 2021

ENH: add support for array-likes in polynomial evaluation

Closes #17949.

Adds an asarray() call in the polybase __call__ method so that evaluation works with array-likes too. See #17949 for the initial reproduced code example; after the above change, evaluation works as expected:

import numpy as np
>>> np.polynomial.Polynomial([1, 2])(np.array([3, 4]))
array([7., 9.])
>>> np.polynomial.Polynomial([1, 2])([3, 4])
array([7., 9.])

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

The idea is logical, but see in-line for potential problems and a suggested solution. Regardless, it would be good to add tests with a list and masked array.

@@ -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.

@@ -479,6 +479,7 @@ def __setstate__(self, dict):

def __call__(self, arg):
off, scl = pu.mapparms(self.domain, self.window)
arg = np.asarray(arg)
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.

@InessaPawson
Copy link
Member

@cval26 Thank you for working on the solution for #17949! Do you think you could finish the PR?

@seberg seberg added the 57 - Close? Issues which may be closable unless discussion continued label Apr 28, 2023
@cval26 cval26 closed this Aug 1, 2023
@InessaPawson InessaPawson removed the 57 - Close? Issues which may be closable unless discussion continued label Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Polynomial can't be evaluated on list of numbers only ndarray of numbers
4 participants