Skip to content

TwiceDifferentiableScalarField

core/OPT/ScalarField.h        Extends: DifferentiableScalarField

Template class used to represent a scalar field whose hessian function is known analitically at every point.

template <unsigned int N> 
class TwiceDifferentiableScalarField : public DifferentiableScalarField<N> { ... };

Methods

TwiceDifferentiableScalarField(std::function<double(SVector<N>)> f_, 
                               std::function<SVector<N>(SVector<N>)> df_, 
                               std::function<SMatrix<N>(SVector<N>)> ddf_)

Constructor

Args Description
std::function<double(SVector<N>)> f_ An std::function implementing the analytical expression of the field taking an N dimensional point in input and returning a double in output
std::function<SVector<N>(SVector<N>)> df_ An std::function implementing the analytical expression of the vector field taking an N dimensional point in input and returning an N dimensional point in output representing the gradient expression
std::function<SMatrix<N>(SVector<N>)> ddf_ An std::function implementing the analytical expression of the hessian function taking an N dimensional point in input and returning an N dimensional square matrix in output representing the hessian expression
std::function<SMatrix<N>(SVector<N>)> deriveTwice() const override;

Returns the analytical expression of the field's hessian as a Callable object.

Examples

Define a scalar field having exact gradient equal to and exact hessian matrix given by

// define the field analytical expression: 2*x^2 + 2*y^2
std::function<double(SVector<2>)> g = [](SVector<2> x) -> double { 
    return 2*std::pow(x[0],2) + 2*std::pow(x[1],2); 
    };

// define analytical expression of gradient field
std::function<SVector<2>(SVector<2>)> dg = [](SVector<2> x) -> SVector<2> { 
    return SVector<2>({4*x[0], 
                       4*x[1]}); 
    };

// define analytical expression of hessian matrix
std::function<SMatrix<2>(SVector<2>)> ddg = [](SVector<2> x) -> SMatrix<2> { 
    return SMatrix<2>({{4, 0},
                       {0, 4}});
    };

// define twice differentiable field
TwiceDifferentiableScalarField<2> field(g, dg, ddg);

std::cout << "evaluation of field at point" << std::endl;
std::cout << field.evaluateAtPoint(SVector<2>(4,1)) << std::endl;

// get approximation of hessian at point
SVector<2> hess = field.getHessianApprox(SVector<2>(2,1), 0.001);

std::cout << "approximation of gradient at point" << std::endl;
std::cout << hess << std::endl;

// evaluate exact hessian at point
SVector<2> exactHess = field.deriveTwice()(SVector<2>(2,1));

std::cout << "exact hessian at point" << std::endl;
std::cout << exactHess << std::endl;