ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 25 Oct 2017 00:26:03 -0500Addition polynomial in finite field errorhttps://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/I try to do some additions with finite field (GF) just like below. What happen with A?
R.<x> = GF(2)[]
K.<x> = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1)
A = 1 + 1
B = x + x
C = x + x^2
print A
print B
print C
Result:
2
0
x^2 + xTue, 17 Oct 2017 09:46:19 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/Answer by slelievre for <p>I try to do some additions with finite field (GF) just like below. What happen with A?</p>
<pre><code>R.<x> = GF(2)[]
K.<x> = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1)
A = 1 + 1
B = x + x
C = x + x^2
print A
print B
print C
Result:
2
0
x^2 + x
</code></pre>
https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?answer=39198#post-id-39198Note that you defined `x` in two different ways.
Suggestion: use different names.
To add 1 + 1 in R or in K, one needs to specify it.
Otherwise 1 is the 1 in ZZ.
Here is a rewrite using different names; and addition performed in R or in K.
sage: R.<y> = GF(2)[]
....: x = polygen(ZZ)
....: K.<z> = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1)
....: print 'Compute 1 + 1 in R and in K'
....: print R(1) + R(1)
....: print K(1) + K(1)
....: print 'Compute y + y and z + z'
....: print y + y
....: print z + z
....: print 'Compute y + y^2 and z + z^2'
....: print y + y^2
....: print z + z^2
....:
Compute 1 + 1 in R and in K
0
0
Compute y + y and z + z
0
0
Compute y + y^2 and z + z^2
y^2 + y
z^2 + z
Tue, 17 Oct 2017 11:11:43 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?answer=39198#post-id-39198Comment by slelievre for <p>Note that you defined <code>x</code> in two different ways.</p>
<p>Suggestion: use different names.</p>
<p>To add 1 + 1 in R or in K, one needs to specify it.
Otherwise 1 is the 1 in ZZ.</p>
<p>Here is a rewrite using different names; and addition performed in R or in K.</p>
<pre><code>sage: R.<y> = GF(2)[]
....: x = polygen(ZZ)
....: K.<z> = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1)
....: print 'Compute 1 + 1 in R and in K'
....: print R(1) + R(1)
....: print K(1) + K(1)
....: print 'Compute y + y and z + z'
....: print y + y
....: print z + z
....: print 'Compute y + y^2 and z + z^2'
....: print y + y^2
....: print z + z^2
....:
Compute 1 + 1 in R and in K
0
0
Compute y + y and z + z
0
0
Compute y + y^2 and z + z^2
y^2 + y
z^2 + z
</code></pre>
https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39210#post-id-39210If you want to input the element `1` from the ring `R`, you can either use `R(1)` or `R.one()`.
If you just input `1`, Sage considers you mean the element `1` in `ZZ`.Thu, 19 Oct 2017 02:41:52 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39210#post-id-39210Comment by Niyamabrata for <p>Note that you defined <code>x</code> in two different ways.</p>
<p>Suggestion: use different names.</p>
<p>To add 1 + 1 in R or in K, one needs to specify it.
Otherwise 1 is the 1 in ZZ.</p>
<p>Here is a rewrite using different names; and addition performed in R or in K.</p>
<pre><code>sage: R.<y> = GF(2)[]
....: x = polygen(ZZ)
....: K.<z> = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1)
....: print 'Compute 1 + 1 in R and in K'
....: print R(1) + R(1)
....: print K(1) + K(1)
....: print 'Compute y + y and z + z'
....: print y + y
....: print z + z
....: print 'Compute y + y^2 and z + z^2'
....: print y + y^2
....: print z + z^2
....:
Compute 1 + 1 in R and in K
0
0
Compute y + y and z + z
0
0
Compute y + y^2 and z + z^2
y^2 + y
z^2 + z
</code></pre>
https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39200#post-id-39200Thanks, but what happen with my code below:
R.<x> = GF(2)[]
K.<x> = GF(2^3, modulus=x^3+x+1)
M = MatrixSpace(ZZ['x'],8,8)
def add(a, b):
ab = []
for i in range(0, len(a)):
for j in range(0,len(b)):
ab.append(a[i]+b[j])
return ab
a=[0,1,x,x+2,x^2,x^2+1,x^2+x,x^2+x+1]
b=[0,1,x,x+2,x^2,x^2+1,x^2+x,x^2+x+1]
result=M(add(a, b))
print 'Modulus used:',K.modulus()
print result
the result `1 + 1` still 2, when i dont use `R.<x> = GF(2)[] `, my code get error...
Now, my solution just not used 1. I used `x^0` for replacing 1Wed, 18 Oct 2017 10:59:38 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39200#post-id-39200Answer by dan_fulea for <p>I try to do some additions with finite field (GF) just like below. What happen with A?</p>
<pre><code>R.<x> = GF(2)[]
K.<x> = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1)
A = 1 + 1
B = x + x
C = x + x^2
print A
print B
print C
Result:
2
0
x^2 + x
</code></pre>
https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?answer=39201#post-id-39201
Sorry for writing this (as an) answer, but it is not fitting as a comment.
(1) Please do not give an answer as a new question. Just expand the old question or start a new one.
(2) In the original post, the minimal code that illustrates the confusion is as follows:
Let us consider the code:
sage: MyBeautifulField = GF(2)
sage: 1+1
2
Now the question can be stated: But why do we get a `2` instead of `0`? I've just declared the field `GF(2)` to work with?!
The question has a simple answer, yes, the new field is defined, but the computing iron still lives in a general environment that still accepts all kind of inputs, including integers again.
(3) There was an immediate answer telling that it is not good (at all) to redefine implicitly that variable `x`. This is a common reason for mistakes. Now we have the deja vu in the question in the new answer:
R.<x> = GF(2)[]
K.<x> = GF(2^3, modulus=x^3+x+1)
M = MatrixSpace(ZZ['x'],8,8)
After the first line we can ask for the parent of `x`, then after the second one... get the following answers:
sage: R.<x> = GF(2)[]
sage: x.parent()
Univariate Polynomial Ring in x over Finite Field of size 2 (using NTL)
sage: K.<x> = GF(2^3, modulus=x^3+x+1)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
Why do you do that?!
Now the problem with `M` is slightly more complicated.
In there, to make answerers get there enthusiasm to the maximum, we are using `ZZ['x']`. OK, let us take this object separately...
sage: ZZ['x']
Univariate Polynomial Ring in x over Integer Ring
sage: _.gens()
(x,)
A new `x` appears, but somehow implicitly.
No problem, since we moreover even use this implicit `x` in an other black box, the matrix space over the polynomial ring in the hidden variable. So we type
sage: M = MatrixSpace(ZZ['x'],8,8)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
sage: M.base_ring().gens()[0]
x
sage: M.base_ring().gens()[0].multiplicative_order()
---------------------------------------------------------------------------
ArithmeticError Traceback (most recent call last)
And in the hidden generator case we get an error trying to get the `multiplicative_order` of the "other `x`".
So sage uses the same name, `x`, for two different objects. This is the same as people use the name Peter for the one or the other Peter in the world, from the context is / should be always clear which Peter is the addressed Peter.
(4) Now to be more confused about objects, there are canonical coercions, liftings, conversions of objects. For instance, we take the one in `GF(2)` and "lift it" to `ZZ` using the natural constructor:
sage: oneModTwo = GF(2).one()
sage: oneModTwo
1
sage: oneModTwo*2
0
sage: oneModTwo.parent()
Finite Field of size 2
sage: ZZ(oneModTwo)
1
sage: ZZ(oneModTwo).parent()
Integer Ring
sage: ZZ(oneModTwo) * 2
2
(5) Now we build a list with entries in mixed rings, for instance:
sage: add(a,b)[0]
0
sage: add(a,b)[0].parent()
Integer Ring
sage: add(a,b)[1].parent()
Integer Ring
sage: add(a,b)[2].parent()
Finite Field in x of size 2^3
sage: add(a,b)[2]
x
sage: add(a,b)[63]
0
sage: add(a,b)[63].parent()
Finite Field in x of size 2^3
Then apply the constructor `M` on the mixed list. The constructor is doing the best to convert the mixed data to the new characteristic:
sage: M.base_ring().characteristic()
0
And we get some result in characteristic zero.
(7) Of course, asking again for the modulus of `K` does not help. It is like a commercial during the confusion joining the `result`.
(8) Let us sum up with a completely new code, where Peter `x` has each time a different hat:
R.<X> = GF(2)[]
K.<x> = GF(2^3, modulus=X^3+X+1)
S.<y> = ZZ[]
M = MatrixSpace( S, 8, 8 )
def add2(aList, bList):
return [ a+b for a in aList for b in bList ]
a = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
b = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
result = M( add2(a, b) )
print result
And the result is an $8\times 8$ matrix with entries involving polynomials in... `y`, of course. (8x8 is too big to display here... but this does anyway do not matter any longer now.)
[ 0 1 y y y^2 y^2 + 1 y^2 + y y^2 + y + 1]
[ 1 0 y + 1 y + 1 y^2 + 1 y^2 y^2 + y + 1 y^2 + y]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y^2 y^2 + 1 y^2 + y y^2 + y 0 1 y y + 1]
[ y^2 + 1 y^2 y^2 + y + 1 y^2 + y + 1 1 0 y + 1 y]
[ y^2 + y y^2 + y + 1 y^2 y^2 y y + 1 0 1]
[y^2 + y + 1 y^2 + y y^2 + 1 y^2 + 1 y + 1 y 1 0]
Please always write clean code, and have a full control of the objects!
Wed, 18 Oct 2017 13:13:14 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?answer=39201#post-id-39201Comment by Niyamabrata for <p>Sorry for writing this (as an) answer, but it is not fitting as a comment.</p>
<p>(1) Please do not give an answer as a new question. Just expand the old question or start a new one.</p>
<p>(2) In the original post, the minimal code that illustrates the confusion is as follows:</p>
<p>Let us consider the code:</p>
<pre><code>sage: MyBeautifulField = GF(2)
sage: 1+1
2
</code></pre>
<p>Now the question can be stated: But why do we get a <code>2</code> instead of <code>0</code>? I've just declared the field <code>GF(2)</code> to work with?!</p>
<p>The question has a simple answer, yes, the new field is defined, but the computing iron still lives in a general environment that still accepts all kind of inputs, including integers again.</p>
<p>(3) There was an immediate answer telling that it is not good (at all) to redefine implicitly that variable <code>x</code>. This is a common reason for mistakes. Now we have the deja vu in the question in the new answer:</p>
<pre><code>R.<x> = GF(2)[]
K.<x> = GF(2^3, modulus=x^3+x+1)
M = MatrixSpace(ZZ['x'],8,8)
</code></pre>
<p>After the first line we can ask for the parent of <code>x</code>, then after the second one... get the following answers:</p>
<pre><code>sage: R.<x> = GF(2)[]
sage: x.parent()
Univariate Polynomial Ring in x over Finite Field of size 2 (using NTL)
sage: K.<x> = GF(2^3, modulus=x^3+x+1)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
</code></pre>
<p>Why do you do that?!
Now the problem with <code>M</code> is slightly more complicated.
In there, to make answerers get there enthusiasm to the maximum, we are using <code>ZZ['x']</code>. OK, let us take this object separately...</p>
<pre><code>sage: ZZ['x']
Univariate Polynomial Ring in x over Integer Ring
sage: _.gens()
(x,)
</code></pre>
<p>A new <code>x</code> appears, but somehow implicitly.
No problem, since we moreover even use this implicit <code>x</code> in an other black box, the matrix space over the polynomial ring in the hidden variable. So we type </p>
<pre><code>sage: M = MatrixSpace(ZZ['x'],8,8)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
sage: M.base_ring().gens()[0]
x
sage: M.base_ring().gens()[0].multiplicative_order()
---------------------------------------------------------------------------
ArithmeticError Traceback (most recent call last)
</code></pre>
<p>And in the hidden generator case we get an error trying to get the <code>multiplicative_order</code> of the "other <code>x</code>".
So sage uses the same name, <code>x</code>, for two different objects. This is the same as people use the name Peter for the one or the other Peter in the world, from the context is / should be always clear which Peter is the addressed Peter.</p>
<p>(4) Now to be more confused about objects, there are canonical coercions, liftings, conversions of objects. For instance, we take the one in <code>GF(2)</code> and "lift it" to <code>ZZ</code> using the natural constructor:</p>
<pre><code>sage: oneModTwo = GF(2).one()
sage: oneModTwo
1
sage: oneModTwo*2
0
sage: oneModTwo.parent()
Finite Field of size 2
sage: ZZ(oneModTwo)
1
sage: ZZ(oneModTwo).parent()
Integer Ring
sage: ZZ(oneModTwo) * 2
2
</code></pre>
<p>(5) Now we build a list with entries in mixed rings, for instance:</p>
<pre><code>sage: add(a,b)[0]
0
sage: add(a,b)[0].parent()
Integer Ring
sage: add(a,b)[1].parent()
Integer Ring
sage: add(a,b)[2].parent()
Finite Field in x of size 2^3
sage: add(a,b)[2]
x
sage: add(a,b)[63]
0
sage: add(a,b)[63].parent()
Finite Field in x of size 2^3
</code></pre>
<p>Then apply the constructor <code>M</code> on the mixed list. The constructor is doing the best to convert the mixed data to the new characteristic:</p>
<pre><code>sage: M.base_ring().characteristic()
0
</code></pre>
<p>And we get some result in characteristic zero.</p>
<p>(7) Of course, asking again for the modulus of <code>K</code> does not help. It is like a commercial during the confusion joining the <code>result</code>.</p>
<p>(8) Let us sum up with a completely new code, where Peter <code>x</code> has each time a different hat:</p>
<pre><code>R.<X> = GF(2)[]
K.<x> = GF(2^3, modulus=X^3+X+1)
S.<y> = ZZ[]
M = MatrixSpace( S, 8, 8 )
def add2(aList, bList):
return [ a+b for a in aList for b in bList ]
a = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
b = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
result = M( add2(a, b) )
print result
</code></pre>
<p>And the result is an $8\times 8$ matrix with entries involving polynomials in... <code>y</code>, of course. (8x8 is too big to display here... but this does anyway do not matter any longer now.)</p>
<pre><code>[ 0 1 y y y^2 y^2 + 1 y^2 + y y^2 + y + 1]
[ 1 0 y + 1 y + 1 y^2 + 1 y^2 y^2 + y + 1 y^2 + y]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y^2 y^2 + 1 y^2 + y y^2 + y 0 1 y y + 1]
[ y^2 + 1 y^2 y^2 + y + 1 y^2 + y + 1 1 0 y + 1 y]
[ y^2 + y y^2 + y + 1 y^2 y^2 y y + 1 0 1]
[y^2 + y + 1 y^2 + y y^2 + 1 y^2 + 1 y + 1 y 1 0]
</code></pre>
<p>Please always write clean code, and have a full control of the objects!</p>
https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39273#post-id-39273Thanks...nice. Sorry for x+2 was there...my mistake.it's x+1Wed, 25 Oct 2017 00:26:03 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39273#post-id-39273Comment by dan_fulea for <p>Sorry for writing this (as an) answer, but it is not fitting as a comment.</p>
<p>(1) Please do not give an answer as a new question. Just expand the old question or start a new one.</p>
<p>(2) In the original post, the minimal code that illustrates the confusion is as follows:</p>
<p>Let us consider the code:</p>
<pre><code>sage: MyBeautifulField = GF(2)
sage: 1+1
2
</code></pre>
<p>Now the question can be stated: But why do we get a <code>2</code> instead of <code>0</code>? I've just declared the field <code>GF(2)</code> to work with?!</p>
<p>The question has a simple answer, yes, the new field is defined, but the computing iron still lives in a general environment that still accepts all kind of inputs, including integers again.</p>
<p>(3) There was an immediate answer telling that it is not good (at all) to redefine implicitly that variable <code>x</code>. This is a common reason for mistakes. Now we have the deja vu in the question in the new answer:</p>
<pre><code>R.<x> = GF(2)[]
K.<x> = GF(2^3, modulus=x^3+x+1)
M = MatrixSpace(ZZ['x'],8,8)
</code></pre>
<p>After the first line we can ask for the parent of <code>x</code>, then after the second one... get the following answers:</p>
<pre><code>sage: R.<x> = GF(2)[]
sage: x.parent()
Univariate Polynomial Ring in x over Finite Field of size 2 (using NTL)
sage: K.<x> = GF(2^3, modulus=x^3+x+1)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
</code></pre>
<p>Why do you do that?!
Now the problem with <code>M</code> is slightly more complicated.
In there, to make answerers get there enthusiasm to the maximum, we are using <code>ZZ['x']</code>. OK, let us take this object separately...</p>
<pre><code>sage: ZZ['x']
Univariate Polynomial Ring in x over Integer Ring
sage: _.gens()
(x,)
</code></pre>
<p>A new <code>x</code> appears, but somehow implicitly.
No problem, since we moreover even use this implicit <code>x</code> in an other black box, the matrix space over the polynomial ring in the hidden variable. So we type </p>
<pre><code>sage: M = MatrixSpace(ZZ['x'],8,8)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
sage: M.base_ring().gens()[0]
x
sage: M.base_ring().gens()[0].multiplicative_order()
---------------------------------------------------------------------------
ArithmeticError Traceback (most recent call last)
</code></pre>
<p>And in the hidden generator case we get an error trying to get the <code>multiplicative_order</code> of the "other <code>x</code>".
So sage uses the same name, <code>x</code>, for two different objects. This is the same as people use the name Peter for the one or the other Peter in the world, from the context is / should be always clear which Peter is the addressed Peter.</p>
<p>(4) Now to be more confused about objects, there are canonical coercions, liftings, conversions of objects. For instance, we take the one in <code>GF(2)</code> and "lift it" to <code>ZZ</code> using the natural constructor:</p>
<pre><code>sage: oneModTwo = GF(2).one()
sage: oneModTwo
1
sage: oneModTwo*2
0
sage: oneModTwo.parent()
Finite Field of size 2
sage: ZZ(oneModTwo)
1
sage: ZZ(oneModTwo).parent()
Integer Ring
sage: ZZ(oneModTwo) * 2
2
</code></pre>
<p>(5) Now we build a list with entries in mixed rings, for instance:</p>
<pre><code>sage: add(a,b)[0]
0
sage: add(a,b)[0].parent()
Integer Ring
sage: add(a,b)[1].parent()
Integer Ring
sage: add(a,b)[2].parent()
Finite Field in x of size 2^3
sage: add(a,b)[2]
x
sage: add(a,b)[63]
0
sage: add(a,b)[63].parent()
Finite Field in x of size 2^3
</code></pre>
<p>Then apply the constructor <code>M</code> on the mixed list. The constructor is doing the best to convert the mixed data to the new characteristic:</p>
<pre><code>sage: M.base_ring().characteristic()
0
</code></pre>
<p>And we get some result in characteristic zero.</p>
<p>(7) Of course, asking again for the modulus of <code>K</code> does not help. It is like a commercial during the confusion joining the <code>result</code>.</p>
<p>(8) Let us sum up with a completely new code, where Peter <code>x</code> has each time a different hat:</p>
<pre><code>R.<X> = GF(2)[]
K.<x> = GF(2^3, modulus=X^3+X+1)
S.<y> = ZZ[]
M = MatrixSpace( S, 8, 8 )
def add2(aList, bList):
return [ a+b for a in aList for b in bList ]
a = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
b = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
result = M( add2(a, b) )
print result
</code></pre>
<p>And the result is an $8\times 8$ matrix with entries involving polynomials in... <code>y</code>, of course. (8x8 is too big to display here... but this does anyway do not matter any longer now.)</p>
<pre><code>[ 0 1 y y y^2 y^2 + 1 y^2 + y y^2 + y + 1]
[ 1 0 y + 1 y + 1 y^2 + 1 y^2 y^2 + y + 1 y^2 + y]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y^2 y^2 + 1 y^2 + y y^2 + y 0 1 y y + 1]
[ y^2 + 1 y^2 y^2 + y + 1 y^2 + y + 1 1 0 y + 1 y]
[ y^2 + y y^2 + y + 1 y^2 y^2 y y + 1 0 1]
[y^2 + y + 1 y^2 + y y^2 + 1 y^2 + 1 y + 1 y 1 0]
</code></pre>
<p>Please always write clean code, and have a full control of the objects!</p>
https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39250#post-id-39250Please state a clear question, or a task in a mathematical language.
For instance, seeing
a=[0,1,x,x+2,x^2,x^2+1,x^2+x,x^2+x+1]
**without** glasses, i was in the same second convinced we want to type the addition law table in `K`, which can easily be obtained via:
R.<X> = GF(2)[]
K.<x> = GF(2^3, modulus=X^3+X+1)
A = matrix( K, 8, 8, [ a+b for a in K for b in K ] )
A
which is...
[ 0 x x^2 x + 1 x^2 + x x^2 + x + 1 x^2 + 1 1]
[ x 0 x^2 + x 1 x^2 x^2 + 1 x^2 + x + 1 x + 1]
[ x^2 x^2 + x 0 x^2 + x + 1 x x + 1 1 x^2 + 1]
and so on... But `x+2` is there, why?
Which is the question behind the question?Sun, 22 Oct 2017 11:43:03 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39250#post-id-39250Comment by Niyamabrata for <p>Sorry for writing this (as an) answer, but it is not fitting as a comment.</p>
<p>(1) Please do not give an answer as a new question. Just expand the old question or start a new one.</p>
<p>(2) In the original post, the minimal code that illustrates the confusion is as follows:</p>
<p>Let us consider the code:</p>
<pre><code>sage: MyBeautifulField = GF(2)
sage: 1+1
2
</code></pre>
<p>Now the question can be stated: But why do we get a <code>2</code> instead of <code>0</code>? I've just declared the field <code>GF(2)</code> to work with?!</p>
<p>The question has a simple answer, yes, the new field is defined, but the computing iron still lives in a general environment that still accepts all kind of inputs, including integers again.</p>
<p>(3) There was an immediate answer telling that it is not good (at all) to redefine implicitly that variable <code>x</code>. This is a common reason for mistakes. Now we have the deja vu in the question in the new answer:</p>
<pre><code>R.<x> = GF(2)[]
K.<x> = GF(2^3, modulus=x^3+x+1)
M = MatrixSpace(ZZ['x'],8,8)
</code></pre>
<p>After the first line we can ask for the parent of <code>x</code>, then after the second one... get the following answers:</p>
<pre><code>sage: R.<x> = GF(2)[]
sage: x.parent()
Univariate Polynomial Ring in x over Finite Field of size 2 (using NTL)
sage: K.<x> = GF(2^3, modulus=x^3+x+1)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
</code></pre>
<p>Why do you do that?!
Now the problem with <code>M</code> is slightly more complicated.
In there, to make answerers get there enthusiasm to the maximum, we are using <code>ZZ['x']</code>. OK, let us take this object separately...</p>
<pre><code>sage: ZZ['x']
Univariate Polynomial Ring in x over Integer Ring
sage: _.gens()
(x,)
</code></pre>
<p>A new <code>x</code> appears, but somehow implicitly.
No problem, since we moreover even use this implicit <code>x</code> in an other black box, the matrix space over the polynomial ring in the hidden variable. So we type </p>
<pre><code>sage: M = MatrixSpace(ZZ['x'],8,8)
sage: x.parent()
Finite Field in x of size 2^3
sage: x.multiplicative_order()
7
sage: M.base_ring().gens()[0]
x
sage: M.base_ring().gens()[0].multiplicative_order()
---------------------------------------------------------------------------
ArithmeticError Traceback (most recent call last)
</code></pre>
<p>And in the hidden generator case we get an error trying to get the <code>multiplicative_order</code> of the "other <code>x</code>".
So sage uses the same name, <code>x</code>, for two different objects. This is the same as people use the name Peter for the one or the other Peter in the world, from the context is / should be always clear which Peter is the addressed Peter.</p>
<p>(4) Now to be more confused about objects, there are canonical coercions, liftings, conversions of objects. For instance, we take the one in <code>GF(2)</code> and "lift it" to <code>ZZ</code> using the natural constructor:</p>
<pre><code>sage: oneModTwo = GF(2).one()
sage: oneModTwo
1
sage: oneModTwo*2
0
sage: oneModTwo.parent()
Finite Field of size 2
sage: ZZ(oneModTwo)
1
sage: ZZ(oneModTwo).parent()
Integer Ring
sage: ZZ(oneModTwo) * 2
2
</code></pre>
<p>(5) Now we build a list with entries in mixed rings, for instance:</p>
<pre><code>sage: add(a,b)[0]
0
sage: add(a,b)[0].parent()
Integer Ring
sage: add(a,b)[1].parent()
Integer Ring
sage: add(a,b)[2].parent()
Finite Field in x of size 2^3
sage: add(a,b)[2]
x
sage: add(a,b)[63]
0
sage: add(a,b)[63].parent()
Finite Field in x of size 2^3
</code></pre>
<p>Then apply the constructor <code>M</code> on the mixed list. The constructor is doing the best to convert the mixed data to the new characteristic:</p>
<pre><code>sage: M.base_ring().characteristic()
0
</code></pre>
<p>And we get some result in characteristic zero.</p>
<p>(7) Of course, asking again for the modulus of <code>K</code> does not help. It is like a commercial during the confusion joining the <code>result</code>.</p>
<p>(8) Let us sum up with a completely new code, where Peter <code>x</code> has each time a different hat:</p>
<pre><code>R.<X> = GF(2)[]
K.<x> = GF(2^3, modulus=X^3+X+1)
S.<y> = ZZ[]
M = MatrixSpace( S, 8, 8 )
def add2(aList, bList):
return [ a+b for a in aList for b in bList ]
a = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
b = [ K(0), K(1), x, x+2, x^2, x^2+1, x^2+x, x^2+x+1 ]
result = M( add2(a, b) )
print result
</code></pre>
<p>And the result is an $8\times 8$ matrix with entries involving polynomials in... <code>y</code>, of course. (8x8 is too big to display here... but this does anyway do not matter any longer now.)</p>
<pre><code>[ 0 1 y y y^2 y^2 + 1 y^2 + y y^2 + y + 1]
[ 1 0 y + 1 y + 1 y^2 + 1 y^2 y^2 + y + 1 y^2 + y]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y y + 1 0 0 y^2 + y y^2 + y + 1 y^2 y^2 + 1]
[ y^2 y^2 + 1 y^2 + y y^2 + y 0 1 y y + 1]
[ y^2 + 1 y^2 y^2 + y + 1 y^2 + y + 1 1 0 y + 1 y]
[ y^2 + y y^2 + y + 1 y^2 y^2 y y + 1 0 1]
[y^2 + y + 1 y^2 + y y^2 + 1 y^2 + 1 y + 1 y 1 0]
</code></pre>
<p>Please always write clean code, and have a full control of the objects!</p>
https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39208#post-id-39208ty so much...but i need the result in x, not in y.Wed, 18 Oct 2017 22:23:36 -0500https://ask.sagemath.org/question/39195/addition-polynomial-in-finite-field-error/?comment=39208#post-id-39208