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.Tue, 09 Apr 2013 07:39:19 +0200Polynomial transpositionhttps://ask.sagemath.org/question/9987/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).Thu, 04 Apr 2013 14:23:21 +0200https://ask.sagemath.org/question/9987/polynomial-transposition/Answer by Volker Braun for <p>Let's say I have</p>
<pre><code>R1 = ZZ['y']['x']
R2 = ZZ['x']['y']
</code></pre>
<p>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 <code>y*x+y</code> should become <code>(x+1)*y</code>, not <code>x*y+x</code>.</p>
<p>I can do it by first coercing to <code>ZZ['x','y']</code> and then to <code>R2</code>, 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 <code>R1</code> (then I can just coerce the result to <code>R2</code> afterwards).</p>
https://ask.sagemath.org/question/9987/polynomial-transposition/?answer=14753#post-id-14753Especially 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
Sun, 07 Apr 2013 09:37:48 +0200https://ask.sagemath.org/question/9987/polynomial-transposition/?answer=14753#post-id-14753Comment by Volker Braun for <p>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.</p>
<p>Bonus: its then easy to substitute other variables since all variables are treated at the same level, for example:</p>
<pre><code>sage: S1.<x1,y1> = ZZ[]
sage: S2.<y2,x2> = ZZ[]
sage: p1 = y1*x1 + y1
sage: p1(y2,x2)
y2*x2 + x2
</code></pre>
https://ask.sagemath.org/question/9987/polynomial-transposition/?comment=17923#post-id-17923_Only_ for dense polynomials where the degree in the outer variable isn't too large.Tue, 09 Apr 2013 07:39:19 +0200https://ask.sagemath.org/question/9987/polynomial-transposition/?comment=17923#post-id-17923Comment by fredrik for <p>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.</p>
<p>Bonus: its then easy to substitute other variables since all variables are treated at the same level, for example:</p>
<pre><code>sage: S1.<x1,y1> = ZZ[]
sage: S2.<y2,x2> = ZZ[]
sage: p1 = y1*x1 + y1
sage: p1(y2,x2)
y2*x2 + x2
</code></pre>
https://ask.sagemath.org/question/9987/polynomial-transposition/?comment=17925#post-id-17925This is not accurate -- ZZ['x']['y'] is much faster than ZZ['x','y'], especially for large (dense) polynomials.Mon, 08 Apr 2013 16:40:13 +0200https://ask.sagemath.org/question/9987/polynomial-transposition/?comment=17925#post-id-17925Answer by John Palmieri for <p>Let's say I have</p>
<pre><code>R1 = ZZ['y']['x']
R2 = ZZ['x']['y']
</code></pre>
<p>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 <code>y*x+y</code> should become <code>(x+1)*y</code>, not <code>x*y+x</code>.</p>
<p>I can do it by first coercing to <code>ZZ['x','y']</code> and then to <code>R2</code>, 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 <code>R1</code> (then I can just coerce the result to <code>R2</code> afterwards).</p>
https://ask.sagemath.org/question/9987/polynomial-transposition/?answer=14750#post-id-14750I 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(...)`.
Sun, 07 Apr 2013 02:41:17 +0200https://ask.sagemath.org/question/9987/polynomial-transposition/?answer=14750#post-id-14750