LinearConstraint.cc 2.75 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2
//=============================================================================
//
David Bommes's avatar
David Bommes committed
3
//  CLASS LinearConstraint
Jan Möbius's avatar
Jan Möbius committed
4 5 6 7 8 9 10 11 12 13 14 15
//
//=============================================================================


#ifndef COMISO_LINEARCONSTRAINT_CC
#define COMISO_LINEARCONSTRAINT_CC


//== INCLUDES =================================================================

#include <CoMISo/Config/CoMISoDefines.hh>
#include "NConstraintInterface.hh"
David Bommes's avatar
David Bommes committed
16
#include "LinearConstraint.hh"
Jan Möbius's avatar
Jan Möbius committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

//== FORWARDDECLARATIONS ======================================================

//== NAMESPACES ===============================================================

namespace COMISO {

//== Implementation =========================================================

      
/// Default constructor
LinearConstraint::LinearConstraint(const ConstraintType _type) : NConstraintInterface(_type)
{}

// linear equation of the form -> coeffs_^T *x  + b_=_type= 0
LinearConstraint::LinearConstraint(const SVectorNC& _coeffs, const double _b, const ConstraintType _type) : NConstraintInterface(_type)
{
  coeffs_ = _coeffs;
  b_ = _b;
}

int LinearConstraint::n_unknowns()
{
  return coeffs_.innerSize();
}

Andreas Fabri's avatar
Andreas Fabri committed
43
  void LinearConstraint::resize(const std::size_t _n)
David Bommes's avatar
David Bommes committed
44
{
Andreas Fabri's avatar
Andreas Fabri committed
45
  if(coeffs_.innerSize() != static_cast<std::ptrdiff_t>(_n))
David Bommes's avatar
David Bommes committed
46 47
  {
    // resize while maintaining all values in range
Andreas Fabri's avatar
Andreas Fabri committed
48
    SVectorNC coeffs_new(static_cast<int>(_n));
David Bommes's avatar
David Bommes committed
49 50 51 52 53 54 55 56 57 58 59 60
    coeffs_new.setZero();
    coeffs_new.reserve(coeffs_.nonZeros());

    SVectorNC::InnerIterator it(coeffs_);
    for(; it; ++it)
      if(it.index() < SVectorNC::Index(_n))
        coeffs_new.insertBack(it.index()) = it.value();

    coeffs_.swap(coeffs_new);
    //  coeffs_.m_size = _n;
    //  coeffs_.resize(_n);
  }
David Bommes's avatar
David Bommes committed
61 62
}

David Bommes's avatar
David Bommes committed
63 64 65 66 67 68 69
void LinearConstraint::clear()
{
  coeffs_.setZero();
  b_ = 0.0;
}


David Bommes's avatar
David Bommes committed
70 71 72 73
const LinearConstraint::SVectorNC& LinearConstraint::coeffs() const
{
  return coeffs_;
}
Jan Möbius's avatar
Jan Möbius committed
74 75 76 77
LinearConstraint::SVectorNC& LinearConstraint::coeffs() 
{ 
  return coeffs_;
}
David Bommes's avatar
David Bommes committed
78 79 80 81
const double&    LinearConstraint::b() const
{
  return b_;
}
Jan Möbius's avatar
Jan Möbius committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
double&    LinearConstraint::b()      
{ 
  return b_;
}

double LinearConstraint::eval_constraint ( const double* _x )
{
  double v = b_;

  SVectorNC::InnerIterator c_it(coeffs_);
  for(; c_it; ++c_it)
    v += c_it.value()*_x[c_it.index()];

  return v;
}

void LinearConstraint::eval_gradient( const double* _x, SVectorNC& _g      )
{
  _g = coeffs_;
}

void LinearConstraint::eval_hessian    ( const double* _x, SMatrixNC& _h      )
{
  _h.clear();
  _h.resize(coeffs_.innerSize(), coeffs_.innerSize());
}


//=============================================================================
} // namespace COMISO
//=============================================================================
#endif // ACG_LINEARCONSTRAINT_HH defined
//=============================================================================