diagonal#
- class brainunit.linalg.diagonal(x, *, offset=0, **kwargs)#
Extract the diagonals of a matrix or stack of matrices.
SaiUnit implementation of
numpy.linalg.diagonal().Unlike
saiunit.math.diagonal()(which followsnumpy.diagonal()and uses the first two axes by default), this function followslinalgsemantics and extracts the diagonal of the last two axes.- Parameters:
- Returns:
out – Diagonals of shape
x.shape[:-2] + (K,)withK = min(M, N)foroffset=0. Carries the same unit as x.- Return type:
Union[Array, saiunit.Quantity]
Examples
>>> import saiunit as u >>> import jax.numpy as jnp >>> x = jnp.arange(18.).reshape(2, 3, 3) * u.meter >>> u.linalg.diagonal(x) ArrayImpl([[ 0., 4., 8.], [ 9., 13., 17.]], dtype=float32) * meter