# Replacing parts of a complicated expression with shorthand symbols

I have a list, each element of which contains a different complicated expression. Here is a simplified example of the first three entries of the list, displayed one above the other:

$\frac{Ey \ \mu \ e^{t_1-t_2}}{\hbar}+\frac{Ex \ \mu \ e^{t_1-t_6}}{\hbar}$

$-e^{t_2-t_9}(a+b+c)+\frac{Ex \ \mu \ e^{t_2-t_{24}}}{\hbar}$

$\frac{e^{t_3-t_{17}}}{\hbar}+\frac{Ey \ \mu \ e^{t_3-t_{24}}}{\hbar}$

I would like to replace certain parts of each expression with a shorthand notation. I have put in a matrix (let's call it matrix1) the bits of the expressions which I want to replace, where a particular row of the matrix contains sub-expressions from the expression which appears in the same row number in the list, and the column number corresponds to the subscript of t in the sub-expression that I want to replace. For example, the sub-expression $\frac{\mu \ e^{t_2-t_{24}}}{\hbar}$ is situated in row 2 (because we have $t_2$), column 24 (because we have $-t_{24}$. All of the sub-expressions to be substituted with shorthands conform to this pattern).

The shorthand notation is contained in another matrix (let it be called matrix2) which is organised so that there is a one-to-one correspondence between this and matrix1 - i.e. such that the sub-expression in element (i,j) of matrix1 should be replaced in the list with the contents of element (i,j) of matrix2.

Here is a simplified example of what I mean: The first entry of the list is $\frac{Ey \ \mu \ e^{t_1-t_2}}{\hbar}+\frac{Ex \ \mu \ e^{t_1-t_6}}{\hbar}$. I would like to replace $\frac{\mu \ e^{t_1-t_2}}{\hbar}$ in the first term with $Cp_{1,2}$ (I choose the subscripts 1,2 for the following reason: 1 because it's the first expression in the list and 2 because there is $-t_2$ contained in the term to be replaced). Similarly, I would like to replace $\frac{\mu \ e^{t_1-t_6}}{\hbar}$ in the second term with $Cp_{1,6}$.

My question is, how do I automate this process so that all sub-expressions in matrix1 are replaced in the list with the shorthands in one go? All I want to do is essentially search and replace in the list by matching sub-expressions from matrix1 within the list and replacing them with the corresponding terms in matrix2.

Here is the code for the actual first expression in the list:

(E^(time/Subscript[t, 1] - time/Subscript[t, 9]) Ey \[Micro] Uparam[time])/(2 \[HBar]) +
(-E^(time/Subscript[t, 1] - time/Subscript[t, 16]) Subscript[\[CapitalDelta], hh] -
E^(time/Subscript[t, 1] - time/Subscript[t, 16]) Subscript[\[CapitalDelta], sp] +
E^(time/Subscript[t, 1] - time/Subscript[t, 16]) Subscript[\[Omega], 0]) Uparam[time] -
(E^(time/Subscript[t, 1] - time/Subscript[t, 24]) Ex \[Micro] Uparam[time])/(2 \[HBar])


The first row of matrix1 contains all of the sub-expressions I wish to replace in element one of the list, so here is the first row of matrix1:

{0, 0, 0, 0, 0, 0, 0, 0, (E^(time (1/Subscript[t, 1] - 1/Subscript[t, 9])) \[Micro])/(
2 \[HBar]), 0, 0, 0, 0, 0, 0, -E^(time (1/Subscript[t, 1] - 1/Subscript[t, 16]))
(Subscript[\[CapitalDelta], hh] + Subscript[\[CapitalDelta], sp] - Subscript[\[Omega],
0]), 0, 0, 0, 0, 0, 0, 0, -((E^(time (1/Subscript[t, 1] - 1/Subscript[t, 24]))
\[Micro])/(2 \[HBar])), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}


Note that matrix1 is sparse.

And here is row 1 of matrix2:

{Subscript[Cp, 1, 1], Subscript[Cp, 1, 2], Subscript[Cp, 1, 3], Subscript[Cp, 1, 4],
Subscript[Cp, 1, 5], Subscript[Cp, 1, 6], Subscript[Cp, 1, 7], Subscript[Cp, 1, 8],
Subscript[Cp, 1, 9], Subscript[Cp, 1, 10], Subscript[Cp, 1, 11], Subscript[Cp, 1, 12],
Subscript[Cp, 1, 13], Subscript[Cp, 1, 14], Subscript[Cp, 1, 15], Subscript[Cp, 1, 16],
Subscript[Cp, 1, 17], Subscript[Cp, 1, 18], Subscript[Cp, 1, 19], Subscript[Cp, 1, 20],
Subscript[Cp, 1, 21], Subscript[Cp, 1, 22], Subscript[Cp, 1, 23], Subscript[Cp, 1, 24],
Subscript[Cp, 1, 25], Subscript[Cp, 1, 26], Subscript[Cp, 1, 27], Subscript[Cp, 1, 28],
Subscript[Cp, 1, 29], Subscript[Cp, 1, 30], Subscript[Cp, 1, 31], Subscript[Cp, 1, 32],
Subscript[Cp, 1, 33], Subscript[Cp, 1, 34], Subscript[Cp, 1, 35]}


I expected that this simple code:

For[j = 1, j < 36,
Vector[] = ExpandAll[Simplify[Vector[], matrix1[][[j]] == matrix2[][[j]]]];
j++]


would do the right thing. However, surprisingly, the substitution only works when j is 16, but does nothing to the other two terms. I have also tried using the Replace[] function, but that doesn't work at all.

Of course, eventually I would like to write a '2D' for loop or similar to transform the rest of the expressions in the list. However, at this stage I am trying to get the substitutions to work on the first one to begin with.

• It probably doesn't simplify because Mathematica considers the original expression to be simpler. One person's "simple" is another person's "horribly complex", because of course it depends on what you want to do with it. I suggest using replacement rules instead. – march Nov 26 '15 at 2:22
• One possibility is to do something like Vector /. Exp[time/Subscript[t, a_] - time/Subscript[t, b_]] :> cp[a, b]. This doesn't exactly make the replacements that you wanted, but maybe you can work from there. – march Nov 26 '15 at 2:50