[R] Trace of product of matrices

Berend Hasselman bhh at xs4all.nl
Tue Oct 28 11:38:29 CET 2014


On 28-10-2014, at 11:05, Martin Maechler <maechler at stat.math.ethz.ch> wrote:

> …...
> Thank you, Peter, and Spencer.
> 
> For a few years now, I have had in my TODO file for the Matrix
> package:
> 
> ** TODO tr(A %*% B) {and even  tr(A %*% B %*% C) ...} are also needed
>  frequently in some computations {conditional normal distr. ...}.
>  Since this can be done faster than by
>    sum(diag(A %*% B))  even for traditional matrices, e.g.
>    	       sum(A * t(B)) or {even faster for "full" mat}
> 	       crossprod(as.vector(A), as.vector(B))


Shouldn’t that be  crossprod(as.vector(t(A)),as.vector(B)) or crossprod(as.vector(A),as.vector(t(B))) ?

Berend

>  and even more so for, e.g.  <sparse> %*% <dense>
>  {used in Soeren's 'gR' computations},
>  we should also provide a generic and methods.
> 
> ** TODO diag(A %*% B) might look like a "generalization" of tr(A %*% B),
>  but as the above tricks show, is not really.
>  Still, it's well worth to provide  diag.prod(A, B):
> 
>  Well, if A %*% B is square,   diag(A %*% B)  ===  colSums(t(A) * B)
>  and we should probably teach people about that !
> 
> -----------
> 
> Are there good suggestions for a sensible function name for
> these potential matrix utility function?
> 
>  trprod()
>  traceprod()
> 
>  diagprod()
> ?
> 
> --
> Martin <Maechler at stat.math.ethz.ch>  http://stat.ethz.ch/people/maechler
> Seminar für Statistik, ETH Zürich  HG G 16      Rämistrasse 101
> CH-8092 Zurich, SWITZERLAND
> phone: +41-44-632-3408       fax: ...-1228      <><
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list