Ask Your Question

Revision history [back]

Your teacher is using SageMath, and you are using pure Python.

Apparently the Python you are using has Sage installed, since it accepted the line

from sage.all import *

However, Sage also includes a preparser which takes care of a small number of Sage syntax additions on top of Python.

If running Python itself rather than Sage, like you seem to be doing, you must activate the preparser for it to work.

To solve your problem,

  • either run Sage instead of Python
  • or include a line to activate the preparser

This should activate the preparser for you:

preparser(True)

For example, you could run that right after the line

from sage.all import *

so that everything that follows is preparsed as Sage code.

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

Apparently 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 has Sage installed, seems to have Sage installed, since it accepted the line

from sage.all import *

However, Sage also includes a preparser which takes care of a small number of Sage syntax additions on top of Python.

If running Python itself rather than Sage, like you seem to be doing, you must activate the preparser for it to work.

To solve your problem,problem, you have three choices

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

This should The first choice will make your life easier.

The second choice is still quite easy. Right after the import line, activate the preparser for you: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)

so that everything that follows is preparsed as Sage code.

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.