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

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

    Inverse, determinant, dot product and orthogonal projection

    Beginner 70 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 01 (matrices as maps, span, basis, independence) and lesson 02 (rank, rank–nullity, the four subspaces). Eigenvalues appear once, for det⁡A=λ1⋯λn\det A = \lambda_1 \cdots \lambda_n; here you only need the definition Av=λvAv = \lambda v with v≠0v \ne 0 (lesson 05 treats eigenvalues in full). Time: about 70 minutes, plus the notebook.


    Why this matters #

    Three questions come up in almost every numerical method. Can I undo this map (inverse)? How much does it change volumes (determinant)? What is the closest point in a subspace (projection)? Least squares is a projection. Gaussian likelihoods and normalizing flows need log⁡det⁡\log\det. Attention scores and cosine similarity are dot products. And in practice you almost never form A−1A^{-1}.

    1. Predict first #

    2. The picture #

    Inverse. A−1A^{-1} undoes AA: A−1(Ax)=xA^{-1}(Ax) = x. This is possible exactly when AA loses nothing. No nonzero input may go to 0 (otherwise two inputs give the same output), and every output must be reached. For a square matrix these two conditions are the same, by rank–nullity:

    dim⁡N(A)=0  ⟺  rank⁡A=n  ⟺  C(A)=Rn.\dim N(A) = 0 \iff \operatorname{rank} A = n \iff C(A) = \mathbb{R}^n .
    Interactive figure: loading…

    Do it, then undo it. Press Apply A: the grid, the unit square and the letter F move to their images. Then press Apply A⁻¹: everything returns, because A−1A=IA^{-1}A = I. Now pick singular: det 0. The magenta dots lie on one line x+N(A)x + N(A), and AA sends all of them to the same point. No map can send one point back to five, so A−1A^{-1} does not exist.

    Determinant in 2D. The columns (a,c)(a, c) and (b,d)(b, d) of A=(abcd)A = \begin{pmatrix} a & b \\ c & d \end{pmatrix} span a parallelogram of area ∣ad−bc∣|ad - bc|. The sign of det⁡A=ad−bc\det A = ad - bc is ++ if AA keeps the orientation (the turn from the first column to the second is counter-clockwise, as from e1e_1 to e2e_2) and −- if it makes a mirror image. Every area is multiplied by ∣det⁡A∣|\det A|. If det⁡A=0\det A = 0, the plane is squeezed onto a line or a point.

    Interactive figure: loading…

    Signed area. Drag the orange and blue columns. The unit square becomes a parallelogram of area ∣det⁡A∣|\det A|, and the F inside it grows by the same factor. Drag a2a_2 to the other side of a1a_1: the turn arrow reverses, the F becomes a mirror image and det⁡A<0\det A < 0. Make the columns parallel: the area is 0 and the plane collapses onto a line.

    Quick check

    AA and BB are 2×22 \times 2 with det⁡A=2\det A = 2 and det⁡B=−3\det B = -3. What does ABAB do to the unit square?

    Dot product and projection. u⊤v=∥u∥∥v∥cos⁡θu^\top v = \|u\| \|v\| \cos\theta, where θ\theta is the angle between uu and vv. The projection pp of bb onto a line or a subspace is the foot of the perpendicular from bb: the error b−pb - p is perpendicular to every vector of the subspace. Section 3 shows both in figures.

    3. Definitions and main results #

    Inverse #

    A square n×nn \times n matrix AA is invertible if there is a matrix A−1A^{-1} with A−1A=AA−1=IA^{-1}A = AA^{-1} = I. The inverse is unique: if BA=IBA = I and AC=IAC = I, then B=B(AC)=(BA)C=CB = B(AC) = (BA)C = C.

    Theorem (invertible matrix theorem). For a square n×nn \times n matrix AA, these statements are equivalent:

    • (a) AA is invertible;
    • (b) N(A)={0}N(A) = \{0\}: Ax=0Ax = 0 only for x=0x = 0;
    • (c) rank⁡A=n\operatorname{rank} A = n: elimination (with row exchanges) gives nn nonzero pivots;
    • (d) the columns of AA are independent, so they form a basis of Rn\mathbb{R}^n;
    • (e) C(A)=RnC(A) = \mathbb{R}^n: Ax=bAx = b has a solution for every bb;
    • (f) Ax=bAx = b has exactly one solution for every bb;
    • (g) det⁡A≠0\det A \ne 0;
    • (h) 0 is not an eigenvalue of AA;
    • (i) A⊤A^\top is invertible (so the rows are independent too);
    • (j) there is a BB with BA=IBA = I, or there is a CC with AC=IAC = I (one side is enough).

    Lesson 08 adds one more: all singular values are positive. The short version: a square matrix is invertible exactly when it loses no direction.

    Proof of the main links
    1. (a) ⇒ (b): if Ax=0Ax = 0, then x=A−1Ax=0x = A^{-1}Ax = 0.
    2. (b) ⇔ (d) is the null-space test of lesson 01. (b) ⇔ (c) ⇔ (e) is rank–nullity: dim⁡N(A)=n−rank⁡A\dim N(A) = n - \operatorname{rank} A and dim⁡C(A)=rank⁡A\dim C(A) = \operatorname{rank} A.
    3. (e) and (b) give (f): a solution exists, and two solutions differ by a vector of N(A)={0}N(A) = \{0\}.
    4. (e) ⇒ (a): choose cjc_j with Acj=ejAc_j = e_j and set C=[c1  ⋯  cn]C = [c_1 \; \cdots \; c_n], so AC=IAC = I. Then A(CA−I)=ACA−A=0A(CA - I) = ACA - A = 0, so every column of CA−ICA - I lies in N(A)N(A), which is {0}\{0\} by step 2. Hence CA=ICA = I too.
    5. (b) ⇔ (h): a nonzero xx with Ax=0=0⋅xAx = 0 = 0 \cdot x is exactly an eigenvector for λ=0\lambda = 0.
    6. (c) ⇔ (g): det⁡A=±\det A = \pm (product of the pivots), shown below.
    7. (i): rank⁡A⊤=rank⁡A\operatorname{rank} A^\top = \operatorname{rank} A (row rank = column rank, lesson 02).
    8. (j): BA=IBA = I gives (b) as in step 1; AC=IAC = I gives (e), because b=A(Cb)b = A(Cb) for every bb. ■\blacksquare

    Theorem (one side is enough). If AA and BB are n×nn \times n and BA=IBA = I, then AB=IAB = I.

    Proof.

    1. BA=IBA = I gives N(A)={0}N(A) = \{0\}: if Ax=0Ax = 0, then x=BAx=B0=0x = BAx = B0 = 0.
    2. AA is square, so rank–nullity gives rank⁡A=n\operatorname{rank} A = n and C(A)=RnC(A) = \mathbb{R}^n. So each ej=Acje_j = Ac_j for some cjc_j; with C=[c1  ⋯  cn]C = [c_1 \; \cdots \; c_n] we get AC=IAC = I.
    3. B=B(AC)=(BA)C=CB = B(AC) = (BA)C = C, so AB=AC=IAB = AC = I. ■\blacksquare

    This needs a square matrix. For A=(10)A = \begin{pmatrix} 1 \\ 0 \end{pmatrix} (2×12 \times 1) and B=(10)B = \begin{pmatrix} 1 & 0 \end{pmatrix}, BA=[1]=I1BA = [1] = I_1, but AB=(1000)≠I2AB = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix} \ne I_2. A tall matrix with independent columns has left inverses, for example (A⊤A)−1A⊤(A^\top A)^{-1}A^\top, but no right inverse: rank⁡(AB)≤rank⁡A<m\operatorname{rank}(AB) \le \operatorname{rank} A < m.

    Rules. (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1} (undo the last step first), (A⊤)−1=(A−1)⊤(A^\top)^{-1} = (A^{-1})^\top, and

    (abcd)−1=1ad−bc(d−b−ca).\begin{pmatrix} a & b \\ c & d \end{pmatrix}^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}.

    In practice: solve, do not invert. To solve Ax=bAx = b, factor A=LUA = LU once (about 23n3\tfrac23 n^3 flops, lesson 10) and do two triangular solves (2n22n^2 flops). Forming A−1A^{-1} costs about 2n32n^3 flops, three times more. And x=A−1bx = A^{-1}b is less accurate: its residual ∥b−Ax^∥\|b - A\hat x\| can be larger by a factor up to about κ(A)\kappa(A), the condition number (section 5 shows it).

    Quick check

    Which statement is not equivalent to "the n×nn \times n matrix AA is invertible"?

    Determinant #

    The determinant is the unique function of square matrices with three rules:

    1. det⁡I=1\det I = 1;
    2. exchanging two rows flips the sign;
    3. det⁡\det is linear in each row separately (the other rows fixed).

    Everything else follows from these rules:

    • two equal rows give det⁡=0\det = 0 (exchanging them changes nothing, but must flip the sign);
    • subtracting a multiple of one row from another does not change det⁡\det (rule 3, then the previous point);
    • a triangular matrix has det⁡\det = product of its diagonal entries;
    • so elimination gives det⁡A=±\det A = \pm (product of the pivots), with −- for an odd number of row exchanges; hence det⁡A=0  ⟺  \det A = 0 \iff a pivot is missing   ⟺  A\iff A is singular;
    • det⁡A⊤=det⁡A\det A^\top = \det A.

    For 2×22 \times 2 this gives ad−bcad - bc.

    Determinant by elimination, step by step. Take

    A=(021110243).A = \begin{pmatrix} 0 & 2 & 1 \\ 1 & 1 & 0 \\ 2 & 4 & 3 \end{pmatrix}.
    1. The first pivot position holds 0, so exchange rows 1 and 2. One exchange: the sign flips. (110021243)\begin{pmatrix} 1 & 1 & 0 \\ 0 & 2 & 1 \\ 2 & 4 & 3 \end{pmatrix}, det⁡=−det⁡A\det = -\det A.
    2. Row 3 − 2·row 1: (2,4,3)−(2,2,0)=(0,2,3)(2, 4, 3) - (2, 2, 0) = (0, 2, 3). The determinant does not change.
    3. Row 3 − row 2: (0,2,3)−(0,2,1)=(0,0,2)(0, 2, 3) - (0, 2, 1) = (0, 0, 2). The determinant does not change.
    4. Now U=(110021002)U = \begin{pmatrix} 1 & 1 & 0 \\ 0 & 2 & 1 \\ 0 & 0 & 2 \end{pmatrix} is triangular: det⁡U=1⋅2⋅2=4\det U = 1 \cdot 2 \cdot 2 = 4.
    5. One exchange in total, so det⁡A=(−1)1⋅4=−4\det A = (-1)^1 \cdot 4 = -4.

    Check by cofactor expansion along row 1: 0⋅(1⋅3−0⋅4)−2⋅(1⋅3−0⋅2)+1⋅(1⋅4−1⋅2)=0−6+2=−40 \cdot (1 \cdot 3 - 0 \cdot 4) - 2 \cdot (1 \cdot 3 - 0 \cdot 2) + 1 \cdot (1 \cdot 4 - 1 \cdot 2) = 0 - 6 + 2 = -4 ✓. Cofactor expansion costs about n!n! operations; libraries use the pivots of LU, about 23n3\tfrac23 n^3 flops.

    Geometric meaning. ∣det⁡A∣|\det A| is the volume of the box (parallelepiped) spanned by the columns: the image of the unit cube. Since det⁡A⊤=det⁡A\det A^\top = \det A, the rules also hold for columns, and then they are the rules of volume: the unit cube has volume 1, stretching one edge by cc scales the volume by cc, and a shear keeps base and height. The sign records orientation. Every small cube is scaled by the same factor, so every region's volume is multiplied by ∣det⁡A∣|\det A|.

    Theorem (product rule). det⁡(AB)=det⁡A⋅det⁡B\det(AB) = \det A \cdot \det B.

    Proof.

    1. If BB is singular, so is ABAB (Bx=0Bx = 0 with x≠0x \ne 0 gives ABx=0ABx = 0). Both sides are 0.
    2. If det⁡B≠0\det B \ne 0, define D(A)=det⁡(AB)/det⁡BD(A) = \det(AB) / \det B as a function of AA.
    3. D(I)=det⁡B/det⁡B=1D(I) = \det B / \det B = 1: rule 1.
    4. Row ii of ABAB is (row ii of AA) B\,B. So exchanging two rows of AA exchanges the same rows of ABAB, and DD flips sign: rule 2.
    5. Row ii of ABAB depends linearly on row ii of AA, so DD is linear in each row of AA: rule 3.
    6. The three rules fix the determinant, so D(A)=det⁡AD(A) = \det A. ■\blacksquare

    The picture says the same: ABAB means "apply BB, then AA". Volumes are scaled by det⁡B\det B, then by det⁡A\det A, and the signs multiply. Consequences: det⁡(A−1)=1/det⁡A\det(A^{-1}) = 1/\det A; det⁡(M−1AM)=det⁡A\det(M^{-1}AM) = \det A; det⁡(cA)=cndet⁡A\det(cA) = c^n \det A (each of the nn rows is scaled by cc).

    Theorem (determinant = product of the eigenvalues). Let AA be n×nn \times n with eigenvalues λ1,…,λn\lambda_1, \dots, \lambda_n, complex ones included, each repeated as often as its algebraic multiplicity. Then det⁡A=λ1λ2⋯λn\det A = \lambda_1 \lambda_2 \cdots \lambda_n and tr⁡A=λ1+⋯+λn\operatorname{tr} A = \lambda_1 + \dots + \lambda_n.

    The idea: λ\lambda is an eigenvalue exactly when A−λIA - \lambda I loses a direction, that is, when det⁡(A−λI)=0\det(A - \lambda I) = 0. This determinant is a polynomial in λ\lambda, the characteristic polynomial, and its roots are the eigenvalues. Lesson 05 develops it; the proof below needs only these two facts. Check on a small case: (4123)\begin{pmatrix} 4 & 1 \\ 2 & 3 \end{pmatrix} has eigenvalues 5 and 2 (section 4), and 5⋅2=10=det⁡A5 \cdot 2 = 10 = \det A.

    Proof of det⁡A=λ1⋯λn\det A = \lambda_1 \cdots \lambda_n
    1. λ\lambda is an eigenvalue   ⟺  A−λI\iff A - \lambda I is singular   ⟺  p(λ)=det⁡(A−λI)=0\iff p(\lambda) = \det(A - \lambda I) = 0.
    2. pp is a polynomial of degree nn in λ\lambda with leading term (−λ)n(-\lambda)^n (it comes from the product of the diagonal entries).
    3. Over C\mathbb{C}, pp factors completely into its roots, the eigenvalues: p(λ)=(λ1−λ)(λ2−λ)⋯(λn−λ)p(\lambda) = (\lambda_1 - \lambda)(\lambda_2 - \lambda) \cdots (\lambda_n - \lambda). The leading term (−λ)n(-\lambda)^n matches.
    4. Put λ=0\lambda = 0: det⁡A=p(0)=λ1λ2⋯λn\det A = p(0) = \lambda_1 \lambda_2 \cdots \lambda_n. ■\blacksquare

    The coefficient of λn−1\lambda^{n-1} gives tr⁡A=λ1+⋯+λn\operatorname{tr} A = \lambda_1 + \dots + \lambda_n in the same way: in the product of the diagonal entries (A11−λ)⋯(Ann−λ)(A_{11} - \lambda)\cdots(A_{nn} - \lambda) it is (−1)n−1(A11+⋯+Ann)(-1)^{n-1}(A_{11} + \dots + A_{nn}), the other terms of pp have degree at most n−2n - 2, and in the factored form it is (−1)n−1(λ1+⋯+λn)(-1)^{n-1}(\lambda_1 + \dots + \lambda_n).

    Numerics. The determinant is a bad test of near-singularity. det⁡(0.1 I100)=10−100\det(0.1\, I_{100}) = 10^{-100}, but 0.1 I0.1\, I is perfectly conditioned (κ=1\kappa = 1). diag⁡(105,10−5)\operatorname{diag}(10^5, 10^{-5}) has det⁡=1\det = 1 but κ=1010\kappa = 10^{10}. Use singular values to judge near-singularity (lesson 13), and compute log⁡∣det⁡A∣\log|\det A| from a factorization (slogdet, or Cholesky) when you need it.

    Dot product, norm, orthogonality #

    For u,v∈Rnu, v \in \mathbb{R}^n: the dot product is u⊤v=∑iuiviu^\top v = \sum_i u_i v_i, the norm is ∥v∥=v⊤v\|v\| = \sqrt{v^\top v}, and for nonzero u,vu, v the angle θ∈[0,π]\theta \in [0, \pi] satisfies cos⁡θ=u⊤v/(∥u∥∥v∥)\cos\theta = u^\top v / (\|u\| \|v\|). The vectors are orthogonal (u⊥vu \perp v) if u⊤v=0u^\top v = 0.

    The picture: a shadow. Drop a perpendicular from the tip of vv onto the line through uu. The foot of the perpendicular is the shadow of vv. Its signed length is ∥v∥cos⁡θ\|v\|\cos\theta, and

    u⊤v=∥u∥⋅(∥v∥cos⁡θ)=length of u×signed length of the shadow of v.u^\top v = \|u\| \cdot (\|v\|\cos\theta) = \text{length of } u \times \text{signed length of the shadow of } v .
    Interactive figure: loading…

    Drag the orange tip uu and the blue tip vv. The thick segment is the shadow of vv on the line through uu: aqua when u⊤v>0u^\top v > 0, magenta when u⊤v<0u^\top v < 0. The readout compares u1v1+u2v2u_1v_1 + u_2v_2 with ∥u∥∥v∥cos⁡θ\|u\|\|v\|\cos\theta. Notice that the shadow is never longer than vv itself: that is Cauchy–Schwarz. The two lengths are equal only when vv lies on the line through uu (try opposite).

    • Cauchy–Schwarz: ∣u⊤v∣≤∥u∥∥v∥|u^\top v| \le \|u\| \|v\|, so the cosine lies in [−1,1][-1, 1] and the angle is well defined. In the picture: a shadow is never longer than the vector that casts it. Proof for v≠0v \ne 0: 0≤∥u−tv∥2=∥u∥2−2t u⊤v+t2∥v∥20 \le \|u - tv\|^2 = \|u\|^2 - 2t\,u^\top v + t^2 \|v\|^2 for every number tt; put t=u⊤v/∥v∥2t = u^\top v / \|v\|^2 to get 0≤∥u∥2−(u⊤v)2/∥v∥20 \le \|u\|^2 - (u^\top v)^2/\|v\|^2.
    • Pythagoras: ∥u+v∥2=∥u∥2+2u⊤v+∥v∥2\|u + v\|^2 = \|u\|^2 + 2u^\top v + \|v\|^2, so ∥u+v∥2=∥u∥2+∥v∥2  ⟺  u⊥v\|u + v\|^2 = \|u\|^2 + \|v\|^2 \iff u \perp v.
    • Norm rules: ∥v∥≥0\|v\| \ge 0 with ∥v∥=0\|v\| = 0 only for v=0v = 0; ∥cv∥=∣c∣∥v∥\|cv\| = |c| \|v\|; ∥u+v∥≤∥u∥+∥v∥\|u + v\| \le \|u\| + \|v\| (from Cauchy–Schwarz).
    • Nonzero, mutually orthogonal vectors are independent: take the dot product of ∑jcjqj=0\sum_j c_j q_j = 0 with qiq_i to get ci∥qi∥2=0c_i \|q_i\|^2 = 0.

    Projection onto a line #

    Let a≠0a \ne 0 and bb be vectors in Rm\mathbb{R}^m. The projection p=x^ap = \hat x a is the point of the line through aa closest to bb: the shadow of bb, now as a vector.

    1. The error e=b−x^ae = b - \hat x a is orthogonal to aa: a⊤(b−x^a)=0a^\top(b - \hat x a) = 0.
    2. So x^=a⊤b/a⊤a\hat x = a^\top b / a^\top a and
    p=a⊤ba⊤a a=Pb,P=aa⊤a⊤a.p = \frac{a^\top b}{a^\top a}\, a = Pb, \qquad P = \frac{a a^\top}{a^\top a}.

    PP is m×mm \times m with rank 1, P⊤=PP^\top = P and P2=PP^2 = P.

    Interactive figure: loading…

    Drag the orange tip aa and the black tip bb. The violet arrow p=x^ap = \hat x a is the point of the line closest to bb, and the magenta error e=b−pe = b - p meets the line at a right angle, so a⊤e=0a^\top e = 0. The readout follows the two steps above. Try b on the line (e=0e = 0) and b ⟂ a (p=0p = 0).

    Quick check

    Project b=(1,1)b = (1, 1) onto the line through a=(1,0)a = (1, 0). What is pp?

    Projection onto a subspace #

    Theorem (projection onto C(A)C(A)). Let AA be m×nm \times n with independent columns, and b∈Rmb \in \mathbb{R}^m. The point of C(A)C(A) closest to bb is p=Ax^p = A\hat x, where x^\hat x solves the normal equations A⊤Ax^=A⊤bA^\top A \hat x = A^\top b. So p=Pbp = Pb with

    P=A(A⊤A)−1A⊤.P = A(A^\top A)^{-1}A^\top .

    Derivation in four steps, then the proof that it is closest.

    1. Write pp with unknown coordinates. pp lies in C(A)C(A), so p=Ax^p = A\hat x for some x^∈Rn\hat x \in \mathbb{R}^n.
    2. Ask for a perpendicular error. The error e=b−Ax^e = b - A\hat x must be orthogonal to every column aja_j of AA: aj⊤(b−Ax^)=0a_j^\top(b - A\hat x) = 0 for j=1,…,nj = 1, \dots, n.
    3. Stack the nn equations. The rows of A⊤A^\top are the aj⊤a_j^\top, so step 2 is A⊤(b−Ax^)=0A^\top(b - A\hat x) = 0, that is, A⊤Ax^=A⊤bA^\top A \hat x = A^\top b. (So e∈N(A⊤)e \in N(A^\top), the left null space of lesson 02.)
    4. Solve. A⊤AA^\top A (n×nn \times n) is invertible, because rank⁡(A⊤A)=rank⁡(A)=n\operatorname{rank}(A^\top A) = \operatorname{rank}(A) = n (lesson 02: A⊤Ax=0A^\top A x = 0 gives ∥Ax∥2=0\|Ax\|^2 = 0, so Ax=0Ax = 0 and x=0x = 0). Hence x^=(A⊤A)−1A⊤b\hat x = (A^\top A)^{-1}A^\top b and p=A(A⊤A)−1A⊤b=Pbp = A(A^\top A)^{-1}A^\top b = Pb.
    5. pp is the closest point. For any xx, b−Ax=(b−p)+(p−Ax)b - Ax = (b - p) + (p - Ax) with b−p⊥C(A)b - p \perp C(A) and p−Ax∈C(A)p - Ax \in C(A). Pythagoras gives ∥b−Ax∥2=∥b−p∥2+∥p−Ax∥2≥∥b−p∥2\|b - Ax\|^2 = \|b - p\|^2 + \|p - Ax\|^2 \ge \|b - p\|^2, with equality only when Ax=pAx = p. ■\blacksquare

    For one column (n=1n = 1, A=aA = a) these steps give back the line formula: A⊤A=a⊤aA^\top A = a^\top a is a number.

    Interactive figure: loading…

    Project bb onto a plane. The green patch is C(A)C(A) for the columns a1=(1,0,1)a_1 = (1, 0, 1) and a2=(0,1,1)a_2 = (0, 1, 1) of section 4(c). Drag the figure to rotate it, and move bb with the sliders. The projection pp lies in the plane, and the error ee always meets the plane at a right angle, so A⊤e=0A^\top e = 0. Switch to a line for section 4(b), and to plane, the wrong way to see that the sum of the two line projections misses pp when a1⊤a2≠0a_1^\top a_2 \ne 0.

    Properties of PP.

    • P2=PP^2 = P: A(A⊤A)−1(A⊤A)(A⊤A)−1A⊤=PA(A^\top A)^{-1}(A^\top A)(A^\top A)^{-1}A^\top = P. Projecting twice changes nothing.
    • P⊤=PP^\top = P, because A⊤AA^\top A is symmetric. A matrix with P2=PP^2 = P and P⊤=PP^\top = P is an orthogonal projector.
    • I−PI - P projects onto C(A)⊥=N(A⊤)C(A)^\perp = N(A^\top): (I−P)b=e(I - P)b = e.
    • The eigenvalues are 1 (on C(A)C(A)) and 0 (on N(A⊤)N(A^\top)). So tr⁡P=rank⁡P=n\operatorname{tr} P = \operatorname{rank} P = n, and det⁡P=0\det P = 0 unless n=mn = m (then P=IP = I).
    • Orthonormal columns. If QQ has orthonormal columns (Q⊤Q=IQ^\top Q = I), then P=QQ⊤P = QQ^\top and p=∑i(qi⊤b) qip = \sum_i (q_i^\top b)\, q_i. The projection onto the span is the sum of the projections onto the lines through the basis vectors (for every bb) only when these vectors are mutually orthogonal.
    • Dependent columns. Then A⊤AA^\top A is singular and the formula fails. The projection onto C(A)C(A) still exists: use a basis of C(A)C(A), or P=AA+P = AA^+ with the pseudoinverse (lesson 08).

    In practice you do not form the m×mm \times m matrix PP, and you do not invert A⊤AA^\top A: lesson 12 solves least squares with QR, because κ(A⊤A)=κ(A)2\kappa(A^\top A) = \kappa(A)^2.

    Quick check

    PP is an orthogonal projector: P2=PP^2 = P and P⊤=PP^\top = P. What does P2=PP^2 = P say in words?

    Answers to section 1. (1) Area 3. By the product rule ∣det⁡(AB)∣=∣det⁡A∣ ∣det⁡B∣=6⋅12|\det(AB)| = |\det A|\,|\det B| = 6 \cdot \tfrac12. (2) Not in general: it holds for every bb only if a1⊥a2a_1 \perp a_2. Section 4(c) shows a counterexample.

    4. Worked example #

    (a) Inverse and determinant. A=(4123)A = \begin{pmatrix} 4 & 1 \\ 2 & 3 \end{pmatrix}: det⁡A=4⋅3−1⋅2=10\det A = 4 \cdot 3 - 1 \cdot 2 = 10, so

    A−1=110(3−1−24),AA−1=110(12−2−4+46−6−2+12)=I.A^{-1} = \frac{1}{10}\begin{pmatrix} 3 & -1 \\ -2 & 4 \end{pmatrix}, \qquad AA^{-1} = \frac{1}{10}\begin{pmatrix} 12 - 2 & -4 + 4 \\ 6 - 6 & -2 + 12 \end{pmatrix} = I.

    The unit square goes to the parallelogram spanned by (4,2)(4, 2) and (1,3)(1, 3), with area 10 and the orientation kept. The eigenvalues solve λ2−(tr⁡A)λ+det⁡A=λ2−7λ+10=0\lambda^2 - (\operatorname{tr} A)\lambda + \det A = \lambda^2 - 7\lambda + 10 = 0, so λ=5,2\lambda = 5, 2: product 10=det⁡A10 = \det A ✓, sum 7=tr⁡A7 = \operatorname{tr} A ✓. With the swap S=(0110)S = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} (det⁡S=−1\det S = -1), AS=(1432)AS = \begin{pmatrix} 1 & 4 \\ 3 & 2 \end{pmatrix} (the columns of AA swapped) has det⁡(AS)=2−12=−10=det⁡A⋅det⁡S\det(AS) = 2 - 12 = -10 = \det A \cdot \det S: same area, reversed orientation. (Pick columns swapped in the signed-area figure.)

    Quick check

    Elimination on a 3×33 \times 3 matrix AA needs two row exchanges and gives the pivots 22, −1-1 and 33. What is det⁡A\det A?

    (b) Projection onto a line. a=(1,2,2)a = (1, 2, 2), b=(3,0,3)b = (3, 0, 3). Then a⊤b=3+0+6=9a^\top b = 3 + 0 + 6 = 9 and a⊤a=9a^\top a = 9, so x^=1\hat x = 1, p=(1,2,2)p = (1, 2, 2) and e=b−p=(2,−2,1)e = b - p = (2, -2, 1). Check: a⊤e=2−4+2=0a^\top e = 2 - 4 + 2 = 0 ✓.

    (c) Projection onto a plane. A=(100111)A = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{pmatrix}, b=(1,2,6)b = (1, 2, 6). Follow the four steps.

    Step 1. p=Ax^p = A\hat x with unknown x^=(x^1,x^2)\hat x = (\hat x_1, \hat x_2).

    Steps 2 and 3. The error must be perpendicular to both columns, which gives the normal equations A⊤Ax^=A⊤bA^\top A\hat x = A^\top b. They need A⊤AA^\top A and A⊤bA^\top b:

    A⊤A=(2112),A⊤b=(78).A^\top A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}, \quad A^\top b = \begin{pmatrix} 7 \\ 8 \end{pmatrix}.

    Step 4. Solve with the 2×22 \times 2 inverse formula (det⁡A⊤A=3\det A^\top A = 3):

    x^=13(2−1−12)(78)=(23).\hat x = \frac13 \begin{pmatrix} 2 & -1 \\ -1 & 2 \end{pmatrix}\begin{pmatrix} 7 \\ 8 \end{pmatrix} = \begin{pmatrix} 2 \\ 3 \end{pmatrix}.

    So p=Ax^=(2,3,5)p = A\hat x = (2, 3, 5) and e=b−p=(−1,−1,1)e = b - p = (-1, -1, 1). Check: A⊤e=(−1+1,−1+1)=(0,0)A^\top e = (-1 + 1, -1 + 1) = (0, 0) ✓. The plane is x3=x1+x2x_3 = x_1 + x_2, with normal (1,1,−1)(1, 1, -1), and ee is parallel to that normal. Pythagoras: ∥b∥2=41=∥p∥2+∥e∥2=38+3\|b\|^2 = 41 = \|p\|^2 + \|e\|^2 = 38 + 3 ✓. The projection matrix is

    P=A(A⊤A)−1A⊤=13(2−11−121112),tr⁡P=2=rank⁡A.P = A(A^\top A)^{-1}A^\top = \frac13\begin{pmatrix} 2 & -1 & 1 \\ -1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}, \qquad \operatorname{tr} P = 2 = \operatorname{rank} A .

    Check P⊤=PP^\top = P and P2=PP^2 = P. PP is symmetric: the (1,2)(1, 2) and (2,1)(2, 1) entries are both −13-\tfrac13, and so on. For P2P^2, row 1 of PP times column 1 of PP is 19 (2⋅2+(−1)(−1)+1⋅1)=69=23\tfrac19\,(2 \cdot 2 + (-1)(-1) + 1 \cdot 1) = \tfrac69 = \tfrac23, the (1,1)(1,1) entry of PP; row 1 times column 2 is 19 (2⋅(−1)+(−1)⋅2+1⋅1)=−39=−13\tfrac19\,(2 \cdot (-1) + (-1) \cdot 2 + 1 \cdot 1) = -\tfrac39 = -\tfrac13, the (1,2)(1,2) entry. The other entries work the same way (the code in section 5 checks all of them). And Pb=13(2−2+6, −1+4+6, 1+2+12)=(2,3,5)=pPb = \tfrac13(2 - 2 + 6,\ -1 + 4 + 6,\ 1 + 2 + 12) = (2, 3, 5) = p ✓.

    The wrong way. Project bb onto each column separately and add: 72(1,0,1)+82(0,1,1)=(3.5,4,7.5)≠(2,3,5)\tfrac{7}{2}(1, 0, 1) + \tfrac{8}{2}(0, 1, 1) = (3.5, 4, 7.5) \ne (2, 3, 5). It fails because a1⊤a2=1≠0a_1^\top a_2 = 1 \ne 0: the columns are not orthogonal.

    5. In code #

    The libraries check section 4. The notebook for this lesson has these cells and the exercises.

    import numpy as np
    
    # (a) inverse, determinant, eigenvalues
    A = np.array([[4., 1.], [2., 3.]])
    print(round(np.linalg.det(A), 12))                   # 10.0
    print(np.round(10 * np.linalg.inv(A), 12))           # [[ 3. -1.] [-2.  4.]]
    print(np.sort(np.linalg.eigvals(A).real))            # [2. 5.]
    S = np.array([[0., 1.], [1., 0.]])                   # swap: det S = -1
    print(round(np.linalg.det(A @ S), 12))               # -10.0
    
    # determinant by elimination (section 3): one row exchange, pivots 1, 2, 2
    M = np.array([[0., 2., 1.], [1., 1., 0.], [2., 4., 3.]])
    print(round(np.linalg.det(M), 12))                   # -4.0
    
    # (c) projection onto C(A); lstsq returns the x-hat of the normal equations
    A = np.array([[1., 0.], [0., 1.], [1., 1.]])
    b = np.array([1., 2., 6.])
    x_hat = np.linalg.lstsq(A, b, rcond=None)[0]
    p = A @ x_hat
    print(np.round(x_hat, 12), np.round(p, 12))          # [2. 3.] [2. 3. 5.]
    print(np.abs(A.T @ (b - p)).max() < 1e-12)           # True: e is orthogonal to C(A)
    P = A @ np.linalg.pinv(A)                            # equals A (AᵀA)⁻¹ Aᵀ here
    print(np.allclose(P @ P, P), np.allclose(P, P.T), round(np.trace(P), 12))   # True True 2.0
    
    # a tiny determinant does not mean "nearly singular"
    B = 0.1 * np.eye(100)
    print(np.linalg.det(B), np.linalg.cond(B))           # about 1e-100, and 1.0
    print(np.linalg.slogdet(B))                          # sign 1.0, log|det| = -230.258...
    

    And the reason to solve instead of invert, on the Hilbert matrix Hij=1/(i+j−1)H_{ij} = 1/(i + j - 1) of size 8 (κ≈1.5⋅1010\kappa \approx 1.5 \cdot 10^{10}):

    import numpy as np
    from scipy.linalg import hilbert
    
    H = hilbert(8)
    b = H @ np.random.default_rng(0).standard_normal(8)
    for name, x in [("solve", np.linalg.solve(H, b)), ("inv @ b", np.linalg.inv(H) @ b)]:
        rel_res = np.linalg.norm(b - H @ x) / (np.linalg.norm(H, 2) * np.linalg.norm(x))
        print(f"{name:8s} relative residual {rel_res:.0e}")
    # solve    relative residual 5e-17   (backward stable)
    # inv @ b  relative residual 1e-08   (about 10^8 times worse)
    

    6. Where it shows up #

    • Linear regression. The fitted values are y^=PXy\hat y = P_X y with the "hat matrix" PX=X(X⊤X)−1X⊤P_X = X(X^\top X)^{-1}X^\top, the projector onto the span of the features. The residuals are orthogonal to every feature, and tr⁡PX\operatorname{tr} P_X = the number of parameters.
    • Gaussian models. log⁡N(x;μ,Σ)\log \mathcal{N}(x; \mu, \Sigma) contains −12log⁡det⁡Σ-\tfrac12 \log\det\Sigma. Compute it as 2∑ilog⁡Lii2\sum_i \log L_{ii} from the Cholesky factor Σ=LL⊤\Sigma = LL^\top (lesson 07), never as log⁡(det⁡Σ)\log(\det \Sigma), which can underflow or overflow. Gaussian-process regression needs it at every hyperparameter step.
    • Normalizing flows. For an invertible map z=f(x)z = f(x), log⁡pX(x)=log⁡pZ(f(x))+log⁡∣det⁡Jf(x)∣\log p_X(x) = \log p_Z(f(x)) + \log|\det J_f(x)|. A general determinant costs O(n3)O(n^3), so coupling layers (RealNVP, Glow) make JfJ_f triangular: log⁡∣det⁡Jf∣=∑ilog⁡∣Jii∣\log|\det J_f| = \sum_i \log|J_{ii}| costs O(n)O(n).
    • Attention and embeddings. Attention scores are dot products q⊤kq^\top k (scaled by 1/dk1/\sqrt{d_k}); cosine similarity of embeddings is cos⁡θ=u⊤v/(∥u∥∥v∥)\cos\theta = u^\top v/(\|u\|\|v\|).
    • ML for science: hard constraints. If a surrogate model must satisfy a linear conservation law Cx=0Cx = 0 (for example a discrete mass balance, CC with independent rows), you can enforce it exactly by projecting the prediction onto N(C)N(C): x↦x−C⊤(CC⊤)−1Cxx \mapsto x - C^\top(CC^\top)^{-1}Cx. This is I−PI - P with PP the projector onto C(C⊤)C(C^\top) (take A=C⊤A = C^\top in the theorem). The same idea in function space is the Leray (Helmholtz) projection onto divergence-free velocity fields.

    7. Common mistakes #

    • det⁡(cA)=cdet⁡A\det(cA) = c \det A and det⁡(A+B)=det⁡A+det⁡B\det(A + B) = \det A + \det B. Both wrong: det⁡(cA)=cndet⁡A\det(cA) = c^n \det A for n×nn \times n matrices, and det⁡\det is not additive.
    • Forgetting the sign of row exchanges. det⁡A=(−1)#exchanges×\det A = (-1)^{\#\text{exchanges}} \times (product of the pivots).
    • (AB)−1=A−1B−1(AB)^{-1} = A^{-1}B^{-1}. Wrong order: (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}.
    • x = np.linalg.inv(A) @ b. Use np.linalg.solve(A, b) or a factorization.
    • Adding projections onto non-orthogonal basis vectors. p=∑i(qi⊤b)qip = \sum_i (q_i^\top b) q_i needs orthonormal qiq_i.
    • A(A⊤A)−1A⊤A(A^\top A)^{-1}A^\top with dependent columns. A⊤AA^\top A is singular; use a basis of C(A)C(A).

    8. Check yourself #

    1. AA is 3×33 \times 3 with det⁡A=2\det A = 2. Find det⁡(2A)\det(2A), det⁡(A−1)\det(A^{-1}) and det⁡(A⊤A)\det(A^\top A).
      Answerdet⁡(2A)=23⋅2=16\det(2A) = 2^3 \cdot 2 = 16; det⁡(A−1)=1/2\det(A^{-1}) = 1/2; det⁡(A⊤A)=det⁡A⊤det⁡A=4\det(A^\top A) = \det A^\top \det A = 4.
    2. A 3×33 \times 3 matrix has eigenvalues 2,−1,32, -1, 3. Is it invertible? Give det⁡A\det A and tr⁡A\operatorname{tr} A.
      AnswerYes: 0 is not an eigenvalue. det⁡A=2⋅(−1)⋅3=−6\det A = 2 \cdot (-1) \cdot 3 = -6, tr⁡A=2−1+3=4\operatorname{tr} A = 2 - 1 + 3 = 4.
    3. Project b=(2,1,0)b = (2, 1, 0) onto the line through a=(1,1,1)a = (1, 1, 1). Give pp and ee, and check.
      Answera⊤b=3a^\top b = 3, a⊤a=3a^\top a = 3, so p=(1,1,1)p = (1, 1, 1) and e=(1,0,−1)e = (1, 0, -1); a⊤e=0a^\top e = 0 ✓.
    4. Why does a 3×23 \times 2 matrix AA with independent columns have a left inverse but no right inverse?
      AnswerLeft: (A⊤A)−1A⊤A=I2(A^\top A)^{-1}A^\top A = I_2, and A⊤AA^\top A is invertible because the columns are independent. Right: AB=I3AB = I_3 would need rank 3, but rank⁡(AB)≤rank⁡A=2\operatorname{rank}(AB) \le \operatorname{rank} A = 2.
    5. PP is the orthogonal projector onto a kk-dimensional subspace of Rm\mathbb{R}^m. Give its eigenvalues, trace and determinant.
      AnswerEigenvalue 1 (kk times, on the subspace) and 0 (m−km - k times, on its orthogonal complement). tr⁡P=k\operatorname{tr} P = k. det⁡P=0\det P = 0 unless k=mk = m, where P=IP = I and det⁡P=1\det P = 1.
    6. Compute det⁡(242123011)\det \begin{pmatrix} 2 & 4 & 2 \\ 1 & 2 & 3 \\ 0 & 1 & 1 \end{pmatrix} by elimination.
      AnswerRow 2 − ½·row 1 gives (0,0,2)(0, 0, 2). The second pivot position is now 0, so exchange rows 2 and 3 (sign flips): rows (2,4,2)(2, 4, 2), (0,1,1)(0, 1, 1), (0,0,2)(0, 0, 2). Pivots 2,1,22, 1, 2, one exchange: det⁡=−(2⋅1⋅2)=−4\det = -(2 \cdot 1 \cdot 2) = -4.
    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. Give three conditions equivalent to "the square matrix AA is invertible".

    2. What does det⁡A\det A mean geometrically, and what does its sign mean?

    3. How do you get det⁡A\det A from elimination?

    4. State the product rule for determinants, and one consequence.

    5. How are det⁡A\det A and tr⁡A\operatorname{tr} A related to the eigenvalues?

    6. Write the angle between nonzero uu and vv with the dot product.

    7. Give the projection of bb onto the line through aa.

    8. What equation defines x^\hat x in the projection onto C(A)C(A), and why?

    9. What do P2=PP^2 = P and P⊤=PP^\top = P say about a projection matrix?

    10. For square A,BA, B with BA=IBA = I, why is AB=IAB = I?

    Open the review →

    9. Interview angle #

    • "Why not compute A−1A^{-1} to solve Ax=bAx = b?" — Three times the flops of LU, and not backward stable: the residual can be up to about κ(A)\kappa(A) times worse.
    • "What does det⁡A=0\det A = 0 mean? Is a tiny det⁡\det a problem?" — The columns span a flat box: AA is singular. A tiny det⁡\det alone means little, because it depends on scale; look at κ(A)\kappa(A).
    • "Derive the projection matrix." — The error is orthogonal to C(A)C(A): A⊤(b−Ax^)=0A^\top(b - A\hat x) = 0; A⊤AA^\top A is invertible for independent columns.
    • "How do you compute log⁡det⁡\log\det of a covariance? In a normalizing flow?" — Cholesky, 2∑ilog⁡Lii2\sum_i \log L_{ii}; flows use triangular Jacobians.

    10. Practice #

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

    1. Compute the signed area of a polygon (shoelace formula) and check that a matrix multiplies it by det⁡A\det A, sign included; then test the product rule on random matrices.
    2. Write project_onto_line: return x^\hat x, pp and the rank-1 matrix P=aa⊤/a⊤aP = aa^\top / a^\top a.
    3. Write projector(A) with np.linalg.solve (no inverse), and check P2=PP^2 = P, P⊤=PP^\top = P and tr⁡P=rank⁡A\operatorname{tr} P = \operatorname{rank} A on section 4(c).
    4. Solve or invert? Measure the relative residuals of solve and inv(H) @ b on Hilbert matrices of growing size.
    5. Enforce a conservation law Cx=0Cx = 0 by projecting a prediction onto N(C)N(C), and check that the result is the closest point that satisfies the law.

    11. Watch and read #

    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
    Open on YouTube · the player loads from youtube-nocookie.com when you press play