(x, a, b, dx)
| 1719 | return (f - (df * (x - a))) * (f - (df * (x - b))) < 0 |
| 1720 | |
| 1721 | def newton(x, a, b, dx): |
| 1722 | if abs(dx) < abs(tol * x): |
| 1723 | return x |
| 1724 | |
| 1725 | fx_tup = f_memo(x) |
| 1726 | f = fx_tup[0] |
| 1727 | df = fx_tup[1] |
| 1728 | |
| 1729 | if f == 0: |
| 1730 | return x |
| 1731 | |
| 1732 | a_prime = x if f < 0 else a |
| 1733 | b_prime = x if f > 0 else b |
| 1734 | |
| 1735 | if ( |
| 1736 | dx != x_max - x_min |
| 1737 | and dx * (f / df) < 0 |
| 1738 | and f_memo(lazy(a_prime))[0] * f_memo(lazy(b_prime))[0] > 0 |
| 1739 | ): |
| 1740 | raise ValueError("failed to bracket the root in find_root_deriv") |
| 1741 | |
| 1742 | if isinstance(a, Number) and isinstance(b, Number): |
| 1743 | is_in_bounds = in_bounds(x, f, df, a, b) |
| 1744 | else: |
| 1745 | is_in_bounds = in_bounds(x, f, df, x_min, x_max) |
| 1746 | |
| 1747 | if is_in_bounds: |
| 1748 | return newton(x - (f / df), a_prime, b_prime, f / df) |
| 1749 | |
| 1750 | av = lazy(a) |
| 1751 | bv = lazy(b) |
| 1752 | dx_prime = 0.5 * (bv - av) |
| 1753 | a_pp = av if a == a_prime else a_prime |
| 1754 | b_pp = bv if b == b_prime else b_prime |
| 1755 | |
| 1756 | return newton((av + bv) * 0.5, a_pp, b_pp, dx_prime) |
| 1757 | |
| 1758 | if x_guess is None: |
| 1759 | x_guess = (x_min + x_max) * 0.5 |
no test coverage detected