bundles / numpy 2.4.3 / numpy / linalg / trace
_ArrayFunctionDispatcher
numpy.linalg:trace
source: /numpy/linalg/_linalg.py :3161
Signature
def trace ( x , / , offset = 0 , dtype = None ) Summary
Returns the sum along the specified diagonals of a matrix (or a stack of matrices) x.
Extended Summary
This function is Array API compatible, contrary to numpy.trace.
Parameters
x: (...,M,N) array_likeInput array having shape (..., M, N) and whose innermost two dimensions form MxN matrices.
offset: int, optionalOffset 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.
dtype: dtype, optionalData type of the returned array.
Returns
out: ndarrayAn 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.
Examples
np.linalg.trace(np.eye(3))
✗a = np.arange(8).reshape((2, 2, 2))
✓np.linalg.trace(a)
✗a = np.arange(24).reshape((3, 2, 2, 2)) np.linalg.trace(a).shape✓
a = np.arange(9).reshape((3, 3)); a
✓np.linalg.trace(a, offset=1) # First superdiagonal np.linalg.trace(a, offset=2) # Second superdiagonal np.linalg.trace(a, offset=-1) # First subdiagonal np.linalg.trace(a, offset=-2) # Second subdiagonal✗
See also
Aliases
-
numpy.linalg.trace