Skip to content

add pari algo for polynomial interpolation #39851

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

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions src/sage/rings/polynomial/polynomial_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -2330,7 +2330,8 @@ def divided_difference(self, points, full_table=False):
else:
return [F[i][i] for i in range(n)]

def lagrange_polynomial(self, points, algorithm='divided_difference', previous_row=None):
def lagrange_polynomial(self, points, algorithm='divided_difference',
previous_row=None):
r"""
Return the Lagrange interpolation polynomial through the
given points.
Expand Down Expand Up @@ -2358,6 +2359,8 @@ def lagrange_polynomial(self, points, algorithm='divided_difference', previous_r
table, instead of the full table itself. Generating the
full table can be memory inefficient.

- ``'pari'``: use Pari's function :pari:`polinterpolate`

- ``previous_row`` -- (default: ``None``) this option is only
relevant if used with ``algorithm='neville'``. If provided,
this should be the last row of the table resulting from a
Expand Down Expand Up @@ -2436,24 +2439,23 @@ def lagrange_polynomial(self, points, algorithm='divided_difference', previous_r
....: algorithm='neville', previous_row=p)[-1]
a^2*x^2 + a^2*x + a^2

One can also use ``Pari``'s implementation::

sage: R = PolynomialRing(QQ, 'x')
sage: data = [(0,1), (2,5), (3,10)]
sage: p = R.lagrange_polynomial(data, algorithm='pari'); p
x^2 + 1

TESTS:

The value for ``algorithm`` must be either
``'divided_difference'`` (default), or ``'neville'``::
``'divided_difference'`` (default), ``'neville'`` or ``'pari'``::

sage: R = PolynomialRing(QQ, 'x')
sage: R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)], algorithm='abc')
Traceback (most recent call last):
...
ValueError: algorithm must be one of 'divided_difference' or 'neville'
sage: R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)], algorithm='divided difference')
Traceback (most recent call last):
...
ValueError: algorithm must be one of 'divided_difference' or 'neville'
sage: R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)], algorithm='')
Traceback (most recent call last):
...
ValueError: algorithm must be one of 'divided_difference' or 'neville'
ValueError: algorithm can be 'divided_difference', 'neville' or 'pari'

Make sure that :issue:`10304` is fixed. The return value
should always be an element of ``self`` in the case of
Expand Down Expand Up @@ -2538,24 +2540,13 @@ def lagrange_polynomial(self, points, algorithm='divided_difference', previous_r
P, Q = Q, P # the current row is complete, reuse the old P to hold the next row
return P # return the last row in the Neville table

# # use the definition of Lagrange interpolation polynomial
# elif algorithm == "definition":
# def Pj(j):
# denom = 1
# divis = 1
# for i in range(len(points)):
# if i!=j:
# denom *= (var - points[i][0])
# divis *= (points[j][0] - points[i][0])
# return denom/divis
#
# P = 0
# for j in range(len(points)):
# P += Pj(j)*points[j][1]
# return P
elif algorithm == "pari":
from sage.libs.pari import pari
positions = pari([a for a, b in points])
values = pari([b for a, b in points])
return self(pari.polinterpolate(positions, values))

else:
raise ValueError("algorithm must be one of 'divided_difference' or 'neville'")
raise ValueError("algorithm can be 'divided_difference', 'neville' or 'pari'")

@cached_method
def fraction_field(self):
Expand Down
Loading