# Polynomial transposition

Let's say I have

R1 = ZZ['y']['x']
R2 = ZZ['x']['y']


Is there a builtin function in Sage for converting an element from R1 to R2 by transposing the coefficients, not just switching the variables? For instance y*x+y should become (x+1)*y, not x*y+x.

I can do it by first coercing to ZZ['x','y'] and then to R2, but that seems clumsy (and possibly inefficient). I could also just write a loop, but that seems ugly (and again possibly inefficient). It would be fine with a function that just transposes the polynomial within R1 (then I can just coerce the result to R2 afterwards).

edit retag close merge delete

Sort by » oldest newest most voted I don't know of anything built in to do this, but here's one possible method. I think it requires slightly different definitions of R1 and R2, but maybe you can work something out if you need to keep your definitions:

sage: S1 = ZZ['x,y']
sage: S2 = ZZ['y,x']
sage: a = S1.gen(0) * (S1.gen(1) + 1)
sage: a
x*y + x
sage: a.dict()
{(1, 0): 1, (1, 1): 1}
sage: S2(a.dict())
y*x + y


Edit: sorry, this doesn't do what you want. You should modify the dictionary appropriately:

sage: d = {(p, p):a.dict()[p] for p in a.dict()}
sage: S2(d)
y*x + x


So I guess with your original R1 and R2:

sage: R1 = ZZ['y']['x']
sage: R2 = ZZ['x']['y']
sage: a = R1.base_ring().gen(0) * (R1.gen(0) + 1)
sage: a
y*x + y
sage: a.dict()
{0: y, 1: y}
sage: R2({p: a.dict()[p].subs(R2.base_ring().gen(0)) for p in a.dict()})
x*y + x


Edit: There should be a similar way to modify the dictionary here, too. You might also look into a.map_coefficients(...).

more

Especially if you are concerned about performance you should never create univariate polynomial rings over univariate polynomial rings. Directly using multivariate polynomial rings will be much faster.

Bonus: its then easy to substitute other variables since all variables are treated at the same level, for example:

sage: S1.<x1,y1> = ZZ[]
sage: S2.<y2,x2> = ZZ[]
sage: p1 = y1*x1 + y1
sage: p1(y2,x2)
y2*x2 + x2

more

This is not accurate -- ZZ['x']['y'] is much faster than ZZ['x','y'], especially for large (dense) polynomials.

_Only_ for dense polynomials where the degree in the outer variable isn't too large.