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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Wagner Bonat
Sent: Friday, December 06, 2013 12:02 PM
To: r-help@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]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to