ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 21 Jul 2020 18:49:31 +0200how to get the coefficient of a multivariate polynomial with respect to a specific variable and degree, in a quotient ring ?https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/Here is what I tried.
sage: F = ZZ.quo(3*ZZ); F
sage: A.<X, Y, Z> = PolynomialRing(F); A
sage: R.<x, y, z> = A.quotient(ideal(X^2 - 1, Y^2 - 1, Z^2 - 1))
sage: f = x*z + x*y*z + y + 1
sage: f.coefficient(z, 1)
sage: f.coefficient({z: 1})
sage: f.coeffcient(z)Tue, 21 Jul 2020 10:46:56 +0200https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/Comment by andriam for <p>Here is what I tried.</p>
<pre><code>sage: F = ZZ.quo(3*ZZ); F
sage: A.<X, Y, Z> = PolynomialRing(F); A
sage: R.<x, y, z> = A.quotient(ideal(X^2 - 1, Y^2 - 1, Z^2 - 1))
sage: f = x*z + x*y*z + y + 1
sage: f.coefficient(z, 1)
sage: f.coefficient({z: 1})
sage: f.coeffcient(z)
</code></pre>
https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/?comment=52595#post-id-52595The instructions above, containing "coefficient" do not work. Sage (ver. 9.0 and the online ver. 9.1) return errors, saying that "QuotientRing_generic_with_category.element_class' object has no attribute 'coefficients'"Tue, 21 Jul 2020 10:48:36 +0200https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/?comment=52595#post-id-52595Answer by slelievre for <p>Here is what I tried.</p>
<pre><code>sage: F = ZZ.quo(3*ZZ); F
sage: A.<X, Y, Z> = PolynomialRing(F); A
sage: R.<x, y, z> = A.quotient(ideal(X^2 - 1, Y^2 - 1, Z^2 - 1))
sage: f = x*z + x*y*z + y + 1
sage: f.coefficient(z, 1)
sage: f.coefficient({z: 1})
sage: f.coeffcient(z)
</code></pre>
https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/?answer=52599#post-id-52599## 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
Tue, 21 Jul 2020 16:51:11 +0200https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/?answer=52599#post-id-52599Comment by andriam for <h2>Clarifying the question</h2>
<p>If I understand correctly, the question is to extract
not the coefficient of the monomial <code>z</code> (which would be
zero in this case), but the coefficient of <code>z</code>
when this is seen as a polynomial in <code>z</code>
with coefficients in polynomials in <code>x</code> and <code>y</code>.</p>
<p>In other words the expected answer here is <code>x*y + x</code>.</p>
<p>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:</p>
<pre><code>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
</code></pre>
<p>The question is how to do that when instead of a
symbolic expression in Sage's symbolic ring, our <code>f</code>
is an element in the quotient of a polynomial ring
in three variables modulo an ideal.</p>
<h2>Setup</h2>
<p>We reproduce the setup from the question with
some variations, see notes below.</p>
<p>Define the field with three elements:</p>
<pre><code>sage: K = GF(3); K
Finite Field of size 3
</code></pre>
<p>Define a polynomial ring in three variables:</p>
<pre><code>sage: A.<X, Y, Z> = PolynomialRing(K); A
Multivariate Polynomial Ring in X, Y, Z
over Finite Field of size 3
</code></pre>
<p>Define an ideal in this polynomial ring:</p>
<pre><code>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
</code></pre>
<p>Define the quotient ring:</p>
<pre><code>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)
</code></pre>
<p>Define an element in the quotient ring:</p>
<pre><code>sage: f = x*z + x*y*z + y + 1; f
x*y*z + x*z + y + 1
</code></pre>
<h2>Coefficient of <code>z</code> using polynomial rings</h2>
<p>Lift to the polynomial ring:</p>
<pre><code>sage: F = f.lift(); F
X*Y*Z + X*Z + Y + 1
</code></pre>
<p>Define ring of polynomials in <code>Z</code> with coefficients
in polynomials in <code>X</code> and <code>Y</code> over <code>K</code>:</p>
<pre><code>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
</code></pre>
<p>See <code>F</code> as that kind of polynomial:</p>
<pre><code>sage: G = B(F); G
(X*Y + X)*Z + Y + 1
</code></pre>
<p>Coefficient of <code>Z</code>:</p>
<pre><code>sage: D = G[1]; D
X*Y + X
</code></pre>
<p>Map back to polynomials in three variables:</p>
<pre><code>sage: C = A(D); C
X*Y + X
</code></pre>
<p>Project down to the quotient ring:</p>
<pre><code>sage: c = R(C); c
x*y + x
</code></pre>
<p>Note that this is an element in <code>R</code>;
we might prefer an element in the quotient
ring $K[X, Y] / (X^2 - 1, Y^2 - 1)$.
This is left as an exercise.</p>
<h2>Using strings and the symbolic ring</h2>
<p>We describe a different way to obtain the answer,
using strings and the symbolic ring.</p>
<p>Lift to the polynomial ring:</p>
<pre><code>sage: F = f.lift(); F
X*Y*Z + X*Z + Y + 1
</code></pre>
<p>Extract coefficient of (the symbolic variable) <code>Z</code>:</p>
<pre><code>sage: C_SR = SR(str(F)).coefficient(SR('Z'), 1); C_SR
X*Y + X
</code></pre>
<p>Back to the polynomial ring:</p>
<pre><code>sage: C = A(str(c_SR)); C
X*Y + X
</code></pre>
<p>Project to the quotient:
sage: c = R(C); c
x*y + x</p>
<h2>Some notes</h2>
<h3>Integers modulo three vs the field with three elements</h3>
<p>In Sage one gets different objects depending whether
we construct the ring $\mathbb{Z}/3\mathbb{Z}$
or the field with three elements.</p>
<p>Here are four ways to construct the ring </p>
<pre><code>sage: k = Zmod(3)
sage: k = Integers(3)
sage: k = IntegerModRing(3)
sage: k = ZZ.quo(3*ZZ)
</code></pre>
<p>All give the same result:</p>
<pre><code>sage: k
Ring of integers modulo 3
</code></pre>
<p>Here are two ways to construct the field:</p>
<pre><code>sage: K = FiniteField(3)
sage: K = GF(3)
</code></pre>
<p>Both give the same result:</p>
<pre><code>sage: K
Finite Field of size 3
</code></pre>
<p>Although one can ask Sage whether <code>k</code> is a field,
and it will say yes, Sage does not consider <code>k</code> and <code>K</code>
as equal, and the arithmetic in them is not implemented
the same way.</p>
<pre><code>sage: k.is_field()
True
sage: k == K
False
</code></pre>
<p>When computing with a field, it is recommended
to define it using <code>FiniteField</code> or <code>GF</code>, since
this will take better advantage of finite field
arithmetic.</p>
<h3>Wishlist</h3>
<p>While <code>A</code> can convert a string:</p>
<pre><code>sage: A('X*Y*Z + X*Z + Y + 1')
X*Y*Z + X*Z + Y + 1
</code></pre>
<p>it is not the case of the quotient ring <code>R</code>:</p>
<pre><code>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.
</code></pre>
<p>It would be nice if one could get:</p>
<pre><code>sage: R('x*y*z + x*z + y + 1')
x*y*z + x*z + y + 1
</code></pre>
https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/?comment=52607#post-id-52607Many thanks for your responses, they are very interesting and will be subjects of studies !Tue, 21 Jul 2020 18:49:31 +0200https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/?comment=52607#post-id-52607