skspatial.objects.Vector.unit

Vector.unit() Vector[source]

Return the unit vector in the same direction as the vector.

A unit vector is a vector with a magnitude of one.

Returns:
Vector

Unit vector.

Raises:
ValueError

If the magnitude of the vector is zero.

Examples

>>> from skspatial.objects import Vector
>>> Vector([1, 0]).unit()
Vector([1., 0.])
>>> Vector([-20, 0]).unit()
Vector([-1.,  0.])
>>> Vector([1, 1]).unit().round(3)
Vector([0.707, 0.707])
>>> Vector([1, 1, 1]).unit().round(3)
Vector([0.577, 0.577, 0.577])
>>> Vector([0, 0]).unit()
Traceback (most recent call last):
...
ValueError: The magnitude must not be zero.