# How to compute all the eigenvalues of a large sparse matrix using matlab?

In matlab, there are 2 commands named "eig" for full matrices and "eigs" for sparse matrices to compute eigenvalues of a matrix. And eig(A) computes all the eigenvalues of a full matrix and eigs(A) computes the 6 largest magnitude eigenvalues of matrix A. If we want to compute all the eigenvalues of a sparse matrix, so we must convert the matrix to full type, i.e., using eig(full(A)) when A is sparse. But this will fail becaues of CPU memory. My question is that if I want to compute all the eigenvalues of a large sparse matrix, say (matrix size is 10000). How to implement this? I know we should not compute all the eigenvalues of a large sparse matrix. But sometimes, in Krylove subspace iterative methods, we need to plot the spectral distributions of a preconditioned matrix to investigate the convergence rate. so I need plot the spectral distributions. How to do that? Thanks very much.

Below is my random example and fails. And the matrix is from Poisson equation using centered difference in 2D:

clc;clear;
n=100;
A = gallery('poisson',n);% system size is n*n
R = ichol(A);
P = R'*R;%  construct preconditioner
%   consider the eigenvalue distribution of  preconditioned matrix inv(P)*A
a = eig(full(A),full(P));
plot(real(a),imag(a))


## 1 Answer

"Get more RAM" may be one of your best options. :) Prices are reasonably low right now, and it's one of the best upgrades you can gift your computer anyway. 10k x 10k is borderline but still doable on modern computers: that matrix takes $$10^4 \times 10^4 \times 8$$ bytes, that is, 760 MiB. On my laptop that code runs without problems.

Another option is running your code with a smaller $$n$$. It looks like you are running that computation to get insight on how your preconditioner works, not numbers. You can probably get the same insight with a 5k x 5k matrix, or a 2k x 2k matrix. And those will probably fit in your memory.

• Thanks for your reply, Professor. Yeah, I just want to see how the preconditioner works so I need plot the spectral distributions. So, you mean that I need not test so large matrix? the step size in my problem is h=1/(n+1) in both x and y direction, 2D case, where n+2 equidistant points are used in Poisson equations with Dirichlet. But if the preconditioner works well for small n , I worried that whether the preconditioner still works well for large matrix sizen? or, in other words, how can we guarantee that a preconditioner still works whatever the system size is? thanks. – sunshine Nov 2 '19 at 15:54
• No need to call me professor! :) Yes, I mean to say that you would get similar results with a smaller matrix. As far as I understand, it is quite common to test preconditioners at sizes smaller than the real problem, also because often the real problem is even larger than 10k x 10k and wouldn't fit in memory even with more RAM. You cannot compute the exact spectral picture, but at least you can extrapolate. This is not a fully rigorous proof that the preconditioner continues to work, though (but would it be fully rigorous anyway with eigenvalues computed in machine arithmetic?). – Federico Poloni Nov 2 '19 at 16:17
• Haha, Get it! Affirmative! In my MATLAB with machine arithmetic 1e-16, even with the exact algorithm, the results we obtained are still approximate, so we cannot compute the exact spectral distributions. In turn, we need not to test so large matrix size. Just experimenting for smaller matrix is enough and we can guess it still works approximately for large matrix size. I understand it thoroughly now and I am happy with that I do not need to use eig(full(A)) for large matrix. Thanks again, Teacher Poloni. ^____^ – sunshine Nov 3 '19 at 0:40