pythonnumpypolynomials

What is the equivalent of np.polyval in the new np.polynomial API?


I can't find a direct answer in NumPy documentation. This snippet will populate y with polynomial p values on domain x:

p = [1, 2, 3]
x = np.linspace(0, 1, 10)
y = [np.polyval(p, i) for i in x]

What is the new API equivalent when p = Polynomial(p)?


Solution

  • You can simply evaluate values with p(x). Documentation can be found on "Using the convenience classes" under "Evaluation":

    p = [1, 2, 3]
    
    p = np.polynomial.Polynomial(p)
    
    x = np.linspace(0, 1, 10)
    
    y = p(x)
    

    Note: Coefficients are in reverse order compared to legacy API i.e. coefficients go from lowest order to highest order such that p[i] is the ith-order term.