mathhook_core/algebra/
root_finding.rs

1//! Root-finding algorithms
2//!
3//! Provides numerical methods for finding roots (zeros) of functions.
4//! All methods work with closures for maximum flexibility.
5
6pub mod bisection;
7pub mod newton_raphson;
8pub mod secant;
9
10pub use bisection::BisectionMethod;
11pub use newton_raphson::NewtonRaphson;
12pub use secant::SecantMethod;
13
14use crate::error::MathError;
15
16/// Configuration for root-finding algorithms
17#[derive(Debug, Clone)]
18pub struct RootFindingConfig {
19    pub tolerance: f64,
20    pub max_iterations: usize,
21    pub derivative_h: f64,
22}
23
24impl Default for RootFindingConfig {
25    fn default() -> Self {
26        Self {
27            tolerance: 1e-10,
28            max_iterations: 1000,
29            derivative_h: 1e-8,
30        }
31    }
32}
33
34/// Result of root-finding operation
35#[derive(Debug, Clone)]
36pub struct RootResult {
37    pub root: f64,
38    pub iterations: usize,
39    pub function_value: f64,
40    pub converged: bool,
41}
42
43/// Trait for root-finding methods
44pub trait RootFinder {
45    /// Find a root of the given function
46    ///
47    /// # Arguments
48    ///
49    /// * `f` - Function to find root of
50    /// * `config` - Root-finding configuration
51    ///
52    /// # Returns
53    ///
54    /// Root result with convergence information
55    fn find_root<F>(&self, f: F, config: &RootFindingConfig) -> Result<RootResult, MathError>
56    where
57        F: Fn(f64) -> f64;
58}