# [R] Matrix efficiency in 1.9.0 vs 1.8.1

Douglas Bates bates at stat.wisc.edu
Thu Apr 29 00:21:41 CEST 2004

```Stephen Ellner <spe2 at cornell.edu> writes:

> I'm seeking some advice on effectively using the new Matrix
> library in R1.9.0 for operations with large dense matrices. I'm working on
> integral operator models (implemented numerically via matrix operations)
> and except for the way entries are generated, the examples below really are
> representative of my problem sizes.
>
> My main concern is speed of large dense matrix multiplication.
> In R 1.8.1 (Windows2000 Professional, dual AthlonMP 2800)
> > a=matrix(rnorm(2500*2500),2500,2500); v=rnorm(2500);
> > system.time(a%*%v);
>  0.11 0.00 0.12   NA   NA
>
> In R 1.9.0, same platform:
> > a=matrix(rnorm(2500*2500),2500,2500); v=rnorm(2500);
> > system.time(a%*%v);
>  0.24 0.00 0.25   NA   NA
>
> These differences are consistent. But using the Matrix library
> in 1.9.0, the discrepancy disappears
> > library(Matrix);
> > a=Matrix(rnorm(2500*2500),2500,2500); v=Matrix(rnorm(2500),2500,1);
> > system.time(a%*%v);
>  0.11 0.00 0.11   NA   NA
>
> The problem is
> > b=a/3
> Error in a/3 : non-numeric argument to binary operator
>
> which seems to mean that I can't just rewrite code to use Matrix
> instead of matrix objects -- I would have to do lots and lots of
> conversions between Matrix and matrix. Am I missing a trick
> here somewhere, that would let me use only Matrix objects and do
> with them the things one can do with matrix objects? Or some other
> way to avoid the twofold speed hit in moving to 1.9?

The trick is waiting for the author of the Matrix package to write the
methods for arithmetic operations or contributing said methods
yourself. :-)

Actually I want to at least e-discuss the implementation with John
Chambers and other members of the R Development Core Team before doing
much more implementation.  There are some subtle issues about how to
arrange the classes and this is usually the point where John can
provide invaluable guidance.

```