Orthogonal and unitary matrices, change of basis, similarity
Before this lesson: lesson 01 (basis, coordinates) and lesson 03 (inverse, product rule for , projection). One theorem uses singular values; here "singular values of " means the square roots of the eigenvalues of (lesson 08 builds the SVD). Time: about 60 minutes, plus the notebook.
Why this matters #
Much of linear algebra is the search for a good basis. In a basis of eigenvectors a matrix is diagonal (lesson 05). The SVD uses two orthonormal bases (lesson 08). The Fourier basis makes circular convolution diagonal. Orthogonal matrices are the safest changes of basis: they keep lengths, angles and the condition number, so stable algorithms are built from them (Householder QR, the QR algorithm). In ML they appear as orthogonal initialization and rotary position embeddings (RoPE). In ML for science they are the rotations under which physical laws do not change (equivariant networks).
1. Predict first #
2. The picture #
One vector, many names. An arrow in the plane does not change when you change the basis; only its coordinates change. The coordinates of in the basis are the weights in .
Which way does go? Put the new basis vectors, written in standard coordinates, as the columns of . Then . So turns new coordinates into standard coordinates, and goes the other way:
Drag , or drag the basis vectors. The green grid is the coordinate grid of the basis , . The dashed walk reaches with steps along and steps along , where . Notice that stays where it is: only its coordinates change. Make and parallel and the coordinates stop existing.
The basis is , , so and . What are the new coordinates of ?
The new coordinates are . Check: ✓. The answer is : it uses in the wrong direction. turns new coordinates into standard ones, so to go from standard to new you need .
The same map in another basis. A map is given in standard coordinates. To apply it to a vector given by new coordinates , you need three steps: translate to standard coordinates (), apply , translate back (). So the map in new coordinates is . Section 3 goes through these steps one by one.
Drag the basis vectors and watch . The map is of section 4(b). Column of holds the new coordinates of , drawn as a dashed walk. The table shows what never changes (green) and what changes (magenta). Choose eigenvectors, or drop and on the dashed lines: becomes . Choose orthonormal: now the singular values stay the same too.
Orthogonal matrices. A rotation or a reflection: the grid stays square, and lengths and angles do not change. In an orthonormal basis (a rotated, maybe mirrored, copy of the standard axes) the coordinates are plain dot products, : no system has to be solved.
3. Definitions and main results #
Coordinates in a basis #
Let be a basis of and . is invertible (its columns are independent, lesson 03). The coordinates of in this basis satisfy
For two bases, , so . The matrix is the change-of-basis matrix. In code, compute with a solve of , not with an explicit inverse (lesson 13 explains why).
The same map in another basis #
The main difficulty of this lesson is the direction of and . Draw the two coordinate systems as the two rows of a square:
The top row lives in standard coordinates, where we know the map . The bottom row lives in new coordinates, where we want the matrix . There are two ways from (bottom left) to the bottom right corner:
- Up: turns the new coordinates into the standard vector .
- Across: acts on and gives .
- Down: turns back into new coordinates: .
The direct way is . Both ways must give the same coordinates for every , so . Read the product from right to left: the matrix next to acts first. The same square read the other way gives .
Small numbers. Take , , and .
- Up: .
- Across: .
- Down: . Check: ✓.
Section 4(b) finds for this basis, and indeed .
Press Next step four times. The left panel shows coordinate vectors (black , violet output); its orange and blue axes mean "steps along " and "steps along ". The right panel shows the same arrows in standard coordinates. Watch , then see the shortcut land on the same point. Drag and change the basis: the shortcut always agrees, but is diagonal only for the eigenvector basis .
Theorem. Let in standard coordinates. In the basis given by the columns of , acts on coordinates by .
Proof.
- The vector with new coordinates is .
- Its image, in standard coordinates, is .
- The new coordinates of the image are .
- So in new coordinates is , that is, .
Column of is : the new coordinates of . This is lesson 01's "columns are the images of the basis vectors", now in the new basis.
In , the columns of are the new basis vectors. Which matrix acts first on a coordinate vector ?
: the matrix next to acts first. turns the new coordinates into a standard vector, acts on it, and turns the result back into new coordinates. The order matters, because matrix products do not commute.
Similar matrices #
Two square matrices and are similar if for some invertible : they are the same map, written in two bases.
Theorem (similarity invariants). Similar matrices have the same characteristic polynomial. Hence they have the same eigenvalues with the same algebraic multiplicities, the same trace and the same determinant. They also have the same rank.
Proof.
- , and this is .
- Product rule (lesson 03): . Since , this is .
- The eigenvalues are the roots of this polynomial; the trace and the determinant are two of its coefficients (lesson 03). So they agree.
- Rank: (lesson 01) gives , and gives .
What does change. Eigenvectors: if , then . It is the same arrow under a new name: its new coordinates . Singular values, norms and symmetry also change in general (section 4(b) shows it with numbers).
for some invertible that is not orthogonal. Which quantity can be different for and ?
Trace, determinant and eigenvalues all come from the characteristic polynomial, which similarity keeps. The singular values come from , and is not similar to in general. In section 4(b), has singular values and has . Only an orthogonal keeps them (end of this section).
The converse is false: the same eigenvalues do not make two matrices similar. The shear and the identity both have the eigenvalues , trace 2 and determinant 1. But for every invertible ,
So no basis turns into . In words: keeps every vector, and this does not depend on the basis; the shear moves most vectors. The same argument shows that is not similar to the zero matrix. The Jordan form of lesson 05 gives the full answer to the question "when are two matrices similar?".
and . Are they similar?
is the same matrix in every basis, because . So the only matrix similar to is itself. Equal eigenvalues, trace and determinant are necessary for similarity, but not sufficient. Complex does not help: the same computation holds.
The aim of the next lessons is to choose so that is simple. In a basis of eigenvectors, is diagonal (lesson 05).
Orthogonal matrices #
A real matrix is orthogonal if . Entry of is , so this says: the columns are orthonormal (perpendicular, with length 1).
- , and also (for square matrices one side is enough, lesson 03). So the rows are orthonormal as well.
- Coordinates in the orthonormal basis are , that is, .
- Products and inverses of orthogonal matrices are orthogonal: .
- A tall (, ) with orthonormal columns has but : is the projector onto (lesson 03). Such a is not called orthogonal.
Theorem (orthogonal maps keep the geometry). If , then for all : , , and the angle between and equals the angle between and .
Proof.
- .
- Take : .
- is built from dot products only, so it does not change.
The proof uses only , so it also holds for a tall with orthonormal columns.
Keeping lengths is enough
The converse also holds: if for all , then . The reason is that lengths fix dot products, through the polarization identity
Apply it to and to : the right-hand sides are equal, so for all . That is, for all . With and this says that entry of is 0. So a linear map that keeps every length is orthogonal, and it keeps every angle as well.
Theorem (determinant and eigenvalues). If is orthogonal, then , and every eigenvalue (real or complex) has .
Proof.
- , and by the product rule this is .
- Let with , possibly complex. With the complex norm ( is the conjugate transpose), because is real. Also . So .
Rotations and reflections. : a rotation (orientation kept). : the map contains a reflection. In 2D every orthogonal matrix is one of
Why only these two? The first column is a unit vector, so it is for some . The second column is a unit vector orthogonal to it, so it is . The sign gives and the sign gives . reflects across the line at angle . Other examples: permutation matrices, and Householder reflections with (lesson 12).
Drag and , or change . The default is the rotation of section 4(c), with and . Notice that lengths, the dot product and the angle stay the same (green), and the unit circle maps onto itself. Switch to the reflection : still , but and the F is mirrored. Then try not orthogonal to see everything change (magenta).
Unitary matrices #
For complex vectors the inner product is , and . Here is the conjugate transpose: transpose, and replace every entry by its complex conjugate (). The conjugate is needed so that is a positive real number: for , , while .
A complex matrix is unitary if . The same proofs as for orthogonal matrices give , , , and for every eigenvalue. A real orthogonal matrix is a unitary matrix with real entries.
Example, entry by entry. Take
( is symmetric, so only conjugates the entries.) The product is times:
- entry : ;
- entry : ;
- entry : ;
- entry : .
So : is unitary. Without the conjugate the test fails: . Also , and the eigenvalues are , both of modulus 1.
The DFT matrix is unitary (Parseval)
The most important unitary matrix is the discrete Fourier transform. The DFT matrix () satisfies , so is unitary. Entry of is : a sum of equally spaced points on the unit circle when (it is 0), and when . This is Parseval's theorem: the normalized Fourier transform keeps the energy . Lesson 19 uses it for circulant matrices and Fourier neural operators.
Orthogonal maps keep the singular values #
Theorem. Let () and () be orthogonal, and be . Then has the same singular values as . Hence and .
Proof.
- , and turns this into .
- , so this matrix is : it is similar to .
- Similar matrices have the same eigenvalues. Their square roots are the singular values, so the singular values agree.
- The two norms depend only on the singular values: (lesson 08) and .
Three consequences:
- Conditioning. All singular values of are 1 (), so . A rounding error is not amplified: . Algorithms built only from orthogonal transformations (Householder and Givens QR, the QR algorithm, Golub–Kahan) are backward stable.
- Symmetry. An orthogonal change of basis keeps a symmetric symmetric: . A general does not. This is why the spectral theorem (lesson 06) is written .
- Orthogonal similarity keeps both spectra. A general keeps the eigenvalues but changes the singular values; keeps both.
Answers to section 1. (1) . The vector is itself, so its new coordinates are . (2) Shared: eigenvalues, trace, determinant and rank (all from the characteristic polynomial and the rank argument). Not shared in general: eigenvectors ( becomes ) and singular values (unless is orthogonal).
4. Worked example #
(a) Coordinates. Basis , :
For : . Check: ✓.
(b) One map, three matrices. Take .
- In the basis of (a), use the column rule (column of = new coordinates of ): , so column 1 of is . , so column 2 of is . Hence . The basis vectors are eigenvectors, and in this basis the map is two independent stretches.
- In the basis , , with and , multiply from right to left:
| trace | 5 | 5 | 5 |
| determinant | 6 | 6 | 6 |
| eigenvalues | 3, 2 | 3, 2 | 3, 2 |
| singular values | 4.497, 1.334 | 3, 2 | 3.257, 1.842 |
For : has eigenvalues , so . The singular values change from basis to basis, because and are not orthogonal. Their product stays . The eigenvector of has new coordinates : the eigenvector of .
(c) A rotation and a reflection.
Both have orthonormal columns (, and ). : is the rotation with , (). For and : and . Lengths: . Dot products: and ✓. The eigenvalues of are , both of modulus 1.
: a reflection. and , so fixes the line through and flips the perpendicular direction. Its eigenvalues are and .
The columns of are perpendicular. What is ?
The columns are orthogonal but have length , not 1. So , and . The rule needs orthonormal columns. is orthogonal (a rotation by ).
5. In code #
NumPy and SciPy check the claims of sections 3 and 4. The notebook for this lesson has these cells and the exercises.
import numpy as np
from scipy.linalg import polar
# the three-step path c -> Mc -> AMc -> M^{-1}AMc equals Bc
A = np.array([[4., -2.], [1., 1.]])
M, B = np.array([[2., 1.], [1., 1.]]), np.diag([3., 2.])
c = np.array([1., 1.])
x = M @ c
print(x, A @ x, np.linalg.solve(M, A @ x), B @ c) # [3. 2.] [8. 5.] [3. 2.] [3. 2.]
# (b) one map, three matrices: check AM = MB instead of recomputing B
M2, B2 = np.array([[1., 1.], [0., 1.]]), np.array([[3., 0.], [1., 2.]])
print(np.allclose(A @ M, M @ B), np.allclose(A @ M2, M2 @ B2)) # True True
for X in (A, B, B2):
print(round(np.trace(X), 12), round(np.linalg.det(X), 12),
np.sort(np.linalg.eigvals(X).real), np.round(np.linalg.svd(X, compute_uv=False), 3))
# 5.0 6.0 [2. 3.] [4.497 1.334]
# 5.0 6.0 [2. 3.] [3. 2.]
# 5.0 6.0 [2. 3.] [3.257 1.842]
# (c) a rotation keeps dot products; its eigenvalues have modulus 1
Q = np.array([[3., -4.], [4., 3.]]) / 5
x, y = np.array([1., 2.]), np.array([3., 1.])
print(np.round(Q @ x, 12), np.round(Q @ y, 12), x @ y, round((Q @ x) @ (Q @ y), 12))
# [-1. 2.] [1. 3.] 5.0 5.0
print(np.abs(np.linalg.eigvals(Q))) # [1. 1.]
# a unitary matrix: U* U = I, but U^T U is not I
U = np.array([[1, 1j], [1j, 1]]) / np.sqrt(2)
print(np.allclose(U.conj().T @ U, np.eye(2)), np.allclose(U.T @ U, np.eye(2))) # True False
# orthogonal factors on both sides keep the singular values
rng = np.random.default_rng(0)
Q1, _ = np.linalg.qr(rng.standard_normal((4, 4)))
Q2, _ = np.linalg.qr(rng.standard_normal((3, 3)))
C = rng.standard_normal((4, 3))
print(np.allclose(np.linalg.svd(Q1 @ C @ Q2, compute_uv=False),
np.linalg.svd(C, compute_uv=False))) # True
# the polar factor of a matrix with det < 0 is a reflection, not a rotation
U_polar, P_polar = polar(np.array([[1., 2.], [3., 4.]])) # det = -2
print(round(np.linalg.det(U_polar), 12)) # -1.0
# the normalized DFT is unitary (Parseval)
n = 8
F = np.fft.fft(np.eye(n), norm="ortho") # F[j, k] = exp(-2πi jk/n) / sqrt(n)
print(np.allclose(F.conj().T @ F, np.eye(n))) # True
z = rng.standard_normal(n)
print(np.isclose(np.linalg.norm(np.fft.fft(z, norm="ortho")), np.linalg.norm(z))) # True
np.linalg.solve(M, v) computes without forming . For complex arrays, U.T
does not conjugate; use U.conj().T.
6. Where it shows up #
- PCA of an ensemble forecast is an orthogonal change of basis onto the principal axes. The total variance does not change, ; it is only redistributed so that the first axes carry most of it.
- Rotary position embeddings (RoPE). A query at position is rotated by a block-diagonal orthogonal matrix (2×2 rotations by the angles ), and a key at position by . Since , the score depends only on the offset , and the rotations do not change the norms of and .
- Norm-preserving layers. An orthogonal weight matrix keeps , with all singular values 1, so the linear part neither blows up nor shrinks signals and gradients. This motivates orthogonal initialization (Saxe et al., 2014) and unitary RNNs. The Muon optimizer (2024) replaces the momentum update of each weight matrix by an approximation of (its polar factor: the same singular vectors, all singular values set to 1).
- Equivariant networks for science. If a molecule or a wind field is rotated by , a vector output (forces, velocities) should rotate too, , and a scalar output (energy) should not change. Distances and dot products are invariant under , which is why E(n)-equivariant GNNs build their messages from them (lesson 19).
7. Common mistakes #
- Direction of the conversion. The coordinates are , not ; goes from new coordinates to standard ones.
- Reading from left to right. The matrix next to the vector acts first: , then , then .
- "Similar matrices have the same eigenvectors, singular values or norms." False in general. They share the characteristic polynomial and the rank (and the Jordan form, lesson 05).
- "Same eigenvalues ⇒ similar." False: the shear and .
- "Orthogonal = rotation." means a reflection is included (also for , and ).
- Orthogonal but not normalized columns. Then is diagonal, not , and .
- Tall : . False; is the projector onto .
- Complex case. Use (conjugate transpose), not ; NumPy's
A.Tdoes not conjugate.
8. Check yourself #
- , where is with eigenvalues . Give and
. Is invertible?
Answer
, . Yes: 0 is not an eigenvalue (equivalently ). - and . Find the coordinates of in this basis
without solving a linear system.
Answer
The basis is orthonormal, so : . Check: ✓. - Show that the product of two orthogonal matrices is orthogonal. Is the sum orthogonal?
Answer
. The sum is not, in general: and . - A matrix has orthonormal columns. What are and ? Give the
rank and the trace of .
Answer
. is the orthogonal projector onto , not ; its rank and its trace are both 3. - Does keep symmetry? Try and .
Answer
: not symmetric. With an orthogonal , , so symmetry is kept. - Why does every rotation (orthogonal, ) have the eigenvalue 1?
Answer
The characteristic polynomial is a real cubic, so it has a real root, and every eigenvalue has . If the other two eigenvalues are a complex pair , then . If all three are real (), their product is 1, so the number of 's is even and at least one eigenvalue is . Its eigenvector is the rotation axis.
The columns of are the new basis vectors. How do you get the new coordinates of a vector ?
(solve ). goes the other way: from new coordinates to standard ones.
Explain in words.
Read from right to left: new coordinates to standard (), apply , standard back to new (). is the same map written in the new basis.
What do similar matrices always share?
The characteristic polynomial, hence the eigenvalues (with algebraic multiplicities), the trace and the determinant. Also the rank.
What does a general similarity not keep?
Eigenvectors ( becomes ), singular values, norms and symmetry.
Give two matrices with the same eigenvalues that are not similar.
The shear and . for every , so nothing but is similar to .
Why does keep dot products?
. Lengths and angles are built from dot products, so they are kept too.
What values can take for an orthogonal , and what do they mean?
(a rotation) or (the map contains a reflection), because .
What is for an eigenvalue of an orthogonal matrix, and why?
: from we get .
Define a unitary matrix.
A complex square with , where is the conjugate transpose. It keeps and .
What are the singular values of when , are orthogonal?
The same as those of : is similar to .
9. Interview angle #
- "What is the inverse of an orthogonal matrix, and why are orthogonal transformations numerically safe?" — ; and , so errors are not amplified.
- "Two matrices have the same eigenvalues. Are they similar?" — Not always (a Jordan block versus a diagonal matrix). Yes if both are diagonalizable: both are similar to the same .
- "How does RoPE encode relative position?" — , so the score depends only on .
10. Practice #
The notebook for this lesson runs in Colab. It has five exercises, each with a check cell and a folded solution:
- Convert coordinates in both directions (
to_basis_coords,from_basis_coords) with a solve, not an inverse, and check section 4(a). - Write
matrix_in_basis(A, M), and check which quantities of section 4(b) stay the same. - Write
has_orthonormal_columns(Q)with a tolerance that scales with the size of . - Build and , and check their determinants, their eigenvalues and the mirror line of .
- Implement RoPE on a vector of even length, and check that the score depends only on the offset and that the norms do not change.
11. Watch and read #
- 3Blue1Brown, Change of basis, the text version of chapter 13.
- Gilbert Strang, MIT 18.06 (2010), lecture 31: Change of basis; image compression and lecture 17: Orthogonal matrices and Gram–Schmidt.
- Sheldon Axler, Linear Algebra Done Right, 4th edition, free PDF: §3D (3.84 is the change-of-basis formula) and §7D (isometries, unitary operators and matrix factorization).
- Su et al., RoFormer: Enhanced Transformer with Rotary Position Embedding, §3.2 (equations 14–16 show ).
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.
No Google account needed: Python runs inside your browser. The first start downloads about 40 MB; your changes are saved in this browser only.