# Orthogonal projection - creating a matrix

I know that orthogonal projection of a vector $v$ on a subspace $W$ is given by

$\pi_W(v) = {u_1\cdot v\over u_1\cdot u_1} u_1 + \cdots + {u_k\cdot v\over u_k\cdot u_k} u_k$

where $\{u_1,..,u_k\}$ is an orthogonal basis of $W$.

I am suppose to find a matrix $B$ that projects a vector on $W$.
Now, I know that by applying the above formula with taking $v$ to be $e_1,e_2\dots$ etc. I get vectors and putting these vectors together in a matrix will give the required matrix.

But, for a dimension 2 subspace, my teacher does the following:

${1\over u\cdot u}uu^T + {1\over v\cdot v}vv^T =$ the required matrix.

where $u$ and $v$ are $u_1$ and $u_2$ respectively. How does he do this ?

PS: I'm new to the site and don't know how to do formatting yet. I've my exam in two days and I'll definitely learn how to do it after that. I'll really appericiate if someone edits my question.

All he’s done is rewrite the dot product in the numerators in a different form and rearrange the terms a bit. Let’s look at a single term for your original expression for $\pi_W(v)$: \begin{align} {u_i\cdot v\over u_i\cdot u_i}u_i &= {1 \over u_i\cdot u_i}(u_i\cdot v)u_i \\ &={1 \over u_i\cdot u_i}u_i^Tvu_i. \end{align} Since $u_i^Tv$ is a scalar, it commutes with $u_i$, so we can write this as$${1 \over u_i\cdot u_i}u_iu_i^Tv,$$ hence $$\pi_W=\sum_i{1\over u_i\cdot u_i}u_iu_i^T.$$ By the way, in other sources that I’ve seen it’s usual to expand both dot products, i.e., $$\pi_u={uu^T \over u^Tu}=u(u^Tu)^{-1}u^T.$$ It’s worth remembering that last form because if you replace the vector $u$ by the matrix $U$ which has as its columns the basis of the subspace $W$, you get the matrix for orthogonal projection onto $W$. It’s a good exercise to prove to yourself that these two expressions for $\pi_W$ are equal.