22. Similar matrices and invariants
§19.1's D=P^{-1}AP is a special case of a broader relationship between matrices. This lesson names it — similarity — and collects the quantities that never change under it, which is exactly why a "good" basis (like an eigenbasis) can simplify a matrix's appearance without ever changing what it fundamentally is.
Definition
Matrices A and B (both n\times n) are similar if B=P^{-1}AP for some invertible P. This is precisely §17.3's change-of-basis relationship: A and B represent the same linear transformation, just relative to different bases — A in the standard basis, B in whatever basis P's columns form. Similarity is an equivalence relation (reflexive: A=I^{-1}AI; symmetric: if B=P^{-1}AP then A=PBP^{-1}=(P^{-1})^{-1}B(P^{-1}); transitive: compose the two P's), which is what justifies treating "similar matrices" as a meaningful class rather than an arbitrary pairing.
Invariants
Because similar matrices describe the same transformation, several numbers can be computed from either matrix and always agree:
- Determinant: \det B=\det(P^{-1}AP)=\det(P^{-1})\det A\det P=\det A — exactly the calculation from §19.1's "your turn" question, now stated as the general rule it always was.
- Trace: \operatorname{tr}(P^{-1}AP)=\operatorname{tr}A, using the cyclic property \operatorname{tr}(XY)=\operatorname{tr}(YX) (not proved here, but easy to check entrywise): \operatorname{tr}(P^{-1}(AP))=\operatorname{tr}((AP)P^{-1})=\operatorname{tr}A.
- Characteristic polynomial, and hence eigenvalues (with multiplicity): \det(B-\lambda I)=\det(P^{-1}AP-\lambda P^{-1}P)=\det(P^{-1}(A-\lambda I)P)=\det(A-\lambda I) — same polynomial exactly, so A and B share every eigenvalue and every algebraic multiplicity.
- Rank: P^{-1} and P are invertible, and multiplying by an invertible matrix never changes rank (an invertible map is injective, so it can't collapse any dimension §18.3 didn't already collapse).
What is not preserved: individual entries, eigenvectors themselves (they transform to P^{-1}\vec v, per §17.3's coordinate change), and generally everything about A's appearance rather than its underlying transformation.
Why this matters
Diagonalization (§19.1) is exactly finding the simplest member of A's similarity class — replacing A with the diagonal D it's similar to, when one exists. Since trace, determinant, and eigenvalues are similarity invariants, they can always be read off the simpler matrix: \operatorname{tr}D=\lambda_1+\cdots+\lambda_n and \det D=\lambda_1\lambda_2\cdots\lambda_n give two clean, general facts —
\operatorname{tr}A=\sum_i\lambda_i,\qquad\det A=\prod_i\lambda_i
— true for any matrix with real eigenvalues (counted with algebraic multiplicity), not just diagonalizable ones (both sides are similarity invariants, and every matrix is similar to at least a triangular form with eigenvalues on the diagonal — §22.1's Schur decomposition makes this precise even for defective matrices).
Doing it in Python
import sympy as sp
A = sp.Matrix([[4, 1], [2, 3]])
P = sp.Matrix([[1, 2], [0, 1]]) # an arbitrary invertible matrix
B = P.inv() * A * P
print("A =", list(A))
print("B = P^-1 A P =", list(B))
print()
print("tr(A) =", A.trace(), " tr(B) =", B.trace())
print("det(A) =", A.det(), " det(B) =", B.det())
print("eigenvalues(A) =", A.eigenvals())
print("eigenvalues(B) =", B.eigenvals())
A = [4, 1, 2, 3]
B = P^-1 A P = [0, -5, 2, 7]
tr(A) = 7 tr(B) = 7
det(A) = 10 det(B) = 10
eigenvalues(A) = {5: 1, 2: 1}
eigenvalues(B) = {5: 1, 2: 1}
Confirming \operatorname{tr}A=\sum\lambda_i and \det A=\prod\lambda_i directly, including for a matrix that is not diagonal at all:
import sympy as sp
A = sp.Matrix([[6, -2, 1], [0, 3, 4], [0, 0, -1]]) # upper triangular
eigenvalues = list(A.eigenvals().keys()) # diagonal entries
print("eigenvalues:", eigenvalues)
print("sum of eigenvalues:", sum(eigenvalues), " trace:", A.trace())
print("product of eigenvalues:", sp.prod(eigenvalues), " det:", A.det())
eigenvalues: [-1, 3, 6]
sum of eigenvalues: 8 trace: 8
product of eigenvalues: -18 det: -18
Worked example
Without computing eigenvalues directly, find \operatorname{tr}A and \det A for a matrix known to be similar to D=\begin{pmatrix}3&0&0\\0&-1&0\\0&0&2\end{pmatrix}.
\operatorname{tr}A=\operatorname{tr}D=3+(-1)+2=4. \det A=\det D=3(-1)(2)=-6.
\boxed{\operatorname{tr}A=4,\quad\det A=-6}
Sanity check. These values don't depend on which specific A is similar to D — any A=PDP^{-1} for any invertible P gives the same trace and determinant, since both are similarity invariants proven above. Pick, say, P=\begin{pmatrix}1&0&0\\1&1&0\\0&1&1\end{pmatrix} and compute A=PDP^{-1} explicitly if a direct check is wanted — the trace and determinant will come out 4 and -6 regardless of which invertible P is chosen, which is precisely the point.
Your turn
1. A and B are similar, and A has eigenvalues 2,2,5. What is \det B?
2. Can a matrix with \det A=0 be similar to the identity matrix I? Why or why not?
3. True or false: if A and B have the same trace and determinant, they must be similar.
Solutions
1. \det B=\det A=2\times2\times5=20 (product of eigenvalues, counted with multiplicity — trace and determinant are similarity invariants, so B shares them without needing B's explicit form).
2. No. \det I=1\neq0, and determinant is a similarity invariant — if A were similar to I, \det A would have to equal \det I=1, contradicting \det A=0. (In fact only I itself is similar to I: P^{-1}IP=P^{-1}P=I for every invertible P, so I's similarity class contains nothing but I.)
3. False. Matching trace and determinant is necessary for similarity (both are invariants) but far from sufficient — e.g. A=\begin{pmatrix}1&0\\0&1\end{pmatrix} and B=\begin{pmatrix}1&1\\0&1\end{pmatrix} both have trace 2 and determinant 1, but A=I is similar only to itself (previous solution), while B\neq I — so they cannot be similar to each other. (This particular B is in fact §19.4's simplest example of a defective, non-diagonalizable matrix.)
Check yourself in code
For A=\begin{pmatrix}2&1&0\\0&3&1\\0&0&-1\end{pmatrix} (upper triangular), read off its eigenvalues and verify \operatorname{tr}A=\sum\lambda_i and \det A=\prod\lambda_i.
Print exactly this:
eigenvalues: [-1, 3, 2]
trace check: 4 == 4
det check: -6 == -6
import sympy as sp
A = sp.Matrix([[2, 1, 0], [0, 3, 1], [0, 0, -1]])
eigenvalues = list(A.eigenvals().keys())
print("eigenvalues:", eigenvalues)
# print "trace check: <sum> == <A.trace()>" and "det check: <product> == <A.det()>"
import sympy as sp
A = sp.Matrix([[2, 1, 0], [0, 3, 1], [0, 0, -1]])
eigenvalues = list(A.eigenvals().keys())
print("eigenvalues:", eigenvalues)
s, p = sum(eigenvalues), sp.prod(eigenvalues)
print(f"trace check: {s} == {A.trace()}")
print(f"det check: {p} == {A.det()}")
A and B=P^{-1}AP are similar — the same linear transformation in different bases — and similarity preserves determinant, trace, the full characteristic polynomial (hence all eigenvalues with multiplicity), and rank, even though individual entries and eigenvectors change. Diagonalization is the search for the simplest matrix in a given similarity class.
Next, closing the "what if diagonalization fails" thread: two advanced tools for defective matrices — generalized eigenvectors and the Jordan canonical form, the best structure available when a full eigenbasis doesn't exist.