Very similar to what has been done to create a function to perform fast multiplication of large matrices using the Strassen algorithm (see previous post), now we write the functions to quickly calculate the inverse of a matrix.
To avoid rewriting pages and pages of comments and formulas, as I did for matrix multiplication, this time I’ll show you directly the code of the function (the reasoning behind it is quite similar). Please, copy and paste all the code in an external editor to see it properly.
We run now some test. First check if the function successfully invert the matrix and compare them with the results of the standard R function (Function
A <- matrix(trunc(rnorm(512*512)*100), 512,512) all( round(solve(A),8) == round(strassenInv(A),8) )  TRUE all( round(solve(A),8) == round(strassenInv2(A),8) )  TRUE all( round(solve(A),6) == round(strassenInv3(A),6) )  TRUE
The function performs the operations correctly. But there is a problem of approximation: in fact the first two functions are accurate to the eighth decimal place, while the third through sixth. Probably not an issue of calculus, but it is a problem of expression of numbers in binary format and 32-bit, which causes these errors.
Now we analyze the computation time. See in the table the result, obtained by running the following code:
The results are quite obvious, and using a modification of Strassen algorithm for matrix inversion, there is a real time saving.
Please, remember these two recommendations already made:
- The code is to be improved, and if anyone wants to help me, I will be happy to update my code
- If you consider it useful to use these function for any work, a citation is always welcome (contact me at my e-mail for details)