pub trait Function<T, U, E>where
T: Float + FromPrimitive,{
// Required method
fn evaluate(&self, x: &[T], user_data: &mut U) -> Result<T, E>;
// Provided method
fn gradient(
&self,
x: &[T],
grad: &mut [T],
user_data: &mut U,
) -> Result<(), E> { ... }
}Expand description
A trait which describes a function $f(\mathbb{R}^n) \to \mathbb{R}$
Such a function may also take a user_data: &mut U field which can be used to pass external
arguments to the function during minimization, or can be modified by the function itself.
The Function trait takes a generic T which represents a numeric scalar, a generic U
representing the type of user data/arguments, and a generic E representing any possible
errors that might be returned during function execution.
There is also a default implementation of a gradient function which uses a central finite-difference method to evaluate derivatives. If an exact gradient is known, it can be used to speed up gradient-dependent algorithms.
Required Methods§
sourcefn evaluate(&self, x: &[T], user_data: &mut U) -> Result<T, E>
fn evaluate(&self, x: &[T], user_data: &mut U) -> Result<T, E>
The evaluation of the function at a point x with the given arguments/user data.
§Errors
Returns an Err(E) if the evaluation fails. Users should implement this trait to return a
std::convert::Infallible if the function evaluation never fails.
Provided Methods§
sourcefn gradient(&self, x: &[T], grad: &mut [T], user_data: &mut U) -> Result<(), E>
fn gradient(&self, x: &[T], grad: &mut [T], user_data: &mut U) -> Result<(), E>
The evaluation of the gradient at a point x with the given arguments/user data. The
gradient is passed by mutable reference as an input, which saves a bit on memory allocation
and also provides a similar format to some other fitting libraries.
§Errors
Returns an Err(E) if the evaluation fails. See Function::evaluate for more
information.