11 throw std::invalid_argument(
"bisection: f(a) and f(b) must have opposite signs");
17 return {
mid,
i + 1, std::abs(
fm),
true};
29 if (std::abs(
fx) <
tol)
30 return {x,
i + 1, std::abs(
fx),
true};
32 if (std::abs(
dfx) < 1
e-14)
33 return {x,
i + 1, std::abs(
fx),
false};
36 return {x,
max_iter, std::abs(
f(x)),
false};
42 if (std::abs(
f1) <
tol)
43 return {
x1,
i + 1, std::abs(
f1),
true};
45 if (std::abs(
df) < 1
e-14)
46 return {
x1,
i + 1, std::abs(
f1),
false};
57 throw std::invalid_argument(
"brent: f(a) and f(b) must have opposite signs");
66 if (std::abs(
fc) < std::abs(
fb)) {
76 return {
b,
i + 1, std::abs(
fb),
true};
78 if (std::abs(
e) >=
tol1 && std::abs(
fa) > std::abs(
fb)) {
90 p = s * (2.0 *
mid * t * (t -
r) - (
b -
a) * (
r - 1.0));
91 q = (t - 1.0) * (
r - 1.0) * (s - 1.0);
93 if (p > 0.0) q = -q;
else p = -p;
97 if (2.0 * p < std::min(3.0 *
mid * q - std::abs(
tol1 * q),
RootResult secant(ScalarFn f, real x0, real x1, real tol=1e-10, idx max_iter=1000)
Secant method (Newton without derivative)
RootResult bisection(ScalarFn f, real a, real b, real tol=1e-10, idx max_iter=1000)
Bisection method.
RootResult brent(ScalarFn f, real a, real b, real tol=1e-10, idx max_iter=1000)
Brent's method (bisection + secant + inverse quadratic interpolation)
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
RootResult newton(ScalarFn f, ScalarFn df, real x0, real tol=1e-10, idx max_iter=1000)
Newton-Raphson method.
std::function< real(real)> ScalarFn
Real-valued scalar function f(x)
Root-finding methods for scalar equations f(x) = 0.