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.Wed, 10 Nov 2021 21:00:42 +0100Using SageMath Finite Field Extension on Python.https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/Yes, I want to the reverse, use the SageMath in Python.
I've seen this on [ask.sagemath](https://ask.sagemath.org/question/39742/make-pycharm-recognise-the-sage-python-interpreter/) and [stackoverflow](https://stackoverflow.com/questions/67440834/how-to-use-sagemath-on-python)
I want to use this in Python
k = GF(2)
R.<x> = k[]
k.extension(x^1000 + x^5 + x^4 + x^3 + 1, 'a')
The python code
from sage.all import *
F = GF(2)
R.<x> = k[]
K = F.extension(x^4 + x + 1, 'a')
print(K)
the `R.<x> = k[]` fails...
Is there a way to do this in python?
My final aim is finding the multiplicative inverse of an element using python with the sagemath import.Wed, 10 Nov 2021 17:43:18 +0100https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/Answer by John Palmieri for <p>Yes, I want to the reverse, use the SageMath in Python.</p>
<p>I've seen this on <a href="https://ask.sagemath.org/question/39742/make-pycharm-recognise-the-sage-python-interpreter/">ask.sagemath</a> and <a href="https://stackoverflow.com/questions/67440834/how-to-use-sagemath-on-python">stackoverflow</a></p>
<p>I want to use this in Python</p>
<pre><code>k = GF(2)
R.<x> = k[]
k.extension(x^1000 + x^5 + x^4 + x^3 + 1, 'a')
</code></pre>
<p>The python code</p>
<pre><code>from sage.all import *
F = GF(2)
R.<x> = k[]
K = F.extension(x^4 + x + 1, 'a')
print(K)
</code></pre>
<p>the <code>R.<x> = k[]</code> fails...</p>
<p>Is there a way to do this in python?</p>
<p>My final aim is finding the multiplicative inverse of an element using python with the sagemath import.</p>
https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?answer=59674#post-id-59674There is a minor issue in your code:
F = GF(2)
R.<x> = k[]
Presumably `F` should be `k` or vice versa. The major issue with using this in Python is that `R.<x> = k[]` is not allowable Python syntax. Sage preparses it first. You can find out how it does this as follows:
sage: k = GF(2)
sage: preparse('R.<x> = k[]')
"R = k['x']; (x,) = R._first_ngens(1)"
So you should be able to do
from sage.all import *
k = GF(2)
R = k['x']; (x,) = R._first_ngens(1)
K = F.extension(x^4 + x + 1, 'a')Wed, 10 Nov 2021 18:29:57 +0100https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?answer=59674#post-id-59674Comment by John Palmieri for <p>There is a minor issue in your code:</p>
<pre><code>F = GF(2)
R.<x> = k[]
</code></pre>
<p>Presumably <code>F</code> should be <code>k</code> or vice versa. The major issue with using this in Python is that <code>R.<x> = k[]</code> is not allowable Python syntax. Sage preparses it first. You can find out how it does this as follows:</p>
<pre><code>sage: k = GF(2)
sage: preparse('R.<x> = k[]')
"R = k['x']; (x,) = R._first_ngens(1)"
</code></pre>
<p>So you should be able to do</p>
<pre><code>from sage.all import *
k = GF(2)
R = k['x']; (x,) = R._first_ngens(1)
K = F.extension(x^4 + x + 1, 'a')
</code></pre>
https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?comment=59680#post-id-59680`from sage.all import GF` is slightly better, but not ideal.Wed, 10 Nov 2021 21:00:42 +0100https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?comment=59680#post-id-59680Comment by John Palmieri for <p>There is a minor issue in your code:</p>
<pre><code>F = GF(2)
R.<x> = k[]
</code></pre>
<p>Presumably <code>F</code> should be <code>k</code> or vice versa. The major issue with using this in Python is that <code>R.<x> = k[]</code> is not allowable Python syntax. Sage preparses it first. You can find out how it does this as follows:</p>
<pre><code>sage: k = GF(2)
sage: preparse('R.<x> = k[]')
"R = k['x']; (x,) = R._first_ngens(1)"
</code></pre>
<p>So you should be able to do</p>
<pre><code>from sage.all import *
k = GF(2)
R = k['x']; (x,) = R._first_ngens(1)
K = F.extension(x^4 + x + 1, 'a')
</code></pre>
https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?comment=59679#post-id-59679In principle, `import_statements(GF)` will tell you what you need to import in order for `GF` to work (https://doc.sagemath.org/html/en/reference/misc/sage/misc/dev_tools.html#sage.misc.dev_tools.import_statement_string). In practice, it is unfortunately more complicated.Wed, 10 Nov 2021 20:53:49 +0100https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?comment=59679#post-id-59679Comment by klx for <p>There is a minor issue in your code:</p>
<pre><code>F = GF(2)
R.<x> = k[]
</code></pre>
<p>Presumably <code>F</code> should be <code>k</code> or vice versa. The major issue with using this in Python is that <code>R.<x> = k[]</code> is not allowable Python syntax. Sage preparses it first. You can find out how it does this as follows:</p>
<pre><code>sage: k = GF(2)
sage: preparse('R.<x> = k[]')
"R = k['x']; (x,) = R._first_ngens(1)"
</code></pre>
<p>So you should be able to do</p>
<pre><code>from sage.all import *
k = GF(2)
R = k['x']; (x,) = R._first_ngens(1)
K = F.extension(x^4 + x + 1, 'a')
</code></pre>
https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?comment=59675#post-id-59675This should be the code. You have forgotten the `**` instead of `^`
from sage.all import *
F = GF(2)
R = F['x']; (x,) = R._first_ngens(1)
K = F.extension(x**4 + x + 1, 'a')
print(K)
So, the preparse is the key to mapping to Python, Great. What about the real import instead of importing all?Wed, 10 Nov 2021 18:41:21 +0100https://ask.sagemath.org/question/59672/using-sagemath-finite-field-extension-on-python/?comment=59675#post-id-59675