18 #ifndef HLIDAC_PES_SPARSE_MATRIX_H
19 #define HLIDAC_PES_SPARSE_MATRIX_H
53 T
get(
const uint32_t& i,
const uint32_t& j)
const {
56 if (rowElement.first == j)
57 return rowElement.second;
64 using Row = std::vector<std::pair<uint32_t, T> >;
68 assert(!row.empty() &&
checkColumn(row) &&
"Invalid matrix row!");
74 assert(i <
mSparseMatrix.size() &&
"Row index is out of range!");
80 assert(i <
mSparseMatrix.size() &&
"Row index is out of range!");
84 uint64_t numberOfRows()
const {
return mSparseMatrix.size(); }
86 uint64_t numberOfColumns()
const {
87 int64_t numberOfColumns = -1;
89 for (
const auto& element : row)
90 numberOfColumns = std::max(numberOfColumns, (int64_t) element.first);
93 return (numberOfColumns != -1) ? (uint64_t) numberOfColumns+1 : 0ul;
99 for (
const auto& row : mSparseMatrix)
100 numberOfElements += row.size();
106 return ((
double)
numberOfElements())/((double) numberOfRows()*numberOfColumns());
110 void clear() { mSparseMatrix.clear(); }
120 std::set<uint32_t> uniqueColumns;
121 for (
const auto& el : row) {
122 if (uniqueColumns.count(el.first) > 0)
125 uniqueColumns.insert(el.first);
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) {
143 for (uint32_t c = 0; c < m.numberOfColumns(); ++c)
144 out<<
" "<<m.get(r,c);
146 if (r+1 < m.numberOfRows())
uint64_t numberOfElements() const
Number of non-zero elements of the matrix.
Memory efficient storage of the constraint matrix.
double densityOfMatrix() const
Percentage of filled elements.
Row & operator[](const uint32_t &i)
It returns i-th row of the matrix.
T mDefaultValue
Default value for unfilled elements.
bool checkColumn(const Row &row) const
It checks whether the column is uniquely defined.
const Row & operator[](const uint32_t &i) const
It returns i-th row of the matrix.
void clear()
Destroys the matrix.
SparseMatrix(T defaultValue=T())
Creates empty matrix with a default value for unfilled elements.
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...
std::vector< Row > mSparseMatrix
Sparse constraint matrix.
void addRow(Row &row)
Adds precreated row to the matrix. Passed argument is destroyed.