Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_return_stats():
A, _ = diagonal(100)
evals, evecs, stats = primme.eigsh(A, 3, tol=1e-6, which='LA',
return_stats=True, return_history=True)
assert(stats["hist"]["numMatvecs"])
svecs_left, svals, svecs_right, stats = primme.svds(A, 3, tol=1e-6,
which='SM', return_stats=True, return_history=True)
assert(stats["hist"]["numMatvecs"])
"""
Test cases for primme.eighs with csr and LinearOperator matrix types.
"""
n = 10
for dtype in (np.float64, np.complex64):
A = toStandardProblem(MikotaPair(n, dtype=dtype))
evals, evecs = np.linalg.eigh(A)
sigma0 = evals[0]*.51 + evals[-1]*.49
for op in ((lambda x : x), csr_matrix, aslinearoperator):
which, sigma = 'SM', sigma0
prec = jacobi_prec(A, sigma)
k = 5
M = op(prec) if prec is not None else None
case_desc = ("A=%s(%d, %s), k=%d, M=%s, which=%s, sigma=%s" %
(MikotaPair.__name__, n, dtype, k, prec is None, which, sigma))
yield (eigsh_check, eigsh, op(A), None, 1, k, M, which, sigma, 1e-6, evals, dtype, case_desc, False)
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# PRIMME: https://github.com/primme/primme
# Contact: Andreas Stathopoulos, a n d r e a s _at_ c s . w m . e d u
import numpy as np
from numpy.testing import assert_allclose
import scipy.sparse
import primme
# Sparse diagonal matrix of size 100
A = scipy.sparse.spdiags(np.asarray(range(100), dtype=np.float32), [0], 100, 100)
# Compute the three largest eigenvalues of A with a residual norm tolerance of 1e-6
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA')
assert_allclose(evals, [ 99., 98., 97.], atol=1e-6*100)
print(evals) # [ 99., 98., 97.]
# Compute the three largest eigenvalues of A orthogonal to the previous computed
# eigenvectors, i.e., the next three eigenvalues
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA', lock=evecs)
assert_allclose(evals, [ 96., 95., 94.], atol=1e-6*100)
print(evals) # [ 96., 95., 94.]
# Compute the three closest eigenvalues to 50.1
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which=50.1)
assert_allclose(evals, [ 50., 51., 49.], atol=1e-6*100)
print(evals) # [ 50., 51., 49.]
# Estimation of the largest eigenvalue in magnitude
def convtest_lm(eval, evecl, rnorm):
assert_allclose(evals, [ 50., 51., 49.], atol=1e-6*100)
print(evals) # [ 50., 51., 49.]
# Estimation of the largest eigenvalue in magnitude
def convtest_lm(eval, evecl, rnorm):
return np.abs(eval) > 0.1 * rnorm
eval, evec = primme.eigsh(A, 1, which='LM', convtest=convtest_lm)
assert_allclose(eval, [ 99.], atol=.1)
# User-defined matvec: implicit diagonal matrix
Adiag = np.arange(0, 100).reshape((100,1))
def Amatmat(x):
if len(x.shape) == 1: x = x.reshape((100,1))
return Adiag * x # equivalent to diag(Adiag).dot(x)
A = scipy.sparse.linalg.LinearOperator((100,100), matvec=Amatmat, matmat=Amatmat)
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA')
assert_allclose(evals, [ 99., 98., 97.], atol=1e-6*100)
# Sparse singular mass matrix
A = scipy.sparse.spdiags(np.asarray(range(100), dtype=np.float32), [0], 100, 100)
M = scipy.sparse.spdiags(np.asarray(range(99,-1,-1), dtype=np.float32), [0], 100, 100)
evals, evecs = primme.eigsh(A, 3, M=M, tol=1e-6, which='SA')
assert_allclose(evals, [ 0./99., 1./98., 2./97.], atol=1e-6*100)
print(evals)
# Sparse rectangular matrix 100x10 with non-zeros on the main diagonal
A = scipy.sparse.spdiags(range(10), [0], 100, 10)
# Compute the three closest to 4.1 singular values and the left and right corresponding
# singular vectors
svecs_left, svals, svecs_right = primme.svds(A, 3, tol=1e-6, which=4.1)
# Compute the three largest eigenvalues of A orthogonal to the previous computed
# eigenvectors, i.e., the next three eigenvalues
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA', lock=evecs)
assert_allclose(evals, [ 96., 95., 94.], atol=1e-6*100)
print(evals) # [ 96., 95., 94.]
# Compute the three closest eigenvalues to 50.1
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which=50.1)
assert_allclose(evals, [ 50., 51., 49.], atol=1e-6*100)
print(evals) # [ 50., 51., 49.]
# Estimation of the largest eigenvalue in magnitude
def convtest_lm(eval, evecl, rnorm):
return np.abs(eval) > 0.1 * rnorm
eval, evec = primme.eigsh(A, 1, which='LM', convtest=convtest_lm)
assert_allclose(eval, [ 99.], atol=.1)
# User-defined matvec: implicit diagonal matrix
Adiag = np.arange(0, 100).reshape((100,1))
def Amatmat(x):
if len(x.shape) == 1: x = x.reshape((100,1))
return Adiag * x # equivalent to diag(Adiag).dot(x)
A = scipy.sparse.linalg.LinearOperator((100,100), matvec=Amatmat, matmat=Amatmat)
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA')
assert_allclose(evals, [ 99., 98., 97.], atol=1e-6*100)
# Sparse singular mass matrix
A = scipy.sparse.spdiags(np.asarray(range(100), dtype=np.float32), [0], 100, 100)
M = scipy.sparse.spdiags(np.asarray(range(99,-1,-1), dtype=np.float32), [0], 100, 100)
evals, evecs = primme.eigsh(A, 3, M=M, tol=1e-6, which='SA')
assert_allclose(evals, [ 0./99., 1./98., 2./97.], atol=1e-6*100)
eval, evec = primme.eigsh(A, 1, which='LM', convtest=convtest_lm)
assert_allclose(eval, [ 99.], atol=.1)
# User-defined matvec: implicit diagonal matrix
Adiag = np.arange(0, 100).reshape((100,1))
def Amatmat(x):
if len(x.shape) == 1: x = x.reshape((100,1))
return Adiag * x # equivalent to diag(Adiag).dot(x)
A = scipy.sparse.linalg.LinearOperator((100,100), matvec=Amatmat, matmat=Amatmat)
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA')
assert_allclose(evals, [ 99., 98., 97.], atol=1e-6*100)
# Sparse singular mass matrix
A = scipy.sparse.spdiags(np.asarray(range(100), dtype=np.float32), [0], 100, 100)
M = scipy.sparse.spdiags(np.asarray(range(99,-1,-1), dtype=np.float32), [0], 100, 100)
evals, evecs = primme.eigsh(A, 3, M=M, tol=1e-6, which='SA')
assert_allclose(evals, [ 0./99., 1./98., 2./97.], atol=1e-6*100)
print(evals)
# Sparse rectangular matrix 100x10 with non-zeros on the main diagonal
A = scipy.sparse.spdiags(range(10), [0], 100, 10)
# Compute the three closest to 4.1 singular values and the left and right corresponding
# singular vectors
svecs_left, svals, svecs_right = primme.svds(A, 3, tol=1e-6, which=4.1)
assert_allclose(sorted(svals), [ 3., 4., 5.], atol=1e-6*10)
print(svals) # [ 4., 5., 3.]
# Sparse random rectangular matrix 10^5x100
A = scipy.sparse.rand(10000, 100, density=0.001, random_state=10)
from numpy.testing import assert_allclose
import scipy.sparse
import primme
# Sparse diagonal matrix of size 100
A = scipy.sparse.spdiags(np.asarray(range(100), dtype=np.float32), [0], 100, 100)
# Compute the three largest eigenvalues of A with a residual norm tolerance of 1e-6
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA')
assert_allclose(evals, [ 99., 98., 97.], atol=1e-6*100)
print(evals) # [ 99., 98., 97.]
# Compute the three largest eigenvalues of A orthogonal to the previous computed
# eigenvectors, i.e., the next three eigenvalues
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which='LA', lock=evecs)
assert_allclose(evals, [ 96., 95., 94.], atol=1e-6*100)
print(evals) # [ 96., 95., 94.]
# Compute the three closest eigenvalues to 50.1
evals, evecs = primme.eigsh(A, 3, tol=1e-6, which=50.1)
assert_allclose(evals, [ 50., 51., 49.], atol=1e-6*100)
print(evals) # [ 50., 51., 49.]
# Estimation of the largest eigenvalue in magnitude
def convtest_lm(eval, evecl, rnorm):
return np.abs(eval) > 0.1 * rnorm
eval, evec = primme.eigsh(A, 1, which='LM', convtest=convtest_lm)
assert_allclose(eval, [ 99.], atol=.1)
# User-defined matvec: implicit diagonal matrix
Adiag = np.arange(0, 100).reshape((100,1))