Skip to content

Commit 7a38c84

Browse files
author
Release Manager
committed
sagemathgh-39851: add pari algo for polynomial interpolation
Fixes sagemath#39833 ### 📝 Checklist - [x] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: sagemath#39851 Reported by: Frédéric Chapoton Reviewer(s):
2 parents 1d14a08 + 649c401 commit 7a38c84

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

build/pkgs/configure/checksums.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
tarball=configure-VERSION.tar.gz
2-
sha1=3371667ee074aefdde8fc64ad60928a29a1e3578
3-
sha256=4e93801f29ba7d242285ed1461300586b50db56474e84cefe0b793f8dbf54842
2+
sha1=8a3ce3d350a3a750f562de1af26f07fce4dcef72
3+
sha256=fc8bedfe9af21649b0878294149f4c5d75b58e4d7c771893a29a00b4c73397a1
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a2b47d65bcbe9d176b07208feb6aae513fc41244
1+
9d7288c7fba20f1d442ffb5bb4e24031473a8342

src/sage/rings/polynomial/polynomial_ring.py

+19-28
Original file line numberDiff line numberDiff line change
@@ -2348,7 +2348,8 @@ def divided_difference(self, points, full_table=False):
23482348
else:
23492349
return [F[i][i] for i in range(n)]
23502350

2351-
def lagrange_polynomial(self, points, algorithm='divided_difference', previous_row=None):
2351+
def lagrange_polynomial(self, points, algorithm='divided_difference',
2352+
previous_row=None):
23522353
r"""
23532354
Return the Lagrange interpolation polynomial through the
23542355
given points.
@@ -2376,6 +2377,8 @@ def lagrange_polynomial(self, points, algorithm='divided_difference', previous_r
23762377
table, instead of the full table itself. Generating the
23772378
full table can be memory inefficient.
23782379
2380+
- ``'pari'``: use Pari's function :pari:`polinterpolate`
2381+
23792382
- ``previous_row`` -- (default: ``None``) this option is only
23802383
relevant if used with ``algorithm='neville'``. If provided,
23812384
this should be the last row of the table resulting from a
@@ -2454,24 +2457,23 @@ def lagrange_polynomial(self, points, algorithm='divided_difference', previous_r
24542457
....: algorithm='neville', previous_row=p)[-1]
24552458
a^2*x^2 + a^2*x + a^2
24562459
2460+
One can also use ``Pari``'s implementation::
2461+
2462+
sage: R = PolynomialRing(QQ, 'x')
2463+
sage: data = [(0,1), (2,5), (3,10)]
2464+
sage: p = R.lagrange_polynomial(data, algorithm='pari'); p
2465+
x^2 + 1
2466+
24572467
TESTS:
24582468
24592469
The value for ``algorithm`` must be either
2460-
``'divided_difference'`` (default), or ``'neville'``::
2470+
``'divided_difference'`` (default), ``'neville'`` or ``'pari'``::
24612471
24622472
sage: R = PolynomialRing(QQ, 'x')
24632473
sage: R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)], algorithm='abc')
24642474
Traceback (most recent call last):
24652475
...
2466-
ValueError: algorithm must be one of 'divided_difference' or 'neville'
2467-
sage: R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)], algorithm='divided difference')
2468-
Traceback (most recent call last):
2469-
...
2470-
ValueError: algorithm must be one of 'divided_difference' or 'neville'
2471-
sage: R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)], algorithm='')
2472-
Traceback (most recent call last):
2473-
...
2474-
ValueError: algorithm must be one of 'divided_difference' or 'neville'
2476+
ValueError: algorithm can be 'divided_difference', 'neville' or 'pari'
24752477
24762478
Make sure that :issue:`10304` is fixed. The return value
24772479
should always be an element of ``self`` in the case of
@@ -2556,24 +2558,13 @@ def lagrange_polynomial(self, points, algorithm='divided_difference', previous_r
25562558
P, Q = Q, P # the current row is complete, reuse the old P to hold the next row
25572559
return P # return the last row in the Neville table
25582560

2559-
# # use the definition of Lagrange interpolation polynomial
2560-
# elif algorithm == "definition":
2561-
# def Pj(j):
2562-
# denom = 1
2563-
# divis = 1
2564-
# for i in range(len(points)):
2565-
# if i!=j:
2566-
# denom *= (var - points[i][0])
2567-
# divis *= (points[j][0] - points[i][0])
2568-
# return denom/divis
2569-
#
2570-
# P = 0
2571-
# for j in range(len(points)):
2572-
# P += Pj(j)*points[j][1]
2573-
# return P
2561+
elif algorithm == "pari":
2562+
from sage.libs.pari import pari
2563+
positions = pari([a for a, b in points])
2564+
values = pari([b for a, b in points])
2565+
return self(pari.polinterpolate(positions, values))
25742566

2575-
else:
2576-
raise ValueError("algorithm must be one of 'divided_difference' or 'neville'")
2567+
raise ValueError("algorithm can be 'divided_difference', 'neville' or 'pari'")
25772568

25782569
@cached_method
25792570
def fraction_field(self):

0 commit comments

Comments
 (0)