@@ -2348,7 +2348,8 @@ def divided_difference(self, points, full_table=False):
2348
2348
else :
2349
2349
return [F [i ][i ] for i in range (n )]
2350
2350
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 ):
2352
2353
r"""
2353
2354
Return the Lagrange interpolation polynomial through the
2354
2355
given points.
@@ -2376,6 +2377,8 @@ def lagrange_polynomial(self, points, algorithm='divided_difference', previous_r
2376
2377
table, instead of the full table itself. Generating the
2377
2378
full table can be memory inefficient.
2378
2379
2380
+ - ``'pari'``: use Pari's function :pari:`polinterpolate`
2381
+
2379
2382
- ``previous_row`` -- (default: ``None``) this option is only
2380
2383
relevant if used with ``algorithm='neville'``. If provided,
2381
2384
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
2454
2457
....: algorithm='neville', previous_row=p)[-1]
2455
2458
a^2*x^2 + a^2*x + a^2
2456
2459
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
+
2457
2467
TESTS:
2458
2468
2459
2469
The value for ``algorithm`` must be either
2460
- ``'divided_difference'`` (default), or ``'neville '``::
2470
+ ``'divided_difference'`` (default), ``'neville'`` or ``'pari '``::
2461
2471
2462
2472
sage: R = PolynomialRing(QQ, 'x')
2463
2473
sage: R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)], algorithm='abc')
2464
2474
Traceback (most recent call last):
2465
2475
...
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'
2475
2477
2476
2478
Make sure that :issue:`10304` is fixed. The return value
2477
2479
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
2556
2558
P , Q = Q , P # the current row is complete, reuse the old P to hold the next row
2557
2559
return P # return the last row in the Neville table
2558
2560
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 ))
2574
2566
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'" )
2577
2568
2578
2569
@cached_method
2579
2570
def fraction_field (self ):
0 commit comments