def test_call_with_cast_to_complex_without_umfpack(self): use_solver(useUmfpack=False) solve = factorized(self.A) b = random.rand(4) for t in [np.complex64, np.complex128]: with assert_raises(TypeError, message="Cannot cast array data"): solve(b.astype(t))
def setup_method(self): use_solver(useUmfpack=False) n = 40 d = arange(n) + 1 self.n = n self.A = spdiags((d, 2*d, d[::-1]), (-3, 0, 5), n, n) random.seed(1234)
def test_shape_compatibility(self): use_solver(useUmfpack=True) A = csc_matrix([[1., 0], [0, 2]]) bs = [ [1, 6], array([1, 6]), [[1], [6]], array([[1], [6]]), csc_matrix([[1], [6]]), csr_matrix([[1], [6]]), dok_matrix([[1], [6]]), bsr_matrix([[1], [6]]), array([[1., 2., 3.], [6., 8., 10.]]), csc_matrix([[1., 2., 3.], [6., 8., 10.]]), csr_matrix([[1., 2., 3.], [6., 8., 10.]]), dok_matrix([[1., 2., 3.], [6., 8., 10.]]), bsr_matrix([[1., 2., 3.], [6., 8., 10.]]), ] for b in bs: x = np.linalg.solve(A.toarray(), toarray(b)) for spmattype in [csc_matrix, csr_matrix, dok_matrix, lil_matrix]: x1 = spsolve(spmattype(A), b, use_umfpack=True) x2 = spsolve(spmattype(A), b, use_umfpack=False) # check solution if x.ndim == 2 and x.shape[1] == 1: # interprets also these as "vectors" x = x.ravel() assert_array_almost_equal(toarray(x1), x, err_msg=repr((b, spmattype, 1))) assert_array_almost_equal(toarray(x2), x, err_msg=repr((b, spmattype, 2))) # dense vs. sparse output ("vectors" are always dense) if isspmatrix(b) and x.ndim > 1: assert_(isspmatrix(x1), repr((b, spmattype, 1))) assert_(isspmatrix(x2), repr((b, spmattype, 2))) else: assert_(isinstance(x1, np.ndarray), repr((b, spmattype, 1))) assert_(isinstance(x2, np.ndarray), repr((b, spmattype, 2))) # check output shape if x.ndim == 1: # "vector" assert_equal(x1.shape, (A.shape[1],)) assert_equal(x2.shape, (A.shape[1],)) else: # "matrix" assert_equal(x1.shape, x.shape) assert_equal(x2.shape, x.shape) A = csc_matrix((3, 3)) b = csc_matrix((1, 3)) assert_raises(ValueError, spsolve, A, b)
def test_call_with_incorrectly_sized_matrix_without_umfpack(self): use_solver(useUmfpack=False) solve = factorized(self.A) b = random.rand(4) B = random.rand(4, 3) BB = random.rand(self.n, 3, 9) with assert_raises(ValueError, message="is of incompatible size"): solve(b) with assert_raises(ValueError, message="is of incompatible size"): solve(B) with assert_raises(ValueError, message="object too deep for desired array"): solve(BB)
def test_call_with_incorrectly_sized_matrix_without_umfpack(self): use_solver(useUmfpack=False) solve = factorized(self.A) b = random.rand(4) B = random.rand(4, 3) BB = random.rand(self.n, 3, 9) with assert_raises(ValueError, match="is of incompatible size"): solve(b) with assert_raises(ValueError, match="is of incompatible size"): solve(B) with assert_raises(ValueError, match="object too deep for desired array"): solve(BB)
def test_call_with_incorrectly_sized_matrix_with_umfpack(self): use_solver(useUmfpack=True) solve = factorized(self.A) b = random.rand(4) B = random.rand(4, 3) BB = random.rand(self.n, 3, 9) # does not raise solve(b) msg = "object too deep for desired array" with assert_raises(ValueError, message=msg): solve(B) with assert_raises(ValueError, message=msg): solve(BB)
def test_call_with_incorrectly_sized_matrix_with_umfpack(self): use_solver(useUmfpack=True) solve = factorized(self.A) b = random.rand(4) B = random.rand(4, 3) BB = random.rand(self.n, 3, 9) # does not raise solve(b) msg = "object too deep for desired array" with assert_raises(ValueError, match=msg): solve(B) with assert_raises(ValueError, match=msg): solve(BB)
def test_assume_sorted_indices_flag(self): # a sparse matrix with unsorted indices unsorted_inds = np.array([2, 0, 1, 0]) data = np.array([10, 16, 5, 0.4]) indptr = np.array([0, 1, 2, 4]) A = csc_matrix((data, unsorted_inds, indptr), (3, 3)) b = ones(3) # should raise when incorrectly assuming indices are sorted use_solver(useUmfpack=True, assumeSortedIndices=True) assert_raises_regex(RuntimeError, "UMFPACK_ERROR_invalid_matrix", factorized, A) # should sort indices and succeed when not assuming indices are sorted use_solver(useUmfpack=True, assumeSortedIndices=False) expected = splu(A.copy()).solve(b) assert_equal(A.has_sorted_indices, 0) assert_array_almost_equal(factorized(A)(b), expected) assert_equal(A.has_sorted_indices, 1)
def test_assume_sorted_indices_flag(self): # a sparse matrix with unsorted indices unsorted_inds = np.array([2, 0, 1, 0]) data = np.array([10, 16, 5, 0.4]) indptr = np.array([0, 1, 2, 4]) A = csc_matrix((data, unsorted_inds, indptr), (3, 3)) b = ones(3) # should raise when incorrectly assuming indices are sorted use_solver(useUmfpack=True, assumeSortedIndices=True) with assert_raises(RuntimeError, message="UMFPACK_ERROR_invalid_matrix"): factorized(A) # should sort indices and succeed when not assuming indices are sorted use_solver(useUmfpack=True, assumeSortedIndices=False) expected = splu(A.copy()).solve(b) assert_equal(A.has_sorted_indices, 0) assert_array_almost_equal(factorized(A)(b), expected) assert_equal(A.has_sorted_indices, 1)
def test_factorizes_nonsquare_matrix_with_umfpack(self): use_solver(useUmfpack=True) # does not raise factorized(self.A[:,:4])
def setup_method(self): use_solver(useUmfpack=False)
def test_non_singular_with_umfpack(self): use_solver(useUmfpack=True) self._check_non_singular()
def test_cannot_factorize_nonsquare_matrix_without_umfpack(self): use_solver(useUmfpack=False) msg = "can only factor square matrices" with assert_raises(ValueError, message=msg): factorized(self.A[:, :4])
def test_singular_with_umfpack(self): use_solver(useUmfpack=True) with suppress_warnings() as sup: sup.filter(RuntimeWarning, "divide by zero encountered in double_scalars") assert_warns(umfpack.UmfpackWarning, self._check_singular)
def test_non_singular_without_umfpack(self): use_solver(useUmfpack=False) self._check_non_singular()
def setUp(self): use_solver(useUmfpack=False)
def test_singular_without_umfpack(self): use_solver(useUmfpack=False) assert_raises_regex(RuntimeError, "Factor is exactly singular", self._check_singular)
def test_bug_8278(self): check_free_memory(8000) use_solver(useUmfpack=True) A, b = setup_bug_8278() x = spsolve(A, b) assert_array_almost_equal(A @ x, b)
def test_call_with_cast_to_complex_with_umfpack(self): use_solver(useUmfpack=True) solve = factorized(self.A) b = random.rand(4) for t in [np.complex64, np.complex128]: assert_warns(np.ComplexWarning, solve, b.astype(t))
def test_cannot_factorize_nonsquare_matrix_without_umfpack(self): use_solver(useUmfpack=False) assert_raises_regex(ValueError, "can only factor square matrices", factorized, self.A[:,:4])
import warnings from numpy import array, finfo from numpy.testing import * from scipy.linalg import norm, inv from scipy.sparse import spdiags, SparseEfficiencyWarning from scipy.sparse.linalg.dsolve import spsolve, use_solver warnings.simplefilter('ignore',SparseEfficiencyWarning) #TODO add more comprehensive tests use_solver( useUmfpack = False ) class TestLinsolve(TestCase): ## this crashes SuperLU #def test_singular(self): # A = csc_matrix( (5,5), dtype='d' ) # b = array([1, 2, 3, 4, 5],dtype='d') # x = spsolve(A,b) def test_twodiags(self): A = spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]], [0, 1], 5, 5) b = array([1, 2, 3, 4, 5]) # condition number of A cond_A = norm(A.todense(),2) * norm(inv(A.todense()),2) for t in ['f','d','F','D']: eps = finfo(t).eps #floating point epsilon
import scipy.linalg from scipy.linalg import norm, inv from scipy.sparse import (spdiags, SparseEfficiencyWarning, csc_matrix, csr_matrix, identity, isspmatrix, dok_matrix, lil_matrix, bsr_matrix) from scipy.sparse.linalg import SuperLU from scipy.sparse.linalg.dsolve import (spsolve, use_solver, splu, spilu, MatrixRankWarning, _superlu, spsolve_triangular) from scipy._lib._numpy_compat import suppress_warnings sup_sparse_efficiency = suppress_warnings() sup_sparse_efficiency.filter(SparseEfficiencyWarning) # TODO add more comprehensive tests use_solver(useUmfpack=False) def toarray(a): if isspmatrix(a): return a.toarray() else: return a class TestLinsolve(TestCase): def test_singular(self): with warnings.catch_warnings(): warnings.simplefilter("ignore", category=MatrixRankWarning) A = csc_matrix((5,5), dtype='d')
def test_singular_without_umfpack(self): use_solver(useUmfpack=False) with assert_raises(RuntimeError, match="Factor is exactly singular"): self._check_singular()
def test_cannot_factorize_nonsquare_matrix_without_umfpack(self): use_solver(useUmfpack=False) msg = "can only factor square matrices" with assert_raises(ValueError, match=msg): factorized(self.A[:, :4])
def test_singular_without_umfpack(self): use_solver(useUmfpack=False) with assert_raises(RuntimeError, message="Factor is exactly singular"): self._check_singular()
def test_factorizes_nonsquare_matrix_with_umfpack(self): use_solver(useUmfpack=True) # does not raise factorized(self.A[:, :4])
# purpose. # # Please cite the author in any work or product based on this material. # # =========================================================================== # # Code author: Aleksandar Stojmirovic # """Routines and classes for solving discrete Laplace equation.""" import numpy from scipy import linalg from scipy.sparse.linalg import dsolve # Use UMFPACK if possible. SuperLU seems slower under first impression but I # did not thoroughly test. dsolve.use_solver(useUmfpack=True, assumeSortedIndices=True) class BasicLaplacian(object): """ Constructs the discrete Laplacian corresponding to the given adjacency matrix of a graph (normalized to a stochastic matrix) in a sparse form, with the boundary specified at the construction time. Precomputes the inverse of the Laplacian (Green's function) as a sparse LU-decomposition using a direct linear solver (UMFPACK or SuperLU). The inverse is accessed using the solve() method. Arguments: * W: graph adjacency matrix storage class instance; * df_mask: an array of the same size as data array of adjacency_matrix. Should contain the damping factors that multiply