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, 13 Sep 2019 13:49:00 -0500Finite Fields: Basic Questionhttps://ask.sagemath.org/question/47894/finite-fields-basic-question/Good people!
So I'm currently working on a problem where I am dealing with a variety of variables which takes values in finite fields. After originally having tried for a week to do this the hard way from scratch in Python, the other day, my supervisor suggested that, hey, you know, SageMath already comes with the full functionality of finite fields, so why not give it a go? And so I installed it just yesterday!
I'm having problems finding the appropriate tutorials online to figure out the answer for a problem that is quite simple. Specifically, let's say that I create the finite field of order $3^2$,
k = GF(9, 'a')
Then that has elements ${0,a,a + 1,2a + 1,2,2a,2a + 2,a + 2,1}$. Now, I just want to start assigning various variables values in this field, for example, to just take something out of the blue, set $x = 2a+1$, then my natural assumption would be that, "ah, you just write:"
x = 2*a+1
But, well, turns out you cannot do that, which in retrospect is fairly obvious, since $k$ is some sort of array or another.
Can someone please give either some pointer or direct me to a place where it is explained how you work with variables that take values in a finite field?Fri, 13 Sep 2019 10:36:37 -0500https://ask.sagemath.org/question/47894/finite-fields-basic-question/Answer by tmonteil for <p>Good people!</p>
<p>So I'm currently working on a problem where I am dealing with a variety of variables which takes values in finite fields. After originally having tried for a week to do this the hard way from scratch in Python, the other day, my supervisor suggested that, hey, you know, SageMath already comes with the full functionality of finite fields, so why not give it a go? And so I installed it just yesterday!</p>
<p>I'm having problems finding the appropriate tutorials online to figure out the answer for a problem that is quite simple. Specifically, let's say that I create the finite field of order $3^2$,</p>
<pre><code>k = GF(9, 'a')
</code></pre>
<p>Then that has elements ${0,a,a + 1,2a + 1,2,2a,2a + 2,a + 2,1}$. Now, I just want to start assigning various variables values in this field, for example, to just take something out of the blue, set $x = 2a+1$, then my natural assumption would be that, "ah, you just write:"</p>
<pre><code>x = 2*a+1
</code></pre>
<p>But, well, turns out you cannot do that, which in retrospect is fairly obvious, since $k$ is some sort of array or another.</p>
<p>Can someone please give either some pointer or direct me to a place where it is explained how you work with variables that take values in a finite field?</p>
https://ask.sagemath.org/question/47894/finite-fields-basic-question/?answer=47901#post-id-47901When you write
sage: k = GF(9, 'a')
The Python name `k` points to the field with 9 elements:
sage: k
Finite Field in a of size 3^2
But the Python name `a` does not point to anything:
sage: a
NameError: name 'a' is not defined
If you want to get the generator of the field, you can do:
sage: k.gen()
a
sage: k.gen()^8
1
So, if you want the Python name `a` to point to that generator of the field, you can just do:
sage: a = k.gen()
sage: a
a
sage: a^2
a + 1
sage: x = 2*a+1
sage: x
2*a + 1
sage: x^3
a
Now, Sage has some magic to do that, you could also do (it is very convenient for objects with several generators):
sage: k.inject_variables()
Defining a
Also, there is something else, which is convenient for mathematicians, but violates the Python language (there is a specific Sage preparsing behind the scenes), you can do:
sage: k.<a> = GF(9)
This defines both `k` and `a` at the same time.Fri, 13 Sep 2019 11:33:26 -0500https://ask.sagemath.org/question/47894/finite-fields-basic-question/?answer=47901#post-id-47901Comment by StormyTeacup for <p>When you write</p>
<pre><code>sage: k = GF(9, 'a')
</code></pre>
<p>The Python name <code>k</code> points to the field with 9 elements:</p>
<pre><code>sage: k
Finite Field in a of size 3^2
</code></pre>
<p>But the Python name <code>a</code> does not point to anything:</p>
<pre><code>sage: a
NameError: name 'a' is not defined
</code></pre>
<p>If you want to get the generator of the field, you can do:</p>
<pre><code>sage: k.gen()
a
sage: k.gen()^8
1
</code></pre>
<p>So, if you want the Python name <code>a</code> to point to that generator of the field, you can just do:</p>
<pre><code>sage: a = k.gen()
sage: a
a
sage: a^2
a + 1
sage: x = 2*a+1
sage: x
2*a + 1
sage: x^3
a
</code></pre>
<p>Now, Sage has some magic to do that, you could also do (it is very convenient for objects with several generators):</p>
<pre><code>sage: k.inject_variables()
Defining a
</code></pre>
<p>Also, there is something else, which is convenient for mathematicians, but violates the Python language (there is a specific Sage preparsing behind the scenes), you can do:</p>
<pre><code>sage: k.<a> = GF(9)
</code></pre>
<p>This defines both <code>k</code> and <code>a</code> at the same time.</p>
https://ask.sagemath.org/question/47894/finite-fields-basic-question/?comment=47905#post-id-47905Now this is just a perfect little summary of everything I wanted to know! Thank you very much! :DFri, 13 Sep 2019 13:49:00 -0500https://ask.sagemath.org/question/47894/finite-fields-basic-question/?comment=47905#post-id-47905Answer by rburing for <p>Good people!</p>
<p>So I'm currently working on a problem where I am dealing with a variety of variables which takes values in finite fields. After originally having tried for a week to do this the hard way from scratch in Python, the other day, my supervisor suggested that, hey, you know, SageMath already comes with the full functionality of finite fields, so why not give it a go? And so I installed it just yesterday!</p>
<p>I'm having problems finding the appropriate tutorials online to figure out the answer for a problem that is quite simple. Specifically, let's say that I create the finite field of order $3^2$,</p>
<pre><code>k = GF(9, 'a')
</code></pre>
<p>Then that has elements ${0,a,a + 1,2a + 1,2,2a,2a + 2,a + 2,1}$. Now, I just want to start assigning various variables values in this field, for example, to just take something out of the blue, set $x = 2a+1$, then my natural assumption would be that, "ah, you just write:"</p>
<pre><code>x = 2*a+1
</code></pre>
<p>But, well, turns out you cannot do that, which in retrospect is fairly obvious, since $k$ is some sort of array or another.</p>
<p>Can someone please give either some pointer or direct me to a place where it is explained how you work with variables that take values in a finite field?</p>
https://ask.sagemath.org/question/47894/finite-fields-basic-question/?answer=47900#post-id-47900You have to define `a = k.gen()`.
The shorthand notation to define both `k` and `a` is `k.<a> = GF(9)`.Fri, 13 Sep 2019 11:31:47 -0500https://ask.sagemath.org/question/47894/finite-fields-basic-question/?answer=47900#post-id-47900