ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 17 Jul 2014 00:56:33 -0500the permutation of subscripts of a multivariate polynomialhttp://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/ I want to define a function, the input are a permutation sigma belonging to Sn, and a multivariate polynomial f=f(x1,x2,...,xn) over Q, and the output is a multivariate polynomial f=f(x(sigma(1)),x(sigma(2)),...,x(sigma(n))),
for example, if sigma=(3,1,2),f=x1+x2*x3, then the output should be f=x3+x1*x2.
It is worth mentioning that this should work for any n not just when n=3.
How should I do that?Sat, 12 Jul 2014 09:32:22 -0500http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/Comment by rws for <p>I want to define a function, the input are a permutation sigma belonging to Sn, and a multivariate polynomial f=f(x1,x2,...,xn) over Q, and the output is a multivariate polynomial f=f(x(sigma(1)),x(sigma(2)),...,x(sigma(n))),
for example, if sigma=(3,1,2),f=x1+x2<em>x3, then the output should be f=x3+x1</em>x2.
It is worth mentioning that this should work for any n not just when n=3.
How should I do that?</p>
http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?comment=23388#post-id-23388Please correct the typo in the title.Sat, 12 Jul 2014 12:13:17 -0500http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?comment=23388#post-id-23388Answer by slelievre for <p>I want to define a function, the input are a permutation sigma belonging to Sn, and a multivariate polynomial f=f(x1,x2,...,xn) over Q, and the output is a multivariate polynomial f=f(x(sigma(1)),x(sigma(2)),...,x(sigma(n))),
for example, if sigma=(3,1,2),f=x1+x2<em>x3, then the output should be f=x3+x1</em>x2.
It is worth mentioning that this should work for any n not just when n=3.
How should I do that?</p>
http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?answer=23386#post-id-23386You can apply a permutation to a tuple, in particular to the tuple of generators of the polynomial ring.
You can then apply the function to the permuted variables.
Here is an example, from which you could build a function `permute_variables(sigma,f)` (left as an exercise).
sage: version() # just for reference
'Sage Version 6.3.beta5, Release Date: 2014-07-01'
sage: R = PolynomialRing(ZZ,n,'x')
sage: R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Integer Ring
sage: Sn = SymmetricGroup(range(n))
sage: f = R.random_element()
sage: f
x1*x2 - x3^2 + x2*x4 + 3*x1 - 1
sage: sigma = Sn.random_element()
sage: sigma
(0,2,4)
sage: R.gens()
(x0, x1, x2, x3, x4)
sage: sigma(R.gens())
(x2, x1, x4, x3, x0)
sage: f(*sigma(R.gens()))
-x3^2 + x0*x4 + x1*x4 + 3*x1 - 1
Rather than a random permutation, you probably prefer to input it yourself. For this, you need to define `x0` etc. and make them point to the variables of polynomials in `R`. This is done with `R.inject(variables)`. And to input permutations as products of cycles, use something like `Sn('(0,1)(2,3)')`.
An example where you input the polynomial and the permutation yourself:
sage: n = 5
sage: R = PolynomialRing(ZZ,n,'x')
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4
sage: Sn = SymmetricGroup(range(n))
sage: f = x1 + x2
sage: sigma = Sn((1,2,3))
sage: sigma
(1,2,3)
sage: f(*sigma(R.gens()))
x2 + x3
sage: sigma = Sn('(0,1)(2,3)')
sage: f(*sigma(R.gens()))
x0 + x3
Be careful: If you define the permutation group as SymmetricGroup(5), they are indexed from 1 to n, so it's tricky to follow what goes on when you let them act on the generators of $R$, which are indexed from 0 to n-1 (you have to offset your indices by one, then apply the permutation, then undo the offset). In particular the permutation $(1,2,3)$ of $S_5$, would then act on $(x_0, x_1, x_2, x_3, x_4)$, acts as the cyclic permutation $(x_0, x_1, x_2)$.
Sat, 12 Jul 2014 11:41:32 -0500http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?answer=23386#post-id-23386Comment by slelievre for <p>You can apply a permutation to a tuple, in particular to the tuple of generators of the polynomial ring.
You can then apply the function to the permuted variables.</p>
<p>Here is an example, from which you could build a function <code>permute_variables(sigma,f)</code> (left as an exercise).</p>
<pre><code>sage: version() # just for reference
'Sage Version 6.3.beta5, Release Date: 2014-07-01'
sage: R = PolynomialRing(ZZ,n,'x')
sage: R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Integer Ring
sage: Sn = SymmetricGroup(range(n))
sage: f = R.random_element()
sage: f
x1*x2 - x3^2 + x2*x4 + 3*x1 - 1
sage: sigma = Sn.random_element()
sage: sigma
(0,2,4)
sage: R.gens()
(x0, x1, x2, x3, x4)
sage: sigma(R.gens())
(x2, x1, x4, x3, x0)
sage: f(*sigma(R.gens()))
-x3^2 + x0*x4 + x1*x4 + 3*x1 - 1
</code></pre>
<p>Rather than a random permutation, you probably prefer to input it yourself. For this, you need to define <code>x0</code> etc. and make them point to the variables of polynomials in <code>R</code>. This is done with <code>R.inject(variables)</code>. And to input permutations as products of cycles, use something like <code>Sn('(0,1)(2,3)')</code>.
An example where you input the polynomial and the permutation yourself:</p>
<pre><code>sage: n = 5
sage: R = PolynomialRing(ZZ,n,'x')
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4
sage: Sn = SymmetricGroup(range(n))
sage: f = x1 + x2
sage: sigma = Sn((1,2,3))
sage: sigma
(1,2,3)
sage: f(*sigma(R.gens()))
x2 + x3
sage: sigma = Sn('(0,1)(2,3)')
sage: f(*sigma(R.gens()))
x0 + x3
</code></pre>
<p>Be careful: If you define the permutation group as SymmetricGroup(5), they are indexed from 1 to n, so it's tricky to follow what goes on when you let them act on the generators of $R$, which are indexed from 0 to n-1 (you have to offset your indices by one, then apply the permutation, then undo the offset). In particular the permutation $(1,2,3)$ of $S_5$, would then act on $(x_0, x_1, x_2, x_3, x_4)$, acts as the cyclic permutation $(x_0, x_1, x_2)$.</p>
http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?comment=23447#post-id-23447Don't forget the line `R.inject_variables()`. Or, if you prefer, define the x_i's by "x0, x1, x2, x3, x4 = R.gens()". *Do not* define x1, x2, x3 as symbolic variables (with `var('x1')` etc). Does it work now?Thu, 17 Jul 2014 00:56:33 -0500http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?comment=23447#post-id-23447Comment by ruidongshuai@gmail.com for <p>You can apply a permutation to a tuple, in particular to the tuple of generators of the polynomial ring.
You can then apply the function to the permuted variables.</p>
<p>Here is an example, from which you could build a function <code>permute_variables(sigma,f)</code> (left as an exercise).</p>
<pre><code>sage: version() # just for reference
'Sage Version 6.3.beta5, Release Date: 2014-07-01'
sage: R = PolynomialRing(ZZ,n,'x')
sage: R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Integer Ring
sage: Sn = SymmetricGroup(range(n))
sage: f = R.random_element()
sage: f
x1*x2 - x3^2 + x2*x4 + 3*x1 - 1
sage: sigma = Sn.random_element()
sage: sigma
(0,2,4)
sage: R.gens()
(x0, x1, x2, x3, x4)
sage: sigma(R.gens())
(x2, x1, x4, x3, x0)
sage: f(*sigma(R.gens()))
-x3^2 + x0*x4 + x1*x4 + 3*x1 - 1
</code></pre>
<p>Rather than a random permutation, you probably prefer to input it yourself. For this, you need to define <code>x0</code> etc. and make them point to the variables of polynomials in <code>R</code>. This is done with <code>R.inject(variables)</code>. And to input permutations as products of cycles, use something like <code>Sn('(0,1)(2,3)')</code>.
An example where you input the polynomial and the permutation yourself:</p>
<pre><code>sage: n = 5
sage: R = PolynomialRing(ZZ,n,'x')
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4
sage: Sn = SymmetricGroup(range(n))
sage: f = x1 + x2
sage: sigma = Sn((1,2,3))
sage: sigma
(1,2,3)
sage: f(*sigma(R.gens()))
x2 + x3
sage: sigma = Sn('(0,1)(2,3)')
sage: f(*sigma(R.gens()))
x0 + x3
</code></pre>
<p>Be careful: If you define the permutation group as SymmetricGroup(5), they are indexed from 1 to n, so it's tricky to follow what goes on when you let them act on the generators of $R$, which are indexed from 0 to n-1 (you have to offset your indices by one, then apply the permutation, then undo the offset). In particular the permutation $(1,2,3)$ of $S_5$, would then act on $(x_0, x_1, x_2, x_3, x_4)$, acts as the cyclic permutation $(x_0, x_1, x_2)$.</p>
http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?comment=23397#post-id-23397Thanks for your answer very much!
However, there is still a small problem, when I type in a polynomial f=x1+x2, and n=5, sigma=(1,2,3),it shows an error says that the number of arguments must be less than or equal to 2.
How can I make the arguments of f be x1,x2,x3 instead of just x1 and x2?Sun, 13 Jul 2014 08:39:58 -0500http://ask.sagemath.org/question/23385/the-permutation-of-subscripts-of-a-multivariate-polynomial/?comment=23397#post-id-23397