Fastest way in R to compute the inverse for large matrices

Chasing the code for this line by line is a little difficult because the setup of R code is a little on the complicated side. But read on, pointers below.

The important part is that the topic has been discussed many times: what happens is that R dispatches this to the BLAS (Basic Linear Algebra Subprogram) and LAPACK (Linear Algebra PACKage) libraries. Which contain the most efficient code known to man for this. In general, you cannot gain on it by rewriting.

One can gain performance differences by switching one BLAS/LAPACK implementation for another—there are many, many posts on this online too. R itself comes with the so-called ‘reference BLAS’ known to be correct, but slowest. You can switch to Atlas, OpenBLAS, MKL, … depending on your operating system; instructions on how to do so are in some of the R manuals that come with your installation.

For completeness, per file src/main/names.c the commands %*%, crossprod and tcrossprod all refer to do_matprod. This is in file src/main/array.c and does much argument checking and arranging and branching on types of arguments but e.g. one path then calls

F77_CALL(dsyrk)(uplo, trans, &nc, &nr, &one, x, &nr, &zero, z, &nc
        FCONE FCONE); 

which is this LAPACK function. It is essentially the same for all others making this an unlikely venue for your optimisation.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top