Unit Vector¶
- unit_vector(vector)¶
Calculates the unit vector corresponding to a given vector (and therefore having the same direction)
- Parameters
vector (list of int or float) – List of numbers representing a vector
- Raises
TypeError – Argument must be a 1-dimensional list
TypeError – Elements of argument must be integers or floats
- Returns
unit – Vector with a magnitue of 1 in the same direction as the original vector
- Return type
list of float
Notes
Comparison vector: \(\mathbf{a} = \langle a_1, a_2, \cdots, a_n \rangle\)
Unit vector with same direction: \(\mathbf{u}= \frac{\mathbf{a}}{\|\mathbf{a}\|}\)
Examples
- Import unit_vector function from regressions library
>>> from regressions.vectors.unit import unit_vector
- Determine the unit vector of the vector with components [7, 5, -1]
>>> unit_3d = unit_vector([7, 5, -1]) >>> print(unit_3d) [0.8082903768654759, 0.5773502691896257, -0.11547005383792514]
- Determine the unit vector of the vector with components [3, 2]
>>> unit_2d = unit_vector([3, 2]) >>> print(unit_2d) [0.8320502943378437, 0.5547001962252291]