[R] Help - Trace of matrices

Doran, Harold HDoran at air.org
Fri Dec 6 18:43:40 CET 2013


A fast computation I use is based on the following:

A <- matrix(rnorm(16), ncol = 4)
B <- matrix(rnorm(16), ncol = 4)
C <- A %*% B
sum(diag(C))

### This is less expensive to compute when the matrix multiplication is expensive
sum(A * t(B))

So, it just uses the elementwise calculations and sums over all cels

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Wagner Bonat
Sent: Friday, December 06, 2013 12:02 PM
To: r-help at r-project.org
Subject: [R] Help - Trace of matrices

Dear,

I need to calculate the following equation

tr(Sigma^-1 %*% D.Sigma)

I know only Sigma (positive definite) and D.Sigma (derivative of Sigma), a naive code is

sum(diag(solve(Sigma,D.Sigma)))

but these matrices are dense and big dimension (10000 x 10000), and I need to evaluate this equation many times.
What is the better way to evaluate this equation in R ?
Note that I need only the diagonal, I think is possible to calculate only the diagnonal, but how ??

--
Wagner Hugo Bonat
LEG - Laboratório de Estatística e Geoinformação UFPR - Universidade Federal do Paraná

	[[alternative HTML version deleted]]



More information about the R-help mailing list