pub trait Solver: Send + Sync {
// Required methods
fn solve<F, D>(
&self,
f: F,
derivative: Option<D>,
initial_guess: f64,
bounds: Option<(f64, f64)>,
config: &SolverConfig,
) -> MathResult<SolverResult>
where F: Fn(f64) -> f64,
D: Fn(f64) -> f64;
fn name(&self) -> &'static str;
}Expand description
Trait for root-finding solvers with optional derivative.
This trait provides a unified interface for all solvers, allowing the caller to optionally provide a derivative function for faster convergence.
§Example
use convex_math::solvers::{Solver, NewtonSolver, SolverConfig};
let solver = NewtonSolver::default();
let f = |x: f64| x * x - 2.0;
let df = |x: f64| 2.0 * x;
let result = solver.solve(f, Some(df), 1.5, None, &SolverConfig::default()).unwrap();
assert!((result.root - std::f64::consts::SQRT_2).abs() < 1e-10);Required Methods§
Sourcefn solve<F, D>(
&self,
f: F,
derivative: Option<D>,
initial_guess: f64,
bounds: Option<(f64, f64)>,
config: &SolverConfig,
) -> MathResult<SolverResult>
fn solve<F, D>( &self, f: F, derivative: Option<D>, initial_guess: f64, bounds: Option<(f64, f64)>, config: &SolverConfig, ) -> MathResult<SolverResult>
Solves for a root of the given function.
§Arguments
f- The function for which to find a rootderivative- Optional derivative function (used if available)initial_guess- Starting point for the searchbounds- Optional bracketing interval (a, b)config- Solver configuration
§Returns
The solution result including root, iterations, and residual.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.