Ask Your Question

Using SageMath Finite Field Extension on Python.

asked 2021-11-10 17:43:18 +0100

klx gravatar image

updated 2022-10-15 13:41:32 +0100

FrédéricC gravatar image

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


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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2021-11-10 18:29:57 +0100

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')
edit flag offensive delete link 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')


So, the preparse is the key to mapping to Python, Great. What about the real import instead of importing all?

klx gravatar imageklx ( 2021-11-10 18:41:21 +0100 )edit

In principle, import_statements(GF) will tell you what you need to import in order for GFto work ( In practice, it is unfortunately more complicated.

John Palmieri gravatar imageJohn Palmieri ( 2021-11-10 20:53:49 +0100 )edit

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

John Palmieri gravatar imageJohn Palmieri ( 2021-11-10 21:00:42 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-11-10 17:43:18 +0100

Seen: 499 times

Last updated: Nov 10 '21