To work with polynomials of several variables, we declare the polynomial ring and variables first, in one of two ways.
sage: R = PolynomialRing(GF(5),3,"z") # here, 3 = number of variables sage: R Multivariate Polynomial Ring in z0, z1, z2 over Finite Field of size 5
sage: GF(5)['z0, z1, z2'] Multivariate Polynomial Ring in z0, z1, z2 over Finite Field of size 5 sage: R.<z0,z1,z2> = GF(5)[]; R Multivariate Polynomial Ring in z0, z1, z2 over Finite Field of size 5
sage: PolynomialRing(GF(5), 3, 'xyz') Multivariate Polynomial Ring in x, y, z over Finite Field of size 5
Next let's do some arithmetic.
sage: z = GF(5)['z0, z1, z2'].gens() sage: z (z0, z1, z2) sage: (z[0]+z[1]+z[2])^2 z0^2 + 2*z0*z1 + z1^2 + 2*z0*z2 + 2*z1*z2 + z2^2
sage: R = GF(5)['x,y,z'] sage: x,y,z = R.gens() sage: QQ['x'] Univariate Polynomial Ring in x over Rational Field sage: QQ['x,y'].gens() (x, y) sage: QQ['x'].objgens() (Univariate Polynomial Ring in x over Rational Field, (x,))
Multivariate polynomials are implemented in Sage using Python dictionaries and the ``distributive representation'' of a polynomial. Sage makes some use of Singular [Si], e.g., for computation of gcd's and Gröbner basis of ideals.
sage: R, (x, y) = PolynomialRing(RationalField(), 2, 'xy').objgens() sage: f = (x^3 + 2*y^2*x)^2 sage: g = x^2*y^2 sage: f.gcd(g) x^2
Next we create the ideal (f,g)
generated by f and g,
by simply multiplying (f,g)
by R
(we could also write
ideal([f,g])
or ideal(f,g)
).
sage: I = (f, g)*R; I Ideal (x^6 + 4*x^4*y^2 + 4*x^2*y^4, x^2*y^2) of Multivariate Polynomial Ring in x, y over Rational Field sage: B = I.groebner_basis(); B [x^2*y^2, x^6] sage: x^2 in I False
Incidentally, the Gröbner basis above is not a list but an immutable sequence. This means that it has a universe, parent, and cannot be changed (which is good because changing the basis would break other routines that use the Gröbner basis).
sage: B.parent() Category of sequences in Multivariate Polynomial Ring in x, y over Rational Field sage: B.universe() Multivariate Polynomial Ring in x, y over Rational Field sage: B[1] = x Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
Some (read: not as much as we would like) commutative algebra is available in Sage, implemented via Singular. For example, we can compute the primary decomposition and associated primes of I:
sage: I.primary_decomposition() [Ideal (x^2) of Multivariate Polynomial Ring in x, y over Rational Field, Ideal (y^2,x^6) of Multivariate Polynomial Ring in x, y over Rational Field] sage: I.associated_primes() [Ideal (x) of Multivariate Polynomial Ring in x, y over Rational Field, Ideal (y, x) of Multivariate Polynomial Ring in x, y over Rational Field]
See About this document... for information on suggesting changes.