Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant RootFinder Trait

From Leeroopedia


Knowledge Sources
Domains Numerical_Methods, Mathematics
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete trait and supporting data structure for root-finding algorithms provided by the RustQuant library.

Description

The rootfinder module defines the Rootfinder trait and the RootfinderData struct, which together form the foundation of all root-finding algorithms in RustQuant.

The Rootfinder<F> trait is generic over a function type F: Fn(f64) -> f64 and requires four methods:

  • value(&self, x: f64) -> f64 -- evaluates the objective function at x.
  • derivative(&self, x: f64) -> f64 -- evaluates the derivative at x (returns 0.0 for derivative-free methods).
  • solve_impl(&mut self) -> f64 -- the back-end implementation of the specific algorithm, called after a valid bracket is found.
  • solve(&mut self) -> f64 -- the front-end entry point that performs validation, bracket searching, and delegates to solve_impl().

The trait also defines a constant MAX_ITERATIONS: i32 = 1000 that all implementations share.

The RootfinderData struct holds all shared state for root-finding algorithms:

  • Public fields: root (current root estimate), stepsize (initial step for bracket expansion), accuracy (convergence tolerance), interval (lower/upper bound tuple), and interval_enforced (whether bounds are strictly enforced).
  • Internal fields (crate-visible): x_min, x_max, y_min, y_max (bracket endpoints and their function values), and iteration_count.

Key utility methods on RootfinderData include:

  • new() -- constructs with explicit parameters.
  • Default::default() -- provides sensible defaults (interval [f64::MIN, f64::MAX], stepsize 1e-6, accuracy sqrt(EPSILON)).
  • enforce_bounds() -- clamps a value to the configured interval if enforcement is enabled.
  • close(x, y) -- checks if two values are approximately equal using a tolerance of 42 * EPSILON with relative comparison.
  • nrsign(a, b) -- returns |a| with the sign of b (used internally by Brent's method).

Usage

Use the Rootfinder trait and RootfinderData when building or selecting root-finding algorithms. All concrete solvers in RustQuant (Bisection, Brent, Newton-Raphson) implement this trait, so you can write generic code that works with any solver. Construct a RootfinderData to configure accuracy, step size, and bounds, then pass it to the chosen solver.

Code Reference

Source Location

Signature

pub trait Rootfinder<F>
where
    F: Fn(f64) -> f64,
{
    const MAX_ITERATIONS: i32 = 1000;

    fn value(&self, x: f64) -> f64;
    fn derivative(&self, x: f64) -> f64;
    fn solve_impl(&mut self) -> f64;
    fn solve(&mut self) -> f64;
}

#[derive(Debug, Clone, Copy)]
pub struct RootfinderData {
    pub root: f64,
    pub stepsize: f64,
    pub accuracy: f64,
    pub interval: (f64, f64),
    pub interval_enforced: bool,
    // crate-private: x_min, x_max, y_min, y_max, iteration_count
}

impl RootfinderData {
    pub fn new(
        accuracy: f64,
        stepsize: f64,
        lower_bound: f64,
        upper_bound: f64,
        interval_enforced: bool,
    ) -> Self;
}

impl Default for RootfinderData { ... }

Import

use RustQuant::math::rootfinding::{Rootfinder, RootfinderData};

I/O Contract

Inputs

Name Type Required Description
accuracy f64 Yes Convergence tolerance for the root estimate
stepsize f64 Yes Initial step size for bracket expansion during the search phase
lower_bound f64 Yes Lower bound of the search interval
upper_bound f64 Yes Upper bound of the search interval
interval_enforced bool Yes Whether to clamp values to [lower_bound, upper_bound]

Outputs

Name Type Description
RootfinderData RootfinderData A configuration struct to be passed to any solver implementing the Rootfinder trait

Usage Examples

use RustQuant::math::rootfinding::{Rootfinder, RootfinderData};

// Create a RootfinderData with explicit parameters:
//   accuracy = 1e-15, step_size = 1e-5,
//   lower_bound = 0.0, upper_bound = 2.0,
//   interval_enforced = true
let data = RootfinderData::new(1e-15, 1e-5, 0.0, 2.0, true);

// Or use sensible defaults:
//   interval = [f64::MIN, f64::MAX], stepsize = 1e-6,
//   accuracy = sqrt(EPSILON), interval_enforced = true
let default_data = RootfinderData::default();

// Pass `data` to any solver (Bisection, Brent, NewtonRaphson).

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment