14
14
using namespace mlir ;
15
15
using namespace presburger ;
16
16
17
- template <typename T> Matrix<T>::Matrix(unsigned rows, unsigned columns, unsigned reservedRows,
18
- unsigned reservedColumns)
17
+ template <typename T>
18
+ Matrix<T>::Matrix(unsigned rows, unsigned columns, unsigned reservedRows,
19
+ unsigned reservedColumns)
19
20
: nRows(rows), nColumns(columns),
20
21
nReservedColumns (std::max(nColumns, reservedColumns)),
21
22
data(nRows * nReservedColumns) {
22
23
data.reserve (std::max (nRows, reservedRows) * nReservedColumns);
23
24
}
24
25
25
- template <typename T> Matrix<T> Matrix<T>::identity(unsigned dimension) {
26
+ template <typename T>
27
+ Matrix<T> Matrix<T>::identity(unsigned dimension) {
26
28
Matrix matrix (dimension, dimension);
27
29
for (unsigned i = 0 ; i < dimension; ++i)
28
30
matrix (i, i) = 1 ;
29
31
return matrix;
30
32
}
31
33
32
- template <typename T> unsigned Matrix<T>::getNumReservedRows() const {
34
+ template <typename T>
35
+ unsigned Matrix<T>::getNumReservedRows() const {
33
36
return data.capacity () / nReservedColumns;
34
37
}
35
38
36
- template <typename T> void Matrix<T>::reserveRows(unsigned rows) {
39
+ template <typename T>
40
+ void Matrix<T>::reserveRows(unsigned rows) {
37
41
data.reserve (rows * nReservedColumns);
38
42
}
39
43
40
- template <typename T> unsigned Matrix<T>::appendExtraRow() {
44
+ template <typename T>
45
+ unsigned Matrix<T>::appendExtraRow() {
41
46
resizeVertically (nRows + 1 );
42
47
return nRows - 1 ;
43
48
}
44
49
45
- template <typename T> unsigned Matrix<T>::appendExtraRow(ArrayRef<T> elems) {
50
+ template <typename T>
51
+ unsigned Matrix<T>::appendExtraRow(ArrayRef<T> elems) {
46
52
assert (elems.size () == nColumns && " elems must match row length!" );
47
53
unsigned row = appendExtraRow ();
48
54
for (unsigned col = 0 ; col < nColumns; ++col)
49
55
at (row, col) = elems[col];
50
56
return row;
51
57
}
52
58
53
- template <typename T> void Matrix<T>::resizeHorizontally(unsigned newNColumns) {
59
+ template <typename T>
60
+ void Matrix<T>::resizeHorizontally(unsigned newNColumns) {
54
61
if (newNColumns < nColumns)
55
62
removeColumns (newNColumns, nColumns - newNColumns);
56
63
if (newNColumns > nColumns)
57
64
insertColumns (nColumns, newNColumns - nColumns);
58
65
}
59
66
60
- template <typename T> void Matrix<T>::resize(unsigned newNRows, unsigned newNColumns) {
67
+ template <typename T>
68
+ void Matrix<T>::resize(unsigned newNRows, unsigned newNColumns) {
61
69
resizeHorizontally (newNColumns);
62
70
resizeVertically (newNRows);
63
71
}
64
72
65
- template <typename T> void Matrix<T>::resizeVertically(unsigned newNRows) {
73
+ template <typename T>
74
+ void Matrix<T>::resizeVertically(unsigned newNRows) {
66
75
nRows = newNRows;
67
76
data.resize (nRows * nReservedColumns);
68
77
}
69
78
70
- template <typename T> void Matrix<T>::swapRows(unsigned row, unsigned otherRow) {
79
+ template <typename T>
80
+ void Matrix<T>::swapRows(unsigned row, unsigned otherRow) {
71
81
assert ((row < getNumRows () && otherRow < getNumRows ()) &&
72
82
" Given row out of bounds" );
73
83
if (row == otherRow)
@@ -76,7 +86,8 @@ template <typename T> void Matrix<T>::swapRows(unsigned row, unsigned otherRow)
76
86
std::swap (at (row, col), at (otherRow, col));
77
87
}
78
88
79
- template <typename T> void Matrix<T>::swapColumns(unsigned column, unsigned otherColumn) {
89
+ template <typename T>
90
+ void Matrix<T>::swapColumns(unsigned column, unsigned otherColumn) {
80
91
assert ((column < getNumColumns () && otherColumn < getNumColumns ()) &&
81
92
" Given column out of bounds" );
82
93
if (column == otherColumn)
@@ -85,23 +96,30 @@ template <typename T> void Matrix<T>::swapColumns(unsigned column, unsigned othe
85
96
std::swap (at (row, column), at (row, otherColumn));
86
97
}
87
98
88
- template <typename T> MutableArrayRef<T> Matrix<T>::getRow(unsigned row) {
99
+ template <typename T>
100
+ MutableArrayRef<T> Matrix<T>::getRow(unsigned row) {
89
101
return {&data[row * nReservedColumns], nColumns};
90
102
}
91
103
92
- template <typename T> ArrayRef<T> Matrix<T>::getRow(unsigned row) const {
104
+ template <typename T>
105
+ ArrayRef<T> Matrix<T>::getRow(unsigned row) const {
93
106
return {&data[row * nReservedColumns], nColumns};
94
107
}
95
108
96
- template <typename T> void Matrix<T>::setRow(unsigned row, ArrayRef<T> elems) {
109
+ template <typename T>
110
+ void Matrix<T>::setRow(unsigned row, ArrayRef<T> elems) {
97
111
assert (elems.size () == getNumColumns () &&
98
112
" elems size must match row length!" );
99
113
for (unsigned i = 0 , e = getNumColumns (); i < e; ++i)
100
114
at (row, i) = elems[i];
101
115
}
102
116
103
- template <typename T> void Matrix<T>::insertColumn(unsigned pos) { insertColumns (pos, 1 ); }
104
- template <typename T> void Matrix<T>::insertColumns(unsigned pos, unsigned count) {
117
+ template <typename T>
118
+ void Matrix<T>::insertColumn(unsigned pos) {
119
+ insertColumns (pos, 1 );
120
+ }
121
+ template <typename T>
122
+ void Matrix<T>::insertColumns(unsigned pos, unsigned count) {
105
123
if (count == 0 )
106
124
return ;
107
125
assert (pos <= nColumns);
@@ -142,8 +160,12 @@ template <typename T> void Matrix<T>::insertColumns(unsigned pos, unsigned count
142
160
}
143
161
}
144
162
145
- template <typename T> void Matrix<T>::removeColumn(unsigned pos) { removeColumns (pos, 1 ); }
146
- template <typename T> void Matrix<T>::removeColumns(unsigned pos, unsigned count) {
163
+ template <typename T>
164
+ void Matrix<T>::removeColumn(unsigned pos) {
165
+ removeColumns (pos, 1 );
166
+ }
167
+ template <typename T>
168
+ void Matrix<T>::removeColumns(unsigned pos, unsigned count) {
147
169
if (count == 0 )
148
170
return ;
149
171
assert (pos + count - 1 < nColumns);
@@ -156,8 +178,12 @@ template <typename T> void Matrix<T>::removeColumns(unsigned pos, unsigned count
156
178
nColumns -= count;
157
179
}
158
180
159
- template <typename T> void Matrix<T>::insertRow(unsigned pos) { insertRows (pos, 1 ); }
160
- template <typename T> void Matrix<T>::insertRows(unsigned pos, unsigned count) {
181
+ template <typename T>
182
+ void Matrix<T>::insertRow(unsigned pos) {
183
+ insertRows (pos, 1 );
184
+ }
185
+ template <typename T>
186
+ void Matrix<T>::insertRows(unsigned pos, unsigned count) {
161
187
if (count == 0 )
162
188
return ;
163
189
@@ -170,8 +196,12 @@ template <typename T> void Matrix<T>::insertRows(unsigned pos, unsigned count) {
170
196
at (r, c) = 0 ;
171
197
}
172
198
173
- template <typename T> void Matrix<T>::removeRow(unsigned pos) { removeRows (pos, 1 ); }
174
- template <typename T> void Matrix<T>::removeRows(unsigned pos, unsigned count) {
199
+ template <typename T>
200
+ void Matrix<T>::removeRow(unsigned pos) {
201
+ removeRows (pos, 1 );
202
+ }
203
+ template <typename T>
204
+ void Matrix<T>::removeRows(unsigned pos, unsigned count) {
175
205
if (count == 0 )
176
206
return ;
177
207
assert (pos + count - 1 <= nRows);
@@ -180,50 +210,57 @@ template <typename T> void Matrix<T>::removeRows(unsigned pos, unsigned count) {
180
210
resizeVertically (nRows - count);
181
211
}
182
212
183
- template <typename T> void Matrix<T>::copyRow(unsigned sourceRow, unsigned targetRow) {
213
+ template <typename T>
214
+ void Matrix<T>::copyRow(unsigned sourceRow, unsigned targetRow) {
184
215
if (sourceRow == targetRow)
185
216
return ;
186
217
for (unsigned c = 0 ; c < nColumns; ++c)
187
218
at (targetRow, c) = at (sourceRow, c);
188
219
}
189
220
190
- template <typename T> void Matrix<T>::fillRow(unsigned row, const T &value) {
221
+ template <typename T>
222
+ void Matrix<T>::fillRow(unsigned row, const T &value) {
191
223
for (unsigned col = 0 ; col < nColumns; ++col)
192
224
at (row, col) = value;
193
225
}
194
226
195
- template <typename T> void Matrix<T>::addToRow(unsigned sourceRow, unsigned targetRow,
196
- const T &scale) {
227
+ template <typename T>
228
+ void Matrix<T>::addToRow(unsigned sourceRow, unsigned targetRow,
229
+ const T &scale) {
197
230
addToRow (targetRow, getRow (sourceRow), scale);
198
231
}
199
232
200
- template <typename T> void Matrix<T>::addToRow( unsigned row, ArrayRef<T> rowVec,
201
- const T &scale) {
233
+ template <typename T>
234
+ void Matrix<T>::addToRow( unsigned row, ArrayRef<T> rowVec, const T &scale) {
202
235
if (scale == 0 )
203
236
return ;
204
237
for (unsigned col = 0 ; col < nColumns; ++col)
205
238
at (row, col) += scale * rowVec[col];
206
239
}
207
240
208
- template <typename T> void Matrix<T>::addToColumn(unsigned sourceColumn, unsigned targetColumn,
209
- const T &scale) {
241
+ template <typename T>
242
+ void Matrix<T>::addToColumn(unsigned sourceColumn, unsigned targetColumn,
243
+ const T &scale) {
210
244
if (scale == 0 )
211
245
return ;
212
246
for (unsigned row = 0 , e = getNumRows (); row < e; ++row)
213
247
at (row, targetColumn) += scale * at (row, sourceColumn);
214
248
}
215
249
216
- template <typename T> void Matrix<T>::negateColumn(unsigned column) {
250
+ template <typename T>
251
+ void Matrix<T>::negateColumn(unsigned column) {
217
252
for (unsigned row = 0 , e = getNumRows (); row < e; ++row)
218
253
at (row, column) = -at (row, column);
219
254
}
220
255
221
- template <typename T> void Matrix<T>::negateRow(unsigned row) {
256
+ template <typename T>
257
+ void Matrix<T>::negateRow(unsigned row) {
222
258
for (unsigned column = 0 , e = getNumColumns (); column < e; ++column)
223
259
at (row, column) = -at (row, column);
224
260
}
225
261
226
- template <typename T> SmallVector<T, 8 > Matrix<T>::preMultiplyWithRow(ArrayRef<T> rowVec) const {
262
+ template <typename T>
263
+ SmallVector<T, 8 > Matrix<T>::preMultiplyWithRow(ArrayRef<T> rowVec) const {
227
264
assert (rowVec.size () == getNumRows () && " Invalid row vector dimension!" );
228
265
229
266
SmallVector<T, 8 > result (getNumColumns (), T (0 ));
@@ -233,8 +270,8 @@ template <typename T> SmallVector<T, 8> Matrix<T>::preMultiplyWithRow(ArrayRef<T
233
270
return result;
234
271
}
235
272
236
- template <typename T> SmallVector<T, 8 >
237
- Matrix<T>::postMultiplyWithColumn(ArrayRef<T> colVec) const {
273
+ template <typename T>
274
+ SmallVector<T, 8 > Matrix<T>::postMultiplyWithColumn(ArrayRef<T> colVec) const {
238
275
assert (getNumColumns () == colVec.size () &&
239
276
" Invalid column vector dimension!" );
240
277
@@ -250,26 +287,32 @@ Matrix<T>::postMultiplyWithColumn(ArrayRef<T> colVec) const {
250
287
// / sourceCol. This brings M(row, targetCol) to the range [0, M(row,
251
288
// / sourceCol)). Apply the same column operation to otherMatrix, with the same
252
289
// / integer multiple.
253
- static void modEntryColumnOperation (Matrix<MPInt> &m, unsigned row, unsigned sourceCol,
254
- unsigned targetCol, Matrix<MPInt> &otherMatrix) {
290
+ static void modEntryColumnOperation (Matrix<MPInt> &m, unsigned row,
291
+ unsigned sourceCol, unsigned targetCol,
292
+ Matrix<MPInt> &otherMatrix) {
255
293
assert (m (row, sourceCol) != 0 && " Cannot divide by zero!" );
256
294
assert (m (row, sourceCol) > 0 && " Source must be positive!" );
257
295
MPInt ratio = -floorDiv (m (row, targetCol), m (row, sourceCol));
258
296
m.addToColumn (sourceCol, targetCol, ratio);
259
297
otherMatrix.addToColumn (sourceCol, targetCol, ratio);
260
298
}
261
299
262
- template <typename T> void Matrix<T>::print(raw_ostream &os) const {
300
+ template <typename T>
301
+ void Matrix<T>::print(raw_ostream &os) const {
263
302
for (unsigned row = 0 ; row < nRows; ++row) {
264
303
for (unsigned column = 0 ; column < nColumns; ++column)
265
304
os << at (row, column) << ' ' ;
266
305
os << ' \n ' ;
267
306
}
268
307
}
269
308
270
- template <typename T> void Matrix<T>::dump() const { print (llvm::errs ()); }
309
+ template <typename T>
310
+ void Matrix<T>::dump() const {
311
+ print (llvm::errs ());
312
+ }
271
313
272
- template <typename T> bool Matrix<T>::hasConsistentState() const {
314
+ template <typename T>
315
+ bool Matrix<T>::hasConsistentState() const {
273
316
if (data.size () != nRows * nReservedColumns)
274
317
return false ;
275
318
if (nColumns > nReservedColumns)
@@ -287,8 +330,8 @@ namespace mlir {
287
330
namespace presburger {
288
331
template class Matrix <MPInt>;
289
332
template class Matrix <Fraction>;
290
- }
291
- }
333
+ } // namespace presburger
334
+ } // namespace mlir
292
335
293
336
IntMatrix IntMatrix::identity (unsigned dimension) {
294
337
IntMatrix matrix (dimension, dimension);
@@ -297,7 +340,6 @@ IntMatrix IntMatrix::identity(unsigned dimension) {
297
340
return matrix;
298
341
}
299
342
300
-
301
343
std::pair<IntMatrix, IntMatrix> IntMatrix::computeHermiteNormalForm () const {
302
344
// We start with u as an identity matrix and perform operations on h until h
303
345
// is in hermite normal form. We apply the same sequence of operations on u to
0 commit comments