Skip to content

Polynomial can't be evaluated on list of numbers only ndarray of numbers #17949

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
jjmortensen opened this issue Dec 7, 2020 · 4 comments · Fixed by #24349
Closed

Polynomial can't be evaluated on list of numbers only ndarray of numbers #17949

jjmortensen opened this issue Dec 7, 2020 · 4 comments · Fixed by #24349

Comments

@jjmortensen
Copy link

Is it on purpose that this doesn't work:

>>> np.polynomial.Polynomial([1, 2])([3, 4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jensj/.local/lib/python3.7/site-packages/numpy/polynomial/_polybase.py", line 482, in __call__
    arg = off + scl*arg
TypeError: can't multiply sequence by non-int of type 'numpy.float64'

but this does:

>>> np.polynomial.Polynomial([1, 2])(np.array([3, 4]))
array([7., 9.])
@rossbar
Copy link
Contributor

rossbar commented Dec 7, 2020

My feeling is that __call__ should support array-likes, so this should be fixed.

@IngErnestoAlvarez
Copy link

It would be sufficient if it casts an array-like parameter to np.array inside the __call__ function?

WarrenWeckesser added a commit to WarrenWeckesser/numpy that referenced this issue Aug 6, 2023
… method.

Use the utility function `polyutils.mapdomain()` in the `__call__` method of
`ABCPolyBase`.  This closes numpygh-17949 because `mapdomain` calls `np.asanyarray(x)`
on its first argument.

Closes numpygh-17949.
@WarrenWeckesser
Copy link
Member

I just ran into this. The issue still exists with the main development branch:

In [11]: np.__version__
Out[11]: '2.0.0.dev0+766.g8627dc962'

In [12]: p = np.polynomial.Polynomial([1, -2.5, 3, 1])

In [13]: p([2, 3, 4])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[13], line 1
----> 1 p([2, 3, 4])

File ~/repos/git/forks/numpy/build-install/usr/lib/python3.11/site-packages/numpy/polynomial/_polybase.py:512, in ABCPolyBase.__call__(self, arg)
    510 def __call__(self, arg):
    511     off, scl = pu.mapparms(self.domain, self.window)
--> 512     arg = off + scl*arg
    513     return self._val(arg, self.coef)

TypeError: can't multiply sequence by non-int of type 'numpy.float64'

@charris
Copy link
Member

charris commented Aug 6, 2023

Polynomials can be evaluated on objects, matrices for example, and a list is just another object, but fails because it cannot be multiplied by floats (coefficients).

rossbar pushed a commit that referenced this issue Aug 6, 2023
… method. (#24349)

Use the utility function `polyutils.mapdomain()` in the `__call__` method of
`ABCPolyBase`.  This closes gh-17949 because `mapdomain` calls `np.asanyarray(x)`
on its first argument.

Closes gh-17949.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment