Search code examples
c++c++11lambdaeigenfunctor

Transform functor struct to take a different argument


I got the following (unconstrained quadratic objective) defined borrowing matrices and vectors from the Eigen-library:

#ifndef QP_UNCON_HPP
#define QP_UNCON_HPP
#include "EigenDataTypes.hpp"

template <int Nx>
struct objective
{
private:
    const spMat Q;
    const Vec<Nx> c;

public:
    objective(spMat Q_, Vec<Nx> c_) : Q(Q_), c(c_) {}

    inline scalar operator()(const Vec<Nx> &x)
    {
        return (.5 * x.transpose() * Q * x + c.transpose() * x);
    }

    inline Vec<Nx> Eval_grad(const Vec<Nx> &x)
    {
        return Q.transpose() * x;
    }

    inline Mat<Nx, Nx> Eval_hessian(const Vec<Nx> &x)
    {
        return Q;
    }
};

This makes it possible to evaluate the objective at different states x:

objective<2> f(Q, c);
Vec<2> x {0,1};

#Objective-value
f(x);
#gradient:
f.Eval_grad(x);
#hessian:
f.Eval_hessian(x);

I want to create a new struct Phi (for line search) where a scalar is used as input arguments instead, something like this:

p_objective<2> Phi(f, x0, p);
double alpha = 0.9;

#Objective-value
Phi(alpha);
#gradient:
Phi.Eval_grad(alpha);
#hessian:
Phi.Eval_hessian(alpha);

Which corresponds to this:

#Objective-value
f(x + alpha*p);
#gradient:
f.Eval_grad(x + alpha*p);
#hessian:
f.Eval_hessian(x + alpha*p);

This would be simple for a single function using lambda functions, but are there smooth ways of 'lambdifying' a functor struct?

EigenDataTypes.hpp

#ifndef EIGENDATATYPES_H
#define EIGENDATATYPES_H

#include <Eigen/Dense>
#include <Eigen/SparseCore>

#ifdef CSOLVER_USE_SINGLE
typedef float real_t;
#else
typedef double real_t;
#endif

using scalar = Eigen::Matrix<double, 1, 1>;
template <int Rows>
using Vec = Eigen::Matrix<double, Rows, 1>;
template <int Rows, int Cols>
using Mat = Eigen::Matrix<double, Rows, Cols>;
using spVec = Eigen::SparseVector<double>;
using spMat = Eigen::SparseMatrix<double>;
using Triplet = Eigen::Triplet<double>;
#endif

1D-example without Eigen-library:

struct objective_1D
{
private:
    const double Q;
    const double c;

public:
    objective_1D(double Q_, double c_) : Q(Q_), c(c_) {}

    double operator()(const double &x)
    {
        return (.5 * x * Q * x + c* x);
    }
    double Eval_grad(const double &x)
    {
        return Q * x;
    }
    double Eval_hessian(const double &x)
    {
        return Q;
    }
};

TLDR; I want to create a functor struct p_objective that works as a lambda for struct objective:

p_objective = [&x, &p] (double alpha) (return objective-methods at (x + alpha*p))

Solution

  • I want to create a functor struct p_objective that works as a lambda for struct objective.

    If you have already written the objective, you can also similarly make a p_objective. Following is an example.

    Here is the (demo)

    template <int Nx> class p_objective
    {
        objective<Nx> f;
        const spMat x;
        const Vec<Nx> p;
    
    public:
        explicit p_objective(const objective<Nx>& ob, spMat const& Q_, const Vec<Nx>& c_)
            : f{ ob }
            , x{ Q_ }
            , p{ c_ }
        {}
    
        scalar operator()(const double alpha)
        {
            return f(x + (alpha*p));
        }
    
        Vec<Nx> Eval_grad(const double alpha)
        {
            return f.Eval_grad(x + alpha * p);
        }
    
        Mat<Nx, Nx> Eval_hessian(const double alpha)
        {
            return f.Eval_hessian(x + alpha * p);
        }
    };
    

    Since both the objective and the p_objective have member functions, (i.e. Eval_grad, and Eval_hessian), it will be more readable, when they are normal classes, than a lambda function.