# Code to find the similarity transformation between two matrices

For example, I know that matrices $$A=\left( \begin{array}{cccc} 1 & 1 & 1 & 0 \\ -6 & -1 & 4 & -5 \\ 12 & 0 & -5 & 5 \\ 12 & 0 & -6 & 5 \\ \end{array} \right)$$ and $$B=\left( \begin{array}{cccc} 0 & 0 & 0 & 35 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 2 \\ 0 & 0 & 1 & 0 \\ \end{array} \right)$$ are similar. Is there any efficient way to find $$P$$ such that $$A=PBP^{-1}$$? Let's say a code SimTransform[A_,B_]:= ... that returns $$P$$ when $$A$$ and $$B$$ are similar.

• P={{p1,p2,p3,p4},{p5,p6,p7,p8},{p9,p10,p11,p12},{p13,p14,p15,p16}}; Q=P/.ToRules[Reduce[A==P.B.Inverse[P],Flatten[P]]] returns {{p1,p2,7*p1,7*p2}, {p5,p6,-12*p1-5*p5,-12*p2-5*p6}, {-p1+p2-p5,7*p1-p2-p6,5*p1+7*p2+5*p5,49*p1+5*p2+5*p6}, {(-10*p1+4*p2-5*p5-p6)/5,8*p1-2*p2+p5-p6,-2*p1+8*p2+5*p5+p6,44*p1-2*p2-5*p5+5*p6}} if I haven't made any mistakes translating latex back into Mathematica. And Simplify[A==Q.B.Inverse[Q]] returns True
– Bill
Oct 16, 2022 at 22:40
• I'd go with linear algebra:amat = {{1, 1, 1, 0}, {-6, -1, 4, -5}, {12, 0, -5, 5}, {12, 0, -6, 5}}; bmat = {{0, 0, 0, 35}, {1, 0, 0, 0}, {0, 1, 0, 2}, {0, 0, 1, 0}}; simmat = Array[p, {4, 4}]; simmat /. Solve[amat . simmat == simmat . bmat, Flatten[simmat]] Oct 17, 2022 at 14:11

A = {{1, 1, 1, 0}, {-6, -1, 4, -5}, {12, 0, -5, 5}, {12, 0, -6, 5}};
B = {{0, 0, 0, 35}, {1, 0, 0, 0}, {0, 1, 0, 2}, {0, 0, 1, 0}};
p = Partition[Table[Unique["x"], 16], 4];
pvalue = Partition[Flatten[Values[FindInstance[p.B.Inverse[p]==A,Catenate[p]]]],4]


{{0, -1, 0, -7}, {-1, 1, 5, 7}, {0, 0, -12, 0}, {0, 0, -12, 12}}

Check:

pvalue.B.Inverse[pvalue] == A


True

This works if it so happens that Eigensystem returns the eigenvalues of $$A$$ and $$B$$ in the same order:

SimTransform[A_,B_]:=Module[{valsA,vecsA,valsB,vecsB},
{valsA,vecsA}=Eigensystem[A];
{valsB,vecsB}=Eigensystem[B];
If[valsA===valsB,
Transpose[vecsA].Inverse[Transpose[vecsB]],
\$Failed]];


Example:

A={{1,1,1,0},{-6,-1,4,-5},{12,0,-5,5},{12,0,-6,5}};
B={{0,0,0,35},{1,0,0,0},{0,1,0,2},{0,0,1,0}};

P=SimTransform[A,B]//Simplify
(* {{1/72,1/72,7/72,7/72},
{-1/36,1/18,-1/36,-4/9},
{1/36,1/36,1/36,37/36},
{0,0,0,1}} *)

A-P.B.Inverse[P]//Simplify
(* gives zero *)

• Your method only work if A and B are diagonalizable
– yode
Oct 17, 2022 at 4:57
Clear["Global*"]

SimTransform[A_?MatrixQ, B_?MatrixQ] := Module[
{P = Array[p, {Length@A, Length@A}]},
P /. ToRules@Reduce[A == P . B . Inverse[P], Flatten@P]] /;
And @@ {Dimensions@A === Dimensions@B, Tr[A] == Tr[B],
Eigenvalues@A == Eigenvalues@B}

A = {{1, 1, 1, 0}, {-6, -1, 4, -5}, {12, 0, -5, 5}, {12, 0, -6, 5}};

B = {{0, 0, 0, 35}, {1, 0, 0, 0}, {0, 1, 0, 2}, {0, 0, 1, 0}};

Format[p[arg__]] := Subscript[p, Row[{arg}]]

P = SimTransform[A, B]


Verifying,

A == P . B . Inverse[P] // Simplify

(* True *)


A = {{1, 1, 1, 0}, {-6, -1, 4, -5}, {12, 0, -5, 5}, {12, 0, -6, 5}};
B = {{0, 0, 0, 35}, {1, 0, 0, 0}, {0, 1, 0, 2}, {0, 0, 1, 0}};
{aTrasf, aJF} = JordanDecomposition[A];
{bTrasf, bJF} = JordanDecomposition[B];
If[aJF === bJF, T = Simplify[aTrasf . Inverse[bTrasf]]];


If Jordan forms are the same, then T is the similarity transform. I think they can differ by constant factor (at least). Didn't check that case

Simplify[T . B . Inverse[T]]===A
`

(* True *)