# 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 and stackoverflow

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.

edit retag close merge delete

Sort by ยป oldest newest most voted

There 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')

more

This 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?

( 2021-11-10 18:41:21 +0200 )edit
1

In principle, import_statements(GF) will tell you what you need to import in order for GFto work (https://doc.sagemath.org/html/en/refe...). In practice, it is unfortunately more complicated.

( 2021-11-10 20:53:49 +0200 )edit
1

from sage.all import GF is slightly better, but not ideal.

( 2021-11-10 21:00:42 +0200 )edit