diagonal(input: Tensor, offset: _int = ..., dim1: _int = ..., dim2: _int = ...)Implementing kernel
C++ engine symbols that back this Python API.Extract a (off-)diagonal from each matrix in a batched input.
Returns the elements of lying on the diagonal selected by
offset from the matrix slice formed by (dim1, dim2). For
a 2-D matrix and offset=0 this is
Positive offset selects super-diagonals (); negative offsets select sub-diagonals.
Parameters
Input of shape
(*, m, n) (or higher rank).offsetint, keyword-onlyDiagonal index relative to the main diagonal. Default
0.dim1int, keyword-onlyFirst matrix dimension. Default
-2.dim2int, keyword-onlySecond matrix dimension. Default
-1.Returns
TensorDiagonal values with the two matrix axes replaced by a single axis of length .
Notes
Shares the engine kernel with the top-level lucid.diagonal;
the only difference is the keyword-only / matrix-aware defaults
(dim1=-2, dim2=-1) that match the standard linalg
convention.
Examples
>>> import lucid
>>> from lucid.linalg import diagonal
>>> A = lucid.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> diagonal(A)
Tensor([1.0000, 4.0000])
>>> diagonal(A, offset=1)
Tensor([2.0000])