# computing the inverse of a large block diagonal sparse matrix in r

I would like to compute the inverse of some large block diagonal sparse matrix. The number of rows and columns is somewhat over 50,000. The blocks are 12 by 12 and are sparse (27 non zero elements).

I tried to compute the inverse of the entire matrix (using solve). This was not possible, the entire matrix is too big.

After that, i use a for-loop. Within each iteration, i take out one block, compute its inverse and place it back.

That method works, but iT takes about 5 minutes. I wonder if there is some faster way.

• There are a number of very good reasons to avoid explicitly computing inverses, and this is even more important for large sparse matrices. Are you sure you really need the inverse? Otherwise, you should rewrite your code to get around the inversion (e.g., apply each diagonal block separately to a slice of the vector). Apr 7, 2014 at 15:34
• I just did 4200 inverses of random dense 12x12 matrices in matlab (for about 50k rows total). The actual inverses took only 63ms. When you create your sparse matrix, are you allocating dense blocks? Even if your 12x12 is sparse, its inverse will be dense, and you may be moving the entire matrix every time you reinsert one of the inverses. Apr 7, 2014 at 17:15
• I agree with Federico that explicitly computing inverses should be avoided unless absolutely necessary. If all you need is the solution, there's a lot of good sparse solvers out there, GMRES, Conjugate Gradient, etc. These iterative solvers allow you to avoid construct the entire matrix, and are generally faster. Feb 5, 2015 at 6:52

I made by code much faster, by:

1) storing the inverse of the block diagonal matrices in a list, rather than 'placing them back in the large matrix. At the end I built the entire matrix from the list by using the bdiag() command. 2) by considering the block diagonal matrices not one by one, but in groups of about 10 So, I repeatedly calculate the inverse of a submatrix consisting of 10 block diagonal matrices.

In the original question i did not tell that i do not only take the inverse of each block, but that i also applied some transformation on each block. Now the transformation is done in advance for the entire block diagonal matrix, which also saves time.