[R] trace of matrix product

2006-04-05 Thread Ken Knoblauch
How about ; m <- matrix(rnorm(9), ncol = 3) mm <- matrix(rnorm(9), ncol = 3) sum(sapply(1:3, function(i, m, mm) m[i, ] %*% mm[ ,i], m, mm)) ** what is the best way to calculate the trace of a product of two matrices? If A <- matrix(rnorm(48),nrow=6) B <- matrix(r

Re: [R] trace of matrix product

2006-04-05 Thread Carlos J. Gil Bellosta
It is still better to do sum( A * t(B) ) Sorry!! Carlos J. Gil Bellosta http://www.datanalytics.com Quoting Robin Hankin <[EMAIL PROTECTED]>: > Hi > > what is the best way to calculate the trace of a product > of two matrices? > > If > > A <- matrix(rnorm(48),nrow=6) > B <- matrix(rnorm(48),nr

Re: [R] trace of matrix product

2006-04-05 Thread Carlos J. Gil Bellosta
Try sum( rowSums( A * t(B)) ) Then you do not make any calculation you do not really need. Carlos J. Gil Bellosta http://www.datanalytics.com Quoting Robin Hankin <[EMAIL PROTECTED]>: > Hi > > what is the best way to calculate the trace of a product > of two matrices? > > If > > A <- matrix(rn

[R] trace of matrix product

2006-04-05 Thread Robin Hankin
Hi what is the best way to calculate the trace of a product of two matrices? If A <- matrix(rnorm(48),nrow=6) B <- matrix(rnorm(48),nrow=8) Is there a better (faster, more elegant) way to compute the trace of A%*%B than sum(diag(A %*% B))? I would call the above solution inelegant beca