I need to invert a large number (currently 1e6, could maybe be optimized to 3e3) of symmetric complex tridiagonal matrices. The matrices M
are all very similar. In fact M
is a function of two parameters M(a, b)
, and I need to calculate the inverse on a grid.
What I currently do is
import numpy as np
from numpy.linalg import inv
N = 40
mat_indices = range(0, N)
a = np.linspace(-5, 5, dtype=np.complex256, num=500)
b = np.linspace(-5, 5, dtype=np.complex256, num=500)
w += 5j * max((w[1] - w[0], eps[1] - eps[0]))
M = np.zeros(a.shape + b.shape + (N, N), dtype=np.complex)
for lay in layers:
M[..., lay, lay] = fa(a[:, np.newaxis]) + fb(b[np.newaxis, :])
for lay in layers[:-1]:
G_latt_inv[..., lay, lay+1] = fsub(a, b)
G_latt_inv[..., lay+1, lay] = fsub(a, b)
M_inv = inv(M)
Obviously this is a terrible solution, I neither use symmetry nor sparsity of the matrix. Furthermore, an iterative method should be advantageous, as my matrices are all very similar.
I know there is scipy.sparse
, probably I could use a solve
method for b=Unity
. But I still don't use any of my many advantageous properties.
I do not need an optimal solution, I rather prefer simplicity because I don't have a lot of time to test and verify my results. But currently it is way too inefficient.