示例#1
0
 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, match="Cannot cast array data"):
             solve(b.astype(t))
示例#2
0
 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)
示例#3
0
    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)
示例#4
0
 def test_bug_8278(self):
     check_free_memory(8000)
     use_solver(useUmfpack=True)
     A, b = setup_bug_8278()
     A = A.tocsc()
     f = factorized(A)
     x = f(b)
     assert_array_almost_equal(A @ x, b)
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
    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, match="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)
示例#8
0
 def test_non_singular_with_umfpack(self):
     use_solver(useUmfpack=True)
     self._check_non_singular()
示例#9
0
 def test_non_singular_without_umfpack(self):
     use_solver(useUmfpack=False)
     self._check_non_singular()
示例#10
0
 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)
示例#11
0
 def test_singular_without_umfpack(self):
     use_solver(useUmfpack=False)
     with assert_raises(RuntimeError, match="Factor is exactly singular"):
         self._check_singular()
示例#12
0
 def setup_method(self):
     use_solver(useUmfpack=False)
示例#13
0
 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)
示例#14
0
 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))
示例#15
0
 def test_factorizes_nonsquare_matrix_with_umfpack(self):
     use_solver(useUmfpack=True)
     # does not raise
     factorized(self.A[:, :4])
示例#16
0
 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])