Here is a challenge for you. I have an expression that has an arbitrary number of terms that look like
expression = a[i] * f[g[y, i]]
+ b[i, j] * f[g[x, j], g[y, i]]
+ c[i, j, k] * f[g[x, j], g[y, k], g[x, i]]
+ d[i, j, k, l] * f[g[y, j], g[x, l], g[y, i], g[y, k]]
+ ...
There could be any number of terms with a variable number of indices {i,j,k,l,...}
. The first argument of g
is always either x
or y
.
I want to rearrange the second arguments of g
in each term such that they are in alphabetical order in the entire term. In the above example, this would give the output:
output = a[i] * f[g[y, i]]
+ b[j, i] * f[g[x, i], g[y, j]]
+ c[k, i, j] * f[g[x, i], g[y, j], g[x, k]]
+ d[k, i, l, j] * f[g[y, i], g[x, j], g[y, k], g[y, l]]
+ ...
Another way to say this is that I want to rearrange Einstein-summed dummy indices in the sum such that the indices within f
are in alphabetical order.
What is the most straightforward way to accomplish this that you can think of? I'm reaching the limit of my knowledge of Mathematica patterns with this one.