ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 10 Nov 2012 16:48:53 -0600Creating a polynomial ring where the variables are code generatedhttp://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/Hi, The standard way to create a multivariate polynomial ring over `ZZ` is as follows.
sage: R.<x,y> = PolynomialRing(ZZ)
I want to create a polynomial ring over `ZZ` where the variables are generated by code.
sage: n = 5
sage: lstx = list(var('x_%d' % i) for i in range(n))
sage: lstx
[x_0, x_1, x_2, x_3, x_4]
My attempts at creating the desired ring whose variables are the elements of `lstx` result in error. Any help will be appreciated.Tue, 06 Nov 2012 02:14:46 -0600http://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/Answer by DSM for <p>Hi, The standard way to create a multivariate polynomial ring over <code>ZZ</code> is as follows.</p>
<pre><code>sage: R.<x,y> = PolynomialRing(ZZ)
</code></pre>
<p>I want to create a polynomial ring over <code>ZZ</code> where the variables are generated by code.</p>
<pre><code>sage: n = 5
sage: lstx = list(var('x_%d' % i) for i in range(n))
sage: lstx
[x_0, x_1, x_2, x_3, x_4]
</code></pre>
<p>My attempts at creating the desired ring whose variables are the elements of <code>lstx</code> result in error. Any help will be appreciated.</p>
http://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/?answer=14230#post-id-14230You can simply pass the `PolynomialRing` object a list of names:
sage: n = 5
sage: xs = [var("x_%d" % i) for i in range(n)]
sage: R = PolynomialRing(ZZ, names=xs)
sage: R
Multivariate Polynomial Ring in x_0, x_1, x_2, x_3, x_4 over Integer Ring
sage: x_4
x_4
sage: x_4 in R
True
In practice you're probably going to want to refer to the generators in some structure and not explicitly (if you decide you want `n` to be 10, you don't want to have to go around and add up to `x_9` everywhere) so `R.gens()` and `R.gens_dict()` will come in handy:
sage: R.gens()
(x_0, x_1, x_2, x_3, x_4)
sage: R.gens_dict()
{'x_4': x_4, 'x_2': x_2, 'x_3': x_3, 'x_0': x_0, 'x_1': x_1}
Tue, 06 Nov 2012 03:13:45 -0600http://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/?answer=14230#post-id-14230Answer by kcrisman for <p>Hi, The standard way to create a multivariate polynomial ring over <code>ZZ</code> is as follows.</p>
<pre><code>sage: R.<x,y> = PolynomialRing(ZZ)
</code></pre>
<p>I want to create a polynomial ring over <code>ZZ</code> where the variables are generated by code.</p>
<pre><code>sage: n = 5
sage: lstx = list(var('x_%d' % i) for i in range(n))
sage: lstx
[x_0, x_1, x_2, x_3, x_4]
</code></pre>
<p>My attempts at creating the desired ring whose variables are the elements of <code>lstx</code> result in error. Any help will be appreciated.</p>
http://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/?answer=14229#post-id-14229You don't need variables in that sense. Note that
sage: PolynomialRing?
has very extensive documentation on constructing such rings. E.g., it has examples basically like what you want.
sage: R = PolynomialRing(ZZ, 'x', 5)
sage: R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Integer Ring
sage: x0
NameError: name 'x0' is not defined
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4
sage: x0
x0
Tue, 06 Nov 2012 03:08:06 -0600http://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/?answer=14229#post-id-14229Answer by Iftikhar Burhanuddin for <p>Hi, The standard way to create a multivariate polynomial ring over <code>ZZ</code> is as follows.</p>
<pre><code>sage: R.<x,y> = PolynomialRing(ZZ)
</code></pre>
<p>I want to create a polynomial ring over <code>ZZ</code> where the variables are generated by code.</p>
<pre><code>sage: n = 5
sage: lstx = list(var('x_%d' % i) for i in range(n))
sage: lstx
[x_0, x_1, x_2, x_3, x_4]
</code></pre>
<p>My attempts at creating the desired ring whose variables are the elements of <code>lstx</code> result in error. Any help will be appreciated.</p>
http://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/?answer=14247#post-id-14247Thanks for the answers.Sat, 10 Nov 2012 16:48:53 -0600http://ask.sagemath.org/question/9504/creating-a-polynomial-ring-where-the-variables-are-code-generated/?answer=14247#post-id-14247