Couenne  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CouenneMatrix.hpp
Go to the documentation of this file.
1 /* $Id: CouenneMatrix.hpp 1094 2015-01-10 16:59:20Z pbelotti $
2  *
3  * Name: CouenneMatrix.hpp
4  * Author: Pietro Belotti
5  * Purpose: define the class of expression matrices
6  *
7  * This file is licensed under the Eclipse Public License (EPL)
8  */
9 
10 #ifndef CouenneMatrix_hpp
11 #define CouenneMatrix_hpp
12 
13 #include <set>
14 #include <vector>
15 
16 #include "CouenneExprClone.hpp"
17 
18 namespace Couenne {
19 
20  class expression;
21  class CouenneExprMatrix;
22 
23  // Base class for elements of our sparse structures ////////////////////////////
24 
25  class CouenneScalar {
26 
27  protected:
28 
29  int index_;
31 
32  public:
33 
34  CouenneScalar (int index, expression *elem):
35  index_ (index),
36  elem_ (elem -> code () == COU_EXPRCONST ? elem : new exprClone (elem)) {}
37 
38  ~CouenneScalar ();
39 
41  index_ (rhs.index_),
42  elem_ (new exprClone (rhs.elem_)) {}
43 
45  index_ = rhs.index_;
46  elem_ = new exprClone (rhs.elem_);
47  return *this;
48  }
49 
50  inline CouenneScalar *clone () {return new CouenneScalar (*this);}
51 
52  inline int getIndex () const {return index_;}
53  inline expression *getElem () const {return elem_;}
54 
55  inline bool operator< (const CouenneScalar &rhs) const {return (index_ < rhs.index_);}
56 
57  friend bool operator< (const CouenneScalar &first, const CouenneScalar &second);
58 
59  void print () const;
60  };
61 
62  inline bool operator< (const CouenneScalar &first, const CouenneScalar &second) {return (first.index_ < second.index_);}
63 
64  // Sparse vector of expressions /////////////////////////////////////////////////
65 
67 
68  public:
69 
70  struct compare_scalars {
71  inline bool operator() (register CouenneScalar * const &a,
72  register CouenneScalar * const &b)
73  {return a -> getIndex () < b -> getIndex ();}
74  };
75 
76  protected:
77 
78  std::set <CouenneScalar *, compare_scalars> elem_;
79 
80  public:
81 
84 
87  CouenneSparseVector *clone () {return new CouenneSparseVector (*this);}
88 
89  void add_element (int index, expression *elem);
90 
91  void print () const;
92 
93  const std::set <CouenneScalar *, compare_scalars> &getElements () {return elem_;}
94 
95  double operator * (const CouenneSparseVector &factor) const;
97 
98  double multiply_thres (const CouenneSparseVector &v2, double thres) const;
99  };
100 
101 
102  // Sparse matrix of expressions ///////////////////////////////////////////////////
103 
105 
106  public:
107 
109  inline bool operator() (register const std::pair <int, CouenneSparseVector *> &a,
110  register const std::pair <int, CouenneSparseVector *> &b) const
111  {return a. first < b. first;}
112  };
113 
114  protected:
115 
116  std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> row_;
117  std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> col_;
118 
119  std::vector <expression *> varIndices_;
120 
121  public:
122 
125 
128 
129  CouenneExprMatrix *clone () {return new CouenneExprMatrix (*this);}
130 
131  const std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> &getRows () const {return row_;}
132  const std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> &getCols () const {return col_;}
133 
134  std::vector <expression *> &varIndices () {return varIndices_;}
135 
136  void add_element (int row, int column, expression *elem);
137  void print () const;
138  long unsigned int size ();
139 
140  CouenneSparseVector &operator * (const CouenneSparseVector &factor) const;
141  CouenneExprMatrix &operator * (const CouenneExprMatrix &post) const;
142  };
143 }
144 
145 #endif
const std::set< CouenneScalar *, compare_scalars > & getElements()
returns elements of vector as (ordered) set
const std::set< std::pair< int, CouenneSparseVector * >, compare_pair_ind > & getCols() const
CouenneSparseVector & operator=(const CouenneSparseVector &rhs)
CouenneScalar * clone()
bool operator()(register CouenneScalar *const &a, register CouenneScalar *const &b)
CouenneScalar(int index, expression *elem)
void add_element(int index, expression *elem)
std::vector< expression * > varIndices_
if used in sdp cuts, contains indices of x_i used in X_ij = x_i * x_j
std::set< CouenneScalar *, compare_scalars > elem_
CouenneExprMatrix * clone()
CouenneSparseVector & operator*(const CouenneSparseVector &factor) const
matrix * vector
double operator*(const CouenneSparseVector &factor) const
vector * vector (dot product)
CouenneScalar(const CouenneScalar &rhs)
expression clone (points to another expression)
std::set< std::pair< int, CouenneSparseVector * >, compare_pair_ind > row_
row major
CouenneExprMatrix & operator=(const CouenneExprMatrix &rhs)
std::vector< expression * > & varIndices()
CouenneScalar & operator=(const CouenneScalar &rhs)
bool operator()(register const std::pair< int, CouenneSparseVector * > &a, register const std::pair< int, CouenneSparseVector * > &b) const
CouenneSparseVector * clone()
Expression base class.
const std::set< std::pair< int, CouenneSparseVector * >, compare_pair_ind > & getRows() const
void add_element(int row, int column, expression *elem)
expression * elem_
element
int index_
index of element in vector
bool operator<(const CouenneScalar &rhs) const
expression * getElem() const
bool operator<(const CouenneScalar &first, const CouenneScalar &second)
double multiply_thres(const CouenneSparseVector &v2, double thres) const
stops multiplication if above threshold
std::set< std::pair< int, CouenneSparseVector * >, compare_pair_ind > col_
col major
long unsigned int size()