# Reversing the flow of the equation sign. Compacting an equation. Choosing which variables to show

Here's a simple example of what I'm trying to get at with that mysterious title.

I start off with a Hermitian matrix that I derived analytically

M = {{a, g}, {g, b}}; M // MatrixForm


Then, before doing the job myself analytically, I desire to inspect this matrix in a different basis through the unitary transformation

U = (1/Sqrt[2]) {{1, 1}, {1, -1}};
M = U.M.ConjugateTranspose[U] // Simplify; M // MatrixForm


I have a couple quantities that are defined in terms of the variables a, b, and g

w = (a + b)/2;
d = (a - b)/2;
p = w + g;
m = w - g;


I would like to express M in terms of these definitions. Sort of reverse the flow of those equation signs above, and show the matrix in the most compact form possible. I can do a hack job as below

M = M /. {a + b -> 2 w, a - b -> 2 d};
M = M /. {2 w + 2 g -> 2 p, 2 w - 2 g -> 2 m};
M // MatrixForm


However in this case I have to carefully craft the replacements to work properly. And it's not practical for anything more complicated than this toy example I provided. I hope people understand what I'm getting at here and can suggest a solution. Please let me know if I should clarify.

• Your code does not produce the posted results. M=U.M.ConjugateTranspose[M] evaluates to {{(a/Sqrt[2] + g/Sqrt[2])* Conjugate[a] + (b/Sqrt[2] + g/Sqrt[2])* Conjugate[g], (b/Sqrt[2] + g/Sqrt[2])* Conjugate[b] + (a/Sqrt[2] + g/Sqrt[2])* Conjugate[g]}, {(a/Sqrt[2] - g/Sqrt[2])* Conjugate[a] + (-(b/Sqrt[2]) + g/Sqrt[2])* Conjugate[g], (-(b/Sqrt[2]) + g/Sqrt[2])* Conjugate[b] + (a/Sqrt[2] - g/Sqrt[2])* Conjugate[g]}} I would guess that you have made use of some assumptions that you have not provided. – Bob Hanlon Sep 27 at 13:58
• Try one first tiny step: M={{a+b+2g,a-b},{a-b,a+b-2g}};Simplify[M,{a+b==2w}] and look at the output to see if it replaced the "more complicated" a+b+2g with the "simpler" 2(g+w).You can determine "simpler" using LeafCount[expr]. Then try Simplify[M,{a+b==2w,a-b==2d,w+g==p}] This seems to usually work only if it results in an immediate "simplification". See how far this will get you in your more complicated actual problem. Perhaps you can then use other methods to get to your final goal. – Bill Sep 27 at 18:29
• @BobHanlon Damn. Sorry. You're right. I made a real mess of that question. I have fixed it. – Tom Sep 28 at 19:30

Clear["Global*"]

(M = {{a, g}, {g, b}}) // MatrixForm


U = (1/Sqrt[2]) {{1, 1}, {1, -1}};
(M = U.M.ConjugateTranspose[U]
// Simplify) // MatrixForm


repl = Solve[{
w == (a + b)/2, d == (a - b)/2,
p == w + g, m == w - g},
{a, b, g}, {w}][[1]]

(* {a -> 1/2 (2 d + m + p), b -> 1/2 (-2 d + m + p), g -> 1/2 (-m + p)} *)

(M = M /. repl // Simplify) // MatrixForm
`