# [Scipy-svn] r6995 - in trunk/scipy/sparse/linalg/eigen/arpack: . tests

scipy-svn@scip... scipy-svn@scip...
Sat Dec 4 15:04:44 CST 2010

```Author: ptvirtan
Date: 2010-12-04 15:04:44 -0600 (Sat, 04 Dec 2010)
New Revision: 6995

Modified:
trunk/scipy/sparse/linalg/eigen/arpack/arpack.py
trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py
Log:
ENH: sparse/arpack: infer LinearOperator dtype if it is missing in eigs/eigsh (#1119)

Modified: trunk/scipy/sparse/linalg/eigen/arpack/arpack.py
===================================================================
--- trunk/scipy/sparse/linalg/eigen/arpack/arpack.py	2010-12-04 20:26:15 UTC (rev 6994)
+++ trunk/scipy/sparse/linalg/eigen/arpack/arpack.py	2010-12-04 21:04:44 UTC (rev 6995)
@@ -451,6 +451,13 @@
else:
return d

+def _aslinearoperator_with_dtype(m):
+    m = aslinearoperator(m)
+    if not hasattr(m, 'dtype'):
+        x = np.zeros(m.shape[1])
+        m.dtype = (m*x).dtype
+    return m
+
def eigs(A, k=6, M=None, sigma=None, which='LM', v0=None,
ncv=None, maxiter=None, tol=0,
return_eigenvectors=True):
@@ -548,7 +555,7 @@
Solution of Large Scale Eigenvalue Problems by Implicitly Restarted
Arnoldi Methods. SIAM, Philadelphia, PA, 1998.
"""
-    A = aslinearoperator(A)
+    A = _aslinearoperator_with_dtype(A)
if A.shape[0] != A.shape[1]:
raise ValueError('expected square matrix (shape=%s)' % (A.shape,))
n = A.shape[0]
@@ -657,7 +664,7 @@
Solution of Large Scale Eigenvalue Problems by Implicitly Restarted
Arnoldi Methods. SIAM, Philadelphia, PA, 1998.
"""
-    A = aslinearoperator(A)
+    A = _aslinearoperator_with_dtype(A)
if A.shape[0] != A.shape[1]:
raise ValueError('expected square matrix (shape=%s)' % (A.shape,))
n = A.shape[0]

Modified: trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py
===================================================================
--- trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py	2010-12-04 20:26:15 UTC (rev 6994)
+++ trunk/scipy/sparse/linalg/eigen/arpack/tests/test_arpack.py	2010-12-04 21:04:44 UTC (rev 6995)
@@ -8,10 +8,11 @@

from numpy.testing import assert_almost_equal, assert_array_almost_equal, \
assert_array_almost_equal_nulp, TestCase, run_module_suite, dec, \
-        assert_raises, verbose
+        assert_raises, verbose, assert_equal

from numpy import array, finfo, argsort, dot, round, conj, random
from scipy.sparse import csc_matrix, isspmatrix
+from scipy.sparse.linalg import LinearOperator
from scipy.sparse.linalg.eigen.arpack import eigs, eigsh, svds, \
ArpackNoConvergence

@@ -352,6 +353,11 @@
A = csc_matrix(np.zeros((2,3)))
assert_raises(ValueError, eigs, A)

+def test_eigs_operator():
+    # Check inferring LinearOperator dtype
+    fft_op = LinearOperator((6, 6), np.fft.fft)
+    w, v = eigs(fft_op, k=3)
+    assert_equal(w.dtype, np.complex_)

def sorted_svd(m, k):
"""Compute svd of a dense matrix m, and return singular vectors/values

```