-
-
Save adeak/a2a3db398f7e614ea2cf53d9311e25ec to your computer and use it in GitHub Desktop.
sparse eigsh for Euryris
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import scipy.sparse as sp | |
import scipy.sparse.linalg as spl | |
denseM = [[ 0.75, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , | |
0. , 0. , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0.25, 0.5 , 0. , 0. , 0. , 0. , 0. , 0. , | |
0. , 0. , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0.5 , -0.25, 0. , 0.5 , 0. , 0. , 0. , 0. , | |
0. , 0. , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0.25, 0. , 0.5 , 0. , 0. , 0. , | |
0. , 0. , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0.5 , 0. , -0.25, 0. , 0. , 0. , 0.5 , | |
0. , 0. , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0.5 , 0. , -0.75, 0.5 , 0. , 0. , | |
0.5 , 0. , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0.5 , -0.25, 0. , 0. , | |
0. , 0.5 , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.25, 0. , | |
0. , 0. , 0.5 , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0.5 , 0. , 0. , 0. , 0.25, | |
0. , 0. , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0.5 , 0. , 0. , 0. , | |
-0.25, 0.5 , 0. , 0. , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 0. , 0. , | |
0.5 , -0.75, 0. , 0.5 , 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 0. , | |
0. , 0. , -0.25, 0. , 0.5 , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , | |
0. , 0.5 , 0. , 0.25, 0. , 0. , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , | |
0. , 0. , 0.5 , 0. , -0.25, 0.5 , 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , | |
0. , 0. , 0. , 0. , 0.5 , 0.25, 0. ], | |
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , | |
0. , 0. , 0. , 0. , 0. , 0. , 0.75]] | |
denseM = np.array(denseM) | |
dense_vals = np.linalg.eigh(denseM) | |
sparseM = sp.csr_matrix(denseM) | |
sparse_vals = spl.eigsh(sparseM, sparseM.shape[0] - 1, which='SA') | |
dense_eval, dense_evec = dense_vals[0][0], dense_vals[1][:, 0] | |
sparse_eval, sparse_evec = sparse_vals[0][0], sparse_vals[1][:, 0] | |
# checks | |
if not denseM.dtype == sparseM.dtype == np.float64: | |
print('wrong dtypes') | |
if not np.array_equal(denseM, denseM.T): | |
print('not symmetric') | |
if np.isclose(*dense_vals[0][:2]): | |
print('degenerate ground state') | |
if not (denseM == sparseM).all(): | |
print('dense != sparse') | |
if not np.allclose(denseM @ dense_evec - dense_eval * dense_evec, 0): | |
print('not a dense eigenvector') | |
if not np.allclose(sparseM @ sparse_evec - sparse_eval * sparse_evec, 0): | |
print('not a sparse eigenvector') | |
if not np.isclose(dense_eval, sparse_eval): | |
print('not the same eigenvalues') | |
if not np.allclose(sparse_evec, dense_evec) and not np.allclose(-sparse_evec, dense_evec): | |
print('not the same eigenvectors') | |
# print "every" eigenvalue and eigenvector | |
print(dense_vals[0]) | |
print(dense_vals[1]) | |
print(sparse_vals[0]) | |
print(sparse_vals[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment