Calculating trace of products

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating trace of products

Søren Højsgaard
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]]


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Calculating trace of products

RKoenker
I would suspect that something simple like

        sum(diag(crossprod(A,B)))

would be quite competitive...

url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    [hidden email]            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820


On Aug 14, 2006, at 6:58 AM, Søren Højsgaard wrote:

> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Calculating trace of products

Dimitris Rizopoulos
In reply to this post by Søren Højsgaard
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" <[hidden email]>
To: "R-help list" <[hidden email]>
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]]




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


> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.