Struct Bisection

Source
pub struct Bisection<F> { /* private fields */ }
Expand description

Simple bisection method.

See https://en.wikipedia.org/wiki/Bisection_method for details on the algorithm.

The x returned is an array [f64;2] which bounds the root. The midpoint of the range is the best estimate of the root, and x_mid and f_mid (the function evalution at the midpoint) are also available on the algo

§Examples

from https://en.wikipedia.org/wiki/Bisection_method#Example:_Finding_the_root_of_a_polynomial

use stepwise::{Driver as _, fixed_iters, algos::Bisection, assert_approx_eq};

let f = |x: f64| x.powi(3) - x - 2.0;

let algo = Bisection::from_fn(f, [1.0, 2.0]).expect("range didn't bound the solution");
let mut driver = fixed_iters(algo, 15);

println!("iter       x0        x1         x-mid         f");

let (solved, step) = driver
    .on_step(|algo, step| {
       let [x0, x1] = algo.x();
       println!(
          "{i:>4}  [{x0:.7}, {x1:.7}]  {xmid:.7}  {f:+.7}",
          i = step.iteration(),
          xmid = (x0 + x1) / 2.0,
          f = algo.f_mid()
       );
    })
    .solve()
    .unwrap();

assert_approx_eq!(solved.x().as_slice(), &[1.5213, 1.5213], 0.0001);

Implementations§

Source§

impl Bisection<()>

Source

pub fn from_fn<G>( g: G, range: [f64; 2], ) -> Result<Bisection<impl FnMut(f64) -> Result<f64, Infallible>>, DriverError>
where G: FnMut(f64) -> f64,

Create a new bisection algo from an infalliable function f: f64 -> f64

Source§

impl Bisection<()>

Source

pub fn from_falliable_fn<F, E>( f: F, [lo, hi]: [f64; 2], ) -> Result<Bisection<F>, DriverError>
where F: FnMut(f64) -> Result<f64, E>, E: 'static + Send + Sync + Error,

Create a new bisection algo from an falliable function f: f64 -> Result<f64,E>

§Errors
Source§

impl<F> Bisection<F>

Source

pub fn x(&self) -> [f64; 2]

Source

pub fn x_mid(&self) -> f64

The midpoint of the current range.

Source

pub fn x_spread(&self) -> f64

The width or spread of the current range.

Source

pub fn f_mid(&self) -> f64

The function, f, evaluated at the midpoint of the current range.

Trait Implementations§

Source§

impl<E, F> Algo for Bisection<F>
where F: FnMut(f64) -> Result<f64, E>, E: 'static + Send + Sync + Error,

Source§

type Error = E

Source§

fn step(&mut self) -> (ControlFlow<()>, Result<(), E>)

Source§

fn read_file(&mut self, fmt: &FileFormat, r: &mut dyn Read) -> Result<(), Error>

Implement this method to support recovery from a checkpoint file Read more
Source§

fn write_file(&self, fmt: &FileFormat, w: &mut dyn Write) -> Result<(), Error>

Implement this trait to support writing checkpoint files Read more
Source§

impl<F: Clone> Clone for Bisection<F>

Source§

fn clone(&self) -> Bisection<F>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F> Debug for Bisection<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: PartialEq> PartialEq for Bisection<F>

Source§

fn eq(&self, other: &Bisection<F>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F> StructuralPartialEq for Bisection<F>

Auto Trait Implementations§

§

impl<F> Freeze for Bisection<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for Bisection<F>
where F: RefUnwindSafe,

§

impl<F> Send for Bisection<F>
where F: Send,

§

impl<F> Sync for Bisection<F>
where F: Sync,

§

impl<F> Unpin for Bisection<F>
where F: Unpin,

§

impl<F> UnwindSafe for Bisection<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.