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)
Hi Andy, there's probably a problem of approximation (different calculations mean different approximations). Try re-running that code, in order to obtain a different matrix. Or similarly change the dimensions of the matrix. that code is used only to show that the functions have the same purpose; but don't worry about it.
For example look at this code (it is clarifying, in my opinion): a <- rnorm(200)
all (abs(a)/2 == ((sqrt(abs(a)))^2)/2 ) [1] FALSE
all (round(abs(a)/2,6) == round(((sqrt(abs(a)))^2)/2,6) ) [1] TRUE
diff <- abs(a)/2 - ((sqrt(abs(a)))^2)/2
mean(diff) [1] -8.827574e-18
sd(diff) [1] 6.016011e-17
This means that approximations problems (due to binary system) are generally infinitevely small. I hope this will be useful for you :)
I can't replicate your result:
ReplyDelete> A <- matrix(trunc(rnorm(512*512)*100), 512,512)
> all( round(solve(A),8) == round(strassenInv(A),8) )
[1] TRUE
> all( round(solve(A),8) == round(strassenInv2(A),8) )
[1] FALSE
> all( round(solve(A),6) == round(strassenInv3(A),6) )
[1] FALSE
I'll double check your strassenInv functions were copied appropriately, but everything looks ok on my end. Any thoughts?
Hi Andy,
ReplyDeletethere's probably a problem of approximation (different calculations mean different approximations).
Try re-running that code, in order to obtain a different matrix. Or similarly change the dimensions of the matrix.
that code is used only to show that the functions have the same purpose; but don't worry about it.
For example look at this code (it is clarifying, in my opinion):
a <- rnorm(200)
all (abs(a)/2 == ((sqrt(abs(a)))^2)/2 )
[1] FALSE
all (round(abs(a)/2,6) == round(((sqrt(abs(a)))^2)/2,6) )
[1] TRUE
diff <- abs(a)/2 - ((sqrt(abs(a)))^2)/2
mean(diff)
[1] -8.827574e-18
sd(diff)
[1] 6.016011e-17
This means that approximations problems (due to binary system) are generally infinitevely small.
I hope this will be useful for you :)