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.Fri, 03 Apr 2020 04:31:35 -0500Why is SageMath fail to generate elements in finite field $GF(3^{15})$https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/ I would like to generate elements of finite filed $GF(3^{15})$. To do so I have used the following code :
F.<x> = GF(3^15)
for i in range(3^15):
print i,"=>", F.fetch_int(i)
But this code failed to generate elements, occurring errors. On there hand the above code works fine for $GF(3^{2})$ given below:
F.<x> = GF(3^2)
for i in range(3^2):
print i,"=>", F.fetch_int(i)
Produces:
0 => 0
1 => 1
2 => 2
3 => x
4 => x + 1
5 => x + 2
6 => 2*x
7 => 2*x + 1
8 => 2*x + 2.
Where is the problem?Fri, 03 Apr 2020 00:11:09 -0500https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/Comment by Sébastien for <p>I would like to generate elements of finite filed $GF(3^{15})$. To do so I have used the following code : </p>
<pre><code>F.<x> = GF(3^15)
for i in range(3^15):
print i,"=>", F.fetch_int(i)
</code></pre>
<p>But this code failed to generate elements, occurring errors. On there hand the above code works fine for $GF(3^{2})$ given below: </p>
<pre><code>F.<x> = GF(3^2)
for i in range(3^2):
print i,"=>", F.fetch_int(i)
</code></pre>
<p>Produces: </p>
<pre><code>0 => 0
1 => 1
2 => 2
3 => x
4 => x + 1
5 => x + 2
6 => 2*x
7 => 2*x + 1
8 => 2*x + 2.
</code></pre>
<p>Where is the problem?</p>
https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/?comment=50484#post-id-50484You may just iterate on F directly:
sage: F.<x> = GF(3^15)
sage: for a in F:
....: print(a)
....:
[...]Fri, 03 Apr 2020 04:31:35 -0500https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/?comment=50484#post-id-50484Answer by rburing for <p>I would like to generate elements of finite filed $GF(3^{15})$. To do so I have used the following code : </p>
<pre><code>F.<x> = GF(3^15)
for i in range(3^15):
print i,"=>", F.fetch_int(i)
</code></pre>
<p>But this code failed to generate elements, occurring errors. On there hand the above code works fine for $GF(3^{2})$ given below: </p>
<pre><code>F.<x> = GF(3^2)
for i in range(3^2):
print i,"=>", F.fetch_int(i)
</code></pre>
<p>Produces: </p>
<pre><code>0 => 0
1 => 1
2 => 2
3 => x
4 => x + 1
5 => x + 2
6 => 2*x
7 => 2*x + 1
8 => 2*x + 2.
</code></pre>
<p>Where is the problem?</p>
https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/?answer=50482#post-id-50482See the [documentation of GF](http://doc.sagemath.org/html/en/reference/finite_rings/sage/rings/finite_rings/finite_field_constructor.html); the different implementations depending on the size are the first thing mentioned.
In particular, $\log_3(2^{16}) \approx 10.09$ means `GF(3^2), ..., GF(3^10)` use the Givaro implementation while `GF(3^11), GF(3^12), ...` use the PARI implementation. The `fetch_int` method is not available in the PARI implementation, which explains the error.
Note that the mapping you are interested in is very simple: for example $7$ is $2\cdot 3 + 1$, so its ternary (base 3) digits are `21`, and the corresponding element of `F.<x> = GF(3^k)` is `2*x + 1` ($3$ is replaced by $x$).
Since `F` is a vector space over `GF(3)` with basis `1`, `x`, `x^2`, ..., `x^(k-1)`, a vector (or list) of elements of `GF(3)` can be converted into `F`. For example, `F([1,2])` yields `2*x + 1`; the reason for the "reversal" is that the "digits" are interpreted in [little endian](https://en.wikipedia.org/wiki/Endianness) order. This is convenient because the output of e.g. `7.digits(3)` is also in little endian order, yielding `[1,2]`. We can put this together:
F.<x> = GF(3^15)
for i in srange(F.cardinality()):
print(i, '=>', F(i.digits(3)))
Though it is kind of pointless to print that gigantic list when you already understand the mapping.
Also, I suppose that a `fetch_int` method could be added to the PARI implementation for convenience.Fri, 03 Apr 2020 03:44:56 -0500https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/?answer=50482#post-id-50482Comment by BSFU for <p>See the <a href="http://doc.sagemath.org/html/en/reference/finite_rings/sage/rings/finite_rings/finite_field_constructor.html">documentation of GF</a>; the different implementations depending on the size are the first thing mentioned.</p>
<p>In particular, $\log_3(2^{16}) \approx 10.09$ means <code>GF(3^2), ..., GF(3^10)</code> use the Givaro implementation while <code>GF(3^11), GF(3^12), ...</code> use the PARI implementation. The <code>fetch_int</code> method is not available in the PARI implementation, which explains the error.</p>
<p>Note that the mapping you are interested in is very simple: for example $7$ is $2\cdot 3 + 1$, so its ternary (base 3) digits are <code>21</code>, and the corresponding element of <code>F.<x> = GF(3^k)</code> is <code>2*x + 1</code> ($3$ is replaced by $x$).</p>
<p>Since <code>F</code> is a vector space over <code>GF(3)</code> with basis <code>1</code>, <code>x</code>, <code>x^2</code>, ..., <code>x^(k-1)</code>, a vector (or list) of elements of <code>GF(3)</code> can be converted into <code>F</code>. For example, <code>F([1,2])</code> yields <code>2*x + 1</code>; the reason for the "reversal" is that the "digits" are interpreted in <a href="https://en.wikipedia.org/wiki/Endianness">little endian</a> order. This is convenient because the output of e.g. <code>7.digits(3)</code> is also in little endian order, yielding <code>[1,2]</code>. We can put this together:</p>
<pre><code>F.<x> = GF(3^15)
for i in srange(F.cardinality()):
print(i, '=>', F(i.digits(3)))
</code></pre>
<p>Though it is kind of pointless to print that gigantic list when you already understand the mapping.</p>
<p>Also, I suppose that a <code>fetch_int</code> method could be added to the PARI implementation for convenience.</p>
https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/?comment=50483#post-id-50483@rburing tank you very much for your solution.Fri, 03 Apr 2020 03:55:28 -0500https://ask.sagemath.org/question/50479/why-is-sagemath-fail-to-generate-elements-in-finite-field-gf315/?comment=50483#post-id-50483