|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import scipy.sparse |
| 4 | + |
| 5 | +import polysolve |
| 6 | + |
| 7 | + |
| 8 | +SPARSE_SOLVER = {"solver": "Eigen::SimplicialLDLT"} |
| 9 | +DENSE_SOLVER = {"solver": "Eigen::PartialPivLU"} |
| 10 | + |
| 11 | + |
| 12 | +def sparse_system(): |
| 13 | + A = scipy.sparse.csc_matrix( |
| 14 | + [ |
| 15 | + [4.0, 1.0, 0.0], |
| 16 | + [1.0, 3.0, 1.0], |
| 17 | + [0.0, 1.0, 2.0], |
| 18 | + ] |
| 19 | + ) |
| 20 | + b = np.array([1.0, 2.0, 3.0]) |
| 21 | + return A, b |
| 22 | + |
| 23 | + |
| 24 | +def assert_solution(A, x, b): |
| 25 | + np.testing.assert_allclose(A @ x, b, atol=1e-10, rtol=1e-10) |
| 26 | + |
| 27 | + |
| 28 | +def test_solve_sparse_system_one_shot(): |
| 29 | + A, b = sparse_system() |
| 30 | + |
| 31 | + x = polysolve.solve( |
| 32 | + A, |
| 33 | + b, |
| 34 | + SPARSE_SOLVER, |
| 35 | + log_level=polysolve.LogLevel.off, |
| 36 | + ) |
| 37 | + |
| 38 | + assert_solution(A, x, b) |
| 39 | + |
| 40 | + |
| 41 | +def test_linear_solver_reuses_factorization_for_multiple_rhs(): |
| 42 | + A, b = sparse_system() |
| 43 | + solver = polysolve.LinearSolver(SPARSE_SOLVER, log_level=polysolve.LogLevel.off) |
| 44 | + |
| 45 | + solver.analyse_pattern(A) |
| 46 | + with pytest.raises(RuntimeError, match="factorize"): |
| 47 | + solver.solve(b) |
| 48 | + |
| 49 | + solver.factorise(A) |
| 50 | + assert solver.name == "Eigen::SimplicialLDLT" |
| 51 | + assert not solver.is_dense |
| 52 | + |
| 53 | + assert_solution(A, solver.solve(b), b) |
| 54 | + |
| 55 | + b2 = np.array([2.0, -1.0, 0.5]) |
| 56 | + assert_solution(A, solver.solve(b2), b2) |
| 57 | + assert solver.info()["solver_info"] == "Success" |
| 58 | + |
| 59 | + |
| 60 | +def test_solve_dense_system_one_shot(): |
| 61 | + A = np.array( |
| 62 | + [ |
| 63 | + [4.0, 1.0, 0.0], |
| 64 | + [1.0, 3.0, 1.0], |
| 65 | + [0.0, 1.0, 2.0], |
| 66 | + ] |
| 67 | + ) |
| 68 | + b = np.array([1.0, 2.0, 3.0]) |
| 69 | + |
| 70 | + x = polysolve.solve( |
| 71 | + A, |
| 72 | + b, |
| 73 | + DENSE_SOLVER, |
| 74 | + log_level=polysolve.LogLevel.off, |
| 75 | + ) |
| 76 | + |
| 77 | + assert_solution(A, x, b) |
| 78 | + |
| 79 | + |
| 80 | +def test_dense_linear_solver_factorize_alias(): |
| 81 | + A = np.array([[4.0, 1.0], [1.0, 3.0]]) |
| 82 | + b = np.array([1.0, 2.0]) |
| 83 | + solver = polysolve.LinearSolver(DENSE_SOLVER, log_level=polysolve.LogLevel.off) |
| 84 | + |
| 85 | + solver.factorize(A) |
| 86 | + |
| 87 | + assert solver.is_dense |
| 88 | + assert_solution(A, solver.solve(b), b) |
| 89 | + |
| 90 | + |
| 91 | +def test_linear_solver_rejects_invalid_shapes(): |
| 92 | + with pytest.raises(RuntimeError, match="square"): |
| 93 | + polysolve.solve( |
| 94 | + np.ones((2, 3)), |
| 95 | + np.ones(2), |
| 96 | + DENSE_SOLVER, |
| 97 | + log_level=polysolve.LogLevel.off, |
| 98 | + ) |
| 99 | + |
| 100 | + solver = polysolve.LinearSolver(DENSE_SOLVER, log_level=polysolve.LogLevel.off) |
| 101 | + solver.factorize(np.eye(2)) |
| 102 | + |
| 103 | + with pytest.raises(RuntimeError, match="right-hand side"): |
| 104 | + solver.solve(np.ones(3)) |
| 105 | + |
| 106 | + with pytest.raises(RuntimeError, match="initial guess"): |
| 107 | + solver.solve(np.ones(2), x0=np.ones(3)) |
| 108 | + |
| 109 | + |
| 110 | +def test_linear_solver_introspection_helpers(): |
| 111 | + available = polysolve.available_linear_solvers() |
| 112 | + |
| 113 | + assert "Eigen::SimplicialLDLT" in available |
| 114 | + assert polysolve.default_linear_solver() in available |
| 115 | + assert polysolve.LinearSolver.available_solvers() == available |
| 116 | + assert polysolve.LinearSolver.default_solver() == polysolve.default_linear_solver() |
| 117 | + assert isinstance(polysolve.available_linear_preconditioners(), list) |
| 118 | + assert isinstance(polysolve.default_linear_preconditioner(), str) |
0 commit comments