几天前,我问了一个问题,如何找到大型稀疏矩阵的特征值。我没有得到答案,所以我决定描述一个潜在的解决方案。
One question remains:
Can I use the python implementation of ARPACK
to compute the eigenvalues of a asymmetric sparse matrix.
一开始我想说的是,完全没有必要直接使用 FOTRAN 驱动程序调用 ARPACK 的子程序。那是相当困难的,我从来没有成功过。但是可以执行以下操作:
#选项 1:Python
#可以安装 numpy 和 scipy 并运行以下代码:
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]
嗯,这一切都很好,特别是因为它让我们很高兴阅读 ARPACK 的“写得很好”的手册。
我对此有疑问,我认为它不适用于非对称矩阵。至少将结果与matlab进行比较并不是很有说服力。
#选项 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
我认为使用 matlab 更简单,适用于非对称矩阵。好吧,我有一个 500000x500000 稀疏矩阵,所以这是否可以在 matlab 中工作....又是一杯茶!我必须注意,使用 python 我能够加载这种大小的矩阵并计算它的特征值而没有太多麻烦。
干杯,