LinearConstraint.cc 2.77 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
43
44
45

//== 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;
}

/// Destructor
LinearConstraint::~LinearConstraint() {}

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

David Bommes's avatar
David Bommes committed
46
47
void LinearConstraint::resize(const unsigned int _n)
{
David Bommes's avatar
David Bommes committed
48
  if(coeffs_.innerSize() != (int)_n)
David Bommes's avatar
David Bommes committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  {
    // resize while maintaining all values in range
    SVectorNC coeffs_new(_n);
    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
64
65
}

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


David Bommes's avatar
David Bommes committed
73
74
75
76
const LinearConstraint::SVectorNC& LinearConstraint::coeffs() const
{
  return coeffs_;
}
Jan Möbius's avatar
Jan Möbius committed
77
78
79
80
LinearConstraint::SVectorNC& LinearConstraint::coeffs() 
{ 
  return coeffs_;
}
David Bommes's avatar
David Bommes committed
81
82
83
84
const double&    LinearConstraint::b() const
{
  return b_;
}
Jan Möbius's avatar
Jan Möbius committed
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
116
117
118
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
//=============================================================================