Skip to main content

Newton

Struct Newton 

Source
pub struct Newton<D: DerivatorSingleVariable = AutoDiffSingle> { /* private fields */ }
Expand description

A scalar Newton solver, optionally damped by a backtracking line search.

The derivative defaults to exact autodiff via AutoDiffSingle (Dual numbers). Any DerivatorSingleVariable can be passed to from_derivator to use a different backend, including finite differences.

With backtracking off (the default), each step is x − f(x)/f′(x). With it on, the step length is halved until |f| decreases, rescuing iterates that would otherwise overshoot the root.

Cost per iteration: 1 function evaluation + 1 derivative evaluation (+ ≤ MAX_BACKTRACK extra function evaluations when backtracking is on).

§Examples

use multicalc::root_finding::Newton;
use multicalc::scalar::c;
use multicalc::scalar_fn;

// f(x) = x² − 2, root at √2 ≈ 1.41421356
let f = scalar_fn!(|x| c(-2.0) + x * x);
let solver: Newton = Newton::default();
let report = solver.solve(&f, 2.0_f64).unwrap();
assert!((report.root - 2.0_f64.sqrt()).abs() < 1e-12);

Implementations§

Source§

impl<D: DerivatorSingleVariable> Newton<D>

Source

pub fn from_derivator(derivator: D) -> Self

Builds a solver with the given derivator and default settings: tolerances of 30 × EPSILON, budget of 100 iterations, backtracking off.

Source

pub fn with_xtol(self, xtol: D::Scalar) -> Self

Sets the step-size tolerance (relative: compared against xtol × (1 + |x|)).

Source

pub fn with_ftol(self, ftol: D::Scalar) -> Self

Sets the residual tolerance: the solver stops when |f(x)| ≤ ftol.

Source

pub fn with_max_iterations(self, max_iterations: usize) -> Self

Sets the maximum number of iterations.

Source

pub fn with_backtracking(self, backtracking: bool) -> Self

Enables or disables backtracking: when on, each Newton step is halved until |f| decreases or the safeguard runs out. Off by default.

Source

pub fn solve<F: ScalarFn>( &self, f: &F, x0: D::Scalar, ) -> Result<RootReport<D::Scalar>, SolveError>
where D::Scalar: Primal,

Finds a root of f starting from x0.

Returns the root estimate and termination reason, or an error: NonFinite if f or its derivative is non-finite, Linalg wrapping Singular if the derivative is zero, or DidNotConverge if the budget is exhausted.

Trait Implementations§

Source§

impl<D: DerivatorSingleVariable + Default> Default for Newton<D>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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, 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.