Lagrange polynomials and numpy Polynomials
See original GitHub issueMy issue is about the documentation of lagrange interpolation here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.lagrange.html lagrange returns a numpy.poly1d that contains “The polynomial’s coefficients, in decreasing powers”
Then, the documentation shows to pass this result to numpy Polynomials. But this class accepts as parameters “Polynomial coefficients in order of increasing degree” https://numpy.org/doc/stable/reference/generated/numpy.polynomial.polynomial.Polynomial.html
Does it seem like a conceptual error? Coefficients are the same, but the order is reversed so when you want to interpolate things do not work well
Reproducing code example:
from numpy.polynomial.polynomial import Polynomial
from scipy.interpolate import lagrange
x = np.array([0, 1, 2])
y = x**3
poly = lagrange(x, y)
poly1d([ 3., -2., 0.]) #3x^2 -2x
Polynomial(poly).coef
array([ 3., -2., 0.]) #-2x +3
Scipy/Numpy/Python version information:
import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
1.7.0 1.20.3 sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
scipy.interpolate.lagrange — SciPy v1.9.3 Manual
Return a Lagrange interpolating polynomial. Given two 1-D arrays x and w, returns the Lagrange interpolating polynomial through the points (x, w) ....
Read more >Lagrange Polynomial Interpolation
WARNING! Lagrange interpolation polynomials are defined outside the area of interpolation, that is outside of the interval [x1 ...
Read more >Polynomials and Interpolation
The scipy.interpolate package has a lagrange command that returns a poly1d object interpolating the points. As we will see, the Lagrange polynomial is...
Read more >Python Program for Lagrange Interpolation Method
In this Python program, x and y are two array for storing x data and y data respectively. Here we create these array...
Read more >Legendre Series
Legendre Series ( numpy.polynomial.legendre )# ... This module provides a number of objects (mostly functions) useful for dealing with Legendre series, including ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
poly1d is considered legacy and its use in new code is discouraged. This function predates numpy Polynomial, so we cannot just change its return type. All in all, best is to show the recommended usage — which this example tried to do 😃.
Nothing planned that I am aware of. The Chebyshev polynomials have the advantage that the coefficients are easy to compute from values sampled at the Chebyshev points, so would work well with the scipy functions that just compute the polynomial values.