I have a $50000\times 50000$ matrix $A$ sparse matrix containing only 5 non-zero elements in each row. Now the problem is that the diagonal elements and the constants (in $B$ matrix such that $AX=B$) get updated after every iteration. If I go by the usual Matlab function inv
or via Gauss Elimination, it takes around 130 seconds for the solution to be computed for a single iteration and the number of iterations required by the problem is somewhat of the order 100-500 to compute the final solution $X$. Need some suggestions on this one if I am not wanting to use parallel computation.
2 Answers
The first idea that comes to my mind is to use an iterative method for solving the linear system. This might speed-up the computation of a single iteration (depending on the method (GMRES, conjugate gradient, BiCGStab,... and the size of B) and it will take advantage of the sparsity of the matrix A.
Moreover, iterative methods let you define a preconditioner. If the diagonal coefficient does not change too much between the iterations, it might be worth spending some time at the begining to build a good preconditioner (e.g. ILU) and reuse it for all the iterations.
Hope it helps!
Edit: The answers to this question might be useful.
You may use a sparse factorization algorithm, it means computing matrices $P$, $L$, $U$, such that $M = PLU$ where $P$ is a permutation matrix, $L$ a sparse lower triangular matrix and $U$ a sparse upper triangular matrix. The permutation matrix is there and computed in such a way that $L$ and $U$ remain reasonably sparse (without it it is not the case in general, as indicated in the other answer). Then when $M$ is factorized in this form, it is trivial to solve a linear system with an arbitrary rhs. If $M$ is symmetric definite, then there is a sparse cholesky factorization ($M = PLL^t$), if it is symmetric only, then it can be decomosed as $M = PLDL^t$, with $D$ a diagonal matrix.
There are several available implementations of sparse factorization: SuperLU, Choldmod, Mumps, TAUCS (depending on whether you need $LU$, $LL^t$ or $LDL^t$). There probably exists MATLAB bindings for most of them.
A
andB
? $\endgroup$X=A\B
(never useinv
for solving linear systems, that's not what it is for!), which is the most efficient "black-box" way of solving your problem in Matlab? $\endgroup$