pub struct FiniteDiff<P> { /* private fields */ }Expand description
Wraps a problem to synthesize its derivatives by finite differences.
Construct with FiniteDiff::new (central gradient/Hessian, forward
Jacobian — see the module docs) and adjust with the builder
methods. The wrapper delegates CostFunction / Residual /
BoxConstraints to the inner problem and implements Gradient /
Jacobian / Hessian via finite differences.
§Backends
Gradient is backend-generic (any V: Clone + VectorLen + VectorIndex). Jacobian and Hessian additionally require
V: DenseMatrixFromFn, so they are available only for the matrix
backends (nalgebra DVector → DMatrix, faer Col → Mat) — Vec<f64>
and ndarray produce a compile-time error, mirroring the analytic
Jacobian / Hessian coverage (tenet 5).
§Examples
Run a gradient solver against a problem that only implements
CostFunction: wrapping it in FiniteDiff synthesizes the
Gradient by central differences.
use basin::{BasicState, CostFunction, Executor, FiniteDiff, GradientDescent, GradientTolerance};
struct Sphere;
impl CostFunction for Sphere {
type Param = Vec<f64>;
type Output = f64;
fn cost(&self, x: &Vec<f64>) -> f64 {
x.iter().map(|xi| xi * xi).sum()
}
}
let result = Executor::new(
FiniteDiff::new(Sphere),
GradientDescent::new(0.1),
BasicState::new(vec![1.0, 1.0]),
)
.max_iter(1_000)
.terminate_on(GradientTolerance(1e-8))
.run();
assert!(result.cost() < 1e-10);Implementations§
Source§impl<P> FiniteDiff<P>
impl<P> FiniteDiff<P>
Sourcepub fn new(problem: P) -> Self
pub fn new(problem: P) -> Self
Wrap problem with default settings: central-difference gradient
and Hessian, forward-difference (MINPACK fdjac2) Jacobian,
function_precision = f64::EPSILON, adaptive step sizes.
Sourcepub fn gradient_method(self, method: Method) -> Self
pub fn gradient_method(self, method: Method) -> Self
Set the stencil used for the gradient (default Method::Central).
Sourcepub fn jacobian_method(self, method: Method) -> Self
pub fn jacobian_method(self, method: Method) -> Self
Set the stencil used for the Jacobian (default Method::Forward,
the MINPACK fdjac2 parity choice).
Sourcepub fn hessian_method(self, method: Method) -> Self
pub fn hessian_method(self, method: Method) -> Self
Set the stencil used for the Hessian (default Method::Central).
Sourcepub fn function_precision(self, epsfcn: f64) -> Self
pub fn function_precision(self, epsfcn: f64) -> Self
Set the assumed relative accuracy of the wrapped function (MINPACK’s
epsfcn). Larger values widen the step, which helps when the
function is noisy. Floored at f64::EPSILON. Default f64::EPSILON.
Sourcepub fn with_step(self, h: f64) -> Self
pub fn with_step(self, h: f64) -> Self
Override the adaptive step rule with a fixed absolute step h used
for every coordinate. Escape hatch — most callers should leave the
adaptive |xⱼ|-scaled rule in place.
Sourcepub fn into_inner(self) -> P
pub fn into_inner(self) -> P
Unwrap and return the inner problem.
Trait Implementations§
Source§impl<P: BoxConstraints> BoxConstraints for FiniteDiff<P>
impl<P: BoxConstraints> BoxConstraints for FiniteDiff<P>
Source§impl<P: Clone> Clone for FiniteDiff<P>
impl<P: Clone> Clone for FiniteDiff<P>
Source§fn clone(&self) -> FiniteDiff<P>
fn clone(&self) -> FiniteDiff<P>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<P: CostFunction> CostFunction for FiniteDiff<P>
impl<P: CostFunction> CostFunction for FiniteDiff<P>
Source§type Param = <P as CostFunction>::Param
type Param = <P as CostFunction>::Param
Source§type Output = <P as CostFunction>::Output
type Output = <P as CostFunction>::Output
f64 (see AGENTS.md’s
provisional choices).Source§impl<P: Debug> Debug for FiniteDiff<P>
impl<P: Debug> Debug for FiniteDiff<P>
Source§impl<P, V> Gradient for FiniteDiff<P>
impl<P, V> Gradient for FiniteDiff<P>
Source§impl<P, V> Hessian for FiniteDiff<P>where
P: CostFunction<Param = V, Output = f64>,
V: Clone + VectorLen + VectorIndex + DenseMatrixFromFn,
impl<P, V> Hessian for FiniteDiff<P>where
P: CostFunction<Param = V, Output = f64>,
V: Clone + VectorLen + VectorIndex + DenseMatrixFromFn,
Source§impl<P, V> Jacobian for FiniteDiff<P>
impl<P, V> Jacobian for FiniteDiff<P>
Source§impl<P: Residual> Residual for FiniteDiff<P>
impl<P: Residual> Residual for FiniteDiff<P>
impl<P: Copy> Copy for FiniteDiff<P>
Auto Trait Implementations§
impl<P> Freeze for FiniteDiff<P>where
P: Freeze,
impl<P> RefUnwindSafe for FiniteDiff<P>where
P: RefUnwindSafe,
impl<P> Send for FiniteDiff<P>where
P: Send,
impl<P> Sync for FiniteDiff<P>where
P: Sync,
impl<P> Unpin for FiniteDiff<P>where
P: Unpin,
impl<P> UnsafeUnpin for FiniteDiff<P>where
P: UnsafeUnpin,
impl<P> UnwindSafe for FiniteDiff<P>where
P: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.