How to replace a group of variables that satisfy a common constraint? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-21T14:26:00Z https://mathematica.stackexchange.com/feeds/question/126531 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/126531 0 How to replace a group of variables that satisfy a common constraint? Xavier https://mathematica.stackexchange.com/users/43150 2016-09-17T11:33:21Z 2016-09-18T11:59:42Z <p>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?</p> <p><strong>Edits:</strong></p> <p>Desirable output for the example above:</p> <p>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}$$ </p> <p>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. </p> https://mathematica.stackexchange.com/questions/126531/-/126560#126560 0 Answer by user42582 for How to replace a group of variables that satisfy a common constraint? user42582 https://mathematica.stackexchange.com/users/42582 2016-09-17T22:19:19Z 2016-09-17T22:19:19Z <p>To begin with, it is <strong>easier</strong> to work with indexes than with subscripts/exponents, so the expression in the beginning of the initial question should be rewritten as </p> <pre><code>expr = (X[a] Y[c, d] + X[d] Y[a, c]) (X[b] Y[e, f] + X[e] Y[f, b]) </code></pre> <p>Notice, how I have not included the coefficient $F^{abcdef}$ in the definition of <em>expr</em> above for reasons that will become apparent shortly.</p> <p>Now, if we expand <em>expr</em> above we get </p> <pre><code>In[2} := exp=Expand[expr] Out := 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] </code></pre> <p>Note that all four terms appear in the right order (<em>X[a_]X[b_]Y[c__]Y[d__]</em>) but we are still missing the coefficients.</p> <p>Define,</p> <pre><code>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] &amp;, Aa, Bb, Cc, 1], 2] ] </code></pre> <p>and then apply the following rule on <em>exp</em> (see above)</p> <pre><code>exp /. { X[a_] X[b_] Y[c_, d_] Y[e_, f_] :&gt; X[a] X[b] Y[c, d] Y[e, f] Plus @@ rule[a, b, c, d, e, f] } </code></pre> <p>to obtain what is (<em>hopefully</em>) the <em>canonical</em> order.</p> https://mathematica.stackexchange.com/questions/126531/-/126589#126589 0 Answer by Xavier for How to replace a group of variables that satisfy a common constraint? Xavier https://mathematica.stackexchange.com/users/43150 2016-09-18T11:35:53Z 2016-09-18T11:59:42Z <p>This code solves the problem: </p> <pre><code>(*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 /. ((# -&gt; List) &amp; /@ 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 -&gt; #] &amp; /@ (Permute[indices, #] &amp; /@ ((FindPermutation[#, indices]) &amp; /@ interchanges[Sequence @@ temporaryIndices[fieldsTemp]])) (*the result*) (Plus @@ (# /. rule[Cases[#, X[__] | Y[__]]])) &amp; /@ expr; DeleteCases[%, X[_] | Y[__], Infinity] </code></pre> <p>The result is </p> <pre><code>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] </code></pre>