Type at least 2 letters. Enter opens the first result, Esc closes.

    Linear Algebra · Lesson 4 · Start here: matrices and subspaces

    Orthogonal and unitary matrices, change of basis, similarity

    Intermediate 60 min 4 interactive figures 3 videos Colab notebook

    Before this lesson: lesson 01 (basis, coordinates) and lesson 03 (inverse, product rule for det⁡\det, projection). One theorem uses singular values; here "singular values of AA" means the square roots of the eigenvalues of A⊤AA^\top A (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 xx in the basis b1,b2b_1, b_2 are the weights c1,c2c_1, c_2 in x=c1b1+c2b2x = c_1 b_1 + c_2 b_2.

    Which way does MM go? Put the new basis vectors, written in standard coordinates, as the columns of M=[b1  b2]M = [b_1 \; b_2]. Then Mc=c1b1+c2b2Mc = c_1 b_1 + c_2 b_2. So MM turns new coordinates into standard coordinates, and M−1M^{-1} goes the other way:

    c  →  M    x=Mc,x  →  M−1    c=M−1x.c \;\xrightarrow{\;M\;}\; x = Mc, \qquad x \;\xrightarrow{\;M^{-1}\;}\; c = M^{-1}x .
    Interactive figure: loading…

    Drag xx, or drag the basis vectors. The green grid is the coordinate grid of the basis b1b_1, b2b_2. The dashed walk reaches xx with c1c_1 steps along b1b_1 and c2c_2 steps along b2b_2, where c=M−1xc = M^{-1}x. Notice that xx stays where it is: only its coordinates change. Make b1b_1 and b2b_2 parallel and the coordinates stop existing.

    Quick check

    The basis is b1=(2,1)b_1 = (2, 1), b2=(1,1)b_2 = (1, 1), so M=(2111)M = \begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix} and M−1=(1−1−12)M^{-1} = \begin{pmatrix} 1 & -1 \\ -1 & 2 \end{pmatrix}. What are the new coordinates of x=(1,0)x = (1, 0)?

    The same map in another basis. A map AA is given in standard coordinates. To apply it to a vector given by new coordinates cc, you need three steps: translate to standard coordinates (McMc), apply AA, translate back (M−1M^{-1}). So the map in new coordinates is M−1AMM^{-1}AM. Section 3 goes through these steps one by one.

    Interactive figure: loading…

    Drag the basis vectors and watch B=M−1AMB = M^{-1}AM. The map is A=(4−211)A = \begin{pmatrix} 4 & -2 \\ 1 & 1 \end{pmatrix} of section 4(b). Column jj of BB holds the new coordinates of AbjAb_j, drawn as a dashed walk. The table shows what never changes (green) and what changes (magenta). Choose eigenvectors, or drop b1b_1 and b2b_2 on the dashed lines: BB becomes diag⁡(3,2)\operatorname{diag}(3, 2). 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, ci=qi⊤xc_i = q_i^\top x: no system has to be solved.

    3. Definitions and main results #

    Coordinates in a basis #

    Let b1,…,bnb_1, \dots, b_n be a basis of Rn\mathbb{R}^n and M=[b1  ⋯  bn]M = [b_1 \; \cdots \; b_n]. MM is invertible (its nn columns are independent, lesson 03). The coordinates cc of xx in this basis satisfy

    x=Mc  ⟺  c=M−1x.x = Mc \quad\iff\quad c = M^{-1}x .

    For two bases, x=Mfrom cfrom=Mto ctox = M_{\text{from}}\, c_{\text{from}} = M_{\text{to}}\, c_{\text{to}}, so cto=Mto−1Mfrom cfromc_{\text{to}} = M_{\text{to}}^{-1} M_{\text{from}}\, c_{\text{from}}. The matrix Mto−1MfromM_{\text{to}}^{-1} M_{\text{from}} is the change-of-basis matrix. In code, compute cc with a solve of Mc=xMc = x, 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 MM and M−1M^{-1}. Draw the two coordinate systems as the two rows of a square:

    x→AAxM↑↓M−1c→BBc\begin{array}{ccc} x & \xrightarrow{\qquad A \qquad} & Ax \\[2pt] {\scriptstyle M}\Big\uparrow & & \Big\downarrow{\scriptstyle M^{-1}} \\[2pt] c & \xrightarrow{\qquad B \qquad} & Bc \end{array}

    The top row lives in standard coordinates, where we know the map AA. The bottom row lives in new coordinates, where we want the matrix BB. There are two ways from cc (bottom left) to the bottom right corner:

    1. Up: MM turns the new coordinates cc into the standard vector x=Mcx = Mc.
    2. Across: AA acts on xx and gives Ax=AMcAx = AMc.
    3. Down: M−1M^{-1} turns AxAx back into new coordinates: M−1AMcM^{-1}AMc.

    The direct way is BcBc. Both ways must give the same coordinates for every cc, so B=M−1AMB = M^{-1}AM. Read the product from right to left: the matrix next to cc acts first. The same square read the other way gives A=MBM−1A = MBM^{-1}.

    Small numbers. Take A=(4−211)A = \begin{pmatrix} 4 & -2 \\ 1 & 1 \end{pmatrix}, b1=(2,1)b_1 = (2, 1), b2=(1,1)b_2 = (1, 1) and c=(1,1)c = (1, 1).

    1. Up: x=Mc=1⋅(2,1)+1⋅(1,1)=(3,2)x = Mc = 1 \cdot (2, 1) + 1 \cdot (1, 1) = (3, 2).
    2. Across: Ax=(4⋅3−2⋅2, 3+2)=(8,5)Ax = (4 \cdot 3 - 2 \cdot 2,\ 3 + 2) = (8, 5).
    3. Down: M−1(8,5)=(8−5, −8+10)=(3,2)M^{-1}(8, 5) = (8 - 5,\ -8 + 10) = (3, 2). Check: 3b1+2b2=(6,3)+(2,2)=(8,5)3b_1 + 2b_2 = (6, 3) + (2, 2) = (8, 5) ✓.

    Section 4(b) finds B=diag⁡(3,2)B = \operatorname{diag}(3, 2) for this basis, and indeed Bc=(3,2)Bc = (3, 2).

    Interactive figure: loading…

    Press Next step four times. The left panel shows coordinate vectors (black cc, violet output); its orange and blue axes mean "steps along b1b_1" and "steps along b2b_2". The right panel shows the same arrows in standard coordinates. Watch c→Mc→AMc→M−1AMcc \to Mc \to AMc \to M^{-1}AMc, then see the shortcut BcBc land on the same point. Drag cc and change the basis: the shortcut always agrees, but BB is diagonal only for the eigenvector basis (2,1),(1,1)(2, 1), (1, 1).

    Theorem. Let T(x)=AxT(x) = Ax in standard coordinates. In the basis given by the columns of MM, TT acts on coordinates by B=M−1AMB = M^{-1}AM.

    Proof.

    1. The vector with new coordinates cc is x=Mcx = Mc.
    2. Its image, in standard coordinates, is Ax=AMcAx = AMc.
    3. The new coordinates of the image are M−1(AMc)M^{-1}(AMc).
    4. So TT in new coordinates is c↦M−1AMcc \mapsto M^{-1}AMc, that is, B=M−1AMB = M^{-1}AM. ■\blacksquare

    Column jj of BB is M−1AbjM^{-1}Ab_j: the new coordinates of T(bj)T(b_j). This is lesson 01's "columns are the images of the basis vectors", now in the new basis.

    Quick check

    In B=M−1AMB = M^{-1}AM, the columns of MM are the new basis vectors. Which matrix acts first on a coordinate vector cc?

    Similar matrices #

    Two square matrices AA and BB are similar if B=M−1AMB = M^{-1}AM for some invertible MM: 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.

    1. B−λI=M−1AM−λM−1MB - \lambda I = M^{-1}AM - \lambda M^{-1}M, and this is M−1(A−λI)MM^{-1}(A - \lambda I)M.
    2. Product rule (lesson 03): det⁡(B−λI)=det⁡(M−1)det⁡(A−λI)det⁡(M)\det(B - \lambda I) = \det(M^{-1})\det(A - \lambda I)\det(M). Since det⁡(M−1)det⁡(M)=1\det(M^{-1})\det(M) = 1, this is det⁡(A−λI)\det(A - \lambda I).
    3. The eigenvalues are the roots of this polynomial; the trace and the determinant are two of its coefficients (lesson 03). So they agree.
    4. Rank: rank⁡(XY)≤min⁡(rank⁡X,rank⁡Y)\operatorname{rank}(XY) \le \min(\operatorname{rank} X, \operatorname{rank} Y) (lesson 01) gives rank⁡B≤rank⁡A\operatorname{rank} B \le \operatorname{rank} A, and A=MBM−1A = MBM^{-1} gives rank⁡A≤rank⁡B\operatorname{rank} A \le \operatorname{rank} B. ■\blacksquare

    What does change. Eigenvectors: if Av=λvAv = \lambda v, then B(M−1v)=λ(M−1v)B(M^{-1}v) = \lambda (M^{-1}v). It is the same arrow under a new name: its new coordinates M−1vM^{-1}v. Singular values, norms and symmetry also change in general (section 4(b) shows it with numbers).

    Quick check

    B=M−1AMB = M^{-1}AM for some invertible MM that is not orthogonal. Which quantity can be different for AA and BB?

    The converse is false: the same eigenvalues do not make two matrices similar. The shear S=(1101)S = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} and the identity II both have the eigenvalues 1,11, 1, trace 2 and determinant 1. But for every invertible MM,

    M−1IM=M−1M=I≠S.M^{-1} I M = M^{-1}M = I \ne S .

    So no basis turns II into SS. In words: II keeps every vector, and this does not depend on the basis; the shear moves most vectors. The same argument shows that N=(0100)N = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix} 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?".

    Quick check

    A=(3103)A = \begin{pmatrix} 3 & 1 \\ 0 & 3 \end{pmatrix} and D=(3003)D = \begin{pmatrix} 3 & 0 \\ 0 & 3 \end{pmatrix}. Are they similar?

    The aim of the next lessons is to choose MM so that BB is simple. In a basis of eigenvectors, BB is diagonal (lesson 05).

    Orthogonal matrices #

    A real n×nn \times n matrix QQ is orthogonal if Q⊤Q=IQ^\top Q = I. Entry (i,j)(i, j) of Q⊤QQ^\top Q is qi⊤qjq_i^\top q_j, so this says: the columns are orthonormal (perpendicular, with length 1).

    • Q−1=Q⊤Q^{-1} = Q^\top, and also QQ⊤=IQQ^\top = I (for square matrices one side is enough, lesson 03). So the rows are orthonormal as well.
    • Coordinates in the orthonormal basis q1,…,qnq_1, \dots, q_n are c=Q⊤xc = Q^\top x, that is, ci=qi⊤xc_i = q_i^\top x.
    • Products and inverses of orthogonal matrices are orthogonal: (Q1Q2)⊤(Q1Q2)=Q2⊤Q1⊤Q1Q2=I(Q_1Q_2)^\top(Q_1Q_2) = Q_2^\top Q_1^\top Q_1 Q_2 = I.
    • A tall QQ (m×nm \times n, m>nm > n) with orthonormal columns has Q⊤Q=InQ^\top Q = I_n but QQ⊤≠ImQQ^\top \ne I_m: QQ⊤QQ^\top is the projector onto C(Q)C(Q) (lesson 03). Such a QQ is not called orthogonal.

    Theorem (orthogonal maps keep the geometry). If Q⊤Q=IQ^\top Q = I, then for all x,yx, y: (Qx)⊤(Qy)=x⊤y(Qx)^\top(Qy) = x^\top y, ∥Qx∥=∥x∥\|Qx\| = \|x\|, and the angle between QxQx and QyQy equals the angle between xx and yy.

    Proof.

    1. (Qx)⊤(Qy)=x⊤Q⊤Qy=x⊤y(Qx)^\top(Qy) = x^\top Q^\top Q y = x^\top y.
    2. Take y=xy = x: ∥Qx∥2=∥x∥2\|Qx\|^2 = \|x\|^2.
    3. cos⁡θ=x⊤y/(∥x∥∥y∥)\cos\theta = x^\top y / (\|x\|\|y\|) is built from dot products only, so it does not change. ■\blacksquare

    The proof uses only Q⊤Q=IQ^\top Q = I, so it also holds for a tall QQ with orthonormal columns.

    Keeping lengths is enough

    The converse also holds: if ∥Qx∥=∥x∥\|Qx\| = \|x\| for all xx, then Q⊤Q=IQ^\top Q = I. The reason is that lengths fix dot products, through the polarization identity

    x⊤y=14(∥x+y∥2−∥x−y∥2).x^\top y = \tfrac14\big(\|x + y\|^2 - \|x - y\|^2\big).

    Apply it to Qx,QyQx, Qy and to x,yx, y: the right-hand sides are equal, so (Qx)⊤(Qy)=x⊤y(Qx)^\top(Qy) = x^\top y for all x,yx, y. That is, x⊤(Q⊤Q−I)y=0x^\top(Q^\top Q - I)y = 0 for all x,yx, y. With x=eix = e_i and y=ejy = e_j this says that entry (i,j)(i, j) of Q⊤Q−IQ^\top Q - I is 0. So a linear map that keeps every length is orthogonal, and it keeps every angle as well.

    Theorem (determinant and eigenvalues). If QQ is orthogonal, then det⁡Q=±1\det Q = \pm 1, and every eigenvalue λ\lambda (real or complex) has ∣λ∣=1|\lambda| = 1.

    Proof.

    1. 1=det⁡I=det⁡(Q⊤Q)1 = \det I = \det(Q^\top Q), and by the product rule this is det⁡(Q⊤)det⁡(Q)=(det⁡Q)2\det(Q^\top)\det(Q) = (\det Q)^2.
    2. Let Qx=λxQx = \lambda x with x≠0x \ne 0, possibly complex. With the complex norm ∥x∥2=x∗x\|x\|^2 = x^* x (x∗x^* is the conjugate transpose), ∥Qx∥2=x∗Q⊤Qx=∥x∥2\|Qx\|^2 = x^* Q^\top Q x = \|x\|^2 because QQ is real. Also ∥Qx∥=∣λ∣ ∥x∥\|Qx\| = |\lambda|\,\|x\|. So ∣λ∣=1|\lambda| = 1. ■\blacksquare

    Rotations and reflections. det⁡Q=+1\det Q = +1: a rotation (orientation kept). det⁡Q=−1\det Q = -1: the map contains a reflection. In 2D every orthogonal matrix is one of

    Rθ=(cos⁡θ−sin⁡θsin⁡θcos⁡θ) (det⁡=+1),Fθ=(cos⁡θsin⁡θsin⁡θ−cos⁡θ) (det⁡=−1).R_\theta = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \ (\det = +1), \qquad F_\theta = \begin{pmatrix} \cos\theta & \sin\theta \\ \sin\theta & -\cos\theta \end{pmatrix} \ (\det = -1).

    Why only these two? The first column is a unit vector, so it is (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta) for some θ\theta. The second column is a unit vector orthogonal to it, so it is ±(−sin⁡θ,cos⁡θ)\pm(-\sin\theta, \cos\theta). The sign ++ gives RθR_\theta and the sign −- gives FθF_\theta. FθF_\theta reflects across the line at angle θ/2\theta/2. Other examples: permutation matrices, and Householder reflections H=I−2uu⊤H = I - 2uu^\top with ∥u∥=1\|u\| = 1 (lesson 12).

    Interactive figure: loading…

    Drag xx and yy, or change θ\theta. The default is the rotation Q=15(3−443)Q = \tfrac15\begin{pmatrix} 3 & -4 \\ 4 & 3 \end{pmatrix} of section 4(c), with x=(1,2)x = (1, 2) and y=(3,1)y = (3, 1). Notice that lengths, the dot product and the angle stay the same (green), and the unit circle maps onto itself. Switch to the reflection FθF_\theta: still Q⊤Q=IQ^\top Q = I, but det⁡=−1\det = -1 and the F is mirrored. Then try not orthogonal to see everything change (magenta).

    Unitary matrices #

    For complex vectors the inner product is ⟨x,y⟩=x∗y=∑ixˉiyi\langle x, y \rangle = x^* y = \sum_i \bar x_i y_i, and ∥x∥2=x∗x=∑i∣xi∣2\|x\|^2 = x^* x = \sum_i |x_i|^2. Here x∗x^* is the conjugate transpose: transpose, and replace every entry by its complex conjugate (a+bi‾=a−bi\overline{a + bi} = a - bi). The conjugate is needed so that ∥x∥2\|x\|^2 is a positive real number: for x=(1,i)x = (1, i), x∗x=1+(−i)(i)=2x^* x = 1 + (-i)(i) = 2, while x⊤x=1+i2=0x^\top x = 1 + i^2 = 0.

    A complex n×nn \times n matrix UU is unitary if U∗U=IU^*U = I. The same proofs as for orthogonal matrices give ⟨Ux,Uy⟩=x∗U∗Uy=⟨x,y⟩\langle Ux, Uy \rangle = x^* U^* U y = \langle x, y \rangle, ∥Ux∥=∥x∥\|Ux\| = \|x\|, U−1=U∗U^{-1} = U^*, ∣det⁡U∣=1|\det U| = 1 and ∣λ∣=1|\lambda| = 1 for every eigenvalue. A real orthogonal matrix is a unitary matrix with real entries.

    Example, entry by entry. Take

    U=12(1ii1),U∗=12(1−i−i1).U = \frac{1}{\sqrt2}\begin{pmatrix} 1 & i \\ i & 1 \end{pmatrix}, \qquad U^* = \frac{1}{\sqrt2}\begin{pmatrix} 1 & -i \\ -i & 1 \end{pmatrix}.

    (UU is symmetric, so U∗U^* only conjugates the entries.) The product U∗UU^*U is 12\tfrac12 times:

    1. entry (1,1)(1, 1): 1⋅1+(−i)(i)=1+1=21 \cdot 1 + (-i)(i) = 1 + 1 = 2;
    2. entry (1,2)(1, 2): 1⋅i+(−i)⋅1=01 \cdot i + (-i) \cdot 1 = 0;
    3. entry (2,1)(2, 1): (−i)⋅1+1⋅i=0(-i) \cdot 1 + 1 \cdot i = 0;
    4. entry (2,2)(2, 2): (−i)(i)+1⋅1=2(-i)(i) + 1 \cdot 1 = 2.

    So U∗U=12(2002)=IU^*U = \tfrac12 \begin{pmatrix} 2 & 0 \\ 0 & 2 \end{pmatrix} = I: UU is unitary. Without the conjugate the test fails: U⊤U=U2=(0ii0)≠IU^\top U = U^2 = \begin{pmatrix} 0 & i \\ i & 0 \end{pmatrix} \ne I. Also det⁡U=12(1−i2)=1\det U = \tfrac12(1 - i^2) = 1, and the eigenvalues are (1±i)/2(1 \pm i)/\sqrt2, both of modulus 1.

    The DFT matrix is unitary (Parseval)

    The most important unitary matrix is the discrete Fourier transform. The DFT matrix Fjk=e−2πijk/nF_{jk} = e^{-2\pi i jk/n} (j,k=0,…,n−1j, k = 0, \dots, n-1) satisfies F∗F=nIF^*F = nI, so F/nF/\sqrt{n} is unitary. Entry (j,k)(j, k) of F∗FF^*F is ∑le2πil(j−k)/n\sum_{l} e^{2\pi i l (j - k)/n}: a sum of nn equally spaced points on the unit circle when j≠kj \ne k (it is 0), and nn when j=kj = k. This is Parseval's theorem: the normalized Fourier transform keeps the energy ∑j∣xj∣2\sum_j |x_j|^2. Lesson 19 uses it for circulant matrices and Fourier neural operators.

    Orthogonal maps keep the singular values #

    Theorem. Let Q1Q_1 (m×mm \times m) and Q2Q_2 (n×nn \times n) be orthogonal, and AA be m×nm \times n. Then Q1AQ2Q_1AQ_2 has the same singular values as AA. Hence ∥Q1AQ2∥2=∥A∥2\|Q_1AQ_2\|_2 = \|A\|_2 and ∥Q1AQ2∥F=∥A∥F\|Q_1AQ_2\|_F = \|A\|_F.

    Proof.

    1. (Q1AQ2)⊤(Q1AQ2)=Q2⊤A⊤Q1⊤Q1AQ2(Q_1AQ_2)^\top(Q_1AQ_2) = Q_2^\top A^\top Q_1^\top Q_1 A Q_2, and Q1⊤Q1=IQ_1^\top Q_1 = I turns this into Q2⊤(A⊤A)Q2Q_2^\top (A^\top A) Q_2.
    2. Q2⊤=Q2−1Q_2^\top = Q_2^{-1}, so this matrix is Q2−1(A⊤A)Q2Q_2^{-1}(A^\top A)Q_2: it is similar to A⊤AA^\top A.
    3. Similar matrices have the same eigenvalues. Their square roots are the singular values, so the singular values agree.
    4. The two norms depend only on the singular values: ∥A∥2=σ1\|A\|_2 = \sigma_1 (lesson 08) and ∥A∥F2=∑iσi2=tr⁡(A⊤A)\|A\|_F^2 = \sum_i \sigma_i^2 = \operatorname{tr}(A^\top A). ■\blacksquare

    Three consequences:

    • Conditioning. All singular values of QQ are 1 (Q⊤Q=IQ^\top Q = I), so κ2(Q)=1\kappa_2(Q) = 1. A rounding error δ\delta is not amplified: ∥Qδ∥=∥δ∥\|Q\delta\| = \|\delta\|. Algorithms built only from orthogonal transformations (Householder and Givens QR, the QR algorithm, Golub–Kahan) are backward stable.
    • Symmetry. An orthogonal change of basis B=Q⊤AQB = Q^\top AQ keeps a symmetric AA symmetric: B⊤=Q⊤A⊤Q=BB^\top = Q^\top A^\top Q = B. A general M−1AMM^{-1}AM does not. This is why the spectral theorem (lesson 06) is written S=QΛQ⊤S = Q\Lambda Q^\top.
    • Orthogonal similarity keeps both spectra. A general M−1AMM^{-1}AM keeps the eigenvalues but changes the singular values; Q⊤AQQ^\top AQ keeps both.

    Answers to section 1. (1) Mc=1⋅(1,0)+1⋅(1,1)=(2,1)Mc = 1 \cdot (1, 0) + 1 \cdot (1, 1) = (2, 1). The vector (1,1)(1, 1) is b2b_2 itself, so its new coordinates are c=M−1(1,1)=(0,1)c = M^{-1}(1, 1) = (0, 1). (2) Shared: eigenvalues, trace, determinant and rank (all from the characteristic polynomial and the rank argument). Not shared in general: eigenvectors (vv becomes M−1vM^{-1}v) and singular values (unless MM is orthogonal).

    4. Worked example #

    (a) Coordinates. Basis b1=(2,1)b_1 = (2, 1), b2=(1,1)b_2 = (1, 1):

    M=(2111),det⁡M=2⋅1−1⋅1=1,M−1=1det⁡M(1−1−12)=(1−1−12).M = \begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix}, \quad \det M = 2 \cdot 1 - 1 \cdot 1 = 1, \quad M^{-1} = \frac{1}{\det M}\begin{pmatrix} 1 & -1 \\ -1 & 2 \end{pmatrix} = \begin{pmatrix} 1 & -1 \\ -1 & 2 \end{pmatrix}.

    For x=(4,3)x = (4, 3): c=M−1x=(4−3,−4+6)=(1,2)c = M^{-1}x = (4 - 3, -4 + 6) = (1, 2). Check: 1⋅(2,1)+2⋅(1,1)=(4,3)1 \cdot (2, 1) + 2 \cdot (1, 1) = (4, 3) ✓.

    (b) One map, three matrices. Take A=(4−211)A = \begin{pmatrix} 4 & -2 \\ 1 & 1 \end{pmatrix}.

    • In the basis of (a), use the column rule (column jj of BB = new coordinates of AbjAb_j): Ab1=(8−2,2+1)=(6,3)=3b1Ab_1 = (8 - 2, 2 + 1) = (6, 3) = 3b_1, so column 1 of BB is (3,0)(3, 0). Ab2=(4−2,1+1)=(2,2)=2b2Ab_2 = (4 - 2, 1 + 1) = (2, 2) = 2b_2, so column 2 of BB is (0,2)(0, 2). Hence B=M−1AM=(3002)B = M^{-1}AM = \begin{pmatrix} 3 & 0 \\ 0 & 2 \end{pmatrix}. The basis vectors are eigenvectors, and in this basis the map is two independent stretches.
    • In the basis b1′=(1,0)b_1' = (1, 0), b2′=(1,1)b_2' = (1, 1), with M2=(1101)M_2 = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} and M2−1=(1−101)M_2^{-1} = \begin{pmatrix} 1 & -1 \\ 0 & 1 \end{pmatrix}, multiply from right to left:
    AM2=(4212),B2=M2−1(AM2)=(4−12−212)=(3012).AM_2 = \begin{pmatrix} 4 & 2 \\ 1 & 2 \end{pmatrix}, \qquad B_2 = M_2^{-1}(AM_2) = \begin{pmatrix} 4 - 1 & 2 - 2 \\ 1 & 2 \end{pmatrix} = \begin{pmatrix} 3 & 0 \\ 1 & 2 \end{pmatrix}.
    AA B=diag⁡(3,2)B = \operatorname{diag}(3, 2) B2B_2
    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 AA: A⊤A=(17−7−75)A^\top A = \begin{pmatrix} 17 & -7 \\ -7 & 5 \end{pmatrix} has eigenvalues 11±8511 \pm \sqrt{85}, so σ=11±85≈4.497,1.334\sigma = \sqrt{11 \pm \sqrt{85}} \approx 4.497, 1.334. The singular values change from basis to basis, because MM and M2M_2 are not orthogonal. Their product stays ∣det⁡∣=6|\det| = 6. The eigenvector (2,1)(2, 1) of AA has new coordinates M−1(2,1)=(1,0)=e1M^{-1}(2, 1) = (1, 0) = e_1: the eigenvector of BB.

    (c) A rotation and a reflection.

    Q=15(3−443),H=15(344−3).Q = \frac15\begin{pmatrix} 3 & -4 \\ 4 & 3 \end{pmatrix}, \qquad H = \frac15\begin{pmatrix} 3 & 4 \\ 4 & -3 \end{pmatrix}.

    Both have orthonormal columns (9+16=259 + 16 = 25, and 3⋅(−4)+4⋅3=03 \cdot (-4) + 4 \cdot 3 = 0). det⁡Q=1\det Q = 1: QQ is the rotation RθR_\theta with cos⁡θ=3/5\cos\theta = 3/5, sin⁡θ=4/5\sin\theta = 4/5 (θ≈53.13°\theta \approx 53.13°). For x=(1,2)x = (1, 2) and y=(3,1)y = (3, 1): Qx=(−1,2)Qx = (-1, 2) and Qy=(1,3)Qy = (1, 3). Lengths: ∥x∥2=∥Qx∥2=5\|x\|^2 = \|Qx\|^2 = 5. Dot products: x⊤y=3+2=5x^\top y = 3 + 2 = 5 and (Qx)⊤(Qy)=−1+6=5(Qx)^\top(Qy) = -1 + 6 = 5 ✓. The eigenvalues of QQ are (3±4i)/5(3 \pm 4i)/5, both of modulus 1.

    det⁡H=−925−1625=−1\det H = -\tfrac{9}{25} - \tfrac{16}{25} = -1: a reflection. H(2,1)=(2,1)H(2, 1) = (2, 1) and H(1,−2)=(−1,2)H(1, -2) = (-1, 2), so HH fixes the line through (2,1)(2, 1) and flips the perpendicular direction. Its eigenvalues are 11 and −1-1.

    Quick check

    The columns of P=(11−11)P = \begin{pmatrix} 1 & 1 \\ -1 & 1 \end{pmatrix} are perpendicular. What is P−1P^{-1}?

    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 M−1vM^{-1}v without forming M−1M^{-1}. 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, tr⁡(Q⊤ΣQ)=tr⁡Σ\operatorname{tr}(Q^\top \Sigma Q) = \operatorname{tr}\Sigma; it is only redistributed so that the first axes carry most of it.
    • Rotary position embeddings (RoPE). A query at position mm is rotated by a block-diagonal orthogonal matrix RmR_m (2×2 rotations by the angles mθim\theta_i), and a key at position nn by RnR_n. Since Rm⊤Rn=Rn−mR_m^\top R_n = R_{n-m}, the score (Rmq)⊤(Rnk)=q⊤Rn−mk(R_m q)^\top (R_n k) = q^\top R_{n-m} k depends only on the offset n−mn - m, and the rotations do not change the norms of qq and kk.
    • Norm-preserving layers. An orthogonal weight matrix keeps ∥Wx∥=∥x∥\|Wx\| = \|x\|, 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 G=UΣV⊤G = U\Sigma V^\top of each weight matrix by an approximation of UV⊤UV^\top (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 QQ, a vector output (forces, velocities) should rotate too, f(Qx)=Qf(x)f(Qx) = Qf(x), and a scalar output (energy) should not change. Distances ∥xi−xj∥\|x_i - x_j\| and dot products are invariant under QQ, which is why E(n)-equivariant GNNs build their messages from them (lesson 19).

    7. Common mistakes #

    • Direction of the conversion. The coordinates are c=M−1xc = M^{-1}x, not MxMx; MM goes from new coordinates to standard ones.
    • Reading M−1AMM^{-1}AM from left to right. The matrix next to the vector acts first: MM, then AA, then M−1M^{-1}.
    • "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 (1101)\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} and II.
    • "Orthogonal = rotation." det⁡Q=−1\det Q = -1 means a reflection is included (also for UU, VV and UV⊤UV^\top).
    • Orthogonal but not normalized columns. Then Q⊤QQ^\top Q is diagonal, not II, and Q−1≠Q⊤Q^{-1} \ne Q^\top.
    • Tall QQ: QQ⊤=IQQ^\top = I. False; QQ⊤QQ^\top is the projector onto C(Q)C(Q).
    • Complex case. Use U∗U^* (conjugate transpose), not U⊤U^\top; NumPy's A.T does not conjugate.

    8. Check yourself #

    1. B=M−1AMB = M^{-1}AM, where AA is 3×33 \times 3 with eigenvalues 1,2,41, 2, 4. Give tr⁡B\operatorname{tr} B and det⁡B\det B. Is BB invertible?
      Answertr⁡B=7\operatorname{tr} B = 7, det⁡B=8\det B = 8. Yes: 0 is not an eigenvalue (equivalently det⁡B≠0\det B \ne 0).
    2. q1=(3/5,4/5)q_1 = (3/5, 4/5) and q2=(−4/5,3/5)q_2 = (-4/5, 3/5). Find the coordinates of x=(5,0)x = (5, 0) in this basis without solving a linear system.
      AnswerThe basis is orthonormal, so ci=qi⊤xc_i = q_i^\top x: c=(3,−4)c = (3, -4). Check: 3(3/5,4/5)−4(−4/5,3/5)=(5,0)3(3/5, 4/5) - 4(-4/5, 3/5) = (5, 0) ✓.
    3. Show that the product of two orthogonal matrices is orthogonal. Is the sum orthogonal?
      Answer(Q1Q2)⊤(Q1Q2)=Q2⊤Q1⊤Q1Q2=Q2⊤Q2=I(Q_1Q_2)^\top(Q_1Q_2) = Q_2^\top Q_1^\top Q_1 Q_2 = Q_2^\top Q_2 = I. The sum is not, in general: I+I=2II + I = 2I and (2I)⊤(2I)=4I(2I)^\top(2I) = 4I.
    4. A 5×35 \times 3 matrix QQ has orthonormal columns. What are Q⊤QQ^\top Q and QQ⊤QQ^\top? Give the rank and the trace of QQ⊤QQ^\top.
      AnswerQ⊤Q=I3Q^\top Q = I_3. QQ⊤QQ^\top is the 5×55 \times 5 orthogonal projector onto C(Q)C(Q), not I5I_5; its rank and its trace are both 3.
    5. Does M−1AMM^{-1}AM keep symmetry? Try A=diag⁡(1,2)A = \operatorname{diag}(1, 2) and M=(1101)M = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}.
      AnswerM−1AM=(1−102)M^{-1}AM = \begin{pmatrix} 1 & -1 \\ 0 & 2 \end{pmatrix}: not symmetric. With an orthogonal QQ, (Q⊤AQ)⊤=Q⊤A⊤Q=Q⊤AQ(Q^\top AQ)^\top = Q^\top A^\top Q = Q^\top AQ, so symmetry is kept.
    6. Why does every 3×33 \times 3 rotation (orthogonal, det⁡=+1\det = +1) have the eigenvalue 1?
      AnswerThe characteristic polynomial is a real cubic, so it has a real root, and every eigenvalue has ∣λ∣=1|\lambda| = 1. If the other two eigenvalues are a complex pair μ,μˉ\mu, \bar\mu, then det⁡=λ⋅∣μ∣2=λ=1\det = \lambda \cdot |\mu|^2 = \lambda = 1. If all three are real (±1\pm 1), their product is 1, so the number of −1-1's is even and at least one eigenvalue is +1+1. Its eigenvector is the rotation axis.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. The columns of MM are the new basis vectors. How do you get the new coordinates cc of a vector xx?

    2. Explain B=M−1AMB = M^{-1}AM in words.

    3. What do similar matrices always share?

    4. What does a general similarity M−1AMM^{-1}AM not keep?

    5. Give two 2×22 \times 2 matrices with the same eigenvalues that are not similar.

    6. Why does Q⊤Q=IQ^\top Q = I keep dot products?

    7. What values can det⁡Q\det Q take for an orthogonal QQ, and what do they mean?

    8. What is ∣λ∣|\lambda| for an eigenvalue of an orthogonal matrix, and why?

    9. Define a unitary matrix.

    10. What are the singular values of Q1AQ2Q_1AQ_2 when Q1Q_1, Q2Q_2 are orthogonal?

    Open the review →

    9. Interview angle #

    • "What is the inverse of an orthogonal matrix, and why are orthogonal transformations numerically safe?" — Q−1=Q⊤Q^{-1} = Q^\top; κ2(Q)=1\kappa_2(Q) = 1 and ∥Qδ∥=∥δ∥\|Q\delta\| = \|\delta\|, 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 Λ\Lambda.
    • "How does RoPE encode relative position?" — Rm⊤Rn=Rn−mR_m^\top R_n = R_{n-m}, so the score depends only on n−mn - m.

    10. Practice #

    The notebook for this lesson runs in Colab. It has five exercises, each with a check cell and a folded solution:

    1. Convert coordinates in both directions (to_basis_coords, from_basis_coords) with a solve, not an inverse, and check section 4(a).
    2. Write matrix_in_basis(A, M) =M−1AM= M^{-1}AM, and check which quantities of section 4(b) stay the same.
    3. Write has_orthonormal_columns(Q) with a tolerance that scales with the size of QQ.
    4. Build RθR_\theta and FθF_\theta, and check their determinants, their eigenvalues and the mirror line of FθF_\theta.
    5. Implement RoPE on a vector of even length, and check that the score depends only on the offset n−mn - m and that the norms do not change.

    11. Watch and read #

    Watch how a vector gets two names, and the "translate, apply, translate back" reading of M−1AMM^{-1}AM.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play