pub trait Gradient {
    type Param;
    type Gradient;

    fn gradient(&self, param: &Self::Param) -> Result<Self::Gradient, Error>;

    fn bulk_gradient<'a, P>(
        &self,
        params: &'a [P]
    ) -> Result<Vec<Self::Gradient>, Error>
    where
        P: Borrow<Self::Param> + SyncAlias,
        Self::Gradient: SendAlias,
        Self: SyncAlias
, { ... } fn parallelize(&self) -> bool { ... } }
Expand description

Defines the computation of the gradient.

Example

use argmin::core::{Gradient, Error};
use argmin_testfunctions::rosenbrock_2d_derivative;

struct Rosenbrock {}

impl Gradient for Rosenbrock {
    type Param = Vec<f64>;
    type Gradient = Vec<f64>;

    /// Compute gradient of rosenbrock function
    fn gradient(&self, param: &Self::Param) -> Result<Self::Gradient, Error> {
        Ok(rosenbrock_2d_derivative(param, 1.0, 100.0))
    }
}

Required Associated Types

Type of the parameter vector

Type of the gradient

Required Methods

Compute gradient

Provided Methods

Compute gradient in bulk. If the rayon feature is enabled, multiple calls to gradient will be run in parallel using rayon, otherwise they will execute sequentially. If the rayon feature is enabled, parallelization can still be turned off by overwriting parallelize to return false. This can be useful in cases where it is preferable to parallelize only certain parts. Note that even if parallelize is set to false, the parameter vectors and the problem are still required to be Send and Sync. Those bounds are linked to the rayon feature. This method can be overwritten.

Indicates whether to parallelize calls to gradient when using bulk_gradient. By default returns true, but can be set manually to false if needed. This allows users to turn off parallelization for certain traits implemented on their problem. Note that parallelization requires the rayon feature to be enabled, otherwise calls to gradient will be executed sequentially independent of how parallelize is set.

Implementors