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.Thu, 28 May 2015 21:05:21 +0200How to flatten polynomial rings?https://ask.sagemath.org/question/26969/how-to-flatten-polynomial-rings/ It is possible in Sage to create a polynomial ring over a polynomial ring. But Sage stores this as a two-tiered structure that is sometimes inconvenient. I would like to flatten it.
For example,
sage: P = PolynomialRing(QQ, 'c', 6)
sage: S = PolynomialRing(P, 't', 6)
creates a polynomial ring in t0, t1, t2, ...., t5 over a polynomial ring in c0, c1, ..., c5. But then
sage: clist = list( P.gens()[i] for i in range(6)) # get names for variables
sage: tlist = list( S.gens()[i] for i in range(6))
sage: poly = clist[0]*tlist[0] # produces polynomial c0*t0 in S
sage: poly.polynomial(clist[0]) # asks for poly as a polynomial in c0
fails with "var must be one of the generators of the parent polynomial
ring."
I would like to `flatten' S so it is simply a single polynomial ring over QQ in 12 variables.
You may ask, why don't I just construct a single ring in the first place? In this instance, because I want to create a polynomial ring in c0, ...., cn and t0, ...., tn where n is a variable, and I don't know how to do this (I'd prefer to avoid just making c0, ..., c2n and trying to keep track of which are actually t's). I can imagine other situations where one would like to be able to flatten also.Thu, 28 May 2015 17:44:37 +0200https://ask.sagemath.org/question/26969/how-to-flatten-polynomial-rings/Answer by tmonteil for <p>It is possible in Sage to create a polynomial ring over a polynomial ring. But Sage stores this as a two-tiered structure that is sometimes inconvenient. I would like to flatten it.</p>
<p>For example,</p>
<pre><code>sage: P = PolynomialRing(QQ, 'c', 6)
sage: S = PolynomialRing(P, 't', 6)
</code></pre>
<p>creates a polynomial ring in t0, t1, t2, ...., t5 over a polynomial ring in c0, c1, ..., c5. But then</p>
<pre><code>sage: clist = list( P.gens()[i] for i in range(6)) # get names for variables
sage: tlist = list( S.gens()[i] for i in range(6))
sage: poly = clist[0]*tlist[0] # produces polynomial c0*t0 in S
sage: poly.polynomial(clist[0]) # asks for poly as a polynomial in c0
</code></pre>
<p>fails with "var must be one of the generators of the parent polynomial
ring."</p>
<p>I would like to `flatten' S so it is simply a single polynomial ring over QQ in 12 variables. </p>
<p>You may ask, why don't I just construct a single ring in the first place? In this instance, because I want to create a polynomial ring in c0, ...., cn and t0, ...., tn where n is a variable, and I don't know how to do this (I'd prefer to avoid just making c0, ..., c2n and trying to keep track of which are actually t's). I can imagine other situations where one would like to be able to flatten also.</p>
https://ask.sagemath.org/question/26969/how-to-flatten-polynomial-rings/?answer=26972#post-id-26972From your data, you can get the list of indeterminates and make a new polynomial ring from them:
sage: R = PolynomialRing(QQ, P.gens()+S.gens())
sage: R(poly)
c0*t0
sage: R(poly).parent()
Multivariate Polynomial Ring in c0, c1, c2, c3, c4, c5, t0, t1, t2, t3, t4, t5 over Rational Field
If you want to only use `S`, you can recover `P` from `S.base_ring()`, and get the desired flattening:
sage: PolynomialRing(QQ, S.base_ring().gens()+S.gens())
Multivariate Polynomial Ring in c0, c1, c2, c3, c4, c5, t0, t1, t2, t3, t4, t5 over Rational Field
If you want to directly build a polynomial ring from a list of strings with two prefixes, you can also do:
sage: PolynomialRing(QQ,["c{}".format(i) for i in range(6)]+["t{}".format(i) for i in range(6)])
Multivariate Polynomial Ring in c0, c1, c2, c3, c4, c5, t0, t1, t2, t3, t4, t5 over Rational FieldThu, 28 May 2015 21:05:21 +0200https://ask.sagemath.org/question/26969/how-to-flatten-polynomial-rings/?answer=26972#post-id-26972