14.6 Multivariate Polynomials

Module: sage.rings.multi_polynomial_element

Author Log:

- Martin Albrecht: improved singular coercions (restructed class hierarchy)

Module-level Functions

degree_lowest_rational_function( r, x)

INPUT:
    r -- a multivariate rational function
    x -- a multivariate polynomial ring generator x

OUTPUT:
    integer -- the degree of r in x and its "leading"
               (in the x-adic sense) coefficient.

sage: R1 = MPolynomialRing(FiniteField(5), 3, names = ["a","b","c"])
sage: F = FractionField(R1)
sage: a,b,c = R1.gens()
sage: f = 3*a*b^2*c^3+4*a*b*c
sage: g = a^2*b*c^2+2*a^2*b^4*c^7

Consider the quotient $ f/g = \frac{4 + 3 bc^{2}}{ac + 2 ab^{3}c^{6}}$ (note the cancellation).

sage: r = f/g; r
(4 + 3*b*c^2)/(a*c + 2*a*b^3*c^6)
sage: degree_lowest_rational_function(r,a)
      (-1, 4)
sage: degree_lowest_rational_function(r,b)
      (0, 4)
sage: degree_lowest_rational_function(r,c)
      (-1, 4)

is_MPolynomialRingElement( x)

Class: MPolynomial

class MPolynomial
MPolynomial( self, parent, x)

Functions: element

Special Functions: __call__,$  $ __div__,$  $ __pow__,$  $ __rpow__,$  $ _add_,$  $ _cmp_,$  $ _div_,$  $ _im_gens_,$  $ _mul_,$  $ _repr_,$  $ _sub_

__call__( self)

Evaluate this multi-variate polynomial at $ x$ , where $ x$ is either the tuple of values to substitute in, or one can use functional notation $ f(a_0,a_1,a_2, \ldots)$ to evaluate $ f$ with the ith variable replaced by $ a_i$ .

sage: R.<x, y> = MPolynomialRing(RationalField(),2)
sage: f = x^2 + y^2
sage: f(1,2)
5
sage: f((1,2))
5

sage: x = MPolynomialRing(RationalField(),3).gens()
sage: f = x[0] + x[1] - 2*x[1]*x[2]
sage: f
x1 - 2*x1*x2 + x0
sage: f(1,2,0)
3
sage: f(1,2,5)
-17

Author: David Kohel, 2005-09-27

__div__( self, right)

sage: R.<x,y> = QQ['x,y']
sage: f = (x + y)/3
sage: f.parent()
Polynomial Ring in x, y over Rational Field

If we do the same over $ \mathbf{Z}$ the result has to lie in the fraction field.

sage: x,y = ZZ['x,y'].gens()
sage: f = (x + y)/3      
sage: f.parent()
Fraction Field of Polynomial Ring in x, y over Integer Ring

Note that / is still a constructor for elements of the fraction field in all cases as long as both arguments have the same parent.

sage: R.<x,y> = PolynomialRing(QQ, 2)
sage: f = x^3 + y
sage: g = R(3)
sage: h = f/g; h
1/3*y + 1/3*x^3
sage: h.parent()
Fraction Field of Polynomial Ring in x, y over Rational Field

_im_gens_( self, codomain, im_gens)

sage: R.<x,y> = PolynomialRing(QQ, 2)
sage: f = R.hom([y,x], R)
sage: f(x^2 + 3*y^5)
y^2 + 3*x^5

Class: MPolynomial_macaulay2_repr

class MPolynomial_macaulay2_repr
Multivariate polynomials that are representable in Macaulay2.

Special Functions: _macaulay2_

_macaulay2_( self, [macaulay2=Macaulay2])

Return corresponding Macaulay2 polynomial.

sage: R.<x,y> = PolynomialRing(GF(7), 2, macaulay2=True)   # optional
sage: f = (x^3 + 2*y^2*x)^7; f          # optional
2*x^7*y^14 + x^21
sage: h = f._macaulay2_(); h            # optional
x^21+2*x^7*y^14
sage: R(h)                              # optional
2*x^7*y^14 + x^21
sage: R(h^20) == f^20                   # optional
True

Class: MPolynomial_polydict

class MPolynomial_polydict
MPolynomial_polydict( self, parent, x)

sage: R, x = MPolynomialRing(QQ, 10).objgens()
sage: x
(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9)
sage: loads(dumps(x)) == x
True

Functions: coefficient,$  $ constant_coefficient,$  $ degree,$  $ exponents,$  $ factor,$  $ fix,$  $ gcd,$  $ homogenize,$  $ inverse_of_unit,$  $ is_constant,$  $ is_homogeneous,$  $ is_monomial,$  $ is_unit,$  $ is_univariate,$  $ monomial_coefficient,$  $ monomials,$  $ newton_polytope,$  $ nvariables,$  $ quo_rem,$  $ total_degree,$  $ univariate_polynomial,$  $ variable,$  $ variables

coefficient( self, mon)

Return the coefficient of mon in self, where mon must have the same parent as self. The coefficient is defined as follows. If f is this polynomial, then the coefficient is the sum T/mon where the sum is over terms T in f that are exactly divisible by mon.

INPUT:
    mon -- a monomial

OUTPUT:
    element of the parent of self

sage: x, y = MPolynomialRing(RationalField(), 2, names = ['x','y']).gens()
sage: f = y^2 - x^9 - 7*x + 5*x*y
sage: f.coefficient(y)
5*x
sage: f = y - x^9*y - 7*x + 5*x*y
sage: f.coefficient(y)
1 + 5*x - x^9

constant_coefficient( self)

Return the constant coefficient of this multivariate polynomial.

sage: x, y = ZZ['x,y'].gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.constant_coefficient()
5
sage: f = 3*x^2 
sage: f.constant_coefficient()
0

degree( self, [x=None])

Return the degree of self in x, where x must be one of the generators for the parent of self.

INPUT:
    x -- multivariate polynmial (a generator of the parent of self)
         If x is not specified (or is None), return the total degree,
         which is the maximum degree of any monomial.

OUTPUT:
    integer

sage: R.<x, y> = MPolynomialRing(QQ, 2)
sage: f = y^2 - x^9 - x
sage: f.degree(x)
9
sage: f.degree(y)
2
sage: (y^10*x - 7*x^2*y^5 + 5*x^3).degree(x)
3
sage: (y^10*x - 7*x^2*y^5 + 5*x^3).degree(y)
10

exponents( self)

Return the exponents of the monomials appearing in self.

sage: R.<a,b,c> = PolynomialRing(QQ, 3)
sage: f = a^3 + b + 2*b^2
sage: f.exponents()
[(0, 2, 0), (3, 0, 0), (0, 1, 0)]

factor( self)

Compute the irreducible factorization of this polynomial.

ALGORITHM: Use Singular.

sage: x, y = PolynomialRing(QQ, 2, ['x','y']).gens()
sage: f = (x^3 + 2*y^2*x) * (x^2 + x + 1); f
2*x*y^2 + 2*x^2*y^2 + x^3 + 2*x^3*y^2 + x^4 + x^5
sage: F = f.factor()
sage: F
x * (2*y^2 + x^2) * (1 + x + x^2)

Next we factor the same polynomial, but over the finite field of order $ 3$ .

sage: x, y = PolynomialRing(GF(3), 2, ['x','y']).gens()
sage: f = (x^3 + 2*y^2*x) * (x^2 + x + 1); f
2*x*y^2 + 2*x^2*y^2 + x^3 + 2*x^3*y^2 + x^4 + x^5
sage: F = f.factor()
sage: F
2 * x * (2 + x)^2 * (y + x) * (y + 2*x)

Note: Singular multi-variate polynomial factorization is very slow in SAGE. This not a fault of Singular but of how the SAGE NTL is built. If you download and install a Singular binary from the Singular website it will not have this problem (you can use it with SAGE by putting it in local/bin/).

fix( self, fixed)

Fixes some given variables in a given multivariate polynomial and returns the changed multivariate polynomials. The polynomial itself is not affected. The variable,value pairs for fixing are to be provided as dictionary of the form variable:value.

This is a special case of evaluating the polynomial with some of the variables constants and the others the original variables, but should be much faster.

INPUT:
    fixed -- dict with variable:value pairs

OUTPUT:
    new MPolynomial

sage: x, y = MPolynomialRing(ZZ,2,'xy').gens()  
sage: f = x^2 + y + x^2*y^2 + 5
sage: f((5,y))
30 + y + 25*y^2
sage: f.fix({x:5})
30 + y + 25*y^2

gcd( self, f)

Compute the greatest common divisor of this polynomial and f.

ALGORITHM: Use Singular.

sage: x, y = PolynomialRing(RationalField(), 2, ['x','y']).gens()
sage: f = (x^3 + 2*y^2*x)^2
sage: g = x^2*y^2
sage: f.gcd(g)
x^2

homogenize( self, [var=h])

Return self is self is homogeneous. Otherwise return a homogeneous polynomial in one more variable such that setting that variable equal to 1 yields self.

INPUT:
    var -- string (default: "h"); a variable name for the new variable 
           to be added in when homogenizing.  
           
OUTPUT:
    a multivariate polynomial

sage: x,y = MPolynomialRing(RationalField(),2,['x','y']).gens()
sage: f = x^2 + y + 1 + 5*x*y^10
sage: g = f.homogenize('z'); g
z^11 + y*z^10 + 5*x*y^10 + x^2*z^9
sage: g.parent()
Polynomial Ring in x, y, z over Rational Field

is_constant( self)

True if polynomial is constant, and False otherwise.

sage: x, y = MPolynomialRing(ZZ,2,'xy').gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.is_constant()
False
sage: g = 10*x^0
sage: g.is_constant()
True

is_homogeneous( self)

Return True if self is a homogeneous polynomial.

sage: x, y = MPolynomialRing(RationalField(), 2, names=['x', 'y']).gens()
sage: (x+y).is_homogeneous()
True
sage: (x.parent()(0)).is_homogeneous()
True
sage: (x+y^2).is_homogeneous()
False
sage: (x^2 + y^2).is_homogeneous()
True
sage: (x^2 + y^2*x).is_homogeneous()
False
sage: (x^2*y + y^2*x).is_homogeneous()
True

is_unit( self)

Return True if self is a unit.

sage: R = PolynomialRing(IntegerRing(), 2, ['x','y']); x,y = R.gens()
sage: (x+y).is_unit()
False
sage: R(0).is_unit()
False
sage: R(-1).is_unit()
True
sage: R(-1 + x).is_unit()
False
sage: R(2).is_unit()
False

is_univariate( self)

Returns True if this multivariate polynomial is univariate and False otherwise.

sage: x, y = MPolynomialRing(ZZ,2,'xy').gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.is_univariate()
False
sage: g = f.fix({x:10}); g
305 - 2*y + 700*y^2
sage: g.is_univariate()
True
sage: f = x^0
sage: f.is_univariate()
True

monomial_coefficient( self, mon)

Return the coefficient of the monomial mon in self, where mon must have the same parent as self.

INPUT:
    mon -- a monomial

OUTPUT:
    ring element

sage: x, y = MPolynomialRing(RationalField(), 2, names = ['x','y']).gens()
sage: f = y^2 - x^9 - 7*x + 5*x*y
sage: f.monomial_coefficient(y^2)
1
sage: f.monomial_coefficient(x*y)
5
sage: f.monomial_coefficient(x^9)
-1
sage: f.monomial_coefficient(x^10)
0

monomials( self)

Returns list of all monomials which occure in this multivariate polynomial.

OUTPUT: list of MPolynomials representing Monomials

sage: x, y = MPolynomialRing(ZZ,2,'xy').gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.monomials()
[y, x^2, 1, x^2*y^2]

newton_polytope( self)

Return the Newton polytope of this polynomial.

You should have the optional polymake package installed.

sage: R.<x,y> = PolynomialRing(QQ,2)
sage: f = 1 + x*y + x^3 + y^3
sage: P = f.newton_polytope()
sage: P
Convex hull of points [[1, 3, 0], [1, 0, 3], [1, 0, 0], [1, 1, 1]]
sage: P.facets()
[(0, 1, 0), (3, -1, -1), (0, 0, 1)]
sage: P.is_simple()
True

nvariables( self)

Number of variables in this polynomial

sage: x, y = MPolynomialRing(ZZ, 2,'xy').gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.nvariables ()
2
sage: g = f.fix({x:10}); g
305 - 2*y + 700*y^2
sage: g.nvariables ()
1

quo_rem( self, right)

Returns quotient and remainder of self and right.

ALGORITHM: Use Singular.

total_degree( self)

Return the total degree of self, which is the maximum degree of any monomial in self.

sage: R.<x,y,z> = MPolynomialRing(QQ, 3)
sage: f=2*x*y^3*z^2
sage: f.total_degree()
6
sage: f=4*x^2*y^2*z^3
sage: f.total_degree()
7
sage: f=99*x^6*y^3*z^9
sage: f.total_degree()
18
sage: f=x*y^3*z^6+3*x^2
sage: f.total_degree()
10
sage: f=z^3+8*x^4*y^5*z
sage: f.total_degree()
10
sage: f=z^9+10*x^4+y^8*x^2
sage: f.total_degree()
10

univariate_polynomial( self, [R=None])

Returns a univariate polynomial associated to this multivariate polynomial.

INPUT:
    R -- (defualt: None) PolynomialRing

If this polynomial is not in at most one variable, then a ValueError exception is raised. This is checked using the is_univariate() method. The new Polynomial is over the same base ring as the given MPolynomial and in the variable 'x' if no ring 'ring' is provided.

sage: x, y = MPolynomialRing(ZZ,2,'xy').gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.univariate_polynomial()
Traceback (most recent call last):
...
ValueError: polynomial (=5 - 2*y + 3*x^2 + 7*x^2*y^2) must involve at most
one variable
sage: g = f.fix({x:10}); g
305 - 2*y + 700*y^2
sage: g.univariate_polynomial ()
700*x^2 - 2*x + 305
sage: g.univariate_polynomial(PolynomialRing(QQ,'z'))
700*z^2 - 2*z + 305
sage: R = PolynomialRing(QQ,'w')
sage: R(g)
700*w^2 - 2*w + 305

variable( self, i)

Returns $ i$ -th variable occuring in this polynomial.

sage: x, y = MPolynomialRing(ZZ,2,'xy').gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.variable(0)
x
sage: f.variable(1)
y

variables( self)

Returns the list of variables occuring in this polynomial.

sage: x, y = MPolynomialRing(ZZ,2,'xy').gens()
sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5
sage: f.variables()
[x, y]
sage: g = f.fix({x:10}); g
305 - 2*y + 700*y^2
sage: g.variables()
[y]

Special Functions: __floordiv__,$  $ __getitem__,$  $ __neg__,$  $ _latex_,$  $ _repr_,$  $ _repr_with_changed_varnames,$  $ _variable_indices_

__floordiv__( self, right)

Quotient of division of self by other. This is denoted //.

__getitem__( self, x)

INPUT:
    x -- a tuple or, in case of a single-variable MPolynomial
         ring x can also be an integer.

sage: R.<x, y> = PolynomialRing(QQ, 2)
sage: f = -10*x^3*y + 17*x*y
sage: f[3,1]
-10
sage: f[1,1]
17
sage: f[0,1]
0

sage: R.<x> = MPolynomialRing(GF(7)); R
Polynomial Ring in x over Finite Field of size 7
sage: f = 5*x^2 + 3; f
3 + 5*x^2
sage: f[2]
5

See About this document... for information on suggesting changes.