Skip to content

Commit

Permalink
add tests for njint_brentq
Browse files Browse the repository at this point in the history
  • Loading branch information
amatissart committed Jun 1, 2024
1 parent d059bb2 commit 26ea68d
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions solidago/tests/test_solvers.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 26ea68d

Please sign in to comment.