trace¶
- 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:
x (array) – input array having shape
(..., M, N)and whose innermost two dimensions formMxNmatrices. Should have a numeric data type.offset (int) –
offset specifying the off-diagonal relative to the main diagonal.
offset = 0: the main diagonal.offset > 0: off-diagonal above the main diagonal.offset < 0: off-diagonal below the main diagonal.
Default:
0.dtype (Optional[dtype]) –
data type of the returned array. If
None,if the default data type corresponding to the data type “kind” (integer, real-valued floating-point, or complex floating-point) of
xhas a smaller range of values than the data type ofx(e.g.,xhas data typeint64and the default data type isint32, orxhas data typeuint64and the default data type isint64), the returned array must have the same data type asx.if
xhas a real-valued floating-point data type, the returned array must have the default real-valued floating-point data type.if
xhas a complex floating-point data type, the returned array must have the default complex floating-point data type.if
xhas a signed integer data type (e.g.,int16), the returned array must have the default integer data type.if
xhas an unsigned integer data type (e.g.,uint16), the returned array must have an unsigned integer data type having the same number of bits as the default integer data type (e.g., if the default integer data type isint32, the returned array must have auint32data type).
If the data type (either specified or resolved) differs from the data type of
x, the input array should be cast to the specified data type before computing the sum. Default:None.Note
keyword argument is intended to help prevent data type overflows.
- 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
xhas rankkand shape(I, J, K, ..., L, M, N), then an output array has rankk-2and shape(I, J, K, ..., L)whereout[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :])
The returned array must have a data type as described by the
dtypeparameter above.
Notes
Special Cases
Let
Nequal the number of elements over which to compute the sum.If
Nis0, the sum is0(i.e., the empty 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.