Skip to main content

Crate iter_solver

Crate iter_solver 

Source
Expand description

§A General Iterative Algorithm Framework

iter-solver is a flexible and general iterative algorithm framework that allows users to customize iteration logic and termination conditions, providing a unified abstraction for different problems and algorithms.

§Example

The following program defines a simple Newton iterative solver to compute ln(1.5):

use iter_solver::Solver;

fn f_and_df(x: f64) -> (f64, f64) {
    let fx = x.exp() - 1.5;
    let dfx = x.exp();
    (fx, dfx) 
}

fn main() {
    let iter_fn = |state: &f64, problem: &fn(f64) -> (f64, f64)| {
        let x_n = *state;
        let (fx, dfx) = problem(x_n);
        x_n - (fx / dfx)
    };
 
    let term_cond = |state: &f64, problem: &fn(f64) -> (f64, f64)| {
        let (fx, _) = problem(*state);
        fx.abs() < 1e-6
    };
 
    let solver = Solver::new(iter_fn, term_cond);
 
    let solution = solver.solve(1.5, &(f_and_df as fn(f64) -> (f64, f64)));
 
    println!("solver's solution: {}", solution);
    println!("use std function ln: {}", 1.5_f64.ln());
}

§Installation

Add the following to your Cargo.toml dependencies:

[dependencies]
iter-solver = "0.3"

or add it directly from the terminal:

cargo add iter-solver

§License

MIT License.

Modules§

error
Error types.

Structs§

Solver
Solver type.
SolverIterator
Solver iterator, used to step through the solving process and output the state of each iteration.

Traits§

IterState
Intermediate state of an iterative algorithm.

Derive Macros§

IterState
A derive macro that automatically derives the IterState trait for structs that satisfy the Clone trait.