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.Sun, 18 Apr 2021 12:35:46 +0200Construct polynomial ring from list of variableshttps://ask.sagemath.org/question/56699/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.Sun, 18 Apr 2021 03:19:54 +0200https://ask.sagemath.org/question/56699/construct-polynomial-ring-from-list-of-variables/Answer by tmonteil for <p>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: <code>R.<x,y></code> .</p>
<p>What if I have a list of (an unknown number of) variables?</p>
<p>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 <code>R.<xs></code> but that doesn't seem to work.</p>
https://ask.sagemath.org/question/56699/construct-polynomial-ring-from-list-of-variables/?answer=56708#post-id-56708Given 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
Sun, 18 Apr 2021 12:35:46 +0200https://ask.sagemath.org/question/56699/construct-polynomial-ring-from-list-of-variables/?answer=56708#post-id-56708