Re: [R] A faster way to calculate Trace?

2006-10-27 Thread Giovanni Petris

You can cut execution time by another 50% by using crossprod. 

 n - 1000
 a - matrix(rnorm(n*n),n,n)
 b - matrix(rnorm(n*n),n,n)
 system.time(print(sum(diag(a %*% b
[1] -905.0063
[1] 8.120 0.000 8.119 0.000 0.000
 system.time(print(sum(a*t(b
[1] -905.0063
[1] 1.510 0.000 1.514 0.000 0.000
 system.time(print(crossprod(as.vector(a), as.vector(t(b)
  [,1]
[1,] -905.0063
[1] 0.700 0.000 0.705 0.000 0.000
 system.time(print(sum(diag(a %*% b %*% a %*% b
[1] 1266733
[1] 24.550  0.000 24.567  0.000  0.000
 system.time({
+ cmat - a %*% b
+ print(crossprod(as.vector(cmat), as.vector(t(cmat
+ })
[,1]
[1,] 1266733
[1] 8.930 0.010 8.941 0.000 0.000

Cheers,
Giovanni

 Date: Fri, 27 Oct 2006 08:42:11 +0800
 From: Berwin A Turlach [EMAIL PROTECTED]
 Sender: [EMAIL PROTECTED]
 Cc: r-help@stat.math.ethz.ch
 Precedence: list
 
 G'day Yongwan,
 
  YC == YONGWAN CHUN [EMAIL PROTECTED] writes:
 
 YC I want to know how to get trace of product of matrices
 YC **faster** when the matrices are really big. Unfortunately the
 YC matrices are not symmetric. If anybody know how to get the
 YC trace of it, please help me. An example is as below.  
 The first one is quite simple to speed up:
 
  n - 2500
  a - matrix(rnorm(n*n),n,n)
  b - matrix(rnorm(n*n),n,n)
  sum(diag(a %*% b))
 [1] 1890.638
 
  tb - t(b)
  sum(a*tb)
 [1] 1890.638
 
 For the second one, you may try:
 
   sum(diag(a %*% b %*% a %*% b))
 [1] 10668786
  cmat - a %*% b
  sum(cmat*t(cmat))
 [1] 10668786
 
 It gives somewhat a speedup, since you only have to multiply two huge
 matrices once instead of thrice, but I wonder whether further
 improvements are possible.
 
 Hope this helps.
 
 Cheers,
 
 Berwin
 
 == Full address 
 Berwin A Turlach  Tel.: +61 (8) 6488 3338 (secr)   
 School of Mathematics and Statistics+61 (8) 6488 3383 (self)  
 The University of Western Australia   FAX : +61 (8) 6488 1028
 35 Stirling Highway   
 Crawley WA 6009e-mail: [EMAIL PROTECTED]
 Australiahttp://www.maths.uwa.edu.au/~berwin
 
 __
 R-help@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.
 
 

-- 

 __
[  ]
[ Giovanni Petris [EMAIL PROTECTED] ]
[ Department of Mathematical Sciences  ]
[ University of Arkansas - Fayetteville, AR 72701  ]
[ Ph: (479) 575-6324, 575-8630 (fax)   ]
[ http://definetti.uark.edu/~gpetris/  ]
[__]

__
R-help@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.


[R] A faster way to calculate Trace?

2006-10-26 Thread YONGWAN CHUN
I want to know how to get trace of product of matrices **faster** when the 
matrices are really big. Unfortunately the matrices are not symmetric. If 
anybody know how to get the trace of it, please help me. An example is as below.

n - 2500
a - matrix(rnorm(n*n),n,n)
b - matrix(rnorm(n*n),n,n)
tr1 - sum(diag(a %*% b))
tr2 - sum(diag(a %*% b %*% a %*% b))

Thanks,

Yongwan Chun

__
R-help@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.


Re: [R] A faster way to calculate Trace?

2006-10-26 Thread Berwin A Turlach
G'day Yongwan,

 YC == YONGWAN CHUN [EMAIL PROTECTED] writes:

YC I want to know how to get trace of product of matrices
YC **faster** when the matrices are really big. Unfortunately the
YC matrices are not symmetric. If anybody know how to get the
YC trace of it, please help me. An example is as below.  
The first one is quite simple to speed up:

 n - 2500
 a - matrix(rnorm(n*n),n,n)
 b - matrix(rnorm(n*n),n,n)
 sum(diag(a %*% b))
[1] 1890.638

 tb - t(b)
 sum(a*tb)
[1] 1890.638

For the second one, you may try:

  sum(diag(a %*% b %*% a %*% b))
[1] 10668786
 cmat - a %*% b
 sum(cmat*t(cmat))
[1] 10668786

It gives somewhat a speedup, since you only have to multiply two huge
matrices once instead of thrice, but I wonder whether further
improvements are possible.

Hope this helps.

Cheers,

Berwin

== Full address 
Berwin A Turlach  Tel.: +61 (8) 6488 3338 (secr)   
School of Mathematics and Statistics+61 (8) 6488 3383 (self)  
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway   
Crawley WA 6009e-mail: [EMAIL PROTECTED]
Australiahttp://www.maths.uwa.edu.au/~berwin

__
R-help@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.