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 solve()):
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:
Time computation
A <- matrix(trunc(rnorm(512*512)*100), 512,512) system.time(solve(A)) system.time(strassenInv(A)) system.time(strassenInv2(A)) system.time(strassenInv3(A))
A <- matrix(trunc(rnorm(1024*1024)*100), 1024,1024) system.time(solve(A)) system.time(strassenInv(A)) system.time(strassenInv2(A)) system.time(strassenInv3(A))
A <- matrix(trunc(rnorm(2048*2048)*100), 2048,2048) system.time(solve(A)) system.time(strassenInv(A)) system.time(strassenInv2(A)) system.time(strassenInv3(A))
A <- matrix(trunc(rnorm(4096*4096)*100), 4096,4096) system.time(solve(A)) system.time(strassenInv(A)) system.time(strassenInv2(A)) system.time(strassenInv3(A))
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)
I tried to implement the Strassen's algorithm for big matrices multiplication in R.
Here I present a pdf with some theory element, some example and a possible solution in R. I'm not a programmer, so the function is not optimize, but it works.
I want to thank G. Grothendieck: suggested me a very nice way on StackOverFlow to create a bigger square matrix starting from small one.
This is just a first version of the function; it needs more work on it. If someone want to collaborate, I'll be very happy. Finally if you find my code useful for your work, I'd love to be cited (ask me via e-mail how to cite me: todoslogos -at- gmail . com).