mod gradient¶
- module gradient¶
Gradient trait and adapters (Analytic / FiniteDiff) plus
DifferentiableObjective. “Native” gradients when the caller can supply them (Ceres-style); explicit central differences otherwise. Gradient<T>` andDifferentiableObjective<T>`: first-derivative support for the typed ``Objalgebra.This is the “native gradient” path, modeled on how Ceres accepts analytic derivatives: when a caller (HMC, projected-gradient polish, GLE with optimal drift, NEB/saddle search in sibling crates, …) can supply a true derivative it is used directly; otherwise an explicit
FiniteDiffGradientadapter falls back to central differences (costing 2·dim extra objective evaluations per gradient).The separation (value-only
Objectivevs.Gradientprovider) mirrors Stan’s log-density / gradient split and Ceres’ residual + optional Jacobian in a single Evaluate. A type can implement both (viaDifferentiableObjective) and supply an efficient fusedvalue_and_gradientto avoid redundant work.Surrogates (
ChebyshevSurrogate,AdditiveSurrogate,ReducedObjective) implement the traits analytically where possible. Built-in test objectives ship with closed-form gradients. Python callables can provide a matchinggrad_fnat construction time ofPyObjective.Traits
- trait DifferentiableObjective<T: Float>¶
An
Objectivethat can also supply its gradient (natively or via adapter).Blanket impl for any O: Objective<T> + Gradient<T>. Concrete types (surrogates, PyObjective with grad, builtins) can override
value_and_gradientfor a fused implementation that re-uses intermediate work (exactly as a Ceres CostFunction computes residuals and Jacobians in one shot).Functions
- fn value_and_gradient(&self, x: ArrayView1<T>) -> (T, Array1<T>)¶
Returns
(f(x), ∇f(x)). Default calls the two methods separately; analytic implementations should override for efficiency.
Implemented for
-
impl<T: Float, O> DifferentiableObjective<T> for O¶
where
O: Objective<T> + Gradient<T>
¶
- trait Gradient<T: Float>¶
First-derivative interface. Types that know an analytic gradient implement this directly (or via
DifferentiableObjective).When only a black-box
Objectiveis available, wrap it withFiniteDiffGradient. When a closure or Python grad callable is known, useAnalyticGradient(or construct aPyObjectivewithgrad_fn).Functions
- fn dim(&self) -> usize¶
Number of input dimensions (must match the paired
Objective::dim).
- fn grad(&self, x: ArrayView1<T>) -> Array1<T>¶
Gradient of the underlying scalar field at
x.
Implemented for
Structs and Unions
-
struct AnalyticGradient<F>¶
where
F: Fn(ArrayView1<f64>) -> Array1<f64> + Send + Sync
¶ Closure-based analytic gradient (the common case for user Rust code that has a hand-derived or AD-produced x |-> ∇f(x)).
- grad_fn: F¶
User-supplied analytic gradient callback.
- dim: usize¶
Number of input dimensions accepted by
grad_fn.
Implementations
-
impl<F> AnalyticGradient<F>¶
where
F: Fn(ArrayView1<f64>) -> Array1<f64> + Send + Sync
¶ Functions
- fn new(dim: usize, grad_fn: F) -> Self¶
Constructs an analytic-gradient adapter from a dimension and callback.
Traits implemented
-
impl<F> Gradient<f64> for AnalyticGradient<F>¶
where
F: Fn(ArrayView1<f64>) -> Array1<f64> + Send + Sync
¶
- struct FiniteDiffGradient<O: Objective<f64>>¶
Central-difference adapter around any Objective<f64>. Use only when no native gradient is available.
- obj: O¶
Objective evaluated by the central-difference stencil.
- h: f64¶
Symmetric perturbation step.
Implementations
- impl<O: Objective<f64>> FiniteDiffGradient<O>¶
Functions
- fn new(obj: O) -> Self¶
Constructs a central-difference adapter with a conservative default step.
- fn with_step(obj: O, h: f64) -> Self¶
Constructs a central-difference adapter with a caller-selected step.
Traits implemented
- impl<O: Objective<f64> + Send + Sync> Gradient<f64> for FiniteDiffGradient<O>¶