Ask Your Question
0

Using SageMath Finite Field Extension on Python.

asked 3 years ago

klx gravatar image

updated 2 years ago

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

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 3 years ago

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')
Preview: (hide)
link

Comments

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?

klx gravatar imageklx ( 3 years ago )
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.

John Palmieri gravatar imageJohn Palmieri ( 3 years ago )
1

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

John Palmieri gravatar imageJohn Palmieri ( 3 years ago )

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

Stats

Asked: 3 years ago

Seen: 787 times

Last updated: Nov 10 '21