norm

ssapy.utils.norm(arr)[source][source]

Compute the Euclidean norm of an array over the last axis while preserving leading axes.

Parameters:

arrarray-like

Input array where the Euclidean norm is computed along the last axis. The array can have any shape, with the last axis representing the vector components.

Returns:

normarray-like

The Euclidean norm of the input array computed along the last axis. The output shape matches the input shape, excluding the last axis.

Notes:

  • This implementation directly computes the norm using np.sqrt and np.einsum, avoiding the intermediate step of calling normSq. This improves performance.

  • The computation uses np.einsum(”…i,…i”, arr, arr) to efficiently sum the squared components along the last axis, followed by taking the square root.

  • Faster than using np.linalg.norm() for this specific use case and generalizes well to arrays of arbitrary shape.

Example:

>>> import numpy as np
>>> arr = np.array([[1, 2, 3], [4, 5, 6]])
>>> norm(arr)
array([3.74165739, 8.77496439])