Normalize vector (Python)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Python

Normalize vector


\widehat{u}
=
\frac{\overrightarrow{u}}{|\overrightarrow{u}|}
=
\frac{1}{|\overrightarrow{u}|} \cdot 
\begin{bmatrix} 
u_1 & u_2 & ... & u_n
\end{bmatrix}
=
\frac{1}{\sqrt{\overrightarrow{u} \cdot \overrightarrow{u}}} \cdot 
\begin{bmatrix} 
u_1 & u_2 & ... & u_n
\end{bmatrix}
=
\begin{bmatrix} 
\frac{u_1}{\sqrt{\overrightarrow{u} \cdot \overrightarrow{u}}} & 
\frac{u_2}{\sqrt{\overrightarrow{u} \cdot \overrightarrow{u}}} &
... & 
\frac{u_n}{\sqrt{\overrightarrow{u} \cdot \overrightarrow{u}}}
\end{bmatrix}

<<normalize_vector_with_vscale_and_magnitude.hs>>=
def normalize(vector):
    return vscale(1/magnitude(vector), vector)
Download code
hijacker
hijacker
hijacker
hijacker