solver  1.0
SparseMatrix.h
Go to the documentation of this file.
1 /*
2  This file is part of the EnergyOptimizatorOfRoboticCells program.
3 
4  EnergyOptimizatorOfRoboticCells is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  EnergyOptimizatorOfRoboticCells is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with EnergyOptimizatorOfRoboticCells. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef HLIDAC_PES_SPARSE_MATRIX_H
19 #define HLIDAC_PES_SPARSE_MATRIX_H
20 
27 #include <algorithm>
28 #include <cassert>
29 #include <iostream>
30 #include <set>
31 #include <vector>
32 #include <utility>
33 #include <stdint.h>
34 
42 template <class T>
43 class SparseMatrix {
44  public:
46  SparseMatrix(T defaultValue = T()) : mDefaultValue(defaultValue) { }
47 
53  T get(const uint32_t& i, const uint32_t& j) const {
54  assert(i < mSparseMatrix.size());
55  for (const auto& rowElement : mSparseMatrix[i]) {
56  if (rowElement.first == j)
57  return rowElement.second;
58  }
59 
60  return mDefaultValue;
61  }
62 
64  using Row = std::vector<std::pair<uint32_t, T> >;
65 
67  void addRow(Row& row) {
68  assert(!row.empty() && checkColumn(row) && "Invalid matrix row!");
69  mSparseMatrix.push_back(std::move(row));
70  }
71 
73  Row& operator[](const uint32_t& i) {
74  assert(i < mSparseMatrix.size() && "Row index is out of range!");
75  return mSparseMatrix[i];
76  }
77 
79  const Row& operator[](const uint32_t& i) const {
80  assert(i < mSparseMatrix.size() && "Row index is out of range!");
81  return mSparseMatrix[i];
82  }
83 
84  uint64_t numberOfRows() const { return mSparseMatrix.size(); }
85 
86  uint64_t numberOfColumns() const {
87  int64_t numberOfColumns = -1;
88  for (const auto& row : mSparseMatrix) {
89  for (const auto& element : row)
90  numberOfColumns = std::max(numberOfColumns, (int64_t) element.first);
91  }
92 
93  return (numberOfColumns != -1) ? (uint64_t) numberOfColumns+1 : 0ul;
94  }
95 
97  uint64_t numberOfElements() const {
98  uint64_t numberOfElements = 0;
99  for (const auto& row : mSparseMatrix)
100  numberOfElements += row.size();
101  return numberOfElements;
102  }
103 
105  double densityOfMatrix() const {
106  return ((double) numberOfElements())/((double) numberOfRows()*numberOfColumns());
107  }
108 
110  void clear() { mSparseMatrix.clear(); }
111 
112  private:
113 
119  bool checkColumn(const Row& row) const {
120  std::set<uint32_t> uniqueColumns;
121  for (const auto& el : row) {
122  if (uniqueColumns.count(el.first) > 0)
123  return false;
124  else
125  uniqueColumns.insert(el.first);
126  }
127 
128  return true;
129  }
130 
134  std::vector<Row> mSparseMatrix;
135 };
136 
138 template <class T>
139 std::ostream& operator<<(std::ostream& out, const SparseMatrix<T>& m) {
140  out<<"A = ["<<std::endl;
141  for (uint32_t r = 0; r < m.numberOfRows(); ++r) {
142  out<<"\t";
143  for (uint32_t c = 0; c < m.numberOfColumns(); ++c)
144  out<<" "<<m.get(r,c);
145 
146  if (r+1 < m.numberOfRows())
147  out<<";"<<std::endl;
148  else
149  out<<std::endl;
150  }
151  out<<"];";
152 
153  return out;
154 }
155 
156 #endif
uint64_t numberOfElements() const
Number of non-zero elements of the matrix.
Definition: SparseMatrix.h:97
Memory efficient storage of the constraint matrix.
Definition: SparseMatrix.h:43
double densityOfMatrix() const
Percentage of filled elements.
Definition: SparseMatrix.h:105
Row & operator[](const uint32_t &i)
It returns i-th row of the matrix.
Definition: SparseMatrix.h:73
T mDefaultValue
Default value for unfilled elements.
Definition: SparseMatrix.h:132
bool checkColumn(const Row &row) const
It checks whether the column is uniquely defined.
Definition: SparseMatrix.h:119
const Row & operator[](const uint32_t &i) const
It returns i-th row of the matrix.
Definition: SparseMatrix.h:79
void clear()
Destroys the matrix.
Definition: SparseMatrix.h:110
SparseMatrix(T defaultValue=T())
Creates empty matrix with a default value for unfilled elements.
Definition: SparseMatrix.h:46
std::vector< std::pair< uint32_t, double > > Row
The matrix row is stored as a vector of pairs where each pair consists of column index and the relate...
Definition: SparseMatrix.h:64
std::vector< Row > mSparseMatrix
Sparse constraint matrix.
Definition: SparseMatrix.h:134
void addRow(Row &row)
Adds precreated row to the matrix. Passed argument is destroyed.
Definition: SparseMatrix.h:67