Computing orthogonal projections

We’ll discuss orthogonal projection at some length in class and it’s also covered in section 6.3 of our text. The purpose of this short set of notes is to illustrate how to implement orthogonal projection onto a subspace with SageMath. You might very well find this useful when doing your homework.

The formula

Our intention is to apply Proposition 6.3.16 of our text, which says

Let \(\mathbf{v}_1, \mathbf{v}_2, \ldots, \mathbf{v}_n\) be an orthonormal basis of a subspace \(V\subset\mathbb R^m\) and let \(Q\) be the matrix whose columns are those vectors, i.e. \[ Q = \begin{bmatrix} \mathbf{v}_1 & \mathbf{v}_2 & \cdots & \mathbf{v}_n\end{bmatrix}. \] Then, \(QQ^T\) is the matrix representing orthogonal projection onto \(V\).

Example 1

I suppose that the most interesting example that we can actually visualize would be the projection of point in 3D space onto a two-dimensional subspace. Suppose, for example that the subspace is spanned by the vectors \[ \mathbf{v}_1 = \begin{bmatrix} 1 \\ 2 \\ 3\end{bmatrix} \text{ and } \mathbf{v}_2 = \begin{bmatrix} 5 \\ -1 \\ -1\end{bmatrix}. \] It’s easy to check that these are orthogonal and they can be normalized by dividing by their length.

Now, suppose we’d like to project the vector \[\mathbf{u} = \frac{1}{5}\left\langle -3, -3, 1 \right\rangle\] onto that subspace. We can do so and illustrate using the following SageMath code:

Example 2

The technique works just as well in higher dimensional subspaces, though we can’t visualize it in the same way. Here’s how to compute the projection of a point in \(\mathbb R^5\) onto a three dimensional subspace, for example: