Issue
I understand that I created a np.poly1d object. But what does it mean by putting it back inside np.poly1d() again?
import numpy as np
f = np.poly1d([1, 1, 1])
print(np.poly1d(f))
FYI running this script, I got
2
1 x + 1 x + 1
Solution
It's call syntax.
Reference: https://docs.python.org/3/reference/expressions.html#calls
np.poly1d
being the callable and f
the argument.
np.poly1d
is a class, which can be used like a callable to create an instance of the class (What is a "callable"?).
In this particular case, f
will be interpreted as an array-like of polynomial coefficients, resulting in a new polynomial which is equivalent to f
, since treating a np.poly1d
instance as an array results in an array of its coefficients.
>>> np.array(f)
array([1, 1, 1])
>>> g = np.poly1d(f)
>>> g == f
True
So without knowing more context, using np.poly1d(f)
instead of f
seems pointless. It could be useful if the intention was to create a copy of a polynomial in order to modify one but not the other, since f
and g
are different objects:
>>> g is f
False
Answered By - mkrieger1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.