Ask Your Question
1

What is wrong with the syntax?

asked 2020-12-11 13:54:03 +0100

obvionaoe gravatar image

updated 2020-12-11 18:42:00 +0100

slelievre gravatar image

Hi, I'm trying to run this piece of code:

from sage.all import *
F = FiniteField(2**130-5)
PR.<X> = PolynomialRing(F)

and I get this error:

PR.<X> = PolynomialRing(F)

File "<ipython-input-5-6d5ce5bc6dd0>", line 1
    PR.<X> = PolynomialRing(F)
       ^
    SyntaxError: invalid syntax

but when my teacher runs the exact same piece of code it works perfectly, what am I doing wrong?

edit retag flag offensive close merge delete

Comments

Welcome to Ask Sage!

Thank you for your question!

slelievre gravatar imageslelievre ( 2020-12-11 18:57:03 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-12-11 18:40:33 +0100

slelievre gravatar image

updated 2020-12-11 18:56:27 +0100

The difference in behaviour is explained by the fact that your teacher is using SageMath, while you are using pure Python.

Sage offers a small number of additions to Python syntax.

One of them is PR.<X> = ... to define at the same time a polynomial ring PR and its generator X.

This syntactic sugar in Sage violates Python's syntax, which does not recognize PR.<...

Sage includes a preparser which takes care of these syntax differences, and translates user code that uses these additions into valid Python code.

To check what Sage would transform the input into, ask it to preparse it:

This command will do that:

preparse("PR.<X> = PolynomialRing(F)")

The output will be:

"PR = PolynomialRing(F, names=('X',)); (X,) = PR._first_ngens(1)"

The good thing is that the Python you are using seems to have Sage installed, since it accepted the line

from sage.all import *

To solve your problem, you have three choices

  • run Sage instead of Python; then everything will work as for your teacher
  • include a line to activate the preparser
  • or preparse things "by hand" and enter the corresponding Python code

The first choice will make your life easier.

The second choice is still quite easy. Right after the import line, activate the preparser:

from sage.all import *
preparser(True)

so that everything that follows is preparsed as Sage code.

The third choice will make you learn a lot (can be useful later but maybe not your priority now).

For example, you could run that right after the line

>>> from sage.all import *
>>> preparse("F = FiniteField(2**130-5)")
'F = FiniteField(Integer(2)**Integer(130)-Integer(5))'
>>> F = FiniteField(Integer(2)**Integer(130)-Integer(5))
>>> F
Finite Field of size 1361129467683753853853498429727072845819
>>> preparse("PR.<X> = PolynomialRing(F)")
"PR = PolynomialRing(F, names=('X',)); (X,) = PR._first_ngens(1)"
>>> PR = PolynomialRing(F, names=('X',)); (X,) = PR._first_ngens(1)
>>> PR
Univariate Polynomial Ring in X over
Finite Field of size 1361129467683753853853498429727072845819
(using NTL)

The last thing to say is how to start Sage.

To run the Sage REPL (read-eval-print loop), ie "Sage in the terminal", open a terminal and run sage. Depending on how you installed Sage, this might work straight away or require a preliminary step.

If you are using Jupyter, use the "Kernel > Change Kernel" menu item and select a SageMath kernel instead of a Python kernel.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2020-12-11 13:54:03 +0100

Seen: 2,272 times

Last updated: Dec 11 '20