pub trait Function<F, A, E>: Send + Syncwhere
F: Field + 'static,{
// Required method
fn evaluate(&self, x: &[F], args: Option<&A>) -> Result<F, E>;
// Provided methods
fn gradient(&self, x: &[F], args: Option<&A>) -> Result<DVector<F>, E> { ... }
fn gradient_and_hessian(
&self,
x: &[F],
args: Option<&A>,
) -> Result<(DVector<F>, DMatrix<F>), E> { ... }
}Expand description
Represents a multivariate function that can be evaluated and differentiated.
This trait is generic over the field type F, an argument type A, and an error type E.
§Type Parameters
F: A type that implementsFieldand has a'staticlifetime.A: A type that can be used to pass arguments to the wrapped function.E: The error type returned by the function’s methods.
Required Methods§
sourcefn evaluate(&self, x: &[F], args: Option<&A>) -> Result<F, E>
fn evaluate(&self, x: &[F], args: Option<&A>) -> Result<F, E>
Evaluates the function at the given point.
§Arguments
x: A slice ofFrepresenting the point at which to evaluate the function.args: An optional argument struct used to pass static arguments to the internal function.
§Returns
The function value at x of type F.
§Errors
Returns an error of type E if the evaluation fails.
Provided Methods§
sourcefn gradient(&self, x: &[F], args: Option<&A>) -> Result<DVector<F>, E>
fn gradient(&self, x: &[F], args: Option<&A>) -> Result<DVector<F>, E>
Computes the gradient of the function at the given point using central finite differences.
Overwrite this method if the true gradient function is known.
§Arguments
x: A slice ofFrepresenting the point at which to compute the gradient.args: An optional argument struct used to pass static arguments to the internal function.
§Returns
A DVector of F representing the gradient.
§Errors
Returns an error of type E if Function::evaluate fails.
sourcefn gradient_and_hessian(
&self,
x: &[F],
args: Option<&A>,
) -> Result<(DVector<F>, DMatrix<F>), E>
fn gradient_and_hessian( &self, x: &[F], args: Option<&A>, ) -> Result<(DVector<F>, DMatrix<F>), E>
Computes both the gradient and the Hessian matrix of the function at the given point.
This method uses central finite differences to approximate both the gradient and the Hessian.
§Arguments
x: A slice ofFrepresenting the point at which to compute the gradient and Hessian.args: An optional argument struct used to pass static arguments to the internal function.
§Returns
A tuple containing:
§Errors
Returns an error of type E if Function::evaluate fails.