Principal angles between subspaces

Companion to the foundations page Principal angles between subspaces. Everything here lives in $\mathbb R^3$ so the subspaces can be drawn: lines (dimension 1) and planes (dimension 2). Drag the 3-D view to turn it.

Two lines meet at one angle. A line and a plane meet at one angle (the line against its own shadow in the plane). Two planes have two angles, and in $\mathbb R^3$ the first is always $0^\circ$, because two planes through the origin share a line. The number of angles is $\min(p,q)$, the smaller dimension.

1. Build two subspaces and read their angles

Presets:
U (blue) V (orange) principal vectors: an arc joins $u_k$ to $v_k$
Drag to rotate. A plane is drawn as a unit square in its own basis; a line as a segment. Each principal pair $(u_k,v_k)$ is the closest pair of unit directions left after the earlier pairs are removed, and the green arc is the angle $\theta_k$ between them.
U
V
For a plane, azimuth and elevation set its normal direction; for a line, its direction.

2. The SVD recipe: the angles are in $Q_U^\top Q_V$, whatever basis you pick

The notes' boxed formula says the singular values of $Q_U^\top Q_V$ are $\cos\theta_i$, where the columns of $Q_U,Q_V$ are any orthonormal bases of the two subspaces. Spin the basis inside each subspace and watch the matrix change while its singular values stay put.

The spin only re-chooses the two basis vectors inside a plane (a line has a single unit vector, so it only flips). The drawing above does not move; the matrix entries do; the singular values $\sigma_i=\cos\theta_i$ do not.

3. The accuracy trap: cosines forget small angles

Take two lines in the plane at a tiny angle $\theta$: $u=(1,0)$, $v=(\cos\theta,\sin\theta)$. Recover $\theta$ two ways in ordinary double-precision arithmetic: from the cosine, $\arccos(u^\top v)$, and from the sine, $\arcsin\lVert (I-P_U)v\rVert$.

At $\theta=10^{-8}$ the cosine is $1-5\times10^{-17}$, which rounds to $1$: the angle has left the number before $\arccos$ ever sees it. The sine is $10^{-8}$ itself, stored with full relative precision. That is why Björck–Golub compute sines for small angles and cosines for large ones. (Here the sine is formed directly from two numbers, so it is exact; the notes' table sends it through an SVD and still sees about $10^{-9}$ relative error at $10^{-8}$, against $160\%$ for the cosine.)