numpy.linalg.trace#

linalg.trace(x, /, *, offset=0, dtype=None)[source]#

Returns the sum along the specified diagonals of a matrix (or a stack of matrices) x.

This function is Array API compatible, contrary to numpy.trace.

Parameters:
x(…,M,N) array_like

Input array having shape (…, M, N) and whose innermost two dimensions form MxN matrices.

offsetint, optional

Offset specifying the off-diagonal relative to the main diagonal, where:

* offset = 0: the main diagonal.
* offset > 0: off-diagonal above the main diagonal.
* offset < 0: off-diagonal below the main diagonal.
dtypedtype, optional

Data type of the returned array.

Returns:
outndarray

An array containing the traces and whose shape is determined by removing the last two dimensions and storing the traces in the last array dimension. For example, if x has rank k and shape: (I, J, K, …, L, M, N), then an output array has rank k-2 and shape: (I, J, K, …, L) where:

out[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :])

The returned array must have a data type as described by the dtype parameter above.

See also

numpy.trace