Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsLinear Algebra

Transpose, symmetric and orthogonal matrices

P1.B1040 min●●○ IntermediateReviewed Nov 30, 2026

Why this matters for machine learning

The transpose moves a matrix to the other side of a dot product, symmetric matrices are the well-behaved ones, and orthogonal matrices turn and mirror vectors without stretching them, so they appear wherever a model sends information backward, compares everything with everything, or must keep sizes stable.

  • Backpropagation sends the gradient backward through a layer y=Wx\mathbf{y}=\mathbf{W}\mathbf{x} by multiplying it by W⊤\mathbf{W}^\top. (P4.8: Backpropagation: step by step with numbers)
  • A covariance matrix is symmetric, and so is the matrix X⊤X\mathbf{X}^\top\mathbf{X} that appears when you fit a line by least squares. (P1.D7: Covariance and the multivariate Gaussian)
  • Attention computes all query-key scores at once as one product with a transposed matrix, written QK⊤\mathbf{Q}\mathbf{K}^\top there (where Q\mathbf{Q} means the queries). (P7.3: Queries, keys and values step by step, coming soon)
  • Rotary position embeddings (RoPE) rotate query and key vectors by an angle that depends on position, and a rotation changes no length. (P7.14: Modern positional methods: RoPE and ALiBi, coming soon)
  • Weight initialization decides the size of signals at the start of training, and an orthogonal matrix is the one kind of square matrix that keeps every length exactly. (P4.12: Weight initialization, coming soon)

Before this: Dot product and cosine similarity, Matrices as data and as transformations, Matrix multiplication, The determinant, Inverse and linear systems

After this lesson you can

  • Transpose a matrix or a vector, give the new shape, and use (AB)⊤=B⊤A⊤(\mathbf{A}\mathbf{B})^\top=\mathbf{B}^\top\mathbf{A}^\top, (A−1)⊤=(A⊤)−1(\mathbf{A}^{-1})^\top=(\mathbf{A}^\top)^{-1} and det⁡A⊤=det⁡A\det\mathbf{A}^\top=\det\mathbf{A}
  • Explain the transpose as the matrix that moves to the other side of a dot product, (Ax)⋅y=x⋅(A⊤y)(\mathbf{A}\mathbf{x})\cdot\mathbf{y}=\mathbf{x}\cdot(\mathbf{A}^\top\mathbf{y}), and say why backpropagation sends gradients backward with W⊤\mathbf{W}^\top
  • Recognise a symmetric matrix and show that A⊤A\mathbf{A}^\top\mathbf{A} and AA⊤\mathbf{A}\mathbf{A}^\top are symmetric for every A\mathbf{A}
  • Define an orthogonal matrix by Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}, prove that it keeps lengths, angles and dot products, use Q−1=Q⊤\mathbf{Q}^{-1}=\mathbf{Q}^\top and det⁡Q=±1\det\mathbf{Q}=\pm1, tell a rotation from a reflection, and avoid the “orthogonal columns” name trap

Two questions have been waiting since earlier lessons. In P1.B5 a rotation by 30∘30^\circ carried the point (3,1)(3,1) to (2.098,2.366)(2.098, 2.366), and its distance from the origin stayed 10≈3.162\sqrt{10}\approx3.162. Why does a rotation never change a length? And in P1.B6 and P1.B9 a small symbol ⊤^\top appeared again and again, always with the promise “P1.B10 explains it”. This lesson keeps both promises. First the transpose (swap rows and columns) and what it really means. Then two special families of matrices: symmetric matrices, which equal their own transpose, and orthogonal matrices, which are exactly the rotations and reflections.

The transpose: flip across the diagonal

The transpose of a matrix A\mathbf{A}, written A⊤\mathbf{A}^\top, swaps its rows and columns. Row 1 of A\mathbf{A} becomes column 1 of A⊤\mathbf{A}^\top, row 2 becomes column 2, and so on. Take the 2×32\times3 matrix from P1.B5 and P1.B6:

A=(123456)A⊤=(142536)\begin{aligned} \mathbf{A}&=\begin{pmatrix}1&2&3\\4&5&6\end{pmatrix}\\ \mathbf{A}^\top&=\begin{pmatrix}1&4\\2&5\\3&6\end{pmatrix} \end{aligned}

In symbols, the entry in row ii and column jj of A⊤\mathbf{A}^\top is the entry of A\mathbf{A} in row jj and column ii:

(A⊤)ij=Aji(\mathbf{A}^\top)_{ij}=A_{ji}

Entries on the diagonal (i=ji=j) stay where they are, and every other entry swaps with its mirror image across the diagonal. The shape changes too: A\mathbf{A} is 2×32\times3 (2 rows, 3 columns) and A⊤\mathbf{A}^\top is 3×23\times2. In general an m×nm\times n matrix has an n×mn\times m transpose. A square matrix keeps its shape.

A vector is a matrix with one column (P1.B5), so its transpose is a matrix with one row:

x=(10−1),x⊤=(10−1)\mathbf{x}=\begin{pmatrix}1\\0\\-1\end{pmatrix},\qquad \mathbf{x}^\top=\begin{pmatrix}1&0&-1\end{pmatrix}

That is why the dot product of P1.B3 can be written a⋅b=a⊤b\mathbf{a}\cdot\mathbf{b}=\mathbf{a}^\top\mathbf{b}: a 1×n1\times n row times an n×1n\times1 column is a 1×11\times1 matrix, a single number, and it is the sum of the products of matching entries. The data matrix of P1.B5 is the stack of the examples written as rows xi⊤\mathbf{x}_i^\top.

The transpose flips a matrix across its diagonal

A is 2 × 3

Aᵀ is 3 × 2

Selected: entry (row 1, column 2) of A is 2. In Aᵀ it sits at row 2, column 1. Row 1 of A became column 1 of Aᵀ (shaded).

change the selected entry (limited to −9…9)

Shape 2 × 3 becomes 3 × 2. Not symmetric, because a matrix must be square to be symmetric.

Tap an entry in either matrix. The ring marks the entry and its mirror image; the shaded strip is one row of A and the matching column of Aᵀ.

Transpose flip. Pick a matrix, then tap any entry in either matrix: a ring marks that entry and its mirror image, and the shaded strip is one row of A and the matching column of the transpose. The shape line shows m × n becoming n × m. Use −1 and +1 to change the selected entry. The status says whether the matrix is symmetric (equal to its own transpose), skew-symmetric or neither. Tick the box to see AᵀA too.

Things to try:

  1. The first matrix is the 2×32\times3 one above. Tap the entry 33 (row 1, column 3): it sits at row 3, column 1 of A⊤\mathbf{A}^\top. The shape reads 2×3→3×22\times3\to3\times2.
  2. Choose Column 3 × 1: a column vector becomes a row vector.
  3. Choose 3 × 3. The diagonal 1,5,91,5,9 does not move. The status says “Not symmetric” and names a pair that differs.
  4. Choose Symmetric 3 × 3: the status says the flip changes nothing. Now tap an entry off the diagonal and press +1. The status turns to “Not symmetric”, because the entry and its mirror image no longer match. Press −1 to fix it.
  5. Tick Also show AᵀA. Whatever matrix you choose, A⊤A\mathbf{A}^\top\mathbf{A} is symmetric. The next sections show why.

Intuition

Picture the matrix as a table on a sheet of paper. Draw the diagonal from the top-left corner to the bottom-right, and flip the sheet over along that diagonal, like turning a page along a slanted hinge. What was a row now reads down as a column. A mirror placed along the diagonal gives the same picture: every entry trades places with its reflection.

The analogy only describes the table. It does not say what the transpose does to vectors, and that is more interesting. A matrix A\mathbf{A} sends a vector from one space to another; the transpose does a different job, which the section after the rules explains. It is not the inverse (it does not undo A\mathbf{A}, except for the special matrices at the end of this lesson).

Math

(A⊤)ij=Aji(\mathbf{A}^\top)_{ij}=A_{ji}. If A\mathbf{A} is m×nm\times n then A⊤\mathbf{A}^\top is n×mn\times m. The ii-th row of A\mathbf{A} is the ii-th column of A⊤\mathbf{A}^\top, and the jj-th column of A\mathbf{A} is the jj-th row of A⊤\mathbf{A}^\top. For a vector, x⊤\mathbf{x}^\top is the row with the same entries, and x⊤y=∑ixiyi=x⋅y\mathbf{x}^\top\mathbf{y}=\sum_ix_iy_i=\mathbf{x}\cdot\mathbf{y}.

A matrix is symmetric if A⊤=A\mathbf{A}^\top=\mathbf{A} and skew-symmetric if A⊤=−A\mathbf{A}^\top=-\mathbf{A}. Both need a square matrix.

Code

import numpy as np

A = np.array([[1, 2, 3], [4, 5, 6]])
print(A.T)        # [[1 4] [2 5] [3 6]]
print(A.shape, A.T.shape)   # (2, 3) (3, 2)

A.T is the transpose. In NumPy it is a view of the same numbers (no copy is made), so changing an entry of A.T changes A. The last section has a warning about 1-D arrays.

Rules of the transpose

Three rules are easy to believe because the transpose only moves entries around:

  • (A⊤)⊤=A(\mathbf{A}^\top)^\top=\mathbf{A}: flip twice and you are back.
  • (A+B)⊤=A⊤+B⊤(\mathbf{A}+\mathbf{B})^\top=\mathbf{A}^\top+\mathbf{B}^\top (same shapes).
  • (cA)⊤=c A⊤(c\mathbf{A})^\top=c\,\mathbf{A}^\top for a number cc.

The fourth one needs care, and you met it in P1.B6 as a promise: the transpose of a product reverses the order.

(AB)⊤=B⊤A⊤(\mathbf{A}\mathbf{B})^\top=\mathbf{B}^\top\mathbf{A}^\top

The shapes show that this is the only possible order. Let A\mathbf{A} be m×nm\times n and B\mathbf{B} be n×pn\times p. Then AB\mathbf{A}\mathbf{B} is m×pm\times p, so (AB)⊤(\mathbf{A}\mathbf{B})^\top is p×mp\times m. Now try both candidates:

  • B⊤A⊤\mathbf{B}^\top\mathbf{A}^\top is (p×n)(p\times n) times (n×m)(n\times m): it fits and gives p×mp\times m.
  • A⊤B⊤\mathbf{A}^\top\mathbf{B}^\top is (n×m)(n\times m) times (p×n)(p\times n): it only fits when m=pm=p, and it would then be n×nn\times n, the wrong shape.

A check with numbers. Take the A\mathbf{A} above and B=(102103)\mathbf{B}=\begin{pmatrix}1&0\\2&1\\0&3\end{pmatrix}. Then

AB=(5111423)(AB)⊤=(5141123)=B⊤A⊤\begin{aligned} \mathbf{A}\mathbf{B}&=\begin{pmatrix}5&11\\14&23\end{pmatrix}\\ (\mathbf{A}\mathbf{B})^\top&=\begin{pmatrix}5&14\\11&23\end{pmatrix}=\mathbf{B}^\top\mathbf{A}^\top \end{aligned}

You can see why entry by entry. Entry (i,j)(i,j) of (AB)⊤(\mathbf{A}\mathbf{B})^\top is entry (j,i)(j,i) of AB\mathbf{A}\mathbf{B}, which is (row jj of A\mathbf{A}) ⋅\cdot (column ii of B\mathbf{B}). For i=1,j=2i=1,j=2: (4,5,6)⋅(1,2,0)=14(4,5,6)\cdot(1,2,0)=14. And (row ii of B⊤\mathbf{B}^\top) ⋅\cdot (column jj of A⊤\mathbf{A}^\top) is the same two lists of numbers, so it is the same dot product. Like the inverse rule (AB)−1=B−1A−1(\mathbf{A}\mathbf{B})^{-1}=\mathbf{B}^{-1}\mathbf{A}^{-1} of P1.B9, it is the shoes-and-socks rule: reverse the order. It extends to longer products, (ABC)⊤=C⊤B⊤A⊤(\mathbf{A}\mathbf{B}\mathbf{C})^\top=\mathbf{C}^\top\mathbf{B}^\top\mathbf{A}^\top.

The payoff you were promised. In P1.B5 and P1.B6 you saw X @ W.T for a layer applied to a data matrix whose rows are examples. If one example is the column x\mathbf{x}, the layer gives Wx\mathbf{W}\mathbf{x}. As a row, that result is (Wx)⊤=x⊤W⊤(\mathbf{W}\mathbf{x})^\top=\mathbf{x}^\top\mathbf{W}^\top by the product rule. Stack the rows xi⊤\mathbf{x}_i^\top into X\mathbf{X} and you get XW⊤\mathbf{X}\mathbf{W}^\top, whose row ii is the output for example ii. With W=A\mathbf{W}=\mathbf{A} above and X=(10−1210)\mathbf{X}=\begin{pmatrix}1&0&-1\\2&1&0\end{pmatrix} this is the (−2−2413)\begin{pmatrix}-2&-2\\4&13\end{pmatrix} of P1.B5.

Two more rules connect the transpose to earlier lessons.

Inverse. If A\mathbf{A} is invertible, so is A⊤\mathbf{A}^\top, and (A−1)⊤=(A⊤)−1(\mathbf{A}^{-1})^\top=(\mathbf{A}^\top)^{-1}. Reason: by the product rule, A⊤(A−1)⊤=(A−1A)⊤=I⊤=I\mathbf{A}^\top(\mathbf{A}^{-1})^\top=(\mathbf{A}^{-1}\mathbf{A})^\top=\mathbf{I}^\top=\mathbf{I}, so (A−1)⊤(\mathbf{A}^{-1})^\top is the inverse of A⊤\mathbf{A}^\top. It is the rule P1.B9 checked on A=(2−112)\mathbf{A}=\begin{pmatrix}2&-1\\1&2\end{pmatrix}.

Determinant. det⁡A⊤=det⁡A\det\mathbf{A}^\top=\det\mathbf{A}. For a 2×22\times2 matrix, ad−cbad-cb is the same number as ad−bcad-bc. The general case is the rule stated in P1.B8. It means that a matrix and its transpose stretch areas and volumes by the same factor, and also that a matrix and its transpose have the same rank (P1.B7).

Intuition

To undo “put on socks, then shoes” you take off the shoes first. The transpose reverses a product in the same way, but for a different reason: it is not undoing anything, it is reading the whole table of products from the other side. The row-times-column pairs that make up AB\mathbf{A}\mathbf{B} are the same pairs that make up B⊤A⊤\mathbf{B}^\top\mathbf{A}^\top, read in the other order.

Math

Entry-wise: ((AB)⊤)ij=(AB)ji=∑kAjkBki=∑k(B⊤)ik(A⊤)kj=(B⊤A⊤)ij((\mathbf{A}\mathbf{B})^\top)_{ij}=(\mathbf{A}\mathbf{B})_{ji}=\sum_kA_{jk}B_{ki}=\sum_k(\mathbf{B}^\top)_{ik}(\mathbf{A}^\top)_{kj}=(\mathbf{B}^\top\mathbf{A}^\top)_{ij}.

Not rules: (AB)⊤≠A⊤B⊤(\mathbf{A}\mathbf{B})^\top\ne\mathbf{A}^\top\mathbf{B}^\top in general, and A⊤≠A−1\mathbf{A}^\top\ne\mathbf{A}^{-1} in general.

Code

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[1, 0], [2, 1], [0, 3]])
print(np.array_equal((A @ B).T, B.T @ A.T))   # True

A.T @ B.T also runs here (3×23\times2 times 2×32\times3 gives a 3×33\times3 matrix), but it is a different matrix, so a program that runs is not proof that you used the right order. Check .shape.

What the transpose really does: moving across a dot product

The product rule gives the transpose its meaning. Take a matrix A\mathbf{A} (m×nm\times n), a vector x\mathbf{x} in Rn\mathbb{R}^n and a vector y\mathbf{y} in Rm\mathbb{R}^m. The dot product (Ax)⋅y(\mathbf{A}\mathbf{x})\cdot\mathbf{y} is

(Ax)⊤y=x⊤A⊤y=x⋅(A⊤y)(\mathbf{A}\mathbf{x})^\top\mathbf{y}=\mathbf{x}^\top\mathbf{A}^\top\mathbf{y}=\mathbf{x}\cdot(\mathbf{A}^\top\mathbf{y})

So you can move A\mathbf{A} to the other side of a dot product if you transpose it. Check it with A\mathbf{A} and x=(1,0,−1)\mathbf{x}=(1,0,-1) from above, and y=(2,−1)\mathbf{y}=(2,-1). Then Ax=(−2,−2)\mathbf{A}\mathbf{x}=(-2,-2), and (Ax)⋅y=−4+2=−2(\mathbf{A}\mathbf{x})\cdot\mathbf{y}=-4+2=-2. On the other side, A⊤y=(2−4, 4−5, 6−6)=(−2,−1,0)\mathbf{A}^\top\mathbf{y}=(2-4,\,4-5,\,6-6)=(-2,-1,0), and x⋅(A⊤y)=−2+0+0=−2\mathbf{x}\cdot(\mathbf{A}^\top\mathbf{y})=-2+0+0=-2. Same number. Mathematicians call A⊤\mathbf{A}^\top the adjoint of A\mathbf{A} (for real matrices): the matrix that makes this identity true for every x\mathbf{x} and y\mathbf{y}.

Why backpropagation uses W⊤\mathbf{W}^\top. A layer computes y=Wx\mathbf{y}=\mathbf{W}\mathbf{x}, and later the loss LL depends on y\mathbf{y}. During backpropagation (P4.8) the gradient g=∇yL\mathbf{g}=\nabla_{\mathbf{y}}L arrives from above. It says: if y\mathbf{y} changes by a small Δy\Delta\mathbf{y}, the loss changes by about g⋅Δy\mathbf{g}\cdot\Delta\mathbf{y}. Now nudge the input by Δx\Delta\mathbf{x}. The output changes by WΔx\mathbf{W}\Delta\mathbf{x}, so the loss changes by about

g⋅(WΔx)=(W⊤g)⋅Δx\mathbf{g}\cdot(\mathbf{W}\Delta\mathbf{x})=(\mathbf{W}^\top\mathbf{g})\cdot\Delta\mathbf{x}

and a gradient with respect to x\mathbf{x} is exactly the vector that, dotted with Δx\Delta\mathbf{x}, gives the change in LL. So ∇xL=W⊤g\nabla_{\mathbf{x}}L=\mathbf{W}^\top\mathbf{g}. With W=A\mathbf{W}=\mathbf{A} above and g=(2,−1)\mathbf{g}=(2,-1) this is (−2,−1,0)(-2,-1,0). Test it: raising x2x_2 by 11 changes y\mathbf{y} by the second column of W\mathbf{W}, (2,5)(2,5), and g⋅(2,5)=4−5=−1\mathbf{g}\cdot(2,5)=4-5=-1, the second entry. (This is a first-order estimate, which is exact here because the layer is linear and LL is treated as linear in y\mathbf{y}.) The shapes agree too: g\mathbf{g} has mm entries, W⊤\mathbf{W}^\top is n×mn\times m, and the result has nn entries, one gradient per input, as it must.

That is the “swap” that P4.8 mentions: in the forward pass a signal goes through W\mathbf{W}, and in the backward pass the gradient goes through W⊤\mathbf{W}^\top. P1.C11 derives the full matrix calculus and P4.10 builds a network this way.

Symmetric matrices

A square matrix is symmetric if it equals its own transpose, A⊤=A\mathbf{A}^\top=\mathbf{A}, which means Aij=AjiA_{ij}=A_{ji}: the entries mirror across the diagonal. For example

S=(2113)\mathbf{S}=\begin{pmatrix}2&1\\1&3\end{pmatrix}

Symmetric matrices are everywhere, because many quantities are between pairs and a pair has no order:

  • A distance table: the distance from town 1 to town 2 equals the distance from 2 to 1. For towns at (0,0)(0,0), (3,4)(3,4) and (6,0)(6,0) the table is (056505650)\begin{pmatrix}0&5&6\\5&0&5\\6&5&0\end{pmatrix}.
  • A covariance matrix Σ\boldsymbol{\Sigma} (P1.D7): the covariance of feature ii with feature jj equals that of jj with ii.
  • A Hessian, the matrix of second derivatives (P1.C8), for smooth functions.
  • The matrices X⊤X\mathbf{X}^\top\mathbf{X} below.

A⊤A\mathbf{A}^\top\mathbf{A} and AA⊤\mathbf{A}\mathbf{A}^\top are symmetric for every matrix A\mathbf{A}, even a non-square one. By the product rule and (A⊤)⊤=A(\mathbf{A}^\top)^\top=\mathbf{A},

(A⊤A)⊤=A⊤(A⊤)⊤=A⊤A(\mathbf{A}^\top\mathbf{A})^\top=\mathbf{A}^\top(\mathbf{A}^\top)^\top=\mathbf{A}^\top\mathbf{A}

and the same for AA⊤\mathbf{A}\mathbf{A}^\top. There is a picture behind it. Entry (i,j)(i,j) of A⊤A\mathbf{A}^\top\mathbf{A} is (column ii of A\mathbf{A}) ⋅\cdot (column jj of A\mathbf{A}), and a dot product does not care about order. For A=(123456)\mathbf{A}=\begin{pmatrix}1&2&3\\4&5&6\end{pmatrix}:

A⊤A=(172227222936273645)AA⊤=(14323277)\begin{aligned} \mathbf{A}^\top\mathbf{A}&=\begin{pmatrix}17&22&27\\22&29&36\\27&36&45\end{pmatrix}\\ \mathbf{A}\mathbf{A}^\top&=\begin{pmatrix}14&32\\32&77\end{pmatrix} \end{aligned}

Entry (1,2)(1,2) of the first is (1,4)⋅(2,5)=22=(1,4)\cdot(2,5)=22= entry (2,1)(2,1). Notice the shapes: A⊤A\mathbf{A}^\top\mathbf{A} is 3×33\times3 (one row and column per column of A\mathbf{A}) and AA⊤\mathbf{A}\mathbf{A}^\top is 2×22\times2 (one per row). If X\mathbf{X} is a data matrix with NN examples as rows and dd features as columns, then X⊤X\mathbf{X}^\top\mathbf{X} is d×dd\times d (features against features) and XX⊤\mathbf{X}\mathbf{X}^\top is N×NN\times N (every example against every other, a Gram matrix of dot products). For X=(10−1210)\mathbf{X}=\begin{pmatrix}1&0&-1\\2&1&0\end{pmatrix} the second one is (2225)\begin{pmatrix}2&2\\2&5\end{pmatrix}. For data whose columns have mean 00, X⊤X\mathbf{X}^\top\mathbf{X} divided by N−1N-1 (or by NN, depending on the convention) is the covariance matrix. Least squares (P1.B11 and P3.A2) leads to the normal equations X⊤X w=X⊤y\mathbf{X}^\top\mathbf{X}\,\mathbf{w}=\mathbf{X}^\top\mathbf{y}, a linear system whose matrix is symmetric.

Two cautions. A product of two symmetric matrices is usually not symmetric, because (ST)⊤=T⊤S⊤=TS(\mathbf{S}\mathbf{T})^\top=\mathbf{T}^\top\mathbf{S}^\top=\mathbf{T}\mathbf{S}, which equals ST\mathbf{S}\mathbf{T} only when they commute (P1.B6). For S=(1221)\mathbf{S}=\begin{pmatrix}1&2\\2&1\end{pmatrix} and T=(1002)\mathbf{T}=\begin{pmatrix}1&0\\0&2\end{pmatrix}, ST=(1422)\mathbf{S}\mathbf{T}=\begin{pmatrix}1&4\\2&2\end{pmatrix}. And an attention score matrix QK⊤\mathbf{Q}\mathbf{K}^\top (P7.3) is not symmetric in general, because the queries and the keys are different matrices. (Its transpose is KQ⊤\mathbf{K}\mathbf{Q}^\top.)

Go deeper Positive semi-definite: AᵀA never makes a negative bowl

For any A\mathbf{A} and any vector x\mathbf{x},

x⊤(A⊤A)x=(Ax)⊤(Ax)=∥Ax∥2≥0\begin{aligned} \mathbf{x}^\top(\mathbf{A}^\top\mathbf{A})\mathbf{x}&=(\mathbf{A}\mathbf{x})^\top(\mathbf{A}\mathbf{x})\\ &=\lVert\mathbf{A}\mathbf{x}\rVert^2\ge0 \end{aligned}

A symmetric matrix S\mathbf{S} with x⊤Sx≥0\mathbf{x}^\top\mathbf{S}\mathbf{x}\ge0 for every x\mathbf{x} is called positive semi-definite. So A⊤A\mathbf{A}^\top\mathbf{A} is always positive semi-definite. A covariance matrix is too: for a vector x\mathbf{x} of length 11, x⊤Σx\mathbf{x}^\top\boldsymbol{\Sigma}\mathbf{x} is the variance of the data projected on that direction, and a variance cannot be negative (P1.D7). P1.C8 uses the same idea to tell bowls from saddles. Here it is only a preview. For A=(123456)\mathbf{A}=\begin{pmatrix}1&2&3\\4&5&6\end{pmatrix} and x=(1,−2,1)\mathbf{x}=(1,-2,1): Ax=(0,0)\mathbf{A}\mathbf{x}=(0,0), so x⊤A⊤Ax=0\mathbf{x}^\top\mathbf{A}^\top\mathbf{A}\mathbf{x}=0: the value 00 is allowed, and A⊤A\mathbf{A}^\top\mathbf{A} has no inverse here (rank 22 for a 3×33\times3 matrix).

Go deeper Every square matrix is a symmetric part plus a skew part

For any square M\mathbf{M}, M=12(M+M⊤)+12(M−M⊤)\mathbf{M}=\tfrac12(\mathbf{M}+\mathbf{M}^\top)+\tfrac12(\mathbf{M}-\mathbf{M}^\top). The first piece is symmetric, and the second is skew-symmetric (K⊤=−K\mathbf{K}^\top=-\mathbf{K}, so its diagonal is 00). For M=(1243)\mathbf{M}=\begin{pmatrix}1&2\\4&3\end{pmatrix} the pieces are (1333)\begin{pmatrix}1&3\\3&3\end{pmatrix} and (0−110)\begin{pmatrix}0&-1\\1&0\end{pmatrix}. The skew piece here is the 90∘90^\circ rotation of P1.B5. A symmetric matrix stretches along perpendicular directions (the next box), and a 2×22\times2 skew-symmetric matrix is a quarter turn times a number. Also: the inverse of an invertible symmetric matrix is symmetric, since (S−1)⊤=(S⊤)−1=S−1(\mathbf{S}^{-1})^\top=(\mathbf{S}^\top)^{-1}=\mathbf{S}^{-1}.

Go deeper Why symmetric matrices are so well behaved (a preview of eigenvalues)

P1.B12 proves the spectral theorem: a real symmetric matrix has real eigenvalues and a set of perpendicular eigenvectors, so it stretches space along perpendicular directions and does nothing else. The symmetric matrix (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix} of P1.B8 stretches the direction (1,1)(1,1) by 33 and the perpendicular direction (1,−1)(1,-1) by 11. That is why PCA (P3.C3) can find perpendicular directions of largest variance in a covariance matrix. A non-symmetric matrix can have eigenvectors that are not perpendicular, or even no real ones (a rotation).

Orthogonal matrices

Now the second special family. From P1.B3, two vectors are orthogonal (perpendicular) if their dot product is 00. A set of vectors is orthonormal if they are orthogonal to each other and each has length 11 (“norm” 11, hence “ortho-normal”). The vectors (1,0)(1,0) and (0,1)(0,1) are orthonormal, and so are (0.6,0.8)(0.6,0.8) and (−0.8,0.6)(-0.8,0.6).

A square matrix Q\mathbf{Q} is orthogonal if its columns are orthonormal. There is a one-line test using the transpose. Entry (i,j)(i,j) of Q⊤Q\mathbf{Q}^\top\mathbf{Q} is (column ii) ⋅\cdot (column jj) =qi⋅qj=\mathbf{q}_i\cdot\mathbf{q}_j, which is 11 if i=ji=j (unit length) and 00 if i≠ji\ne j (perpendicular). So

Q is orthogonal  ⟺  Q⊤Q=I\mathbf{Q}\text{ is orthogonal}\iff\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}

The rotation by 30∘30^\circ is the example from P1.B5. Its columns (0.866,0.5)(0.866,0.5) and (−0.5,0.866)(-0.5,0.866) have length 0.8662+0.52=1\sqrt{0.866^2+0.5^2}=1 and dot product −0.433+0.433=0-0.433+0.433=0. For any angle, each column has length cos⁡2θ+sin⁡2θ=1\sqrt{\cos^2\theta+\sin^2\theta}=1 and the two columns have dot product −cos⁡θsin⁡θ+sin⁡θcos⁡θ=0-\cos\theta\sin\theta+\sin\theta\cos\theta=0, so every rotation matrix is orthogonal. The widget lets you turn, mirror and break it.

Rotation and reflection keep lengths and angles

−4−224−4−224uvQuQv

Rotation by 30° (det = 1): the F is turned, not mirrored. Lengths and the angle between u and v do not change.

Q = 0.866−0.50.50.866
Qᵀ = 0.8660.5−0.50.866
QᵀQ = 1001

QᵀQ equals I (the identity): the columns are perpendicular and have length 1. det Q = 1

Lengths: ‖u‖ = 3.162 → ‖Qu‖ = 3.162; ‖v‖ = 2.236 → ‖Qv‖ = 2.236
Angle between the two vectors: 45° before → 45° after
Dot product: u·v = 5 → Qu·Qv = 5

Dashed: the original vectors u and v (drag their rings), the circle through u and the “F”. Solid: where the matrix puts them. In “any matrix” mode the entries are limited to −2…2 and shown rounded to 3 decimals.

Orthogonal warp. Two vectors u (blue) and v (pink) start at (3, 1) and (1, 2); drag their rings, or tab to one and use the arrow keys. In “Rotation or reflection” mode the angle slider turns the plane and the checkbox adds a mirror. Dashed shapes are the originals (the vectors, the circle through u and an F), solid shapes are where the matrix puts them. The readouts show Q, its transpose, QᵀQ, the determinant, the lengths, the angle between the vectors and their dot product, before and after. “Any matrix” mode lets you set the four entries to compare: the circle becomes an ellipse and lengths change.

Things to try:

  1. At the start, θ=30∘\theta=30^\circ. u=(3,1)\mathbf{u}=(3,1) lands at (2.098,2.366)(2.098, 2.366) and its length stays 3.1623.162. Drag the slider: the solid circle passes through the moving Qu\mathbf{Q}\mathbf{u} at every angle, and the readouts (the lengths, the angle 45∘45^\circ and the dot product 55) never change. Q⊤Q\mathbf{Q}^\top\mathbf{Q} is the identity for every angle.
  2. Press 0°, 90°, 180° and 360°. 0∘0^\circ and 360∘360^\circ give Q=I\mathbf{Q}=\mathbf{I}, and 180∘180^\circ gives Q=−I\mathbf{Q}=-\mathbf{I}: every vector goes to its opposite, yet lengths are still kept.
  3. Tick Reflect. The F comes out mirrored, the determinant is −1-1, and lengths, angle and dot product are still unchanged. Q\mathbf{Q} is now a symmetric matrix as well (a reflection is its own inverse).
  4. Compare Q⊤\mathbf{Q}^\top with Q\mathbf{Q} for a rotation: Q⊤\mathbf{Q}^\top is the rotation by −θ-\theta, so it undoes Q\mathbf{Q} (P1.B9).
  5. Press Any matrix (for contrast). The start matrix is not orthogonal: A⊤A\mathbf{A}^\top\mathbf{A} is not I\mathbf{I}, the circle becomes an ellipse and lengths change.
  6. Press Perpendicular columns, length √2. The columns (1,1)(1,1) and (−1,1)(-1,1) are perpendicular, but the matrix is not orthogonal: A⊤A=2I\mathbf{A}^\top\mathbf{A}=2\mathbf{I} and every length is multiplied by 2\sqrt2.
  7. Press Rotation with rounded entries (a rotation by about 30∘30^\circ with entries rounded to two decimals). The status says “almost orthogonal”, because A⊤A≠I\mathbf{A}^\top\mathbf{A}\ne\mathbf{I} by a hair. Orthogonality is exact: it is true or it is not, and rounding breaks it slightly.

Intuition

An orthogonal matrix moves a rigid object: a coin on a table can be turned (rotation) or flipped over (reflection), but it is not stretched, squashed or sheared. Distances between all points, and so all angles, stay the same. The only things that change are position and orientation.

Where the picture misleads: in higher dimensions there are many more ways to turn than “by an angle θ\theta”. A rotation in 3-D has an axis, and an orthogonal matrix in 100-D can mix all the directions at once. The facts below hold in every dimension, and the 2-D picture is only the easiest one to draw.

Math

Q\mathbf{Q} (n×nn\times n) is orthogonal   ⟺  Q⊤Q=I  ⟺  \iff\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}\iff the columns are an orthonormal set. Then Q\mathbf{Q} is invertible with Q−1=Q⊤\mathbf{Q}^{-1}=\mathbf{Q}^\top, so also QQ⊤=I\mathbf{Q}\mathbf{Q}^\top=\mathbf{I} (the rows are orthonormal too), and:

∥Qx∥=∥x∥(Qx)⋅(Qy)=x⋅ydet⁡Q=±1\begin{aligned} \lVert\mathbf{Q}\mathbf{x}\rVert&=\lVert\mathbf{x}\rVert\\ (\mathbf{Q}\mathbf{x})\cdot(\mathbf{Q}\mathbf{y})&=\mathbf{x}\cdot\mathbf{y}\\ \det\mathbf{Q}&=\pm1 \end{aligned}

2-D rotation by θ\theta (det⁡=1\det=1) and reflection (det⁡=−1\det=-1):

(cos⁡θ−sin⁡θsin⁡θcos⁡θ)\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}(cos⁡θsin⁡θsin⁡θ−cos⁡θ)\begin{pmatrix}\cos\theta&\sin\theta\\\sin\theta&-\cos\theta\end{pmatrix}

Code

import numpy as np

t = np.radians(30)
Q = np.array([[np.cos(t), -np.sin(t)], [np.sin(t), np.cos(t)]])
print(np.allclose(Q.T @ Q, np.eye(2)))   # True

Compare with np.eye(2) using np.allclose rather than ==: floating-point rounding leaves tiny errors.

Why lengths, dot products and angles are kept

The proof is two lines. Write the length with a dot product (P1.B3 and P1.B4), ∥v∥2=v⊤v\lVert\mathbf{v}\rVert^2=\mathbf{v}^\top\mathbf{v}, and use Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}:

∥Qx∥2=(Qx)⊤(Qx)=x⊤Q⊤Q x=x⊤x=∥x∥2\begin{aligned} \lVert\mathbf{Q}\mathbf{x}\rVert^2&=(\mathbf{Q}\mathbf{x})^\top(\mathbf{Q}\mathbf{x})=\mathbf{x}^\top\mathbf{Q}^\top\mathbf{Q}\,\mathbf{x}\\ &=\mathbf{x}^\top\mathbf{x}=\lVert\mathbf{x}\rVert^2 \end{aligned}

The same line with two vectors gives (Qx)⋅(Qy)=x⊤Q⊤Q y=x⋅y(\mathbf{Q}\mathbf{x})\cdot(\mathbf{Q}\mathbf{y})=\mathbf{x}^\top\mathbf{Q}^\top\mathbf{Q}\,\mathbf{y}=\mathbf{x}\cdot\mathbf{y}. Since the angle between two vectors comes from cos⁡θ=a⋅b∥a∥∥b∥\cos\theta=\dfrac{\mathbf{a}\cdot\mathbf{b}}{\lVert\mathbf{a}\rVert\lVert\mathbf{b}\rVert} (P1.B3) and all three quantities are unchanged, the angle is unchanged. Check with the widget’s numbers: Q(3,1)=(2.098,2.366)\mathbf{Q}(3,1)=(2.098,2.366) has 2.0982+2.3662=10.02.098^2+2.366^2=10.0, and the dot product of Q(3,1)\mathbf{Q}(3,1) and Q(1,2)\mathbf{Q}(1,2) is still 55, giving the angle 45∘45^\circ before and after. It also explains why every distance is kept: the distance between x\mathbf{x} and y\mathbf{y} is ∥x−y∥\lVert\mathbf{x}-\mathbf{y}\rVert, and ∥Qx−Qy∥=∥Q(x−y)∥=∥x−y∥\lVert\mathbf{Q}\mathbf{x}-\mathbf{Q}\mathbf{y}\rVert=\lVert\mathbf{Q}(\mathbf{x}-\mathbf{y})\rVert=\lVert\mathbf{x}-\mathbf{y}\rVert.

The converse is also true: a square matrix that keeps the length of every vector is orthogonal (the dot products follow from the lengths). So for square matrices “orthogonal” and “preserves lengths” are the same property. (A tall matrix can keep lengths too, without being orthogonal: see the box on tall matrices below.)

The inverse is the transpose

Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I} says that Q⊤\mathbf{Q}^\top is a left inverse of Q\mathbf{Q}, and for a square matrix a left inverse is the inverse (P1.B9). So

Q−1=Q⊤\mathbf{Q}^{-1}=\mathbf{Q}^\top

This is the cheapest inverse there is: no elimination, just a flip. It is also the promise of P1.B9: the inverse of the rotation by θ\theta is the rotation by −θ-\theta, which is the transpose, because cos⁡(−θ)=cos⁡θ\cos(-\theta)=\cos\theta and sin⁡(−θ)=−sin⁡θ\sin(-\theta)=-\sin\theta swap the two sin⁡\sin entries. Because QQ⊤=I\mathbf{Q}\mathbf{Q}^\top=\mathbf{I} as well, the rows are orthonormal too. And the determinant: det⁡(Q⊤Q)=det⁡Q⊤det⁡Q=(det⁡Q)2=1\det(\mathbf{Q}^\top\mathbf{Q})=\det\mathbf{Q}^\top\det\mathbf{Q}=(\det\mathbf{Q})^2=1, so det⁡Q=±1\det\mathbf{Q}=\pm1, as P1.B8 stated: areas and volumes are kept (∣det⁡∣=1|\det|=1), and the sign tells whether the space is flipped.

Every 2×22\times2 orthogonal matrix is a rotation or a reflection

Let Q\mathbf{Q} be 2×22\times2 and orthogonal. Its first column is a unit vector, so it is (cos⁡θ,sin⁡θ)(\cos\theta,\sin\theta) for some angle θ\theta. The second column is a unit vector perpendicular to the first, and in the plane there are exactly two of those: (−sin⁡θ,cos⁡θ)(-\sin\theta,\cos\theta) and its opposite (sin⁡θ,−cos⁡θ)(\sin\theta,-\cos\theta). So

Q=(cos⁡θ−sin⁡θsin⁡θcos⁡θ)\mathbf{Q}=\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}

or

Q=(cos⁡θsin⁡θsin⁡θ−cos⁡θ)\mathbf{Q}=\begin{pmatrix}\cos\theta&\sin\theta\\\sin\theta&-\cos\theta\end{pmatrix}

The first is the rotation by θ\theta with det⁡=cos⁡2θ+sin⁡2θ=1\det=\cos^2\theta+\sin^2\theta=1. The second has det⁡=−cos⁡2θ−sin⁡2θ=−1\det=-\cos^2\theta-\sin^2\theta=-1: it is a reflection in the line through the origin at angle θ/2\theta/2 (the widget’s checkbox draws it as “flip the yy-axis, then turn by θ\theta”). Points on that line stay where they are, and points perpendicular to it are sent to their opposites. A reflection is symmetric (Q⊤=Q\mathbf{Q}^\top=\mathbf{Q}) and is its own inverse (Q2=I\mathbf{Q}^2=\mathbf{I}). For example θ=90∘\theta=90^\circ gives (0110)\begin{pmatrix}0&1\\1&0\end{pmatrix}, the swap of the two coordinates: a mirror in the line y=xy=x. In higher dimensions the picture is the same: det⁡=+1\det=+1 means a rotation, det⁡=−1\det=-1 means a reflection (possibly combined with a rotation).

More facts about orthogonal matrices

  • Products. If Q1\mathbf{Q}_1 and Q2\mathbf{Q}_2 are orthogonal, so is Q1Q2\mathbf{Q}_1\mathbf{Q}_2: (Q1Q2)⊤(Q1Q2)=Q2⊤Q1⊤Q1Q2=Q2⊤Q2=I(\mathbf{Q}_1\mathbf{Q}_2)^\top(\mathbf{Q}_1\mathbf{Q}_2)=\mathbf{Q}_2^\top\mathbf{Q}_1^\top\mathbf{Q}_1\mathbf{Q}_2=\mathbf{Q}_2^\top\mathbf{Q}_2=\mathbf{I}. A rotation followed by a reflection is orthogonal, and two rotations make a rotation by the sum of the angles. The inverse and the transpose of an orthogonal matrix are orthogonal too.
  • Permutation matrices. A matrix that only reorders coordinates, such as (010001100)\begin{pmatrix}0&1&0\\0&0&1\\1&0&0\end{pmatrix} (which sends (10,20,30)(10,20,30) to (20,30,10)(20,30,10)), has one 11 in each row and column and zeros elsewhere. Its columns are orthonormal, so it is orthogonal. Reordering never changes a length.
  • The name trap. “Orthogonal matrix” means orthonormal columns: perpendicular and of length 1. A matrix with perpendicular columns of other lengths is not orthogonal. D=(2001)\mathbf{D}=\begin{pmatrix}2&0\\0&1\end{pmatrix} has perpendicular columns but D⊤D=(4001)≠I\mathbf{D}^\top\mathbf{D}=\begin{pmatrix}4&0\\0&1\end{pmatrix}\ne\mathbf{I}, and it stretches (1,0)(1,0) to length 22. The matrix N=(1−111)\mathbf{N}=\begin{pmatrix}1&-1\\1&1\end{pmatrix} has N⊤N=2I\mathbf{N}^\top\mathbf{N}=2\mathbf{I}: dividing by 2\sqrt2 gives the orthogonal rotation by 45∘45^\circ. Remember: orthogonal matrices keep lengths; matrices with merely orthogonal columns can stretch. The name is unlucky, and some books say “orthonormal matrix”, but “orthogonal matrix” is the standard term.
  • Coordinates are dot products. If q1,…,qn\mathbf{q}_1,\dots,\mathbf{q}_n are the columns of Q\mathbf{Q}, they form an orthonormal basis (P1.B7), and the coordinates c\mathbf{c} of a vector x\mathbf{x} in that basis satisfy Qc=x\mathbf{Q}\mathbf{c}=\mathbf{x}. In a general basis you must solve this system (P1.B9). Here c=Q⊤x\mathbf{c}=\mathbf{Q}^\top\mathbf{x}: each coordinate is one dot product, ci=qi⋅xc_i=\mathbf{q}_i\cdot\mathbf{x}, and x=∑iciqi\mathbf{x}=\sum_ic_i\mathbf{q}_i. This is the payoff promised in P1.B7. Example: q1=(0.6,0.8)\mathbf{q}_1=(0.6,0.8), q2=(−0.8,0.6)\mathbf{q}_2=(-0.8,0.6) and x=(2,1)\mathbf{x}=(2,1) give c=(0.6⋅2+0.8⋅1, −0.8⋅2+0.6⋅1)=(2,−1)\mathbf{c}=(0.6\cdot2+0.8\cdot1,\ -0.8\cdot2+0.6\cdot1)=(2,-1), and indeed 2q1−q2=(1.2+0.8, 1.6−0.6)=(2,1)2\mathbf{q}_1-\mathbf{q}_2=(1.2+0.8,\ 1.6-0.6)=(2,1). The coordinates have the same length as x\mathbf{x}: 22+12=5\sqrt{2^2+1^2}=\sqrt5.
  • Numerically the best. All stretch factors of Q\mathbf{Q} are 11, so its condition number (largest stretch over smallest, P1.B9) is 11: solving with Q\mathbf{Q} or multiplying by Q\mathbf{Q} does not magnify errors. That is one reason software likes orthogonal matrices.
Go deeper Tall matrices with orthonormal columns

A non-square matrix can have orthonormal columns too. The 3×23\times2 matrix T=13(122−221)\mathbf{T}=\tfrac13\begin{pmatrix}1&2\\2&-2\\2&1\end{pmatrix} has columns 13(1,2,2)\tfrac13(1,2,2) and 13(2,−2,1)\tfrac13(2,-2,1): each has length 139=1\tfrac13\sqrt9=1, and the dot product is 19(2−4+2)=0\tfrac19(2-4+2)=0 (the pair from P1.B3). So T⊤T=I2\mathbf{T}^\top\mathbf{T}=\mathbf{I}_2, and T\mathbf{T} keeps lengths: ∥Tx∥=∥x∥\lVert\mathbf{T}\mathbf{x}\rVert=\lVert\mathbf{x}\rVert, by the same two-line proof. For x=(3,4)\mathbf{x}=(3,4), Tx=13(11,−2,10)\mathbf{T}\mathbf{x}=\tfrac13(11,-2,10), of length 55. But TT⊤\mathbf{T}\mathbf{T}^\top is 3×33\times3 and not the identity:

TT⊤=19(5−24−282425)\mathbf{T}\mathbf{T}^\top=\frac19\begin{pmatrix}5&-2&4\\-2&8&2\\4&2&5\end{pmatrix}

Its trace is 22, not 33. This matrix is a projection, which sends every vector of R3\mathbb{R}^3 onto the plane spanned by the two columns (P1.B11). The inverse Q−1=Q⊤\mathbf{Q}^{-1}=\mathbf{Q}^\top needs a square matrix: for a tall matrix, T⊤\mathbf{T}^\top is only a left inverse. (A wide matrix cannot have more orthonormal columns than rows, since nn perpendicular vectors need at least nn dimensions.)

Go deeper Orthonormal bases from any basis: Gram–Schmidt and QR

Given any independent columns (P1.B7), you can build orthonormal ones that span the same space: take the first vector and scale it to length 11; subtract from the second its component along the first and scale; and so on. This is Gram–Schmidt. Written as a matrix product it is the QR factorization M=QR\mathbf{M}=\mathbf{Q}\mathbf{R}, where Q\mathbf{Q} has orthonormal columns and R\mathbf{R} is upper triangular. In NumPy, np.linalg.qr(M) does this (it uses a different, more stable method than Gram–Schmidt, and may flip signs of columns). The projection step is the topic of P1.B11.

Go deeper Rotations in a position code: why RoPE keeps the dot product tidy

Let R(α)\mathbf{R}(\alpha) be the 2×22\times2 rotation by angle α\alpha. Two facts from this lesson, R(α)⊤=R(−α)\mathbf{R}(\alpha)^\top=\mathbf{R}(-\alpha) and “two rotations add their angles”, give

(R(α)q)⋅(R(β)k)=q⊤R(α)⊤R(β)k=q⊤R(β−α)k\begin{aligned} &(\mathbf{R}(\alpha)\mathbf{q})\cdot(\mathbf{R}(\beta)\mathbf{k})\\ &=\mathbf{q}^\top\mathbf{R}(\alpha)^\top\mathbf{R}(\beta)\mathbf{k}\\ &=\mathbf{q}^\top\mathbf{R}(\beta-\alpha)\mathbf{k} \end{aligned}

The result depends on β−α\beta-\alpha only. Rotary position embeddings (RoPE, P7.14) rotate pairs of entries of the query and key vectors by angles proportional to the token’s position (each pair turning at its own rate), so the attention score depends on the offset between two positions rather than on the positions themselves. A numeric check with q=(1,2)\mathbf{q}=(1,2), k=(3,1)\mathbf{k}=(3,1) and 20∘20^\circ per position: positions (2,5)(2,5), (7,10)(7,10) and (0,3)(0,3) all have offset 33 and all give the score 6.8306.830. And since rotations keep lengths, the rotation never changes the size of q\mathbf{q} or k\mathbf{k}. (This shows the mathematical reason, not the details of any particular model.)

Go deeper Orthogonal matrices in a deep linear chain

Stack LL layers x↦QL⋯Q2Q1x\mathbf{x}\mapsto\mathbf{Q}_L\cdots\mathbf{Q}_2\mathbf{Q}_1\mathbf{x} with every Qℓ\mathbf{Q}_\ell orthogonal. The product is orthogonal, so the length of the signal is exactly the same at the end: ∥QL⋯Q1x∥=∥x∥\lVert\mathbf{Q}_L\cdots\mathbf{Q}_1\mathbf{x}\rVert=\lVert\mathbf{x}\rVert. Compare a chain whose matrices stretch every length by 1.11.1: after 5050 layers the factor is 1.150≈1171.1^{50}\approx117, and for 0.90.9 it is 0.950≈0.0050.9^{50}\approx0.005. Lengths explode or vanish exponentially unless every layer keeps every length, as orthogonal matrices do. The backward pass multiplies by Q⊤\mathbf{Q}^\top at each layer (see above), which is also orthogonal, so the gradient’s length is kept the same way. That is the reasoning behind orthogonal initialization, which starts a weight matrix as a random orthogonal matrix, and behind the vanishing and exploding gradients of P4.16. Real networks have nonlinear activations between the layers, which break the exact statement, so this is a good starting point, not a guarantee.

Try it in NumPy

The first cell checks the product rule, the symmetry of A⊤A\mathbf{A}^\top\mathbf{A}, the dot-product identity and the 1-D trap.

Try it: transpose, symmetric and the 1-D trap

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

What to expect: the two sides of the product rule agree (True), A⊤A\mathbf{A}^\top\mathbf{A} is the 3×33\times3 matrix above and is symmetric, and both sides of the dot-product identity are −2-2. The 1-D line is the trap: for a 1-D array v.T is v’s own shape, (3,). NumPy does not know whether a 1-D array is a row or a column, so there is nothing to flip. (That is also why v @ v is the dot product, a number.) To get a real column or row, make the array 2-D with v.reshape(-1, 1) or v[:, None]. Beware a silent bug: np.outer(v, v) is 3×33\times3, not the number 1414. Check .shape when the transpose seems not to work.

The second cell checks the orthogonal matrix from the widget, the name trap, the tall matrix, and np.linalg.qr.

Try it: orthogonal matrices

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

What to expect: every line that tests Q⊤Q\mathbf{Q}^\top\mathbf{Q} prints True, the length stays 3.1623.162, det⁡Q\det\mathbf{Q} and the condition number print 1.01.0 (when this lesson was checked; the last digits of floating-point results can differ between machines, which is why the cell rounds and uses np.allclose). N⊤N\mathbf{N}^\top\mathbf{N} is 2I2\mathbf{I}, the tall matrix has T^T T = I true and T T^T = I false with trace 22, and np.linalg.qr returns a Q\mathbf{Q} with orthonormal columns and an R\mathbf{R} with QR=M\mathbf{Q}\mathbf{R}=\mathbf{M}. Test orthogonality with np.allclose(Q.T @ Q, np.eye(n)) rather than ==, because rounding leaves tiny errors.

Where you will meet it next

  • Backward pass (P4.8, P4.10, P1.C11). A layer y=Wx\mathbf{y}=\mathbf{W}\mathbf{x} sends gradients back with W⊤\mathbf{W}^\top. If W\mathbf{W} is orthogonal, then the backward map W⊤\mathbf{W}^\top is exactly W−1\mathbf{W}^{-1}, and it keeps the length of the gradient.
  • Covariance and PCA (P1.D7, P3.C3). A covariance matrix is symmetric, so its eigenvectors can be chosen perpendicular, and the principal directions of PCA form an orthonormal set.
  • Attention (P7.3). The scores QK⊤\mathbf{Q}\mathbf{K}^\top are all pairs of dot products in one matrix product. (Here Q\mathbf{Q} means the matrix of queries, not an orthogonal matrix.)
  • Weight tying. Several language models use the same matrix twice: the embedding matrix E\mathbf{E} (one row per word) turns a word into a vector by picking its row, and the output layer reuses the same numbers to give one score per word, hE⊤\mathbf{h}\mathbf{E}^\top for a row vector h\mathbf{h}. The transpose is what makes the shapes fit.
  • SVD (P1.B13). Every matrix can be written as “rotate or reflect, stretch along the axes, rotate or reflect again”, A=UΣV⊤\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top with U\mathbf{U} and V\mathbf{V} orthogonal.
  • Position and initialization (P7.14, P4.12). Rotary embeddings and orthogonal initialization both use the fact that an orthogonal matrix keeps lengths (see the Go deeper boxes above).
  • Arrays and axes (P1.B14). The same idea for arrays with more than two axes is that .T reverses all the axes, NumPy’s swapaxes swaps two of them, and PyTorch’s permute reorders them all; that lesson treats it.

Common mistakes

Check your understanding

  1. A\mathbf{A} is a 4×74\times7 matrix. What is the shape of A⊤A\mathbf{A}^\top\mathbf{A}?

  2. A\mathbf{A} is 2×32\times3 and B\mathbf{B} is 3×53\times5. Which expression equals (AB)⊤(\mathbf{A}\mathbf{B})^\top?

  3. Which matrix is symmetric?

  4. Which matrix is orthogonal?

  5. Q\mathbf{Q} is orthogonal and x=(3,4)\mathbf{x}=(3,4). What is ∥Qx∥\lVert\mathbf{Q}\mathbf{x}\rVert?

  6. A 2×22\times2 orthogonal matrix has determinant −1-1. What does it do?

  7. To compute Q−1\mathbf{Q}^{-1} for an orthogonal Q\mathbf{Q}, the cheapest correct step is to:

  8. A layer computes y=Wx\mathbf{y}=\mathbf{W}\mathbf{x} with W\mathbf{W} of shape 3×53\times5. During backpropagation the gradient g\mathbf{g} with respect to y\mathbf{y} arrives. How is the gradient with respect to x\mathbf{x} computed?

Exercises

  1. Write the transpose of (1−20345)\begin{pmatrix}1&-2\\0&3\\4&5\end{pmatrix} and give both shapes.
    Show answer(104−235)\begin{pmatrix}1&0&4\\-2&3&5\end{pmatrix}. The original is 3×23\times2 and its transpose is 2×32\times3.
  2. Check (AB)⊤=B⊤A⊤(\mathbf{A}\mathbf{B})^\top=\mathbf{B}^\top\mathbf{A}^\top for A=(1234)\mathbf{A}=\begin{pmatrix}1&2\\3&4\end{pmatrix} and B=(0110)\mathbf{B}=\begin{pmatrix}0&1\\1&0\end{pmatrix}.
    Show answerAB=(2143)\mathbf{A}\mathbf{B}=\begin{pmatrix}2&1\\4&3\end{pmatrix} (the columns of A\mathbf{A} swapped), so (AB)⊤=(2413)(\mathbf{A}\mathbf{B})^\top=\begin{pmatrix}2&4\\1&3\end{pmatrix}. And B⊤A⊤=(0110)(1324)=(2413)\mathbf{B}^\top\mathbf{A}^\top=\begin{pmatrix}0&1\\1&0\end{pmatrix}\begin{pmatrix}1&3\\2&4\end{pmatrix}=\begin{pmatrix}2&4\\1&3\end{pmatrix}. They agree. Here A⊤B⊤=(3142)\mathbf{A}^\top\mathbf{B}^\top=\begin{pmatrix}3&1\\4&2\end{pmatrix} is different.
  3. Make (1265)\begin{pmatrix}1&2\\6&5\end{pmatrix} into a symmetric matrix by splitting it into 12(M+M⊤)\tfrac12(\mathbf{M}+\mathbf{M}^\top) and 12(M−M⊤)\tfrac12(\mathbf{M}-\mathbf{M}^\top).
    Show answerThe symmetric part is (1445)\begin{pmatrix}1&4\\4&5\end{pmatrix} and the skew-symmetric part is (0−220)\begin{pmatrix}0&-2\\2&0\end{pmatrix}. Their sum is M\mathbf{M}.
  4. Compute X⊤X\mathbf{X}^\top\mathbf{X} and XX⊤\mathbf{X}\mathbf{X}^\top for X=(10−1210)\mathbf{X}=\begin{pmatrix}1&0&-1\\2&1&0\end{pmatrix}. Which one is 3×33\times3, and what do their entries mean?
    Show answerX⊤X=(52−1210−101)\mathbf{X}^\top\mathbf{X}=\begin{pmatrix}5&2&-1\\2&1&0\\-1&0&1\end{pmatrix} (3×33\times3, features against features: entry (i,j)(i,j) is column i⋅i\cdot column jj) and XX⊤=(2225)\mathbf{X}\mathbf{X}^\top=\begin{pmatrix}2&2\\2&5\end{pmatrix} (2×22\times2, examples against examples). Both are symmetric.
  5. For A=(2−112)\mathbf{A}=\begin{pmatrix}2&-1\\1&2\end{pmatrix} compute A⊤A\mathbf{A}^\top\mathbf{A}. Is A\mathbf{A} orthogonal? What would you divide by to get an orthogonal matrix?
    Show answerA⊤A=(5005)=5I\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}5&0\\0&5\end{pmatrix}=5\mathbf{I}. The columns are perpendicular but have length 5\sqrt5, so A\mathbf{A} is not orthogonal. Dividing by 5\sqrt5 gives an orthogonal matrix (a rotation by about 26.6∘26.6^\circ).
  6. Show that (0.60.80.8−0.6)\begin{pmatrix}0.6&0.8\\0.8&-0.6\end{pmatrix} is orthogonal, find its determinant and say whether it is a rotation or a reflection.
    Show answerThe columns are (0.6,0.8)(0.6,0.8) and (0.8,−0.6)(0.8,-0.6): lengths 11 and 11, dot product 0.48−0.48=00.48-0.48=0, so Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}. The determinant is 0.6⋅(−0.6)−0.8⋅0.8=−10.6\cdot(-0.6)-0.8\cdot0.8=-1, so it is a reflection. It is symmetric, so it is its own inverse.
  7. Find the inverse of the rotation by 90∘90^\circ, (0−110)\begin{pmatrix}0&-1\\1&0\end{pmatrix}, without elimination.
    Show answerThe inverse is the transpose, (01−10)\begin{pmatrix}0&1\\-1&0\end{pmatrix}, which is the rotation by −90∘-90^\circ. Check: the product of the two is I\mathbf{I}.
  8. A vector x\mathbf{x} has length 77 and Q\mathbf{Q} is orthogonal. What are ∥Qx∥\lVert\mathbf{Q}\mathbf{x}\rVert, ∥Q⊤x∥\lVert\mathbf{Q}^\top\mathbf{x}\rVert and ∥Q2x∥\lVert\mathbf{Q}^2\mathbf{x}\rVert?
    Show answerAll three are 77: Q⊤\mathbf{Q}^\top and Q2\mathbf{Q}^2 are orthogonal too, and orthogonal matrices keep lengths.
  9. Find the coordinates of x=(3,1)\mathbf{x}=(3,1) in the orthonormal basis q1=(0.6,0.8)\mathbf{q}_1=(0.6,0.8), q2=(−0.8,0.6)\mathbf{q}_2=(-0.8,0.6).
    Show answerc1=q1⋅x=1.8+0.8=2.6c_1=\mathbf{q}_1\cdot\mathbf{x}=1.8+0.8=2.6 and c2=q2⋅x=−2.4+0.6=−1.8c_2=\mathbf{q}_2\cdot\mathbf{x}=-2.4+0.6=-1.8. Check: 2.6q1−1.8q2=(1.56+1.44, 2.08−1.08)=(3,1)2.6\mathbf{q}_1-1.8\mathbf{q}_2=(1.56+1.44,\ 2.08-1.08)=(3,1), and 2.62+1.82=10\sqrt{2.6^2+1.8^2}=\sqrt{10}, the length of x\mathbf{x}.
  10. Is (001100010)\begin{pmatrix}0&0&1\\1&0&0\\0&1&0\end{pmatrix} orthogonal? What is its inverse, and what does it do to (a,b,c)(a,b,c)?
    Show answerIt is a permutation matrix: each column has a single 11, in different rows, so the columns are orthonormal and Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}. It sends (a,b,c)(a,b,c) to (c,a,b)(c,a,b). The inverse is its transpose (010001100)\begin{pmatrix}0&1&0\\0&0&1\\1&0&0\end{pmatrix}, which sends (a,b,c)(a,b,c) to (b,c,a)(b,c,a) and undoes the move.
  11. A 2×22\times2 orthogonal matrix has first column (0.8,−0.6)(0.8,-0.6). Write both possibilities for the second column and say which matrix is a rotation.
    Show answerThe second column is ±(0.6,0.8)\pm(0.6,0.8). With (0.6,0.8)(0.6,0.8) the matrix is (0.80.6−0.60.8)\begin{pmatrix}0.8&0.6\\-0.6&0.8\end{pmatrix} with det⁡=0.64+0.36=1\det=0.64+0.36=1: a rotation (by about −36.9∘-36.9^\circ). With (−0.6,−0.8)(-0.6,-0.8) the determinant is −0.64−0.36=−1-0.64-0.36=-1: a reflection.
  12. Is ST\mathbf{S}\mathbf{T} symmetric when S\mathbf{S} and T\mathbf{T} are symmetric? What condition makes it so?
    Show answer(ST)⊤=T⊤S⊤=TS(\mathbf{S}\mathbf{T})^\top=\mathbf{T}^\top\mathbf{S}^\top=\mathbf{T}\mathbf{S}, so ST\mathbf{S}\mathbf{T} is symmetric exactly when ST=TS\mathbf{S}\mathbf{T}=\mathbf{T}\mathbf{S}. In general it is not: (1221)(1002)=(1422)\begin{pmatrix}1&2\\2&1\end{pmatrix}\begin{pmatrix}1&0\\0&2\end{pmatrix}=\begin{pmatrix}1&4\\2&2\end{pmatrix}.
  13. A layer has W=(120130)\mathbf{W}=\begin{pmatrix}1&2\\0&1\\3&0\end{pmatrix} and the gradient arriving at its output is g=(1,−1,2)\mathbf{g}=(1,-1,2). Find the gradient at its input.
    Show answerW⊤g\mathbf{W}^\top\mathbf{g} with W⊤=(103210)\mathbf{W}^\top=\begin{pmatrix}1&0&3\\2&1&0\end{pmatrix} is (1+0+6, 2−1+0)=(7,1)(1+0+6,\ 2-1+0)=(7,1): two entries, one per input.
  14. Code: build a random 3×33\times3 matrix with np.random.randn(3, 3), take Q, _ = np.linalg.qr(M), and check that Q.T @ Q is the identity and that np.linalg.det(Q) is ±1\pm1.
    Show answernp.allclose(Q.T @ Q, np.eye(3)) is True and abs(np.linalg.det(Q)) is about 11 (the sign can be +1+1 or −1-1 depending on the random matrix). The columns of Q are orthonormal for any random M with independent columns.

Key takeaways

  • The transpose A⊤\mathbf{A}^\top swaps rows and columns: (A⊤)ij=Aji(\mathbf{A}^\top)_{ij}=A_{ji}, and an m×nm\times n matrix becomes n×mn\times m. A column vector becomes a row vector, and the dot product is a⊤b\mathbf{a}^\top\mathbf{b}.
  • Rules: (A⊤)⊤=A(\mathbf{A}^\top)^\top=\mathbf{A}, (A+B)⊤=A⊤+B⊤(\mathbf{A}+\mathbf{B})^\top=\mathbf{A}^\top+\mathbf{B}^\top, (cA)⊤=cA⊤(c\mathbf{A})^\top=c\mathbf{A}^\top, and the order reverses for a product: (AB)⊤=B⊤A⊤(\mathbf{A}\mathbf{B})^\top=\mathbf{B}^\top\mathbf{A}^\top. Also (A−1)⊤=(A⊤)−1(\mathbf{A}^{-1})^\top=(\mathbf{A}^\top)^{-1} and det⁡A⊤=det⁡A\det\mathbf{A}^\top=\det\mathbf{A}.
  • The transpose moves a matrix across a dot product: (Ax)⋅y=x⋅(A⊤y)(\mathbf{A}\mathbf{x})\cdot\mathbf{y}=\mathbf{x}\cdot(\mathbf{A}^\top\mathbf{y}). This is why a gradient is sent backward through y=Wx\mathbf{y}=\mathbf{W}\mathbf{x} by multiplying with W⊤\mathbf{W}^\top.
  • A matrix is symmetric when A⊤=A\mathbf{A}^\top=\mathbf{A}. For every matrix A\mathbf{A}, both A⊤A\mathbf{A}^\top\mathbf{A} and AA⊤\mathbf{A}\mathbf{A}^\top are symmetric, which is why covariance matrices, Gram matrices and the matrix in the normal equations are symmetric.
  • A square matrix Q\mathbf{Q} is orthogonal when Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}: its columns are perpendicular and have length 1. Then ∥Qx∥=∥x∥\lVert\mathbf{Q}\mathbf{x}\rVert=\lVert\mathbf{x}\rVert, dot products and angles are kept, Q−1=Q⊤\mathbf{Q}^{-1}=\mathbf{Q}^\top (the cheapest inverse) and det⁡Q=±1\det\mathbf{Q}=\pm1.
  • Every 2×22\times2 orthogonal matrix is a rotation (det⁡=1\det=1) or a reflection (det⁡=−1\det=-1). Products of orthogonal matrices are orthogonal, and permutation matrices are orthogonal.
  • In an orthonormal basis the coordinates of x\mathbf{x} are just dot products, c=Q⊤x\mathbf{c}=\mathbf{Q}^\top\mathbf{x}: nothing to solve. A tall matrix can have orthonormal columns (Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}) without QQ⊤=I\mathbf{Q}\mathbf{Q}^\top=\mathbf{I}. In NumPy, .T of a 1-D array does nothing.
Found a mistake? Tell us