mtrace | R Documentation |
Computes the trace of a square numeric matrix. If A
is not numeric and square matrix,
the function terminates with an error message.
mtrace(A)
A |
A square numeric matrix. |
the sum of the values on the diagonal of the matrix A
, i.e. sum(diag(A))
.
Valentin Todorov, valentin.todorov@chello.at
(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)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.