Univariate Polynomial Rings¶
Sage implements sparse and dense polynomials over commutative and non-commutative rings. In the non-commutative case, the polynomial variable commutes with the elements of the base ring.
AUTHOR:
William Stein
Kiran Kedlaya (2006-02-13): added macaulay2 option
Martin Albrecht (2006-08-25): removed it again as it isn’t needed anymore
Simon King (2011-05): Dense and sparse polynomial rings must not be equal.
Simon King (2011-10): Choice of categories for polynomial rings.
EXAMPLES:
sage: z = QQ['z'].0
sage: (z^3 + z - 1)^3
z^9 + 3*z^7 - 3*z^6 + 3*z^5 - 6*z^4 + 4*z^3 - 3*z^2 + 3*z - 1
>>> from sage.all import *
>>> z = QQ['z'].gen(0)
>>> (z**Integer(3) + z - Integer(1))**Integer(3)
z^9 + 3*z^7 - 3*z^6 + 3*z^5 - 6*z^4 + 4*z^3 - 3*z^2 + 3*z - 1
Saving and loading of polynomial rings works:
sage: loads(dumps(QQ['x'])) == QQ['x']
True
sage: k = PolynomialRing(QQ['x'],'y'); loads(dumps(k))==k
True
sage: k = PolynomialRing(ZZ,'y'); loads(dumps(k)) == k
True
sage: k = PolynomialRing(ZZ,'y', sparse=True); loads(dumps(k))
Sparse Univariate Polynomial Ring in y over Integer Ring
>>> from sage.all import *
>>> loads(dumps(QQ['x'])) == QQ['x']
True
>>> k = PolynomialRing(QQ['x'],'y'); loads(dumps(k))==k
True
>>> k = PolynomialRing(ZZ,'y'); loads(dumps(k)) == k
True
>>> k = PolynomialRing(ZZ,'y', sparse=True); loads(dumps(k))
Sparse Univariate Polynomial Ring in y over Integer Ring
Rings with different variable names are not equal; in fact, by Issue #9944, polynomial rings are equal if and only if they are identical (which should be the case for all parent structures in Sage):
sage: QQ['y'] != QQ['x']
True
sage: QQ['y'] != QQ['z']
True
>>> from sage.all import *
>>> QQ['y'] != QQ['x']
True
>>> QQ['y'] != QQ['z']
True
We create a polynomial ring over a quaternion algebra:
sage: # needs sage.combinat sage.modules
sage: A.<i,j,k> = QuaternionAlgebra(QQ, -1,-1)
sage: R.<w> = PolynomialRing(A, sparse=True)
sage: f = w^3 + (i+j)*w + 1
sage: f
w^3 + (i + j)*w + 1
sage: f^2
w^6 + (2*i + 2*j)*w^4 + 2*w^3 - 2*w^2 + (2*i + 2*j)*w + 1
sage: f = w + i ; g = w + j
sage: f * g
w^2 + (i + j)*w + k
sage: g * f
w^2 + (i + j)*w - k
>>> from sage.all import *
>>> # needs sage.combinat sage.modules
>>> A = QuaternionAlgebra(QQ, -Integer(1),-Integer(1), names=('i', 'j', 'k',)); (i, j, k,) = A._first_ngens(3)
>>> R = PolynomialRing(A, sparse=True, names=('w',)); (w,) = R._first_ngens(1)
>>> f = w**Integer(3) + (i+j)*w + Integer(1)
>>> f
w^3 + (i + j)*w + 1
>>> f**Integer(2)
w^6 + (2*i + 2*j)*w^4 + 2*w^3 - 2*w^2 + (2*i + 2*j)*w + 1
>>> f = w + i ; g = w + j
>>> f * g
w^2 + (i + j)*w + k
>>> g * f
w^2 + (i + j)*w - k
Issue #9944 introduced some changes related with coercion. Previously, a dense and a sparse polynomial ring with the same variable name over the same base ring evaluated equal, but of course they were not identical. Coercion maps are cached - but if a coercion to a dense ring is requested and a coercion to a sparse ring is returned instead (since the cache keys are equal!), all hell breaks loose.
Therefore, the coercion between rings of sparse and dense polynomials works as follows:
sage: R.<x> = PolynomialRing(QQ, sparse=True)
sage: S.<x> = QQ[]
sage: S == R
False
sage: S.has_coerce_map_from(R)
True
sage: R.has_coerce_map_from(S)
False
sage: (R.0 + S.0).parent()
Univariate Polynomial Ring in x over Rational Field
sage: (S.0 + R.0).parent()
Univariate Polynomial Ring in x over Rational Field
>>> from sage.all import *
>>> R = PolynomialRing(QQ, sparse=True, names=('x',)); (x,) = R._first_ngens(1)
>>> S = QQ['x']; (x,) = S._first_ngens(1)
>>> S == R
False
>>> S.has_coerce_map_from(R)
True
>>> R.has_coerce_map_from(S)
False
>>> (R.gen(0) + S.gen(0)).parent()
Univariate Polynomial Ring in x over Rational Field
>>> (S.gen(0) + R.gen(0)).parent()
Univariate Polynomial Ring in x over Rational Field
It may be that one has rings of dense or sparse polynomials over
different base rings. In that situation, coercion works by means of
the pushout()
formalism:
sage: R.<x> = PolynomialRing(GF(5), sparse=True)
sage: S.<x> = PolynomialRing(ZZ)
sage: R.has_coerce_map_from(S)
False
sage: S.has_coerce_map_from(R)
False
sage: S.0 + R.0
2*x
sage: (S.0 + R.0).parent()
Univariate Polynomial Ring in x over Finite Field of size 5
sage: (S.0 + R.0).parent().is_sparse()
False
>>> from sage.all import *
>>> R = PolynomialRing(GF(Integer(5)), sparse=True, names=('x',)); (x,) = R._first_ngens(1)
>>> S = PolynomialRing(ZZ, names=('x',)); (x,) = S._first_ngens(1)
>>> R.has_coerce_map_from(S)
False
>>> S.has_coerce_map_from(R)
False
>>> S.gen(0) + R.gen(0)
2*x
>>> (S.gen(0) + R.gen(0)).parent()
Univariate Polynomial Ring in x over Finite Field of size 5
>>> (S.gen(0) + R.gen(0)).parent().is_sparse()
False
Similarly, there is a coercion from the (non-default) NTL implementation for univariate polynomials over the integers to the default FLINT implementation, but not vice versa:
sage: R.<x> = PolynomialRing(ZZ, implementation='NTL') # needs sage.libs.ntl
sage: S.<x> = PolynomialRing(ZZ, implementation='FLINT')
sage: (S.0 + R.0).parent() is S # needs sage.libs.flint sage.libs.ntl
True
sage: (R.0 + S.0).parent() is S # needs sage.libs.flint sage.libs.ntl
True
>>> from sage.all import *
>>> R = PolynomialRing(ZZ, implementation='NTL', names=('x',)); (x,) = R._first_ngens(1)# needs sage.libs.ntl
>>> S = PolynomialRing(ZZ, implementation='FLINT', names=('x',)); (x,) = S._first_ngens(1)
>>> (S.gen(0) + R.gen(0)).parent() is S # needs sage.libs.flint sage.libs.ntl
True
>>> (R.gen(0) + S.gen(0)).parent() is S # needs sage.libs.flint sage.libs.ntl
True
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_cdvf(base_ring, name=None, sparse=False, implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_cdvr
,PolynomialRing_field
A class for polynomial ring over complete discrete valuation fields
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_cdvr(base_ring, name=None, sparse=False, implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_integral_domain
A class for polynomial ring over complete discrete valuation rings
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_commutative(base_ring, name=None, sparse=False, implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_general
Univariate polynomial ring over a commutative ring.
- quotient_by_principal_ideal(f, names=None, **kwds)[source]¶
Return the quotient of this polynomial ring by the principal ideal (generated by) \(f\).
INPUT:
f
– either a polynomial inself
, or a principal ideal ofself
further named arguments that are passed to the quotient constructor
EXAMPLES:
sage: R.<x> = QQ[] sage: I = (x^2 - 1) * R sage: R.quotient_by_principal_ideal(I) # needs sage.libs.pari Univariate Quotient Polynomial Ring in xbar over Rational Field with modulus x^2 - 1
>>> from sage.all import * >>> R = QQ['x']; (x,) = R._first_ngens(1) >>> I = (x**Integer(2) - Integer(1)) * R >>> R.quotient_by_principal_ideal(I) # needs sage.libs.pari Univariate Quotient Polynomial Ring in xbar over Rational Field with modulus x^2 - 1
The same example, using the polynomial instead of the ideal, and customizing the variable name:
sage: R.<x> = QQ[] sage: R.quotient_by_principal_ideal(x^2 - 1, names=('foo',)) # needs sage.libs.pari Univariate Quotient Polynomial Ring in foo over Rational Field with modulus x^2 - 1
>>> from sage.all import * >>> R = QQ['x']; (x,) = R._first_ngens(1) >>> R.quotient_by_principal_ideal(x**Integer(2) - Integer(1), names=('foo',)) # needs sage.libs.pari Univariate Quotient Polynomial Ring in foo over Rational Field with modulus x^2 - 1
- weyl_algebra()[source]¶
Return the Weyl algebra generated from
self
.EXAMPLES:
sage: R = QQ['x'] sage: W = R.weyl_algebra(); W # needs sage.modules Differential Weyl algebra of polynomials in x over Rational Field sage: W.polynomial_ring() == R # needs sage.modules True
>>> from sage.all import * >>> R = QQ['x'] >>> W = R.weyl_algebra(); W # needs sage.modules Differential Weyl algebra of polynomials in x over Rational Field >>> W.polynomial_ring() == R # needs sage.modules True
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_finite_field(base_ring, name='x', element_class=None, implementation=None)[source]¶
Bases:
PolynomialRing_field
Univariate polynomial ring over a finite field.
EXAMPLES:
sage: R = PolynomialRing(GF(27, 'a'), 'x') # needs sage.rings.finite_rings sage: type(R) # needs sage.rings.finite_rings <class 'sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_finite_field_with_category'>
>>> from sage.all import * >>> R = PolynomialRing(GF(Integer(27), 'a'), 'x') # needs sage.rings.finite_rings >>> type(R) # needs sage.rings.finite_rings <class 'sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_finite_field_with_category'>
- irreducible_element(n, algorithm=None)[source]¶
Construct a monic irreducible polynomial of degree \(n\).
INPUT:
n
– integer; degree of the polynomial to constructalgorithm
– string (algorithm to use) orNone
:'random'
orNone
: try random polynomials until an irreducible one is found'first_lexicographic'
: try polynomials in lexicographic order until an irreducible one is found
OUTPUT: a monic irreducible polynomial of degree \(n\) in
self
EXAMPLES:
sage: # needs sage.modules sage.rings.finite_rings sage: f = GF(5^3, 'a')['x'].irreducible_element(2) sage: f.degree() 2 sage: f.is_irreducible() True sage: R = GF(19)['x'] sage: R.irreducible_element(21, algorithm='first_lexicographic') x^21 + x + 5 sage: R = GF(5**2, 'a')['x'] sage: R.irreducible_element(17, algorithm='first_lexicographic') x^17 + a*x + 4*a + 3
>>> from sage.all import * >>> # needs sage.modules sage.rings.finite_rings >>> f = GF(Integer(5)**Integer(3), 'a')['x'].irreducible_element(Integer(2)) >>> f.degree() 2 >>> f.is_irreducible() True >>> R = GF(Integer(19))['x'] >>> R.irreducible_element(Integer(21), algorithm='first_lexicographic') x^21 + x + 5 >>> R = GF(Integer(5)**Integer(2), 'a')['x'] >>> R.irreducible_element(Integer(17), algorithm='first_lexicographic') x^17 + a*x + 4*a + 3
AUTHORS:
Peter Bruin (June 2013)
Jean-Pierre Flori (May 2014)
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_mod_n(base_ring, name=None, element_class=None, implementation=None, category=None)[source]¶
Bases:
PolynomialRing_commutative
- modulus()[source]¶
EXAMPLES:
sage: R.<x> = Zmod(15)[] sage: R.modulus() 15
>>> from sage.all import * >>> R = Zmod(Integer(15))['x']; (x,) = R._first_ngens(1) >>> R.modulus() 15
- residue_field(ideal, names=None)[source]¶
Return the residue finite field at the given ideal.
EXAMPLES:
sage: # needs sage.libs.ntl sage: R.<t> = GF(2)[] sage: k.<a> = R.residue_field(t^3 + t + 1); k Residue field in a of Principal ideal (t^3 + t + 1) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) sage: k.list() [0, a, a^2, a + 1, a^2 + a, a^2 + a + 1, a^2 + 1, 1] sage: R.residue_field(t) Residue field of Principal ideal (t) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) sage: P = R.irreducible_element(8) * R sage: P Principal ideal (t^8 + t^4 + t^3 + t^2 + 1) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) sage: k.<a> = R.residue_field(P); k Residue field in a of Principal ideal (t^8 + t^4 + t^3 + t^2 + 1) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) sage: k.cardinality() 256
>>> from sage.all import * >>> # needs sage.libs.ntl >>> R = GF(Integer(2))['t']; (t,) = R._first_ngens(1) >>> k = R.residue_field(t**Integer(3) + t + Integer(1), names=('a',)); (a,) = k._first_ngens(1); k Residue field in a of Principal ideal (t^3 + t + 1) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) >>> k.list() [0, a, a^2, a + 1, a^2 + a, a^2 + a + 1, a^2 + 1, 1] >>> R.residue_field(t) Residue field of Principal ideal (t) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) >>> P = R.irreducible_element(Integer(8)) * R >>> P Principal ideal (t^8 + t^4 + t^3 + t^2 + 1) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) >>> k = R.residue_field(P, names=('a',)); (a,) = k._first_ngens(1); k Residue field in a of Principal ideal (t^8 + t^4 + t^3 + t^2 + 1) of Univariate Polynomial Ring in t over Finite Field of size 2 (using GF2X) >>> k.cardinality() 256
Non-maximal ideals are not accepted:
sage: # needs sage.libs.ntl sage: R.residue_field(t^2 + 1) Traceback (most recent call last): ... ArithmeticError: ideal is not maximal sage: R.residue_field(0) Traceback (most recent call last): ... ArithmeticError: ideal is not maximal sage: R.residue_field(1) Traceback (most recent call last): ... ArithmeticError: ideal is not maximal
>>> from sage.all import * >>> # needs sage.libs.ntl >>> R.residue_field(t**Integer(2) + Integer(1)) Traceback (most recent call last): ... ArithmeticError: ideal is not maximal >>> R.residue_field(Integer(0)) Traceback (most recent call last): ... ArithmeticError: ideal is not maximal >>> R.residue_field(Integer(1)) Traceback (most recent call last): ... ArithmeticError: ideal is not maximal
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_mod_p(base_ring, name='x', implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_dense_finite_field
,PolynomialRing_dense_mod_n
,PolynomialRing_singular_repr
- fraction_field()[source]¶
Return the fraction field of
self
.EXAMPLES:
sage: R.<t> = GF(5)[] sage: R.fraction_field() Fraction Field of Univariate Polynomial Ring in t over Finite Field of size 5
>>> from sage.all import * >>> R = GF(Integer(5))['t']; (t,) = R._first_ngens(1) >>> R.fraction_field() Fraction Field of Univariate Polynomial Ring in t over Finite Field of size 5
- irreducible_element(n, algorithm=None)[source]¶
Construct a monic irreducible polynomial of degree \(n\).
INPUT:
n
– integer; the degree of the polynomial to constructalgorithm
– string (algorithm to use) orNone
; currently available options are:'adleman-lenstra'
: a variant of the Adleman–Lenstra algorithm as implemented in PARI.'conway'
: look up the Conway polynomial of degree \(n\) over the field of \(p\) elements in the database; raise aRuntimeError
if it is not found.'ffprimroot'
: use the pari:ffprimroot function from PARI.'first_lexicographic'
: return the lexicographically smallest irreducible polynomial of degree \(n\).'minimal_weight'
: return an irreducible polynomial of degree \(n\) with minimal number of non-zero coefficients. Only implemented for \(p = 2\).'primitive'
: return a polynomial \(f\) such that a root of \(f\) generates the multiplicative group of the finite field extension defined by \(f\). This uses the Conway polynomial if possible, otherwise it uses'ffprimroot'
.'random'
: try random polynomials until an irreducible one is found.
If
algorithm
isNone
, use \(x - 1\) in degree 1. In degree > 1, the Conway polynomial is used if it is found in the database. Otherwise, the algorithmminimal_weight
is used if \(p = 2\), and the algorithm'adleman-lenstra'
if \(p > 2\).
OUTPUT: a monic irreducible polynomial of degree \(n\) in
self
EXAMPLES:
sage: # needs sage.rings.finite_rings sage: GF(5)['x'].irreducible_element(2) x^2 + 4*x + 2 sage: GF(5)['x'].irreducible_element(2, algorithm='adleman-lenstra') x^2 + x + 1 sage: GF(5)['x'].irreducible_element(2, algorithm='primitive') x^2 + 4*x + 2 sage: GF(5)['x'].irreducible_element(32, algorithm='first_lexicographic') x^32 + 2 sage: GF(5)['x'].irreducible_element(32, algorithm='conway') Traceback (most recent call last): ... RuntimeError: requested Conway polynomial not in database. sage: GF(5)['x'].irreducible_element(32, algorithm='primitive') x^32 + ...
>>> from sage.all import * >>> # needs sage.rings.finite_rings >>> GF(Integer(5))['x'].irreducible_element(Integer(2)) x^2 + 4*x + 2 >>> GF(Integer(5))['x'].irreducible_element(Integer(2), algorithm='adleman-lenstra') x^2 + x + 1 >>> GF(Integer(5))['x'].irreducible_element(Integer(2), algorithm='primitive') x^2 + 4*x + 2 >>> GF(Integer(5))['x'].irreducible_element(Integer(32), algorithm='first_lexicographic') x^32 + 2 >>> GF(Integer(5))['x'].irreducible_element(Integer(32), algorithm='conway') Traceback (most recent call last): ... RuntimeError: requested Conway polynomial not in database. >>> GF(Integer(5))['x'].irreducible_element(Integer(32), algorithm='primitive') x^32 + ...
In characteristic 2:
sage: GF(2)['x'].irreducible_element(33) # needs sage.rings.finite_rings x^33 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^3 + 1 sage: GF(2)['x'].irreducible_element(33, algorithm='minimal_weight') # needs sage.rings.finite_rings x^33 + x^10 + 1
>>> from sage.all import * >>> GF(Integer(2))['x'].irreducible_element(Integer(33)) # needs sage.rings.finite_rings x^33 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^3 + 1 >>> GF(Integer(2))['x'].irreducible_element(Integer(33), algorithm='minimal_weight') # needs sage.rings.finite_rings x^33 + x^10 + 1
In degree 1:
sage: GF(97)['x'].irreducible_element(1) # needs sage.rings.finite_rings x + 96 sage: GF(97)['x'].irreducible_element(1, algorithm='conway') # needs sage.rings.finite_rings x + 92 sage: GF(97)['x'].irreducible_element(1, algorithm='adleman-lenstra') # needs sage.rings.finite_rings x
>>> from sage.all import * >>> GF(Integer(97))['x'].irreducible_element(Integer(1)) # needs sage.rings.finite_rings x + 96 >>> GF(Integer(97))['x'].irreducible_element(Integer(1), algorithm='conway') # needs sage.rings.finite_rings x + 92 >>> GF(Integer(97))['x'].irreducible_element(Integer(1), algorithm='adleman-lenstra') # needs sage.rings.finite_rings x
AUTHORS:
Peter Bruin (June 2013)
Jeroen Demeyer (September 2014): add “ffprimroot” algorithm, see Issue #8373.
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_padic_field_capped_relative(base_ring, name=None, implementation=None, element_class=None, category=None)[source]¶
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_padic_field_generic(base_ring, name=None, implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_cdvf
A class for dense polynomial ring over \(p\)-adic fields
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_padic_ring_capped_absolute(base_ring, name=None, implementation=None, element_class=None, category=None)[source]¶
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_padic_ring_capped_relative(base_ring, name=None, implementation=None, element_class=None, category=None)[source]¶
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_padic_ring_fixed_mod(base_ring, name=None, implementation=None, element_class=None, category=None)[source]¶
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_dense_padic_ring_generic(base_ring, name=None, implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_cdvr
A class for dense polynomial ring over \(p\)-adic rings
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_field(base_ring, name='x', sparse=False, implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_integral_domain
- divided_difference(points, full_table=False)[source]¶
Return the Newton divided-difference coefficients of the Lagrange interpolation polynomial through
points
.INPUT:
points
– list of pairs \((x_0, y_0), (x_1, y_1), \dots, (x_n, y_n)\) of elements of the base ring ofself
, where \(x_i - x_j\) is invertible for \(i \neq j\). This method converts the \(x_i\) and \(y_i\) into the base ring ofself
.full_table
– boolean (default:False
); ifTrue
, return the full divided-difference table. IfFalse
, only return entries along the main diagonal; these are the Newton divided-difference coefficients \(F_{i,i}\).
OUTPUT:
The Newton divided-difference coefficients of the \(n\)-th Lagrange interpolation polynomial \(P_n(x)\) that passes through the points in
points
(seelagrange_polynomial()
). These are the coefficients \(F_{0,0}, F_{1,1}, \dots, F_{n,n}\) in the base ring ofself
such that\[P_n(x) = \sum_{i=0}^n F_{i,i} \prod_{j=0}^{i-1} (x - x_j)\]EXAMPLES:
Only return the divided-difference coefficients \(F_{i,i}\). This example is taken from Example 1, page 121 of [BF2005]:
sage: # needs sage.rings.real_mpfr sage: points = [(1.0, 0.7651977), (1.3, 0.6200860), (1.6, 0.4554022), ....: (1.9, 0.2818186), (2.2, 0.1103623)] sage: R = PolynomialRing(RR, "x") sage: R.divided_difference(points) [0.765197700000000, -0.483705666666666, -0.108733888888889, 0.0658783950617283, 0.00182510288066044]
>>> from sage.all import * >>> # needs sage.rings.real_mpfr >>> points = [(RealNumber('1.0'), RealNumber('0.7651977')), (RealNumber('1.3'), RealNumber('0.6200860')), (RealNumber('1.6'), RealNumber('0.4554022')), ... (RealNumber('1.9'), RealNumber('0.2818186')), (RealNumber('2.2'), RealNumber('0.1103623'))] >>> R = PolynomialRing(RR, "x") >>> R.divided_difference(points) [0.765197700000000, -0.483705666666666, -0.108733888888889, 0.0658783950617283, 0.00182510288066044]
Now return the full divided-difference table:
sage: # needs sage.rings.real_mpfr sage: points = [(1.0, 0.7651977), (1.3, 0.6200860), (1.6, 0.4554022), ....: (1.9, 0.2818186), (2.2, 0.1103623)] sage: R = PolynomialRing(RR, "x") sage: R.divided_difference(points, full_table=True) [[0.765197700000000], [0.620086000000000, -0.483705666666666], [0.455402200000000, -0.548946000000000, -0.108733888888889], [0.281818600000000, -0.578612000000000, -0.0494433333333339, 0.0658783950617283], [0.110362300000000, -0.571520999999999, 0.0118183333333349, 0.0680685185185209, 0.00182510288066044]]
>>> from sage.all import * >>> # needs sage.rings.real_mpfr >>> points = [(RealNumber('1.0'), RealNumber('0.7651977')), (RealNumber('1.3'), RealNumber('0.6200860')), (RealNumber('1.6'), RealNumber('0.4554022')), ... (RealNumber('1.9'), RealNumber('0.2818186')), (RealNumber('2.2'), RealNumber('0.1103623'))] >>> R = PolynomialRing(RR, "x") >>> R.divided_difference(points, full_table=True) [[0.765197700000000], [0.620086000000000, -0.483705666666666], [0.455402200000000, -0.548946000000000, -0.108733888888889], [0.281818600000000, -0.578612000000000, -0.0494433333333339, 0.0658783950617283], [0.110362300000000, -0.571520999999999, 0.0118183333333349, 0.0680685185185209, 0.00182510288066044]]
The following example is taken from Example 4.12, page 225 of [MF1999]:
sage: points = [(1, -3), (2, 0), (3, 15), (4, 48), (5, 105), (6, 192)] sage: R = PolynomialRing(QQ, "x") sage: R.divided_difference(points) [-3, 3, 6, 1, 0, 0] sage: R.divided_difference(points, full_table=True) [[-3], [0, 3], [15, 15, 6], [48, 33, 9, 1], [105, 57, 12, 1, 0], [192, 87, 15, 1, 0, 0]]
>>> from sage.all import * >>> points = [(Integer(1), -Integer(3)), (Integer(2), Integer(0)), (Integer(3), Integer(15)), (Integer(4), Integer(48)), (Integer(5), Integer(105)), (Integer(6), Integer(192))] >>> R = PolynomialRing(QQ, "x") >>> R.divided_difference(points) [-3, 3, 6, 1, 0, 0] >>> R.divided_difference(points, full_table=True) [[-3], [0, 3], [15, 15, 6], [48, 33, 9, 1], [105, 57, 12, 1, 0], [192, 87, 15, 1, 0, 0]]
- fraction_field()[source]¶
Return the fraction field of
self
.EXAMPLES:
sage: QQbar['x'].fraction_field() Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
>>> from sage.all import * >>> QQbar['x'].fraction_field() Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
- lagrange_polynomial(points, algorithm='divided_difference', previous_row=None)[source]¶
Return the Lagrange interpolation polynomial through the given points.
INPUT:
points
– list of pairs \((x_0, y_0), (x_1, y_1), \dots, (x_n, y_n)\) of elements of the base ring ofself
, where \(x_i - x_j\) is invertible for \(i \neq j\). This method converts the \(x_i\) and \(y_i\) into the base ring ofself
.algorithm
– (default:'divided_difference'
) one of the following:'divided_difference'
: use the method of divided differences.'neville'
: adapt Neville’s method as described on page 144 of [BF2005] to recursively generate the Lagrange interpolation polynomial. Neville’s method generates a table of approximating polynomials, where the last row of that table contains the \(n\)-th Lagrange interpolation polynomial. The adaptation implemented by this method is to only generate the last row of this table, instead of the full table itself. Generating the full table can be memory inefficient.
previous_row
– (default:None
) this option is only relevant if used withalgorithm='neville'
. If provided, this should be the last row of the table resulting from a previous use of Neville’s method. If such a row is passed, thenpoints
should consist of both previous and new interpolating points. Neville’s method will then use that last row and the interpolating points to generate a new row containing an interpolation polynomial for the new points.
OUTPUT:
The Lagrange interpolation polynomial through the points \((x_0, y_0), (x_1, y_1), \dots, (x_n, y_n)\). This is the unique polynomial \(P_n\) of degree at most \(n\) in
self
satisfying \(P_n(x_i) = y_i\) for \(0 \le i \le n\).EXAMPLES:
By default, we use the method of divided differences:
sage: R = PolynomialRing(QQ, 'x') sage: f = R.lagrange_polynomial([(0,1), (2,2), (3,-2), (-4,9)]); f -23/84*x^3 - 11/84*x^2 + 13/7*x + 1 sage: f(0) 1 sage: f(2) 2 sage: f(3) -2 sage: f(-4) 9 sage: # needs sage.rings.finite_rings sage: R = PolynomialRing(GF(2**3, 'a'), 'x') sage: a = R.base_ring().gen() sage: f = R.lagrange_polynomial([(a^2+a, a), (a, 1), (a^2, a^2+a+1)]); f a^2*x^2 + a^2*x + a^2 sage: f(a^2 + a) a sage: f(a) 1 sage: f(a^2) a^2 + a + 1
>>> from sage.all import * >>> R = PolynomialRing(QQ, 'x') >>> f = R.lagrange_polynomial([(Integer(0),Integer(1)), (Integer(2),Integer(2)), (Integer(3),-Integer(2)), (-Integer(4),Integer(9))]); f -23/84*x^3 - 11/84*x^2 + 13/7*x + 1 >>> f(Integer(0)) 1 >>> f(Integer(2)) 2 >>> f(Integer(3)) -2 >>> f(-Integer(4)) 9 >>> # needs sage.rings.finite_rings >>> R = PolynomialRing(GF(Integer(2)**Integer(3), 'a'), 'x') >>> a = R.base_ring().gen() >>> f = R.lagrange_polynomial([(a**Integer(2)+a, a), (a, Integer(1)), (a**Integer(2), a**Integer(2)+a+Integer(1))]); f a^2*x^2 + a^2*x + a^2 >>> f(a**Integer(2) + a) a >>> f(a) 1 >>> f(a**Integer(2)) a^2 + a + 1
Now use a memory efficient version of Neville’s method:
sage: R = PolynomialRing(QQ, 'x') sage: R.lagrange_polynomial([(0,1), (2,2), (3,-2), (-4,9)], ....: algorithm='neville') [9, -11/7*x + 19/7, -17/42*x^2 - 83/42*x + 53/7, -23/84*x^3 - 11/84*x^2 + 13/7*x + 1] sage: # needs sage.rings.finite_rings sage: R = PolynomialRing(GF(2**3, 'a'), 'x') sage: a = R.base_ring().gen() sage: R.lagrange_polynomial([(a^2+a, a), (a, 1), (a^2, a^2+a+1)], ....: algorithm='neville') [a^2 + a + 1, x + a + 1, a^2*x^2 + a^2*x + a^2]
>>> from sage.all import * >>> R = PolynomialRing(QQ, 'x') >>> R.lagrange_polynomial([(Integer(0),Integer(1)), (Integer(2),Integer(2)), (Integer(3),-Integer(2)), (-Integer(4),Integer(9))], ... algorithm='neville') [9, -11/7*x + 19/7, -17/42*x^2 - 83/42*x + 53/7, -23/84*x^3 - 11/84*x^2 + 13/7*x + 1] >>> # needs sage.rings.finite_rings >>> R = PolynomialRing(GF(Integer(2)**Integer(3), 'a'), 'x') >>> a = R.base_ring().gen() >>> R.lagrange_polynomial([(a**Integer(2)+a, a), (a, Integer(1)), (a**Integer(2), a**Integer(2)+a+Integer(1))], ... algorithm='neville') [a^2 + a + 1, x + a + 1, a^2*x^2 + a^2*x + a^2]
Repeated use of Neville’s method to get better Lagrange interpolation polynomials:
sage: R = PolynomialRing(QQ, 'x') sage: p = R.lagrange_polynomial([(0,1), (2,2)], algorithm='neville') sage: R.lagrange_polynomial([(0,1), (2,2), (3,-2), (-4,9)], ....: algorithm='neville', previous_row=p)[-1] -23/84*x^3 - 11/84*x^2 + 13/7*x + 1 sage: # needs sage.rings.finite_rings sage: R = PolynomialRing(GF(2**3, 'a'), 'x') sage: a = R.base_ring().gen() sage: p = R.lagrange_polynomial([(a^2+a, a), (a, 1)], algorithm='neville') sage: R.lagrange_polynomial([(a^2+a, a), (a, 1), (a^2, a^2+a+1)], ....: algorithm='neville', previous_row=p)[-1] a^2*x^2 + a^2*x + a^2
>>> from sage.all import * >>> R = PolynomialRing(QQ, 'x') >>> p = R.lagrange_polynomial([(Integer(0),Integer(1)), (Integer(2),Integer(2))], algorithm='neville') >>> R.lagrange_polynomial([(Integer(0),Integer(1)), (Integer(2),Integer(2)), (Integer(3),-Integer(2)), (-Integer(4),Integer(9))], ... algorithm='neville', previous_row=p)[-Integer(1)] -23/84*x^3 - 11/84*x^2 + 13/7*x + 1 >>> # needs sage.rings.finite_rings >>> R = PolynomialRing(GF(Integer(2)**Integer(3), 'a'), 'x') >>> a = R.base_ring().gen() >>> p = R.lagrange_polynomial([(a**Integer(2)+a, a), (a, Integer(1))], algorithm='neville') >>> R.lagrange_polynomial([(a**Integer(2)+a, a), (a, Integer(1)), (a**Integer(2), a**Integer(2)+a+Integer(1))], ... algorithm='neville', previous_row=p)[-Integer(1)] a^2*x^2 + a^2*x + a^2
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_general(base_ring, name=None, sparse=False, implementation=None, element_class=None, category=None)[source]¶
Bases:
Ring
Univariate polynomial ring over a ring.
- base_extend(R)[source]¶
Return the base extension of this polynomial ring to \(R\).
EXAMPLES:
sage: # needs sage.rings.real_mpfr sage: R.<x> = RR[]; R Univariate Polynomial Ring in x over Real Field with 53 bits of precision sage: R.base_extend(CC) Univariate Polynomial Ring in x over Complex Field with 53 bits of precision sage: R.base_extend(QQ) Traceback (most recent call last): ... TypeError: no such base extension sage: R.change_ring(QQ) Univariate Polynomial Ring in x over Rational Field
>>> from sage.all import * >>> # needs sage.rings.real_mpfr >>> R = RR['x']; (x,) = R._first_ngens(1); R Univariate Polynomial Ring in x over Real Field with 53 bits of precision >>> R.base_extend(CC) Univariate Polynomial Ring in x over Complex Field with 53 bits of precision >>> R.base_extend(QQ) Traceback (most recent call last): ... TypeError: no such base extension >>> R.change_ring(QQ) Univariate Polynomial Ring in x over Rational Field
- change_ring(R)[source]¶
Return the polynomial ring in the same variable as
self
over \(R\).EXAMPLES:
sage: # needs sage.rings.finite_rings sage.rings.real_interval_field sage: R.<ZZZ> = RealIntervalField()[]; R Univariate Polynomial Ring in ZZZ over Real Interval Field with 53 bits of precision sage: R.change_ring(GF(19^2, 'b')) Univariate Polynomial Ring in ZZZ over Finite Field in b of size 19^2
>>> from sage.all import * >>> # needs sage.rings.finite_rings sage.rings.real_interval_field >>> R = RealIntervalField()['ZZZ']; (ZZZ,) = R._first_ngens(1); R Univariate Polynomial Ring in ZZZ over Real Interval Field with 53 bits of precision >>> R.change_ring(GF(Integer(19)**Integer(2), 'b')) Univariate Polynomial Ring in ZZZ over Finite Field in b of size 19^2
- change_var(var)[source]¶
Return the polynomial ring in variable
var
over the same base ring.EXAMPLES:
sage: R.<x> = ZZ[]; R Univariate Polynomial Ring in x over Integer Ring sage: R.change_var('y') Univariate Polynomial Ring in y over Integer Ring
>>> from sage.all import * >>> R = ZZ['x']; (x,) = R._first_ngens(1); R Univariate Polynomial Ring in x over Integer Ring >>> R.change_var('y') Univariate Polynomial Ring in y over Integer Ring
- characteristic()[source]¶
Return the characteristic of this polynomial ring, which is the same as that of its base ring.
EXAMPLES:
sage: # needs sage.rings.real_interval_field sage: R.<ZZZ> = RealIntervalField()[]; R Univariate Polynomial Ring in ZZZ over Real Interval Field with 53 bits of precision sage: R.characteristic() 0 sage: S = R.change_ring(GF(19^2, 'b')); S # needs sage.rings.finite_rings Univariate Polynomial Ring in ZZZ over Finite Field in b of size 19^2 sage: S.characteristic() # needs sage.rings.finite_rings 19
>>> from sage.all import * >>> # needs sage.rings.real_interval_field >>> R = RealIntervalField()['ZZZ']; (ZZZ,) = R._first_ngens(1); R Univariate Polynomial Ring in ZZZ over Real Interval Field with 53 bits of precision >>> R.characteristic() 0 >>> S = R.change_ring(GF(Integer(19)**Integer(2), 'b')); S # needs sage.rings.finite_rings Univariate Polynomial Ring in ZZZ over Finite Field in b of size 19^2 >>> S.characteristic() # needs sage.rings.finite_rings 19
- completion(p=None, prec=20, extras=None)[source]¶
Return the completion of
self
with respect to the irreducible polynomialp
.Currently only implemented for
p=self.gen()
(the default), i.e. you can only complete \(R[x]\) with respect to \(x\), the result being a ring of power series in \(x\). Theprec
variable controls the precision used in the power series ring. Ifprec
is \(\infty\), then this returns aLazyPowerSeriesRing
.EXAMPLES:
sage: P.<x> = PolynomialRing(QQ) sage: P Univariate Polynomial Ring in x over Rational Field sage: PP = P.completion(x) sage: PP Power Series Ring in x over Rational Field sage: f = 1 - x sage: PP(f) 1 - x sage: 1 / f -1/(x - 1) sage: g = 1 / PP(f); g 1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9 + x^10 + x^11 + x^12 + x^13 + x^14 + x^15 + x^16 + x^17 + x^18 + x^19 + O(x^20) sage: 1 / g 1 - x + O(x^20) sage: # needs sage.combinat sage: PP = P.completion(x, prec=oo); PP Lazy Taylor Series Ring in x over Rational Field sage: g = 1 / PP(f); g 1 + x + x^2 + O(x^3) sage: 1 / g == f True
>>> from sage.all import * >>> P = PolynomialRing(QQ, names=('x',)); (x,) = P._first_ngens(1) >>> P Univariate Polynomial Ring in x over Rational Field >>> PP = P.completion(x) >>> PP Power Series Ring in x over Rational Field >>> f = Integer(1) - x >>> PP(f) 1 - x >>> Integer(1) / f -1/(x - 1) >>> g = Integer(1) / PP(f); g 1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9 + x^10 + x^11 + x^12 + x^13 + x^14 + x^15 + x^16 + x^17 + x^18 + x^19 + O(x^20) >>> Integer(1) / g 1 - x + O(x^20) >>> # needs sage.combinat >>> PP = P.completion(x, prec=oo); PP Lazy Taylor Series Ring in x over Rational Field >>> g = Integer(1) / PP(f); g 1 + x + x^2 + O(x^3) >>> Integer(1) / g == f True
- cyclotomic_polynomial(n)[source]¶
Return the \(n\)-th cyclotomic polynomial as a polynomial in this polynomial ring. For details of the implementation, see the documentation for
sage.rings.polynomial.cyclotomic.cyclotomic_coeffs()
.EXAMPLES:
sage: R = ZZ['x'] sage: R.cyclotomic_polynomial(8) x^4 + 1 sage: R.cyclotomic_polynomial(12) x^4 - x^2 + 1 sage: S = PolynomialRing(FiniteField(7), 'x') sage: S.cyclotomic_polynomial(12) x^4 + 6*x^2 + 1 sage: S.cyclotomic_polynomial(1) x + 6
>>> from sage.all import * >>> R = ZZ['x'] >>> R.cyclotomic_polynomial(Integer(8)) x^4 + 1 >>> R.cyclotomic_polynomial(Integer(12)) x^4 - x^2 + 1 >>> S = PolynomialRing(FiniteField(Integer(7)), 'x') >>> S.cyclotomic_polynomial(Integer(12)) x^4 + 6*x^2 + 1 >>> S.cyclotomic_polynomial(Integer(1)) x + 6
- extend_variables(added_names, order='degrevlex')[source]¶
Return a multivariate polynomial ring with the same base ring but with
added_names
as additional variables.EXAMPLES:
sage: R.<x> = ZZ[]; R Univariate Polynomial Ring in x over Integer Ring sage: R.extend_variables('y, z') Multivariate Polynomial Ring in x, y, z over Integer Ring sage: R.extend_variables(('y', 'z')) Multivariate Polynomial Ring in x, y, z over Integer Ring
>>> from sage.all import * >>> R = ZZ['x']; (x,) = R._first_ngens(1); R Univariate Polynomial Ring in x over Integer Ring >>> R.extend_variables('y, z') Multivariate Polynomial Ring in x, y, z over Integer Ring >>> R.extend_variables(('y', 'z')) Multivariate Polynomial Ring in x, y, z over Integer Ring
- flattening_morphism()[source]¶
Return the flattening morphism of this polynomial ring.
EXAMPLES:
sage: QQ['a','b']['x'].flattening_morphism() Flattening morphism: From: Univariate Polynomial Ring in x over Multivariate Polynomial Ring in a, b over Rational Field To: Multivariate Polynomial Ring in a, b, x over Rational Field sage: QQ['x'].flattening_morphism() Identity endomorphism of Univariate Polynomial Ring in x over Rational Field
>>> from sage.all import * >>> QQ['a','b']['x'].flattening_morphism() Flattening morphism: From: Univariate Polynomial Ring in x over Multivariate Polynomial Ring in a, b over Rational Field To: Multivariate Polynomial Ring in a, b, x over Rational Field >>> QQ['x'].flattening_morphism() Identity endomorphism of Univariate Polynomial Ring in x over Rational Field
- gen(n=0)[source]¶
Return the indeterminate generator of this polynomial ring.
EXAMPLES:
sage: R.<abc> = Integers(8)[]; R Univariate Polynomial Ring in abc over Ring of integers modulo 8 sage: t = R.gen(); t abc sage: t.is_gen() True
>>> from sage.all import * >>> R = Integers(Integer(8))['abc']; (abc,) = R._first_ngens(1); R Univariate Polynomial Ring in abc over Ring of integers modulo 8 >>> t = R.gen(); t abc >>> t.is_gen() True
An identical generator is always returned.
sage: t is R.gen() True
>>> from sage.all import * >>> t is R.gen() True
- gens_dict()[source]¶
Return a dictionary whose entries are
{name:variable,...}
, wherename
stands for the variable names of this object (as strings) andvariable
stands for the corresponding generators (as elements of this object).EXAMPLES:
sage: R.<y,x,a42> = RR[] sage: R.gens_dict() {'a42': a42, 'x': x, 'y': y}
>>> from sage.all import * >>> R = RR['y, x, a42']; (y, x, a42,) = R._first_ngens(3) >>> R.gens_dict() {'a42': a42, 'x': x, 'y': y}
- is_exact()[source]¶
EXAMPLES:
sage: class Foo: ....: def __init__(self, x): ....: self._x = x ....: @cached_method ....: def f(self): ....: return self._x^2 sage: a = Foo(2) sage: print(a.f.cache) None sage: a.f() 4 sage: a.f.cache 4
>>> from sage.all import * >>> class Foo: ... def __init__(self, x): ... self._x = x ... @cached_method ... def f(self): ... return self._x**Integer(2) >>> a = Foo(Integer(2)) >>> print(a.f.cache) None >>> a.f() 4 >>> a.f.cache 4
- is_field(proof=True)[source]¶
Return
False
, since polynomial rings are never fields.EXAMPLES:
sage: # needs sage.libs.ntl sage: R.<z> = Integers(2)[]; R Univariate Polynomial Ring in z over Ring of integers modulo 2 (using GF2X) sage: R.is_field() False
>>> from sage.all import * >>> # needs sage.libs.ntl >>> R = Integers(Integer(2))['z']; (z,) = R._first_ngens(1); R Univariate Polynomial Ring in z over Ring of integers modulo 2 (using GF2X) >>> R.is_field() False
- is_integral_domain(proof=True)[source]¶
EXAMPLES:
sage: ZZ['x'].is_integral_domain() True sage: Integers(8)['x'].is_integral_domain() False
>>> from sage.all import * >>> ZZ['x'].is_integral_domain() True >>> Integers(Integer(8))['x'].is_integral_domain() False
- is_sparse()[source]¶
Return
True
if elements of this polynomial ring have a sparse representation.EXAMPLES:
sage: R.<z> = Integers(8)[]; R Univariate Polynomial Ring in z over Ring of integers modulo 8 sage: R.is_sparse() False sage: R.<W> = PolynomialRing(QQ, sparse=True); R Sparse Univariate Polynomial Ring in W over Rational Field sage: R.is_sparse() True
>>> from sage.all import * >>> R = Integers(Integer(8))['z']; (z,) = R._first_ngens(1); R Univariate Polynomial Ring in z over Ring of integers modulo 8 >>> R.is_sparse() False >>> R = PolynomialRing(QQ, sparse=True, names=('W',)); (W,) = R._first_ngens(1); R Sparse Univariate Polynomial Ring in W over Rational Field >>> R.is_sparse() True
- is_unique_factorization_domain(proof=True)[source]¶
EXAMPLES:
sage: ZZ['x'].is_unique_factorization_domain() True sage: Integers(8)['x'].is_unique_factorization_domain() False
>>> from sage.all import * >>> ZZ['x'].is_unique_factorization_domain() True >>> Integers(Integer(8))['x'].is_unique_factorization_domain() False
- karatsuba_threshold()[source]¶
Return the Karatsuba threshold used for this ring by the method
_mul_karatsuba()
to fall back to the schoolbook algorithm.EXAMPLES:
sage: K = QQ['x'] sage: K.karatsuba_threshold() 8 sage: K = QQ['x']['y'] sage: K.karatsuba_threshold() 0
>>> from sage.all import * >>> K = QQ['x'] >>> K.karatsuba_threshold() 8 >>> K = QQ['x']['y'] >>> K.karatsuba_threshold() 0
- krull_dimension()[source]¶
Return the Krull dimension of this polynomial ring, which is one more than the Krull dimension of the base ring.
EXAMPLES:
sage: R.<x> = QQ[] sage: R.krull_dimension() 1 sage: # needs sage.rings.finite_rings sage: R.<z> = GF(9, 'a')[]; R Univariate Polynomial Ring in z over Finite Field in a of size 3^2 sage: R.krull_dimension() 1 sage: S.<t> = R[] sage: S.krull_dimension() 2 sage: for n in range(10): ....: S = PolynomialRing(S, 'w') sage: S.krull_dimension() 12
>>> from sage.all import * >>> R = QQ['x']; (x,) = R._first_ngens(1) >>> R.krull_dimension() 1 >>> # needs sage.rings.finite_rings >>> R = GF(Integer(9), 'a')['z']; (z,) = R._first_ngens(1); R Univariate Polynomial Ring in z over Finite Field in a of size 3^2 >>> R.krull_dimension() 1 >>> S = R['t']; (t,) = S._first_ngens(1) >>> S.krull_dimension() 2 >>> for n in range(Integer(10)): ... S = PolynomialRing(S, 'w') >>> S.krull_dimension() 12
- monics(of_degree=None, max_degree=None)[source]¶
Return an iterator over the monic polynomials of specified degree.
INPUT: Pass exactly one of:
max_degree
– an int; the iterator will generate all monic polynomials which have degree less than or equal tomax_degree
of_degree
– an int; the iterator will generate all monic polynomials which have degreeof_degree
OUTPUT: an iterator
EXAMPLES:
sage: # needs sage.rings.finite_rings sage: P = PolynomialRing(GF(4, 'a'), 'y') sage: for p in P.monics(of_degree=2): print(p) y^2 y^2 + a y^2 + a + 1 y^2 + 1 y^2 + a*y y^2 + a*y + a y^2 + a*y + a + 1 y^2 + a*y + 1 y^2 + (a + 1)*y y^2 + (a + 1)*y + a y^2 + (a + 1)*y + a + 1 y^2 + (a + 1)*y + 1 y^2 + y y^2 + y + a y^2 + y + a + 1 y^2 + y + 1 sage: for p in P.monics(max_degree=1): print(p) 1 y y + a y + a + 1 y + 1 sage: for p in P.monics(max_degree=1, of_degree=3): print(p) Traceback (most recent call last): ... ValueError: you should pass exactly one of of_degree and max_degree
>>> from sage.all import * >>> # needs sage.rings.finite_rings >>> P = PolynomialRing(GF(Integer(4), 'a'), 'y') >>> for p in P.monics(of_degree=Integer(2)): print(p) y^2 y^2 + a y^2 + a + 1 y^2 + 1 y^2 + a*y y^2 + a*y + a y^2 + a*y + a + 1 y^2 + a*y + 1 y^2 + (a + 1)*y y^2 + (a + 1)*y + a y^2 + (a + 1)*y + a + 1 y^2 + (a + 1)*y + 1 y^2 + y y^2 + y + a y^2 + y + a + 1 y^2 + y + 1 >>> for p in P.monics(max_degree=Integer(1)): print(p) 1 y y + a y + a + 1 y + 1 >>> for p in P.monics(max_degree=Integer(1), of_degree=Integer(3)): print(p) Traceback (most recent call last): ... ValueError: you should pass exactly one of of_degree and max_degree
AUTHORS:
Joel B. Mohler
- monomial(exponent)[source]¶
Return the monomial with the
exponent
.INPUT:
exponent
– nonnegative integer
EXAMPLES:
sage: R.<x> = PolynomialRing(ZZ) sage: R.monomial(5) x^5 sage: e=(10,) sage: R.monomial(*e) x^10 sage: m = R.monomial(100) sage: R.monomial(m.degree()) == m True
>>> from sage.all import * >>> R = PolynomialRing(ZZ, names=('x',)); (x,) = R._first_ngens(1) >>> R.monomial(Integer(5)) x^5 >>> e=(Integer(10),) >>> R.monomial(*e) x^10 >>> m = R.monomial(Integer(100)) >>> R.monomial(m.degree()) == m True
- ngens()[source]¶
Return the number of generators of this polynomial ring, which is 1 since it is a univariate polynomial ring.
EXAMPLES:
sage: R.<z> = Integers(8)[]; R Univariate Polynomial Ring in z over Ring of integers modulo 8 sage: R.ngens() 1
>>> from sage.all import * >>> R = Integers(Integer(8))['z']; (z,) = R._first_ngens(1); R Univariate Polynomial Ring in z over Ring of integers modulo 8 >>> R.ngens() 1
- polynomials(of_degree=None, max_degree=None)[source]¶
Return an iterator over the polynomials of specified degree.
INPUT: Pass exactly one of:
max_degree
– an int; the iterator will generate all polynomials which have degree less than or equal tomax_degree
of_degree
– an int; the iterator will generate all polynomials which have degreeof_degree
OUTPUT: an iterator
EXAMPLES:
sage: P = PolynomialRing(GF(3), 'y') sage: for p in P.polynomials(of_degree=2): print(p) y^2 y^2 + 1 y^2 + 2 y^2 + y y^2 + y + 1 y^2 + y + 2 y^2 + 2*y y^2 + 2*y + 1 y^2 + 2*y + 2 2*y^2 2*y^2 + 1 2*y^2 + 2 2*y^2 + y 2*y^2 + y + 1 2*y^2 + y + 2 2*y^2 + 2*y 2*y^2 + 2*y + 1 2*y^2 + 2*y + 2 sage: for p in P.polynomials(max_degree=1): print(p) 0 1 2 y y + 1 y + 2 2*y 2*y + 1 2*y + 2 sage: for p in P.polynomials(max_degree=1, of_degree=3): print(p) Traceback (most recent call last): ... ValueError: you should pass exactly one of of_degree and max_degree
>>> from sage.all import * >>> P = PolynomialRing(GF(Integer(3)), 'y') >>> for p in P.polynomials(of_degree=Integer(2)): print(p) y^2 y^2 + 1 y^2 + 2 y^2 + y y^2 + y + 1 y^2 + y + 2 y^2 + 2*y y^2 + 2*y + 1 y^2 + 2*y + 2 2*y^2 2*y^2 + 1 2*y^2 + 2 2*y^2 + y 2*y^2 + y + 1 2*y^2 + y + 2 2*y^2 + 2*y 2*y^2 + 2*y + 1 2*y^2 + 2*y + 2 >>> for p in P.polynomials(max_degree=Integer(1)): print(p) 0 1 2 y y + 1 y + 2 2*y 2*y + 1 2*y + 2 >>> for p in P.polynomials(max_degree=Integer(1), of_degree=Integer(3)): print(p) Traceback (most recent call last): ... ValueError: you should pass exactly one of of_degree and max_degree
AUTHORS:
Joel B. Mohler
- random_element(degree=(-1, 2), monic=False, *args, **kwds)[source]¶
Return a random polynomial of given degree (bounds).
INPUT:
degree
– (default:(-1, 2)
) integer for fixing the degree or a tuple of minimum and maximum degreesmonic
– boolean (default:False
); indicate whether the sampled polynomial should be monic*args, **kwds
– additional keyword parameters passed on to therandom_element
method for the base ring
EXAMPLES:
sage: R.<x> = ZZ[] sage: f = R.random_element(10, x=5, y=10) sage: f.degree() 10 sage: f.parent() is R True sage: all(a in range(5, 10) for a in f.coefficients()) True sage: R.random_element(6).degree() 6
>>> from sage.all import * >>> R = ZZ['x']; (x,) = R._first_ngens(1) >>> f = R.random_element(Integer(10), x=Integer(5), y=Integer(10)) >>> f.degree() 10 >>> f.parent() is R True >>> all(a in range(Integer(5), Integer(10)) for a in f.coefficients()) True >>> R.random_element(Integer(6)).degree() 6
If a tuple of two integers is given for the
degree
argument, a polynomial is chosen among all polynomials with degree between them. If the base ring can be sampled uniformly, then this method also samples uniformly:sage: R.random_element(degree=(0, 4)).degree() in range(0, 5) True sage: found = [False]*5 sage: while not all(found): ....: found[R.random_element(degree=(0, 4)).degree()] = True
>>> from sage.all import * >>> R.random_element(degree=(Integer(0), Integer(4))).degree() in range(Integer(0), Integer(5)) True >>> found = [False]*Integer(5) >>> while not all(found): ... found[R.random_element(degree=(Integer(0), Integer(4))).degree()] = True
Note that the zero polynomial has degree \(-1\), so if you want to consider it set the minimum degree to \(-1\):
sage: while R.random_element(degree=(-1,2), x=-1, y=1) != R.zero(): ....: pass
>>> from sage.all import * >>> while R.random_element(degree=(-Integer(1),Integer(2)), x=-Integer(1), y=Integer(1)) != R.zero(): ... pass
Monic polynomials are chosen among all monic polynomials with degree between the given
degree
argument:sage: all(R.random_element(degree=(-1, 1), monic=True).is_monic() for _ in range(10^3)) True sage: all(R.random_element(degree=(0, 1), monic=True).is_monic() for _ in range(10^3)) True
>>> from sage.all import * >>> all(R.random_element(degree=(-Integer(1), Integer(1)), monic=True).is_monic() for _ in range(Integer(10)**Integer(3))) True >>> all(R.random_element(degree=(Integer(0), Integer(1)), monic=True).is_monic() for _ in range(Integer(10)**Integer(3))) True
- set_karatsuba_threshold(Karatsuba_threshold)[source]¶
Changes the default threshold for this ring in the method
_mul_karatsuba()
to fall back to the schoolbook algorithm.Warning
This method may have a negative performance impact in polynomial arithmetic. So use it at your own risk.
EXAMPLES:
sage: K = QQ['x'] sage: K.karatsuba_threshold() 8 sage: K.set_karatsuba_threshold(0) sage: K.karatsuba_threshold() 0
>>> from sage.all import * >>> K = QQ['x'] >>> K.karatsuba_threshold() 8 >>> K.set_karatsuba_threshold(Integer(0)) >>> K.karatsuba_threshold() 0
- some_elements()[source]¶
Return a list of polynomials.
This is typically used for running generic tests.
EXAMPLES:
sage: R.<x> = QQ[] sage: R.some_elements() [x, 0, 1, 1/2, x^2 + 2*x + 1, x^3, x^2 - 1, x^2 + 1, 2*x^2 + 2]
>>> from sage.all import * >>> R = QQ['x']; (x,) = R._first_ngens(1) >>> R.some_elements() [x, 0, 1, 1/2, x^2 + 2*x + 1, x^3, x^2 - 1, x^2 + 1, 2*x^2 + 2]
- variable_names_recursive(depth=+Infinity)[source]¶
Return the list of variable names of this ring and its base rings, as if it were a single multi-variate polynomial.
INPUT:
depth
– integer orInfinity
OUTPUT: a tuple of strings
EXAMPLES:
sage: R = QQ['x']['y']['z'] sage: R.variable_names_recursive() ('x', 'y', 'z') sage: R.variable_names_recursive(2) ('y', 'z')
>>> from sage.all import * >>> R = QQ['x']['y']['z'] >>> R.variable_names_recursive() ('x', 'y', 'z') >>> R.variable_names_recursive(Integer(2)) ('y', 'z')
- class sage.rings.polynomial.polynomial_ring.PolynomialRing_integral_domain(base_ring, name='x', sparse=False, implementation=None, element_class=None, category=None)[source]¶
Bases:
PolynomialRing_commutative
,PolynomialRing_singular_repr
,IntegralDomain
- construction()[source]¶
Return the construction functor.
EXAMPLES:
sage: from sage.rings.polynomial.polynomial_ring import PolynomialRing_integral_domain as PRing sage: R = PRing(ZZ, 'x'); R Univariate Polynomial Ring in x over Integer Ring sage: functor, arg = R.construction(); functor, arg (Poly[x], Integer Ring) sage: functor.implementation is None True sage: # needs sage.libs.ntl sage: R = PRing(ZZ, 'x', implementation='NTL'); R Univariate Polynomial Ring in x over Integer Ring (using NTL) sage: functor, arg = R.construction(); functor, arg (Poly[x], Integer Ring) sage: functor.implementation 'NTL'
>>> from sage.all import * >>> from sage.rings.polynomial.polynomial_ring import PolynomialRing_integral_domain as PRing >>> R = PRing(ZZ, 'x'); R Univariate Polynomial Ring in x over Integer Ring >>> functor, arg = R.construction(); functor, arg (Poly[x], Integer Ring) >>> functor.implementation is None True >>> # needs sage.libs.ntl >>> R = PRing(ZZ, 'x', implementation='NTL'); R Univariate Polynomial Ring in x over Integer Ring (using NTL) >>> functor, arg = R.construction(); functor, arg (Poly[x], Integer Ring) >>> functor.implementation 'NTL'
- weil_polynomials(d, q, sign=1, lead=1)[source]¶
Return all integer polynomials whose complex roots all have a specified absolute value.
Such polynomials \(f\) satisfy a functional equation
\[T^d f(q/T) = s q^{d/2} f(T)\]where \(d\) is the degree of \(f\), \(s\) is a sign and \(q^{1/2}\) is the absolute value of the roots of \(f\).
INPUT:
d
– integer; the degree of the polynomialsq
– integer; the square of the complex absolute value of the rootssign
– integer (default: \(1\)); the sign \(s\) of the functional equationlead
– integer; list of integers or list of pairs of integers (default: \(1\)), constraints on the leading few coefficients of the generated polynomials. If pairs \((a, b)\) of integers are given, they are treated as a constraint of the form \(\equiv a \pmod{b}\); the moduli must be in decreasing order by divisibility, and the modulus of the leading coefficient must be 0.
See also
More documentation and additional options are available using the iterator
sage.rings.polynomial.weil.weil_polynomials.WeilPolynomials
directly. In addition, polynomials have a methodis_weil_polynomial()
to test whether or not the given polynomial is a Weil polynomial.EXAMPLES:
sage: # needs sage.libs.flint sage: R.<T> = ZZ[] sage: L = R.weil_polynomials(4, 2) sage: len(L) 35 sage: L[9] T^4 + T^3 + 2*T^2 + 2*T + 4 sage: all(p.is_weil_polynomial() for p in L) True
>>> from sage.all import * >>> # needs sage.libs.flint >>> R = ZZ['T']; (T,) = R._first_ngens(1) >>> L = R.weil_polynomials(Integer(4), Integer(2)) >>> len(L) 35 >>> L[Integer(9)] T^4 + T^3 + 2*T^2 + 2*T + 4 >>> all(p.is_weil_polynomial() for p in L) True
Setting multiple leading coefficients:
sage: R.<T> = QQ[] sage: l = R.weil_polynomials(4, 2, lead=((1,0), (2,4), (1,2))); l # needs sage.libs.flint [T^4 + 2*T^3 + 5*T^2 + 4*T + 4, T^4 + 2*T^3 + 3*T^2 + 4*T + 4, T^4 - 2*T^3 + 5*T^2 - 4*T + 4, T^4 - 2*T^3 + 3*T^2 - 4*T + 4]
>>> from sage.all import * >>> R = QQ['T']; (T,) = R._first_ngens(1) >>> l = R.weil_polynomials(Integer(4), Integer(2), lead=((Integer(1),Integer(0)), (Integer(2),Integer(4)), (Integer(1),Integer(2)))); l # needs sage.libs.flint [T^4 + 2*T^3 + 5*T^2 + 4*T + 4, T^4 + 2*T^3 + 3*T^2 + 4*T + 4, T^4 - 2*T^3 + 5*T^2 - 4*T + 4, T^4 - 2*T^3 + 3*T^2 - 4*T + 4]
We do not require Weil polynomials to be monic. This example generates Weil polynomials associated to K3 surfaces over \(\GF{2}\) of Picard number at least 12:
sage: R.<T> = QQ[] sage: l = R.weil_polynomials(10, 1, lead=2) # needs sage.libs.flint sage: len(l) # needs sage.libs.flint 4865 sage: l[len(l)//2] # needs sage.libs.flint 2*T^10 + T^8 + T^6 + T^4 + T^2 + 2
>>> from sage.all import * >>> R = QQ['T']; (T,) = R._first_ngens(1) >>> l = R.weil_polynomials(Integer(10), Integer(1), lead=Integer(2)) # needs sage.libs.flint >>> len(l) # needs sage.libs.flint 4865 >>> l[len(l)//Integer(2)] # needs sage.libs.flint 2*T^10 + T^8 + T^6 + T^4 + T^2 + 2
- sage.rings.polynomial.polynomial_ring.is_PolynomialRing(x)[source]¶
Return
True
ifx
is a univariate polynomial ring (and not a sparse multivariate polynomial ring in one variable).EXAMPLES:
sage: from sage.rings.polynomial.polynomial_ring import is_PolynomialRing sage: from sage.rings.polynomial.multi_polynomial_ring import is_MPolynomialRing sage: is_PolynomialRing(2) doctest:warning... DeprecationWarning: The function is_PolynomialRing is deprecated; use 'isinstance(..., PolynomialRing_general)' instead. See https://github.com/sagemath/sage/issues/38266 for details. False
>>> from sage.all import * >>> from sage.rings.polynomial.polynomial_ring import is_PolynomialRing >>> from sage.rings.polynomial.multi_polynomial_ring import is_MPolynomialRing >>> is_PolynomialRing(Integer(2)) doctest:warning... DeprecationWarning: The function is_PolynomialRing is deprecated; use 'isinstance(..., PolynomialRing_general)' instead. See https://github.com/sagemath/sage/issues/38266 for details. False
This polynomial ring is not univariate.
sage: is_PolynomialRing(ZZ['x,y,z']) False sage: is_MPolynomialRing(ZZ['x,y,z']) doctest:warning... DeprecationWarning: The function is_MPolynomialRing is deprecated; use 'isinstance(..., MPolynomialRing_base)' instead. See https://github.com/sagemath/sage/issues/38266 for details. True
>>> from sage.all import * >>> is_PolynomialRing(ZZ['x,y,z']) False >>> is_MPolynomialRing(ZZ['x,y,z']) doctest:warning... DeprecationWarning: The function is_MPolynomialRing is deprecated; use 'isinstance(..., MPolynomialRing_base)' instead. See https://github.com/sagemath/sage/issues/38266 for details. True
sage: is_PolynomialRing(ZZ['w']) True
>>> from sage.all import * >>> is_PolynomialRing(ZZ['w']) True
Univariate means not only in one variable, but is a specific data type. There is a multivariate (sparse) polynomial ring data type, which supports a single variable as a special case.
sage: # needs sage.libs.singular sage: R.<w> = PolynomialRing(ZZ, implementation='singular'); R Multivariate Polynomial Ring in w over Integer Ring sage: is_PolynomialRing(R) False sage: type(R) <class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'>
>>> from sage.all import * >>> # needs sage.libs.singular >>> R = PolynomialRing(ZZ, implementation='singular', names=('w',)); (w,) = R._first_ngens(1); R Multivariate Polynomial Ring in w over Integer Ring >>> is_PolynomialRing(R) False >>> type(R) <class 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'>
- sage.rings.polynomial.polynomial_ring.polygen(ring_or_element, name='x')[source]¶
Return a polynomial indeterminate.
INPUT:
polygen(base_ring, name='x')
polygen(ring_element, name='x')
If the first input is a ring, return a polynomial generator over that ring. If it is a ring element, return a polynomial generator over the parent of the element.
EXAMPLES:
sage: z = polygen(QQ, 'z') sage: z^3 + z +1 z^3 + z + 1 sage: parent(z) Univariate Polynomial Ring in z over Rational Field
>>> from sage.all import * >>> z = polygen(QQ, 'z') >>> z**Integer(3) + z +Integer(1) z^3 + z + 1 >>> parent(z) Univariate Polynomial Ring in z over Rational Field
Note
If you give a list or comma-separated string to
polygen()
, you’ll get a tuple of indeterminates, exactly as if you calledpolygens()
.
- sage.rings.polynomial.polynomial_ring.polygens(base_ring, names='x', *args)[source]¶
Return indeterminates over the given base ring with the given names.
EXAMPLES:
sage: x,y,z = polygens(QQ,'x,y,z') sage: (x+y+z)^2 x^2 + 2*x*y + y^2 + 2*x*z + 2*y*z + z^2 sage: parent(x) Multivariate Polynomial Ring in x, y, z over Rational Field sage: t = polygens(QQ, ['x','yz','abc']) sage: t (x, yz, abc)
>>> from sage.all import * >>> x,y,z = polygens(QQ,'x,y,z') >>> (x+y+z)**Integer(2) x^2 + 2*x*y + y^2 + 2*x*z + 2*y*z + z^2 >>> parent(x) Multivariate Polynomial Ring in x, y, z over Rational Field >>> t = polygens(QQ, ['x','yz','abc']) >>> t (x, yz, abc)
The number of generators can be passed as a third argument:
sage: polygens(QQ, 'x', 4) (x0, x1, x2, x3)
>>> from sage.all import * >>> polygens(QQ, 'x', Integer(4)) (x0, x1, x2, x3)