differt.geometry.normalize

Contents

differt.geometry.normalize#

normalize(vectors, keepdims=False)[source]#

Normalize vectors and also return their length.

This function avoids division by zero by checking vectors with zero-length, dividing by one instead.

Parameters:
Return type:

tuple[Float[Array, '*batch 3'], Float[Array, '*batch'] | Float[Array, '*batch 1']]

Returns:

The normalized vector and their length.

Examples

The following examples shows how normalization works and its special behavior at zero.

>>> from differt.geometry import (
...     normalize,
... )
>>>
>>> vector = jnp.array([1.0, 1.0, 1.0])
>>> normalize(vector)  # [1., 1., 1.] / sqrt(3), sqrt(3)
(Array([0.5773503, 0.5773503, 0.5773503], dtype=float32),
 Array(1.7320508, dtype=float32))
>>> zero = jnp.array([0.0, 0.0, 0.0])
>>> normalize(zero)  # Special behavior at 0.
(Array([0., 0., 0.], dtype=float32), Array(0., dtype=float32))