Home > Mobile >  How do I use method lagrange_polynomial over a PolynomialRing of Integers?
How do I use method lagrange_polynomial over a PolynomialRing of Integers?

Time:01-30

R.<x> = PolynomialRing(RR)
points = [(1,2), (2,2), (3,6)]
R.lagrange_polynomial(points)
2.00000000000000*x^2 - 6.00000000000000*x   6.00000000000000

The above works fine but since all the coefficients are integers, I would prefer to do this over integers.

However when I try with

R.<x> = PolynomialRing(ZZ)
R.lagrange_polynomial(points)

I get the error

AttributeError: 'PolynomialRing_integral_domain_with_category' object has no attribute 'lagrange_polynomial'

I know I can use QQ instead of RR & get the coefficients printed as integers, but I am wondering why ZZ is not allowed?

CodePudding user response:

The Lagrange polynomial for three points in ZZ^2 does not always have coefficients in ZZ, so it makes sense to have it as a method of QQ['x'] and not of ZZ['x'].

sage: R.<x> = PolynomialRing(QQ)
sage: points = [(0, 1), (1, 0), (2, 2)]
sage: R.lagrange_polynomial(points)
3/2*x^2 - 5/2*x   1

Since for points = [(1, 2), (2, 2), (3, 6)] the Lagrange polynomial's coefficients end up being integers, one can do:

sage: R.<x> = PolynomialRing(ZZ)
sage: points = [(1, 2), (2, 2), (3, 6)]

sage: q = R.change_ring(QQ).lagrange_polynomial(points)
sage: q
2*x^2 - 6*x   6
sage: parent(q)
Univariate Polynomial Ring in x over Rational Field

sage: p = R(q)
sage: p
2*x^2 - 6*x   6
sage: parent(p)
Univariate Polynomial Ring in x over Integer Ring
  •  Tags:  
  • Related