Skip to content
Prev 44355 / 398506 Next

Matrix mulitplication

Spencer Graves <spencer.graves at pdf.com> writes:
Thanks for pointing that out Spencer.  I was about to do the same.
A minor point on the methodology.  You can do this in one step as

system.time(A2 <- A%*%solve(crossprod(BA)+C., crossprod(t(BA), D)))

Also, in R the second and subsequent timings tend to be a bit faster
than the first.  I think this is due to heap storage being allocated
the first time that large chunks of memory are used and not needing to
be allocated for subsequent uses.
[1] 0.78 0.09 0.87 0.00 0.00
[1] 0.71 0.05 0.76 0.00 0.00
[1] 0.79 0.08 0.87 0.00 0.00
[1] 0.72 0.04 0.76 0.00 0.00
[1] 0.52 0.07 0.59 0.00 0.00
[1] 0.53 0.06 0.59 0.00 0.00
[1] 0.56 0.03 0.59 0.00 0.00
[1] 0.54 0.05 0.59 0.00 0.00
This is using R-devel (to be 1.9.0) on a 2.0 GHz Pentium-4 desktop
computer running Linux and with Goto's BLAS.  I'm not sure exactly
which of the changes from your system are resulting in the much faster
execution time but it is definitely not all due to the processor speed.
My guess is that most of the gain is due to the optimized BLAS.
Goto's BLAS are a big win on a Pentium-4 under Linux.  (Thanks to
Brian Ripley for modifying the configure script for R to accept
--with-blas=-lgoto .)

Corresponding timings on a Athlon XP 2500+ (1.83 GHz) running Linux
with Atlas are
[1] 1.29 0.04 1.34 0.00 0.00
[1] 0.88 0.06 0.95 0.00 0.00
[1] 0.79 0.05 0.85 0.00 0.00
[1] 0.82 0.04 0.87 0.00 0.00
[1] 0.61 0.06 0.67 0.00 0.00
[1] 0.66 0.02 0.69 0.00 0.00
[1] 0.51 0.10 0.61 0.00 0.00
[1] 0.59 0.10 0.71 0.00 0.00

There you can see the faster execution of the second and subsequent
timings.

I completely agree with you that using crossprod and the non-inverse
form of solve, where appropriate, helps.  However, one of the best
optimizations for numerical linear algebra calculations is the use of
optimized BLAS.  (I will avoid going in to the Linux vs Windows
comparisons :-)