Skip to contents

Computes the trace (sum of diagonal elements) of a 2-D array.

Usage

nv_trace(x)

Arguments

x

(arrayish)
One input, with exactly 2 axes. Can be any data type. An R value materializes at its default data type.

Value

(arrayish)
A scalar with x's data type, except a boolean input, which is counted at the default integer data type (see default_dtypes()).

Examples

# the diagonal is summed to a scalar
x <- nv_array(c(1, 0, 0, 0, 2, 0, 0, 0, 3), shape = c(3, 3))
nv_trace(x)
#> AnvlArray
#>  6
#> [ CPUf32{} ]