Learn R Programming

rrcov3way (version 0.5-0)

mtrace: The trace of a square numeric matrix

Description

Computes the trace of a square numeric matrix. If A is not numeric and square matrix, the function terminates with an error message.

Usage

mtrace(A)

Value

the sum of the values on the diagonal of the matrix A, i.e. sum(diag(A)).

Arguments

A

A square numeric matrix.

Author

Valentin Todorov, valentin.todorov@chello.at

Examples

Run this code
(a <- matrix(c(5,2,3, 4,-3,7, 4,1,2), ncol=3))
(b <- matrix(c(1,0,1, 0,1,2, 1,0,3), ncol=3))

mtrace(a)
mtrace(b)

## tr(A+B)=tr(A)+tr(B)
all.equal(mtrace(a) + mtrace(b), mtrace(a+b))

## tr(A)=tr(A')
all.equal(mtrace(a), mtrace(t(a)))

## tr(alphA)=alphatr(A)
alpha <- 0.5
all.equal(mtrace(alpha*a), alpha*mtrace(a))

##  tr(AB)=tr(BA)
all.equal(mtrace(a %*% b), mtrace(b %*% a))


##  tr(A)=tr(BAB-1)
all.equal(mtrace(a), mtrace(b %*% a %*% solve(b)))

Run the code above in your browser using DataLab