trace — Python array API standard 2024.12 documentation (original) (raw)

trace(x: array, /, *, offset: int = 0, dtype: dtype | None = None) → array

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

Parameters:

Returns:

out (array) – 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.

Notes

Special Cases

Let N equal the number of elements over which to compute the sum.

For both real-valued and complex floating-point operands, special cases must be handled as if the operation is implemented by successive application of add().

Changed in version 2022.12: Added complex data type support.

Changed in version 2023.12: Required the function to return a floating-point array having the same data type as the input array when provided a floating-point array.