I am using Python and Numpy to do some data analysis.
I have a large 3D matrix (NxNxN), where each cell is again a matrix, this time a 3×3 matrix. Calling the matrix data, it looks like this:
data[N,N,N,3,3]
I need to find the eigenvalues of all the 3×3 matrices, and for that I use Numpy’s eigvals routine, but it takes ages to do. Right now I pretty much do this:
for i in range(N):
for j in range(N):
for k in range(N):
a = np.linalg.eigvals(data[i,j,k,:,:])
For N = 256, this takes about an hour. Any ideas on how to make this more efficient?
Many thanks for any suggestions!
itertools.productis nicer than nested loops, aesthetically speaking. But I don’t think it will make your code that much faster. My testing suggests that iteration is not your bottleneck.So underestimating:
That’s 14 minutes minimum on my computer, which is pretty new. Let’s see how long the loops take…
Orders of magnitude smaller, as DSM said. Even the work of slicing the array alone is more substantial: