Calculates the trace of a square numeric matrix, i.e., the sum of its diagonal elements

tr(X)

Arguments

X

a numeric matrix

Value

a numeric value, the sum of diag(X)

Examples

X <- matrix(1:9, 3, 3)
tr(X)
#> [1] 15