numerics
Loading...
Searching...
No Matches
roots.hpp
Go to the documentation of this file.
1/// @file roots.hpp
2/// @brief Root-finding methods for scalar equations f(x) = 0
3#pragma once
4
5#include "analysis/types.hpp"
6
7namespace num {
8
9struct RootResult {
12 real residual; ///< |f(root)|
14};
15
16/// @brief Bisection method
17/// @param f Continuous function
18/// @param a,b Bracket: f(a) and f(b) must have opposite signs
19/// @param tol Convergence tolerance on |f(root)| and interval width
20/// @param max_iter Maximum iterations
22 real tol = 1e-10, idx max_iter = 1000);
23
24/// @brief Newton-Raphson method
25/// @param f Function
26/// @param df Derivative of f
27/// @param x0 Initial guess
28/// @param tol Convergence tolerance
29/// @param max_iter Maximum iterations
31 real tol = 1e-10, idx max_iter = 1000);
32
33/// @brief Secant method (Newton without derivative)
34/// @param f Function
35/// @param x0,x1 Two distinct initial guesses
36/// @param tol Convergence tolerance
37/// @param max_iter Maximum iterations
39 real tol = 1e-10, idx max_iter = 1000);
40
41/// @brief Brent's method (bisection + secant + inverse quadratic interpolation)
42///
43/// Guaranteed to converge if f(a) and f(b) have opposite signs.
44/// Superlinear convergence near smooth roots.
45/// Preferred method when a reliable bracket is available.
46///
47/// @param f Function
48/// @param a,b Bracket: f(a) and f(b) must have opposite signs
49/// @param tol Convergence tolerance
50/// @param max_iter Maximum iterations
52 real tol = 1e-10, idx max_iter = 1000);
53
54} // namespace num
Common function type aliases for the analysis module.
RootResult secant(ScalarFn f, real x0, real x1, real tol=1e-10, idx max_iter=1000)
Secant method (Newton without derivative)
Definition roots.cpp:39
double real
Definition types.hpp:10
RootResult bisection(ScalarFn f, real a, real b, real tol=1e-10, idx max_iter=1000)
Bisection method.
Definition roots.cpp:8
RootResult brent(ScalarFn f, real a, real b, real tol=1e-10, idx max_iter=1000)
Brent's method (bisection + secant + inverse quadratic interpolation)
Definition roots.cpp:54
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
std::size_t idx
Definition types.hpp:11
RootResult newton(ScalarFn f, ScalarFn df, real x0, real tol=1e-10, idx max_iter=1000)
Newton-Raphson method.
Definition roots.cpp:25
constexpr real e
Definition math.hpp:41
std::function< real(real)> ScalarFn
Real-valued scalar function f(x)
Definition types.hpp:11
real residual
|f(root)|
Definition roots.hpp:12