Ask Your Question

Polynomial transposition

asked 2013-04-04 07:23:21 -0600

fredrik gravatar image

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 flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-04-06 19:41:17 -0600

updated 2013-04-06 19:48:51 -0600

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[1], p[0]):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(...).

edit flag offensive delete link more

answered 2013-04-07 02:37:48 -0600

Volker Braun gravatar image

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
edit flag offensive delete link more


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

fredrik gravatar imagefredrik ( 2013-04-08 09:40:13 -0600 )edit

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

Volker Braun gravatar imageVolker Braun ( 2013-04-09 00:39:19 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-04-04 07:23:21 -0600

Seen: 68 times

Last updated: Apr 07 '13