Ask Your Question

Revision history [back]

Clarifying the question

If I understand correctly, the question is to extract not the coefficient of the monomial z (which would be zero in this case), but the coefficient of z when this is seen as a polynomial in z with coefficients in polynomials in x and y.

In other words the expected answer here is x*y + x.

Note that in Sage's symbolic ring, if a symbolic expression is a polynomial in one of the symbolic variables, then we can extract the coefficient as follows:

sage: x, y, z = SR.var('x, y, z')
sage: f = x*z + x*y*z + y + 1; f
x*y*z + x*z + y + 1
sage: f.coefficient(z, 1)
x*y + x

The question is how to do that when instead of a symbolic expression in Sage's symbolic ring, our f is an element in the quotient of a polynomial ring in three variables modulo an ideal.

Setup

We reproduce the setup from the question with some variations, see notes below.

Define the field with three elements:

sage: K = GF(3); K
Finite Field of size 3

Define a polynomial ring in three variables:

sage: A.<X, Y, Z> = PolynomialRing(K); A
Multivariate Polynomial Ring in X, Y, Z
over Finite Field of size 3

Define an ideal in this polynomial ring:

sage: J = A.ideal(X^2 - 1, Y^2 - 1, Z^2 - 1)
Ideal (X^2 - 1, Y^2 - 1, Z^2 - 1)
of Multivariate Polynomial Ring in X, Y, Z
over Finite Field of size 3

Define the quotient ring:

sage: R.<x, y, z> = A.quotient(J); R
Quotient of
Multivariate Polynomial Ring in X, Y, Z
over Finite Field of size 3
by the ideal (X^2 - 1, Y^2 - 1, Z^2 - 1)

Define an element in the quotient ring:

sage: f = x*z + x*y*z + y + 1; f
x*y*z + x*z + y + 1

Coefficient of z using polynomial rings

Lift to the polynomial ring:

sage: F = f.lift(); F
X*Y*Z + X*Z + Y + 1

Define ring of polynomials in Z with coefficients in polynomials in X and Y over K:

sage: B = K[('X', 'Y')]['Z']; B
Univariate Polynomial Ring in Z
over Multivariate Polynomial Ring in X, Y
over Finite Field of size 3

See F as that kind of polynomial:

sage: G = B(F); G
(X*Y + X)*Z + Y + 1

Coefficient of Z:

sage: D = G[1]; D
X*Y + X

Map back to polynomials in three variables:

sage: C = A(D); C
X*Y + X

Project down to the quotient ring:

sage: c = R(C); c
x*y + x

Note that this is an element in R; we might prefer an element in the quotient ring $K[X, Y] / (X^2 - 1, Y^2 - 1)$. This is left as an exercise.

Using strings and the symbolic ring

We describe a different way to obtain the answer, using strings and the symbolic ring.

Lift to the polynomial ring:

sage: F = f.lift(); F
X*Y*Z + X*Z + Y + 1

Extract coefficient of (the symbolic variable) Z:

sage: C_SR = SR(str(F)).coefficient(SR('Z'), 1); C_SR
X*Y + X

Back to the polynomial ring:

sage: C = A(str(c_SR)); C
X*Y + X

Project to the quotient: sage: c = R(C); c x*y + x

Some notes

Integers modulo three vs the field with three elements

In Sage one gets different objects depending whether we construct the ring $\mathbb{Z}/3\mathbb{Z}$ or the field with three elements.

Here are four ways to construct the ring

sage: k = Zmod(3)
sage: k = Integers(3)
sage: k = IntegerModRing(3)
sage: k = ZZ.quo(3*ZZ)

All give the same result:

sage: k
Ring of integers modulo 3

Here are two ways to construct the field:

sage: K = FiniteField(3)
sage: K = GF(3)

Both give the same result:

sage: K
Finite Field of size 3

Although one can ask Sage whether k is a field, and it will say yes, Sage does not consider k and K as equal, and the arithmetic in them is not implemented the same way.

sage: k.is_field()
True
sage: k == K
False

When computing with a field, it is recommended to define it using FiniteField or GF, since this will take better advantage of finite field arithmetic.

Wishlist

While A can convert a string:

sage: A('X*Y*Z + X*Z + Y + 1')
X*Y*Z + X*Z + Y + 1

it is not the case of the quotient ring R:

sage: R('x*y*z + x*z + y + 1')
Traceback (most recent call last)
...
NameError: name 'x' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last)
...
TypeError: Could not find a mapping of the passed element to this ring.

It would be nice if one could get:

sage: R('x*y*z + x*z + y + 1')
x*y*z + x*z + y + 1