Appendix I — The Code Emporium
Code — Angles
Use this code to compute the angle \(\theta\) between (non-zero) vectors \(\vec{v}\) and \(\vec{w}\):
\[\theta = \arccos\left( \frac{ \vec{v} \cdot \vec{w} }{ | \vec{v} | | \vec{w} | } \right)\]
Code — Projections & Scalar Components
Use this code to compute the projection of a vector \(\vec{v}\) onto a second (non-zero) vector \(\vec{u}\):
\[\text{proj}_{\vec{u}} \, \vec{v} = \left( \frac{ \vec{v} \cdot \vec{u}}{ |\vec{u} |^2} \right) \vec{u}\]
and the projection’s scalar component:
\[\text{comp}_{\vec{u}} \, \vec{v} = \frac{ \vec{v} \cdot \vec{u} }{ |\vec{u} |}\]
Code — Cross Product
Use this code to compute the cross product \(\vec{v} \times \vec{w}\):
\[\begin{align*}
\vec{v} \times \vec{w} &= \left| \begin{array}{c c } \hat{\imath} & \hat{\jmath} & \hat{k}\\ v_1 & v_2 & v_3 \\ w_1 & w_2 & w_3 \end{array} \right|\\
&= (v_2 w_3 \, - \, v_3 w_2) \, \hat{\imath} \, - \, (v_1 w_3 \, - \, v_3 w_1) \, \hat{\jmath} + (v_1 w_2 \, - \, v_2 w_1) \, \hat{k}
\end{align*}\]
and its magnitude:
\[| \vec{v} \times \vec{w} | = |\vec{v}| |\vec{w}| \sin \theta\]