# Construct polynomial ring from list of variables

In constructing polynomial rings, documentation says that diamond brackets are used "to make the variable ready for use after you define the ring", but the variables are entered explicitly: R.<x,y> .

What if I have a list of (an unknown number of) variables?

E.g. if I have xs = [x_1,x_2,x_3], I want to construct a polynomial ring and subsequently manipulate x_1 + x_2, etc., within that ring instead of "symbolic ring". I'd like to do something like R.<xs> but that doesn't seem to work.

edit retag close merge delete

Sort by » oldest newest most voted

Given the list of strings :

sage: xs = ['x_1','x_2','x_3']


you can define the polynomial with this list as indeterminate names :

sage: R = PolynomialRing(QQ, names=xs)
sage: R
Multivariate Polynomial Ring in x_1, x_2, x_3 over Rational Field


In the R.<x,y> construction, there some Sage preparsing that hides the fact that there are two operations being done:

• Creating the polynomial ring R with 'x' and 'y' as indetereminate names
• Leting the Python names x and y point to the corresponding indeterminates

See:

sage: preparse('R.<x,y> = PolynomialRing(QQ)')
"R = PolynomialRing(QQ, names=('x', 'y',)); (x, y,) = R._first_ngens(2)"


So, in order to be able to wrire x_1+x_2^3, we have to create those Python names. For this, there is a very handy method named inject_variables:

sage: R.inject_variables()
Defining x_1, x_2, x_3


Now, you can do things like:

sage: x_1 + x_2^3
x_2^3 + x_1

more