I have some very long and complex expressions which involve a set of $n$ variables, and I want to be able to permute the labels of the variables. I will give a simple example, instead of my awful expressions. Suppose $n = 3$, and consider s[1] + s[3] + s[1,3]
.
I start by defining a function to turn permutations into replacement rules;
permreplacements[n_] :=
MapIndexed[First[#2] -> #1 &, #] & /@ Permutations[Range[n]]
And then I can apply this to my expression, eg.
s[1] + s[3] + s[1,3] /. permreplacements[3][[2]]
s[1] + s[3] + s[1,3] /. permreplacements[3][[4]]
etc.
This is great for my current expression, but now consider instead the expression s[1] + 3 s[2] + s[1,3]
. When I apply my method to this expression, it permutes the numerical factor '3' as well as the labels on my variables.
Does anyone have a good method to permute only the labels on my variables and not any numerical factors?
If I had only variables of the form s[i]
, I could just generate a set of rules for each variable, eg. {s[1]-> s[3], s[2]-> s[1], s[3]-> s[2]}
. But as I must also consider s[1,2]
, I can't see how to do this without generating a very large set of replacement rules which covers all possible cases of s[i,j]
Extra information which may or may not be relevant:
I also have labels of the form
Subscript[s, 2]
, but I guess if I'm shown how to deal withs[2]
I can extend to subscriptsIn the end, I want to apply the function
sumperms[expr_, n_] := Sum[expr /. permreplacements[3][[i]], {i, n!}]
to sum over all permutations.
Sum[]
can already do that:Sum[(s[#1] + 3 s[#2]) & @@ idx, {idx, Permutations[Range[3]]}]
$\endgroup$s[_Integer]
instead of_Integer
? Then the integer factors tos[_]
terms won't be affected. $\endgroup$s[1] + s[2]
and returns[#1] + s[#2]
? $\endgroup$Subscript[s,i], Subscript[k,i], Subscript[e,i]
as well as variables with two labels,Subscript[s,i, j]
. I would also like my method to be applicable to possible expressions which involve more variables later on too $\endgroup$s[_Integer]
in one way or the other. Here is an answer to the question you asked J.M. :Function[Evaluate[s[3] + 5 s[5] /. s[i_Integer] :> s[Slot[i]]]]
. $\endgroup$