trace#
- ivy.trace(x, /, *, offset=0, axis1=0, axis2=1, out=None)[source]#
Return the sum along the specified diagonals of a matrix (or a stack of matrices)
x
.- Parameters:
x (
Union
[Array
,NativeArray
]) – input array having shape(..., M, N)
and whose innermost two dimensions formMxN
matrices. Should have a numeric data type.offset (
int
) –offset specifying the off-diagonal relative to the main diagonal. (default:
0
) -offset = 0
: the main diagonal. -offset > 0
: off-diagonal above the main diagonal. -offset < 0
: off-diagonal below the main diagonal.Default:
0
.out (
Optional
[Array
]) – optional output array, for writing the result to. It must have a shape that the (default:None
) inputs broadcast to.
- Return type:
- Returns:
ret – 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 rankk
and shape(I, J, K, ..., L, M, N)
, then an output array has rankk-2
and shape(I, J, K, ..., L)
whereout[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :])
The returned array must have the same data type as
x
.
Examples
With
ivy.Array
inputs:>>> x = ivy.array([[2., 0., 3.], ... [3., 5., 6.]]) >>> y = ivy.trace(x, offset=0) >>> print(y) ivy.array(7.)
>>> x = ivy.array([[[1., 2.], ... [3., 4.]], ... [[5., 6.], ... [7., 8.]]]) >>> y = ivy.trace(x, offset=1) >>> print(y) ivy.array([3., 4.])
With
ivy.NativeArray
inputs:>>> x = ivy.native_array([[2., 0., 3.],[3., 5., 6.]]) >>> y = ivy.trace(x, offset=0) >>> print(y) ivy.array(7.)
>>> x = ivy.native_array([[0, 1, 2], ... [3, 4, 5], ... [6, 7, 8]]) >>> y = ivy.trace(x, offset=0) >>> print(y) ivy.array(12)
With
ivy.Container
inputs:>>> x = ivy.Container( ... a = ivy.array([[7, 1, 2], ... [1, 3, 5], ... [0, 7, 4]]), ... b = ivy.array([[4, 3, 2], ... [1, 9, 5], ... [7, 0, 6]]) ... ) >>> y = ivy.trace(x, offset=0) >>> print(y) { a: ivy.array(14), b: ivy.array(19) }
>>> x = ivy.Container( ... a = ivy.array([[7, 1, 2], ... [1, 3, 5], ... [0, 7, 4]]), ... b = ivy.array([[4, 3, 2], ... [1, 9, 5], ... [7, 0, 6]]) ... ) >>> y = ivy.trace(x, offset=1) >>> print(y) { a: ivy.array(6), b: ivy.array(8) }
- Array.trace(self, /, *, offset=0, axis1=0, axis2=1, out=None)#
ivy.Array instance method variant of ivy.trace. This method Returns the sum along the specified diagonals of a matrix (or a stack of matrices).
- Parameters:
self (
Array
) – input array having shape(..., M, N)
and whose innermost two dimensions formMxN
matrices. Should have a floating-point data type.offset (
int
) – Offset of the diagonal from the main diagonal. Can be both positive and (default:0
) negative. Defaults to 0.out (
Optional
[Array
]) – optional output array, for writing the result to. It must have a shape that (default:None
) the inputs broadcast to.
- Return type:
Array
- Returns:
ret – 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 rankk
and shape(I, J, K, ..., L, M, N)
, then an output array has rankk-2
and shape(I, J, K, ..., L)
whereout[i, j, k, …, l] = trace(a[i, j, k, …, l, :, :])
The returned array must have the same data type as
x
.
Examples
>>> x = ivy.array([[1., 2.], [3., 4.]]) >>> y = x.trace() >>> print(y) ivy.array(5.)
>>> x = ivy.array([[1., 2., 4.], [6., 5., 3.]]) >>> y = ivy.Array.trace(x) >>> print(y) ivy.array(6.)
- Container.trace(self, /, *, offset=0, axis1=0, axis2=1, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#
ivy.Container instance method variant of ivy.trace. This method Returns the sum along the specified diagonals of a matrix (or a stack of matrices).
- Parameters:
self (
Container
) – input container having shape(..., M, N)
and whose innermost two dimensions formMxN
matrices. Should have a floating-point data type.offset (
int
) – Offset of the diagonal from the main diagonal. Can be both positive and (default:0
) negative. Defaults to 0.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
]]]) – The key-chains to apply or not apply the method to. Default isNone
. (default:None
)to_apply (
bool
) – If True, the method will be applied to key_chains, otherwise key_chains (default:True
) will be skipped. Default isTrue
.prune_unapplied (
bool
) – Whether to prune key_chains for which the function was not applied. (default:False
) Default isFalse
.map_sequences (
bool
) – Whether to also map method to sequences (lists, tuples). (default:False
) Default isFalse
.out (
Optional
[Container
]) – optional output array, for writing the result to. It must have a shape that (default:None
) the inputs broadcast to.
- Return type:
Container
- Returns:
ret – a container 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 rankk
and shape(I, J, K, ..., L, M, N)
, then an output array has rankk-2
and shape(I, J, K, ..., L)
whereout[i, j, k, …, l] = trace(a[i, j, k, …, l, :, :])
The returned array must have the same data type as
x
.
Examples
With
ivy.Container
input: >>> x = ivy.Container( … a = ivy.array([[7, 1, 2], … [1, 3, 5], … [0, 7, 4]]), … b = ivy.array([[4, 3, 2], … [1, 9, 5], … [7, 0, 6]])) >>> y = x.trace() >>> print(y) {a: ivy.array(14), b: ivy.array(19)
}