问题
Few days ago I asked a question how to find the eigenvalues of a large sparse matrix. I got no answers, so I decided to describe a potential solution.
One question remains:
Can I use the python implementation of ARPACK
to compute the eigenvalues of a asymmetric sparse matrix.
At the beginning I would like to say that it is not at all necessary to call the subroutines of ARPACK directly using FOTRAN driver program. That is quite difficult and I never got it going. But one can do the following:
#OPTION 1: Python
#One can install numpy and scipy and run the following code:
import numpy as np
from scipy.linalg import eigh
from scipy.sparse.linalg import eigsh
from scipy.sparse import *
from scipy import *
# coordinate format storage of the matrix
# rows
ii = array([0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4])
# cols.
jj = array([0, 1, 0, 1, 2, 1, 2, 3, 2, 3, 4])
# and the data
data=array([1.,-1.,-1., 2.,-2.,-2., 1., 1., 1., 1., 1.])
# now put this into sparse storage (CSR-format)
m=csr_matrix( (data,(ii,jj)), shape=(5,5) )
# you can check what you did
matrix([[ 1, -1, 0, 0, 0],
[-1, 2, -2, 0, 0],
[ 0, -2, 1, 1, 0],
[ 0, 0, 1, 1, 0],
[ 0, 0, 0, 0, 1]])
# the real part starts here
evals_large, evecs_large = eigsh(m, 4, which='LM')
# print the largest 4 eigenvalues
print evals_all
# and the values are
[-1.04948118 1. 1.48792836 3.90570354]
Well this is all very nice, specially because it spears us the joy of reading the very "well written" manual of ARPACK.
I have a problem with this, I think that it doesn't work with asymmetric matrices. At least comparing the results to matlab was not very convincing.
#OPTION 2: MATLAB
# % put your data in a file "matrix.dat"
% row col. data
% note that indexing starts at "1"
1 1 1.
1 2 -1.
......
load matrix.dat
M = spconvert(matrix)
[v,d] = eig(M)
% v - contains the eigenvectors
% d - contains the eigenvalues
I think that using matlab is way simpler and works for asymmetric matrices. Well I have a 500000x500000 sparse matrix, so whether this will work in matlab .... Is another cup of tea! I have to note that using python I was able to load matrix of this size and compute it's eigenvalues without too much of a trouble.
Cheers,
来源:https://stackoverflow.com/questions/24613139/finding-eigenvectors-and-eigenvalues-of-a-sparse-matrix-with-arpack-called-for