From 26ea68dac2348578e2c5bf7c925fc43f328eac65 Mon Sep 17 00:00:00 2001 From: Adrien Matissart Date: Sat, 1 Jun 2024 11:59:20 +0200 Subject: [PATCH] add tests for njint_brentq --- solidago/tests/test_solvers.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/solidago/tests/test_solvers.py b/solidago/tests/test_solvers.py index 8aa9982fbb..2a841aaaea 100644 --- a/solidago/tests/test_solvers.py +++ b/solidago/tests/test_solvers.py @@ -1,7 +1,35 @@ import pytest +from numba import njit + +from solidago.solvers.dichotomy import solve as dichotomy_solve +from solidago.solvers.optimize import njit_brentq as brentq -from solidago.solvers.dichotomy import solve def test_dichotomy(): - assert solve(lambda t: t, 1, 0, 3) == pytest.approx(1, abs=1e-4) - assert solve(lambda t: 2 - t**2, 1, 0, 3) == pytest.approx(1, abs=1e-4) + assert dichotomy_solve(lambda t: t, 1, 0, 3) == pytest.approx(1, abs=1e-4) + assert dichotomy_solve(lambda t: 2 - t**2, 1, 0, 3) == pytest.approx(1, abs=1e-4) + + +def test_brentq_fails_to_converge_for_non_zero_method(): + @njit + def one_plus_x_square(x): + return 1 + x * x + + with pytest.raises(ValueError): + brentq(one_plus_x_square, extend_bounds="no") + + +def test_brentq_finds_zeros_of_simple_increasing_linear(): + @njit + def x_plus_five(x): + return x + 5 + + assert brentq(x_plus_five, extend_bounds="ascending") == -5.0 + + +def test_brentq_finds_zeros_of_simple_decreasing_linear(): + @njit + def minus_x_plus_twelve(x): + return -x + 12 + + assert brentq(minus_x_plus_twelve, extend_bounds="descending") == 12.0