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" <[EMAIL PROTECTED]> To: "R-help list" <[email protected]> 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]] -------------------------------------------------------------------------------- > ______________________________________________ > [email protected] 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 ______________________________________________ [email protected] 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.
