ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 11 Dec 2020 18:57:03 +0100What is wrong with the syntax?https://ask.sagemath.org/question/54636/what-is-wrong-with-the-syntax/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?Fri, 11 Dec 2020 13:54:03 +0100https://ask.sagemath.org/question/54636/what-is-wrong-with-the-syntax/Comment by slelievre for <p>Hi, I'm trying to run this piece of code:</p>
<pre><code>from sage.all import *
F = FiniteField(2**130-5)
PR.<X> = PolynomialRing(F)
</code></pre>
<p>and I get this error:</p>
<pre><code>PR.<X> = PolynomialRing(F)
File "<ipython-input-5-6d5ce5bc6dd0>", line 1
PR.<X> = PolynomialRing(F)
^
SyntaxError: invalid syntax
</code></pre>
<p>but when my teacher runs the exact same piece of code
it works perfectly, what am I doing wrong?</p>
https://ask.sagemath.org/question/54636/what-is-wrong-with-the-syntax/?comment=54650#post-id-54650Welcome to Ask Sage!
Thank you for your question!Fri, 11 Dec 2020 18:57:03 +0100https://ask.sagemath.org/question/54636/what-is-wrong-with-the-syntax/?comment=54650#post-id-54650Answer by slelievre for <p>Hi, I'm trying to run this piece of code:</p>
<pre><code>from sage.all import *
F = FiniteField(2**130-5)
PR.<X> = PolynomialRing(F)
</code></pre>
<p>and I get this error:</p>
<pre><code>PR.<X> = PolynomialRing(F)
File "<ipython-input-5-6d5ce5bc6dd0>", line 1
PR.<X> = PolynomialRing(F)
^
SyntaxError: invalid syntax
</code></pre>
<p>but when my teacher runs the exact same piece of code
it works perfectly, what am I doing wrong?</p>
https://ask.sagemath.org/question/54636/what-is-wrong-with-the-syntax/?answer=54648#post-id-54648The 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.Fri, 11 Dec 2020 18:40:33 +0100https://ask.sagemath.org/question/54636/what-is-wrong-with-the-syntax/?answer=54648#post-id-54648