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.Sun, 29 Jun 2014 12:58:14 +0200Binary fieldshttps://ask.sagemath.org/question/11059/binary-fields/Hello, I would like to perform the following on a binary field, i.e. `GF(2^m)`.
1. Define a polynomial and solve the polynomial over a binary field.
2. Convert an element of the binary field into a bit string.
For the first, I've tried the following:
K = GF(2^7,'a');
PK.<x>=K[]; #I've also tried "x = PolynomialRing(GF(2^7,'a'),'x').gen"
f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
print f.roots();
But the error is `TypeError: unable to coerce from a finite field other than the prime subfield`.
For the second, I would like to know how finite field elements are stored in SAGE, are they stored as vectors?
If you have any resources that could point me in the right direction, I'll be very thankful for your help!Thu, 05 Jun 2014 23:05:40 +0200https://ask.sagemath.org/question/11059/binary-fields/Comment by kcrisman for <p>Hello, I would like to perform the following on a binary field, i.e. <code>GF(2^m)</code>.</p>
<ol>
<li>Define a polynomial and solve the polynomial over a binary field.</li>
<li>Convert an element of the binary field into a bit string.</li>
</ol>
<p>For the first, I've tried the following:</p>
<pre><code>K = GF(2^7,'a');
PK.<x>=K[]; #I've also tried "x = PolynomialRing(GF(2^7,'a'),'x').gen"
f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
print f.roots();
</code></pre>
<p>But the error is <code>TypeError: unable to coerce from a finite field other than the prime subfield</code>.</p>
<p>For the second, I would like to know how finite field elements are stored in SAGE, are they stored as vectors?</p>
<p>If you have any resources that could point me in the right direction, I'll be very thankful for your help!</p>
https://ask.sagemath.org/question/11059/binary-fields/?comment=16164#post-id-16164What version of Sage are you using exactly, and were there any other commands before this? The `a` seems to be missing.Thu, 05 Jun 2014 23:41:49 +0200https://ask.sagemath.org/question/11059/binary-fields/?comment=16164#post-id-16164Comment by Blackadder for <p>Hello, I would like to perform the following on a binary field, i.e. <code>GF(2^m)</code>.</p>
<ol>
<li>Define a polynomial and solve the polynomial over a binary field.</li>
<li>Convert an element of the binary field into a bit string.</li>
</ol>
<p>For the first, I've tried the following:</p>
<pre><code>K = GF(2^7,'a');
PK.<x>=K[]; #I've also tried "x = PolynomialRing(GF(2^7,'a'),'x').gen"
f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
print f.roots();
</code></pre>
<p>But the error is <code>TypeError: unable to coerce from a finite field other than the prime subfield</code>.</p>
<p>For the second, I would like to know how finite field elements are stored in SAGE, are they stored as vectors?</p>
<p>If you have any resources that could point me in the right direction, I'll be very thankful for your help!</p>
https://ask.sagemath.org/question/11059/binary-fields/?comment=16163#post-id-16163@kcrisman The version that I'm using is 5.11. The `a` comes from the first line; it is used to generate the finite field of prime power. See http://www.math.ucla.edu/~jimc/mathnet_d/sage/reference/sage/rings/finite_rings/constructor.htmlFri, 06 Jun 2014 00:03:26 +0200https://ask.sagemath.org/question/11059/binary-fields/?comment=16163#post-id-16163Answer by vdelecroix for <p>Hello, I would like to perform the following on a binary field, i.e. <code>GF(2^m)</code>.</p>
<ol>
<li>Define a polynomial and solve the polynomial over a binary field.</li>
<li>Convert an element of the binary field into a bit string.</li>
</ol>
<p>For the first, I've tried the following:</p>
<pre><code>K = GF(2^7,'a');
PK.<x>=K[]; #I've also tried "x = PolynomialRing(GF(2^7,'a'),'x').gen"
f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
print f.roots();
</code></pre>
<p>But the error is <code>TypeError: unable to coerce from a finite field other than the prime subfield</code>.</p>
<p>For the second, I would like to know how finite field elements are stored in SAGE, are they stored as vectors?</p>
<p>If you have any resources that could point me in the right direction, I'll be very thankful for your help!</p>
https://ask.sagemath.org/question/11059/binary-fields/?answer=23110#post-id-23110 Hi,
Finite field elements are almost stored as vectors. It is actually a bit more complicated since there are several implementation and most of the time it uses external libraries (givaro, pari or ntl). You can nevertheless convert your element into a vector with
sage: K = GF(16, 'a')
sage: V = K.vector_space()
sage: K.inject_variables()
sage: V(a^2 + 3*a + 1)
sage: v =V(a^2 + a + 1)
sage: v
(1, 1, 1, 0)
sage: v.parent()
Vector space of dimension 4 over Finite Field of size 2
VincentSun, 29 Jun 2014 12:58:14 +0200https://ask.sagemath.org/question/11059/binary-fields/?answer=23110#post-id-23110Answer by niles for <p>Hello, I would like to perform the following on a binary field, i.e. <code>GF(2^m)</code>.</p>
<ol>
<li>Define a polynomial and solve the polynomial over a binary field.</li>
<li>Convert an element of the binary field into a bit string.</li>
</ol>
<p>For the first, I've tried the following:</p>
<pre><code>K = GF(2^7,'a');
PK.<x>=K[]; #I've also tried "x = PolynomialRing(GF(2^7,'a'),'x').gen"
f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
print f.roots();
</code></pre>
<p>But the error is <code>TypeError: unable to coerce from a finite field other than the prime subfield</code>.</p>
<p>For the second, I would like to know how finite field elements are stored in SAGE, are they stored as vectors?</p>
<p>If you have any resources that could point me in the right direction, I'll be very thankful for your help!</p>
https://ask.sagemath.org/question/11059/binary-fields/?answer=16088#post-id-16088I assume the binary string you want is the list of coefficients of `a`. For this, you could do the following:
- Make an element:
sage: K = GF(2^7,'a');
sage: x = K.random_element(); x
a^5 + a^4 + a^3 + a
- Convert the element to a polynomial and get the list of coefficients:
sage: x.polynomial().coeffs()
[0, 1, 0, 1, 1, 1]
- Go directly from the element to a string by joining the strings of the coefficients:
sage: ''.join(map(str,x.polynomial().coeffs()))
'010111'Fri, 06 Jun 2014 10:56:19 +0200https://ask.sagemath.org/question/11059/binary-fields/?answer=16088#post-id-16088Comment by Blackadder for <p>I assume the binary string you want is the list of coefficients of <code>a</code>. For this, you could do the following:</p>
<ul>
<li><p>Make an element:</p>
<pre><code>sage: K = GF(2^7,'a');
sage: x = K.random_element(); x
a^5 + a^4 + a^3 + a
</code></pre></li>
<li><p>Convert the element to a polynomial and get the list of coefficients:</p>
<pre><code>sage: x.polynomial().coeffs()
[0, 1, 0, 1, 1, 1]
</code></pre></li>
<li><p>Go directly from the element to a string by joining the strings of the coefficients:</p>
<pre><code>sage: ''.join(map(str,x.polynomial().coeffs()))
'010111'
</code></pre></li>
</ul>
https://ask.sagemath.org/question/11059/binary-fields/?comment=16154#post-id-16154P.S. Sorry I can't accept both answers.Sun, 08 Jun 2014 23:33:14 +0200https://ask.sagemath.org/question/11059/binary-fields/?comment=16154#post-id-16154Comment by Blackadder for <p>I assume the binary string you want is the list of coefficients of <code>a</code>. For this, you could do the following:</p>
<ul>
<li><p>Make an element:</p>
<pre><code>sage: K = GF(2^7,'a');
sage: x = K.random_element(); x
a^5 + a^4 + a^3 + a
</code></pre></li>
<li><p>Convert the element to a polynomial and get the list of coefficients:</p>
<pre><code>sage: x.polynomial().coeffs()
[0, 1, 0, 1, 1, 1]
</code></pre></li>
<li><p>Go directly from the element to a string by joining the strings of the coefficients:</p>
<pre><code>sage: ''.join(map(str,x.polynomial().coeffs()))
'010111'
</code></pre></li>
</ul>
https://ask.sagemath.org/question/11059/binary-fields/?comment=16155#post-id-16155This is exactly what I have in mind. Thank you so much. The reverse procedure (if anyone is interested) can be done via `K._cache.fetch_int(0x426)`Sun, 08 Jun 2014 21:22:53 +0200https://ask.sagemath.org/question/11059/binary-fields/?comment=16155#post-id-16155Answer by tmonteil for <p>Hello, I would like to perform the following on a binary field, i.e. <code>GF(2^m)</code>.</p>
<ol>
<li>Define a polynomial and solve the polynomial over a binary field.</li>
<li>Convert an element of the binary field into a bit string.</li>
</ol>
<p>For the first, I've tried the following:</p>
<pre><code>K = GF(2^7,'a');
PK.<x>=K[]; #I've also tried "x = PolynomialRing(GF(2^7,'a'),'x').gen"
f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
print f.roots();
</code></pre>
<p>But the error is <code>TypeError: unable to coerce from a finite field other than the prime subfield</code>.</p>
<p>For the second, I would like to know how finite field elements are stored in SAGE, are they stored as vectors?</p>
<p>If you have any resources that could point me in the right direction, I'll be very thankful for your help!</p>
https://ask.sagemath.org/question/11059/binary-fields/?answer=16087#post-id-16087There is a problem since the variable `a` you are using in your third line seems to come from a previous computation.
At least on the last version of Sage, if you type
sage: K = GF(2^7,'a');
The python variable `a` does not point to the generator of `K` whose name is `'a'`
sage: a
NameError: name 'a' is not defined
For this, you have to do:
sage: K.inject_variables()
Defining a
Then, everything seems to work:
sage: PK.<x>=K[];
sage: f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
sage: print f.roots();
[(a^3 + a, 1), (a^5 + a^3 + a^2 + a, 1)]
Fri, 06 Jun 2014 05:52:25 +0200https://ask.sagemath.org/question/11059/binary-fields/?answer=16087#post-id-16087Comment by Blackadder for <p>There is a problem since the variable <code>a</code> you are using in your third line seems to come from a previous computation.</p>
<p>At least on the last version of Sage, if you type </p>
<pre><code>sage: K = GF(2^7,'a');
</code></pre>
<p>The python variable <code>a</code> does not point to the generator of <code>K</code> whose name is <code>'a'</code></p>
<pre><code>sage: a
NameError: name 'a' is not defined
</code></pre>
<p>For this, you have to do:</p>
<pre><code>sage: K.inject_variables()
Defining a
</code></pre>
<p>Then, everything seems to work:</p>
<pre><code>sage: PK.<x>=K[];
sage: f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
sage: print f.roots();
[(a^3 + a, 1), (a^5 + a^3 + a^2 + a, 1)]
</code></pre>
https://ask.sagemath.org/question/11059/binary-fields/?comment=16161#post-id-16161That worked! I don't recall seeing that anyway on the reference manual though. But a HUGE thank you! If you could help with the second, I'll be much obliged.Fri, 06 Jun 2014 06:19:32 +0200https://ask.sagemath.org/question/11059/binary-fields/?comment=16161#post-id-16161Comment by kcrisman for <p>There is a problem since the variable <code>a</code> you are using in your third line seems to come from a previous computation.</p>
<p>At least on the last version of Sage, if you type </p>
<pre><code>sage: K = GF(2^7,'a');
</code></pre>
<p>The python variable <code>a</code> does not point to the generator of <code>K</code> whose name is <code>'a'</code></p>
<pre><code>sage: a
NameError: name 'a' is not defined
</code></pre>
<p>For this, you have to do:</p>
<pre><code>sage: K.inject_variables()
Defining a
</code></pre>
<p>Then, everything seems to work:</p>
<pre><code>sage: PK.<x>=K[];
sage: f = (a^6 + a^3 + a)*x^2 + (a^6 + a^4 + a^3)*x + (a^5 + a^4 + a^3 + a^2 + 1);
sage: print f.roots();
[(a^3 + a, 1), (a^5 + a^3 + a^2 + a, 1)]
</code></pre>
https://ask.sagemath.org/question/11059/binary-fields/?comment=16160#post-id-16160That missing `a` is exactly what I was asking about! I could never even make it past defining `f`, which would give the error Thierry points out here.Fri, 06 Jun 2014 21:50:14 +0200https://ask.sagemath.org/question/11059/binary-fields/?comment=16160#post-id-16160