LinearProblem.cc 1.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_EIGEN3_AVAILABLE

//== INCLUDES =================================================================
#include "LinearProblem.hh"


namespace COMISO {

Andreas Fabri's avatar
Andreas Fabri committed
11
  LinearProblem::LinearProblem (std::size_t _dimension)
12 13 14 15 16 17 18 19 20 21
{
  // resize and zero elements
  coeffs_.resize(_dimension,0.0);
  x_.resize(_dimension, 0.0);
}

LinearProblem::~LinearProblem()
{
}

Andreas Fabri's avatar
Andreas Fabri committed
22
int LinearProblem::n_unknowns()
23
{
Andreas Fabri's avatar
Andreas Fabri committed
24
  return static_cast<int>(coeffs_.size());
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
}

void LinearProblem::initial_x(double* _x)
{
  if(!x_.empty())
    memcpy(_x, &(x_[0]), x_.size()*sizeof(double));
}

double LinearProblem::eval_f( const double* _x)
{
  double d(0.0);
  for(unsigned int i=0; i<coeffs_.size(); ++i)
    d += coeffs_[i]*_x[i];
  return d;
}

void LinearProblem::eval_gradient( const double* _x, double* _g)
{
  if(!coeffs_.empty())
    memcpy(_g, &(coeffs_[0]), coeffs_.size()*sizeof(double));
}

void LinearProblem::eval_hessian ( const double* _x, SMatrixNP& _H)
{
  // resize and set to zero
  _H.resize(n_unknowns(), n_unknowns());
  _H.setZero();
}

void LinearProblem::store_result ( const double* _x )
{
  if(!x_.empty())
    memcpy(&(x_[0]), _x, x_.size()*sizeof(double));
}


bool LinearProblem::constant_gradient() const
{
  return true;
}

bool LinearProblem::constant_hessian()  const
{
  return true;
}

std::vector<double>& LinearProblem::coeffs()
{
  return coeffs_;
}

std::vector<double>& LinearProblem::x()
{
  return x_;
}


//=============================================================================
} // namespace COMISO
//=============================================================================
#endif // COMISO_EIGEN3_AVAILABLE
//=============================================================================