Gradient

The classes ForwardGradient, BackwardGradient and SymetricGradient allow to approximate the value of the gradient of a differentiable function.

Exemple – Gradient of the sinus function
// Create the function
const auto f = [](const Vect<double> &x) {
    return sin(x(0));
};

// Create its gradient
const auto grad = ForwardGradient<decltype(f)>(f, 1e-7);

// Compute the value of the gradient at a given point
const double value = grad(std::vector<double>{1})(0);
template<typename F>
class ForwardGradient
#include <gradient.hpp>

Functor computing the gradient of a function with a given value of epsilon using the method of forward gradient. The function f is evaluated (n + 1) times, where n is the dimension of the space.

Public Functions

inline ForwardGradient(const F &f, const double epsilon)
inline Vect<double> operator()(Vect<double> x) const

Private Members

F m_f
double m_eps
template<typename F>
class BackwardGradient
#include <gradient.hpp>

Functor computing the gradient of a function with a given value of epsilon using the method of backward gradient. The function f is evaluated (n + 1) times, where n is the dimension of the space.

Public Functions

inline BackwardGradient(const F &f, const double epsilon)
inline Vect<double> operator()(Vect<double> x) const

Private Members

F m_f
double m_eps
template<typename F>
class SymetricGradient
#include <gradient.hpp>

Functor computing the gradient of a function with a given value of epsilon using the method of symetric gradient. The function f is evaluated 2n times, where n is the dimension of the space. However, the method is more precise than the others.

Public Functions

inline SymetricGradient(const F &f, const double epsilon)
inline Vect<double> operator()(Vect<double> x) const

Private Members

F m_f
double m_eps