question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lagrange polynomials and numpy Polynomials

See original GitHub issue

My 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:closed
  • Created 2 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
ev-brcommented, Jul 14, 2021

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

0reactions
charriscommented, Jul 14, 2021

are there any plans w. r. t. np.poly1d

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found