# How to replace a group of variables that satisfy a common constraint?

In an expression how can we replace a group of variables $a, b,c,...$ that satisfy the condition $cond(a,b,c,...)$ with other variables $\alpha,\beta,\gamma,...$? I know how to do this in the case of individual constraints, but not in one where the pattern relies on the whole group of variables. E.g. I want to rename the indices in the formula $$F^{abcdef}\cdot(X_{a}Y_{cd}+X_{d}Y_{ac})\cdot(X_{b}Y_{ef}+X_{e}Y_{fb}),$$ so that it can be put into a standard form: $$\tilde{F}^{\alpha\beta\mu\nu\rho\sigma} X_{\alpha}X_{\beta}Y_{\mu\nu}Y_{\rho\sigma}$$ where $\tilde{F}^{\alpha\beta\mu\nu\rho\sigma}$ is a sum of $Fs$. It can be achieved by replacing $i\_,j\_,k\_,m\_,n\_,l\_$ that appear in the combination $X_{i}X_{j}Y_{km}Y_{nl}$ with $\alpha,\beta,\mu,\nu,\rho,\sigma$ (including all possible matches), so what kind of rule should I apply?

Edits:

Desirable output for the example above:

Taking the second terms in the first parenthesis and in the second parenthesis we get $$K_{22}=F^{abcdef}X_{d}Y_{ac}X_{e}Y_{fb}$$

To change it to the standard form, we can rewrite it as $F^{abcdef}X_{d}X_{e}Y_{ac}Y_{fb}$ and apply the rule: $d\rightarrow\alpha,e\rightarrow\beta,a\rightarrow\mu,c\rightarrow\nu,f\rightarrow\rho,b\rightarrow\sigma$. The result is $F^{\mu\sigma\nu\alpha\beta\rho}X_{\alpha}X_{\beta}Y_{\mu\nu}Y_{\rho\sigma}$. Of course, $K_{22}$ can also be arranged as $F^{abcdef}X_{d}X_{e}Y_{fb}Y_{ac}$, $F^{abcdef}X_{e}X_{d}Y_{ac}Y_{fb}$, and $F^{abcdef}X_{e}X_{d}Y_{fb}Y_{ac}$. They will produce different results, which should be added up to get the final result for $K_{22}$. So $$\tilde{F}^{\alpha\beta\mu\nu\rho\sigma} =F^{\mu\sigma\nu\alpha\beta\rho}+...$$ where the ellipsis stands for the other 15 terms.

• is f a function or an index? – user42582 Sep 17 '16 at 14:27
• @user42582 I've changed $f$ to uppercase to distinguish it from the index $f$. $F$ is a coefficient. – Xavier Sep 17 '16 at 14:41
• please provide the desirable output for $F^{abcdef} X_d Y_{ac} X_e Y_{fb}$ – user42582 Sep 17 '16 at 15:01

To begin with, it is easier to work with indexes than with subscripts/exponents, so the expression in the beginning of the initial question should be rewritten as

expr = (X[a] Y[c, d] + X[d] Y[a, c]) (X[b] Y[e, f] + X[e] Y[f, b])


Notice, how I have not included the coefficient $F^{abcdef}$ in the definition of expr above for reasons that will become apparent shortly.

Now, if we expand expr above we get

In[2} := exp=Expand[expr]
Out[2] := X[b] X[d] Y[a, c] Y[e, f] + X[a] X[b] Y[c, d] Y[e, f] +
+ X[d] X[e] Y[a, c] Y[f, b] + X[a] X[e] Y[c, d] Y[f, b]


Note that all four terms appear in the right order (X[a_]X[b_]Y[c__]Y[d__]) but we are still missing the coefficients.

Define,

rule[a_, b_, c_, d_, e_, f_] := Module[{Aa, Bb, Cc},

Aa = Permutations[{a, b}];
Bb = Permutations[{c, d}];
Cc = Permutations[{e, f}];

Flatten[
Outer[
F[Sequence @@ #1, Sequence @@ #2, Sequence @@ #3] &, Aa, Bb, Cc, 1], 2]

]


and then apply the following rule on exp (see above)

exp /. {
X[a_] X[b_] Y[c_, d_] Y[e_, f_] :>
X[a] X[b] Y[c, d] Y[e, f] Plus @@ rule[a, b, c, d, e, f]
}


to obtain what is (hopefully) the canonical order.

• This code gives F only a copy of the indices of X and Y, but not the permutation of (a,b,c,d,e,f) under which the indices of X and Y transform. Anyway, its really helpful, thanks a lot! – Xavier Sep 18 '16 at 11:20

This code solves the problem:

(*assignments*)
indiceLength = 6; fields = {X, Y};
expr = F[a, b, c, d, e, f] (X[a] Y[c, d] + X[d] Y[a, c]) (X[b] Y[e, f] + X[e] Y[f, b]) //Expand;
indices = Take[CharacterRange["a", "z"], indiceLength] // ToExpression;

(*temporary indices of the fields*)
temporaryIndices[fieldsTemp_] :=
fieldsTemp /. ((# -> List) & /@ fields) // Flatten;

(*including all possible combinations for the same type of fields*)
interchanges[a_, b_, c_, d_, e_, f_] := Module[{permuteX, permuteY},
permuteX = Permutations[{a, b}];
permuteY = Permutations[{{c, d}, {e, f}}];
Partition[
Flatten[Outer[Join, permuteX, permuteY, 1]], indiceLength]
];
(*set the rule*)
rule[fieldsTemp_] :=
Thread[indices -> #] & /@
(Permute[indices, #] & /@
((FindPermutation[#, indices]) & /@
interchanges[Sequence @@ temporaryIndices[fieldsTemp]]))

(*the result*)
(Plus @@ (# /. rule[Cases[#, X[__] | Y[__]]])) & /@ expr;
DeleteCases[%, X[_] | Y[__], Infinity]


The result is

F[a, b, c, d, e, f] + F[a, b, e, f, c, d] + F[a, d, e, f, b, c] +
F[a, f, c, d, b, e] + F[b, a, c, d, e, f] + F[b, a, e, f, c, d] +
F[b, d, e, f, a, c] + F[b, f, c, d, a, e] + F[c, a, d, b, e, f] +
F[c, b, d, a, e, f] + F[c, f, d, a, b, e] + F[c, f, d, b, a, e] +
F[e, a, f, b, c, d] + F[e, b, f, a, c, d] + F[e, d, f, a, b, c] +
F[e, d, f, b, a, c]