Trait ganesh::core::Function

source ·
pub trait Function<F, A, E>: Send + Sync
where 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 implements Field and has a 'static lifetime.
  • 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§

source

fn evaluate(&self, x: &[F], args: Option<&A>) -> Result<F, E>

Evaluates the function at the given point.

§Arguments
  • x: A slice of F representing 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§

source

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 of F representing 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.

source

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 of F representing 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:

  • The gradient at x as a DVector of F
  • The Hessian at x as a DMatrix of F
§Errors

Returns an error of type E if Function::evaluate fails.

Implementors§