Hi everyone,
Just started this morning with Julia after a long overdue-
I was looking for an efficient way to invert a stack of 3x3 matrices. Coming from a python
background it does seem that numpy.linalg.inv
is terribly slow at this.
I briefly looked if someone did anything similar here but couldn’t find anything (feel free to point in response if you have come across a similar post before), so here it goes
using BenchmarkTools
A = randn(1000,4,3,3)
function compute_inv(A)
B = zeros(size(A));
for i in 1:size(A)[1]
for j in 1:size(A)[2]
B[i,j,:,:] = inv(A[i,j,:,:]);
end
end
end
@benchmark compute_inv(A)
# --------------
# minimum time: 3.723 ms (0.00% GC)
# median time: 4.688 ms (0.00% GC)
# mean time: 4.779 ms (5.16% GC)
# maximum time: 10.037 ms (10.81% GC)
# --------------
This was after figuring out that inv
accepts only square matrices and not higher dimensional arrays. I was surprised that it is actually much faster than numpy.linalg.inv
yet a bit slower than a hard coded version. (Of course, I may not be doing things efficiently)
Would be happy to know on how to improve the performance of the above code.