Solution (source code)

= Solution

The assumption $v\notin W^\perp$ means $p=Pv\ne0$. For every <unit vector> $w\in W$, <orthogonality> and Cauchy–Schwarz give $\langle v,w\rangle=\langle p,w\rangle\le\|p\|$. Equality is attained at
$$
\boxed{\hat w=p/\|p\|,\qquad\sup_{w\in W,\ \|w\|=1}\langle v,w\rangle=\|p\|.}
$$
Equality in the real <Cauchy-Schwarz inequality>[Cauchy–Schwarz inequality] requires $w$ to be a scalar multiple of $p$, and attaining the positive <supremum> fixes its sign; the <unit> <norm> fixes its magnitude. This proves uniqueness.