[R] Calculating trace of products

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Mon Aug 14 14:28:18 CEST 2006


check the following:

A <- matrix(rnorm(100*100), 100, 100); A <- A + t(A)
B <- matrix(rnorm(100*100), 100, 100); B <- B + t(B)

sum(diag(A %*% B))
sum(A * B)

system.time(for(i in 1:10000) out <- sum(diag(A %*% B)))
system.time(for(i in 1:10000) out <- sum(A * B))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Søren Højsgaard" <Soren.Hojsgaard at agrsci.dk>
To: "R-help list" <r-help at stat.math.ethz.ch>
Sent: Monday, August 14, 2006 1:58 PM
Subject: [R] Calculating trace of products


Dear all,
I need to calculate tr(A B), tr(A B A B) and similar quantities 
**fast** where the matrices A, B are symmetrical. I've searched for 
built-in functions for that purpose, but without luck. Can anyone 
help?
Thanks in advance
Søren

[[alternative HTML version deleted]]




--------------------------------------------------------------------------------


> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list