# 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.

edit retag close merge delete

Sort by » oldest newest most voted

From 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 Field
`
more