def test_random(self): def random_triangle_matrix(n, lower=True): A = scipy.sparse.random(n, n, density=0.1, format='coo') if lower: A = scipy.sparse.tril(A) else: A = scipy.sparse.triu(A) A = A.tocsr(copy=False) for i in range(n): A[i, i] = np.random.rand() + 1 return A np.random.seed(1234) for lower in (True, False): for n in (10, 10**2, 10**3): A = random_triangle_matrix(n, lower=lower) for m in (1, 10): for b in (np.random.rand(n, m), np.random.randint(-9, 9, (n, m)), np.random.randint(-9, 9, (n, m)) + np.random.randint(-9, 9, (n, m)) * 1j): x = spsolve_triangular(A, b, lower=lower) assert_array_almost_equal(A.dot(x), b) x = spsolve_triangular(A, b, lower=lower, unit_diagonal=True) A.setdiag(1) assert_array_almost_equal(A.dot(x), b)
def test_zero_diagonal(self): n = 5 rng = np.random.default_rng(43876432987) A = rng.standard_normal((n, n)) b = np.arange(n) A = scipy.sparse.tril(A, k=0, format='csr') x = spsolve_triangular(A, b, unit_diagonal=True, lower=True) A.setdiag(1) assert_allclose(A.dot(x), b) # Regression test from gh-15199 A = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0]], dtype=np.float64) b = np.array([1., 2., 3.]) with suppress_warnings() as sup: sup.filter(SparseEfficiencyWarning, "CSR matrix format is") spsolve_triangular(A, b, unit_diagonal=True)
def test_input_types(self): A = array([[1., 0.], [1., 2.]]) b = array([[2., 0.], [2., 2.]]) for matrix_type in (array, csc_matrix, csr_matrix): x = spsolve_triangular(matrix_type(A), b, lower=True) assert_array_almost_equal(A.dot(x), b)