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.Sat, 05 Dec 2015 21:54:41 +0100Polynomials with coefficients in Zp - python SyntaxErrorhttps://ask.sagemath.org/question/31343/polynomials-with-coefficients-in-zp-python-syntaxerror/ Hi,
I am trying to create ℤₚ[x], the set of polynomials with coefficients in ℤₚ, with the following syntax :<br />
Zp = Integers(p)<br />
PRZp.<x> = Zp[]<br />
This works fine in the sage interpreter, but in a python file I get a syntax error at the "<" character. How can I solve this ? I've tried alternatives, like PRZp = PolynomialRing(Zp, 'x'), but this fails too with a "NameError: name 'x' is not defined" when I define a polynomial expression after that.Sat, 05 Dec 2015 14:17:37 +0100https://ask.sagemath.org/question/31343/polynomials-with-coefficients-in-zp-python-syntaxerror/Answer by tmonteil for <p>Hi,</p>
<p>I am trying to create ℤₚ[x], the set of polynomials with coefficients in ℤₚ, with the following syntax :<br/>
Zp = Integers(p)<br/>
PRZp.<x> = Zp[]<br/></p>
<p>This works fine in the sage interpreter, but in a python file I get a syntax error at the "<" character. How can I solve this ? I've tried alternatives, like PRZp = PolynomialRing(Zp, 'x'), but this fails too with a "NameError: name 'x' is not defined" when I define a polynomial expression after that.</p>
https://ask.sagemath.org/question/31343/polynomials-with-coefficients-in-zp-python-syntaxerror/?answer=31355#post-id-31355Let me first answer the secont part. When you write `PRZp = PolynomialRing(Zp, 'x')`, you only define `PRZp` to be a polynomial ring with the symbol `'x'` as an indeterminate, but you did not let the PYthon variable `x` to point to the polynomial undeterminate `'x'`. For this, you can for example do:
sage: x = PRZp.gen()
or
sage: PRZp.inject_variables()
Defining x
Now, for the first part, the notation `PRZp.<x> = Zp[]` is indeed not Pythonic, so it works because of Sage preparser that transforms it to something meaningful to Python:
sage: preparse('PRZp.<x> = Zp[]')
"PRZp = Zp['x']; (x,) = PRZp._first_ngens(1)"
So, you can either writh that in your python file, or, if you want to keep the same syntax, you should use a Sage file instead ot a Python file, so that Sage preparser get applied first. The way to do this is to rename `yourfile.py` into `yourfile.sage`.
Sat, 05 Dec 2015 21:54:41 +0100https://ask.sagemath.org/question/31343/polynomials-with-coefficients-in-zp-python-syntaxerror/?answer=31355#post-id-31355