I read here how to work with polynomials. But when I try this
R = QQ['t']
poly = (t+1) * (t+2); poly
Sage gives me the following error:
NameError: name 't' is not defined
What can I do about it?
I read here how to work with polynomials. But when I try this
R = QQ['t']
poly = (t+1) * (t+2); poly
Sage gives me the following error:
NameError: name 't' is not defined
What can I do about it?
Right, you have to inject the variable name when using polynomial rings. The document you point to points out that
sage: R.<t> = PolynomialRing(QQ)
does do this. Or, you can do
sage: R=QQ['t']
sage: R.inject_variables()
Defining t
sage: t+1
t + 1
You wanted to know how to do it without printing the name:
sage: R.inject_variables(verbose=False)
Have fun!
To complement the answer by @kcrisman, another way to go is:
sage: R = PolynomialRing(QQ, 't')
sage: t = R.gen()
Then t
works as expected:
sage: (t+1) * (t+2)
t^2 + 3*t + 2
Note that the Sage syntax R.<t> = ...
will work in a .sage
file but not in a .py
file, while the above works also in a .py
file.
In a .py
file you would first import PolynomialRing
as follows:
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
To find out what imports you need, you can do
sage: import_statements(PolynomialRing)
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
Finally, if you don't need the ring R
,
you can define t
directly with
sage: t = polygen(QQ)
and if you ever need R
later you can use
sage: R = t.parent()