MLIR 23.0.0git
Matrix.cpp
Go to the documentation of this file.
1//===- Matrix.cpp - MLIR Matrix Class -------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
12#include "llvm/Support/MathExtras.h"
13#include "llvm/Support/raw_ostream.h"
14#include <algorithm>
15#include <cassert>
16#include <utility>
17
18using namespace mlir;
19using namespace presburger;
20
21template <typename T>
22Matrix<T>::Matrix(unsigned rows, unsigned columns, unsigned reservedRows,
23 unsigned reservedColumns)
24 : nRows(rows), nColumns(columns),
25 nReservedColumns(std::max(nColumns, reservedColumns)),
27 data.reserve(std::max(nRows, reservedRows) * nReservedColumns);
28}
29
30/// We cannot use the default implementation of operator== as it compares
31/// fields like `reservedColumns` etc., which are not part of the data.
32template <typename T>
33bool Matrix<T>::operator==(const Matrix<T> &m) const {
34 if (nRows != m.getNumRows())
35 return false;
36 if (nColumns != m.getNumColumns())
37 return false;
38
39 for (unsigned i = 0; i < nRows; i++)
40 if (getRow(i) != m.getRow(i))
41 return false;
42
43 return true;
44}
45
46template <typename T>
47Matrix<T> Matrix<T>::identity(unsigned dimension) {
48 Matrix matrix(dimension, dimension);
49 for (unsigned i = 0; i < dimension; ++i)
50 matrix(i, i) = 1;
51 return matrix;
52}
53
54template <typename T>
56 return data.capacity() / nReservedColumns;
57}
58
59template <typename T>
60void Matrix<T>::reserveRows(unsigned rows) {
61 data.reserve(rows * nReservedColumns);
62}
63
64template <typename T>
67 return nRows - 1;
68}
69
70template <typename T>
72 assert(elems.size() == nColumns && "elems must match row length!");
73 unsigned row = appendExtraRow();
74 for (unsigned col = 0; col < nColumns; ++col)
75 at(row, col) = elems[col];
76 return row;
77}
79template <typename T>
82 for (unsigned row = 0; row < nRows; ++row)
83 for (unsigned col = 0; col < nColumns; ++col)
84 transp(col, row) = at(row, col);
85
86 return transp;
87}
88
89template <typename T>
90void Matrix<T>::resizeHorizontally(unsigned newNColumns) {
91 if (newNColumns < nColumns)
92 removeColumns(newNColumns, nColumns - newNColumns);
93 if (newNColumns > nColumns)
94 insertColumns(nColumns, newNColumns - nColumns);
95}
96
97template <typename T>
98void Matrix<T>::resize(unsigned newNRows, unsigned newNColumns) {
99 resizeHorizontally(newNColumns);
101}
102
103template <typename T>
104void Matrix<T>::resizeVertically(unsigned newNRows) {
105 nRows = newNRows;
106 data.resize(nRows * nReservedColumns);
107}
108
109template <typename T>
110void Matrix<T>::swapRows(unsigned row, unsigned otherRow) {
111 assert((row < getNumRows() && otherRow < getNumRows()) &&
112 "Given row out of bounds");
113 if (row == otherRow)
114 return;
115 for (unsigned col = 0; col < nColumns; col++)
116 std::swap(at(row, col), at(otherRow, col));
119template <typename T>
120void Matrix<T>::swapColumns(unsigned column, unsigned otherColumn) {
121 assert((column < getNumColumns() && otherColumn < getNumColumns()) &&
122 "Given column out of bounds");
123 if (column == otherColumn)
124 return;
125 for (unsigned row = 0; row < nRows; row++)
126 std::swap(at(row, column), at(row, otherColumn));
127}
128
129template <typename T>
131 return {&data[row * nReservedColumns], nColumns};
132}
134template <typename T>
135ArrayRef<T> Matrix<T>::getRow(unsigned row) const {
136 return {&data[row * nReservedColumns], nColumns};
137}
139template <typename T>
140void Matrix<T>::setRow(unsigned row, ArrayRef<T> elems) {
141 assert(elems.size() == getNumColumns() &&
142 "elems size must match row length!");
143 for (unsigned i = 0, e = getNumColumns(); i < e; ++i)
144 at(row, i) = elems[i];
145}
146
147template <typename T>
148void Matrix<T>::insertColumn(unsigned pos) {
149 insertColumns(pos, 1);
151template <typename T>
152void Matrix<T>::insertColumns(unsigned pos, unsigned count) {
153 if (count == 0)
154 return;
155 assert(pos <= nColumns);
156 unsigned oldNReservedColumns = nReservedColumns;
157 if (nColumns + count > nReservedColumns) {
158 nReservedColumns = llvm::NextPowerOf2(nColumns + count);
159 data.resize(nRows * nReservedColumns);
160 }
161 nColumns += count;
162
163 for (int ri = nRows - 1; ri >= 0; --ri) {
164 for (int ci = nReservedColumns - 1; ci >= 0; --ci) {
165 unsigned r = ri;
166 unsigned c = ci;
167 T &dest = data[r * nReservedColumns + c];
168 if (c >= nColumns) { // NOLINT
169 // Out of bounds columns are zero-initialized. NOLINT because clang-tidy
170 // complains about this branch being the same as the c >= pos one.
171 //
172 // TODO: this case can be skipped if the number of reserved columns
173 // didn't change.
174 dest = 0;
175 } else if (c >= pos + count) {
176 // Shift the data occuring after the inserted columns.
177 dest = data[r * oldNReservedColumns + c - count];
178 } else if (c >= pos) {
179 // The inserted columns are also zero-initialized.
180 dest = 0;
181 } else {
182 // The columns before the inserted columns stay at the same (row, col)
183 // but this corresponds to a different location in the linearized array
184 // if the number of reserved columns changed.
185 if (nReservedColumns == oldNReservedColumns)
186 break;
187 dest = data[r * oldNReservedColumns + c];
188 }
190 }
191}
192
193template <typename T>
194void Matrix<T>::removeColumn(unsigned pos) {
196}
197template <typename T>
198void Matrix<T>::removeColumns(unsigned pos, unsigned count) {
199 if (count == 0)
200 return;
201 assert(pos + count - 1 < nColumns);
202 for (unsigned r = 0; r < nRows; ++r) {
203 for (unsigned c = pos; c < nColumns - count; ++c)
204 at(r, c) = at(r, c + count);
205 for (unsigned c = nColumns - count; c < nColumns; ++c)
206 at(r, c) = 0;
207 }
208 nColumns -= count;
210
211template <typename T>
212void Matrix<T>::insertRow(unsigned pos) {
213 insertRows(pos, 1);
214}
215template <typename T>
216void Matrix<T>::insertRows(unsigned pos, unsigned count) {
217 if (count == 0)
218 return;
219
220 assert(pos <= nRows);
221 resizeVertically(nRows + count);
222 for (int r = nRows - 1; r >= int(pos + count); --r)
223 copyRow(r - count, r);
224 for (int r = pos + count - 1; r >= int(pos); --r)
225 for (unsigned c = 0; c < nColumns; ++c)
226 at(r, c) = 0;
227}
228
229template <typename T>
230void Matrix<T>::removeRow(unsigned pos) {
231 removeRows(pos, 1);
232}
233template <typename T>
234void Matrix<T>::removeRows(unsigned pos, unsigned count) {
235 if (count == 0)
236 return;
237 assert(pos + count - 1 <= nRows);
238 for (unsigned r = pos; r + count < nRows; ++r)
239 copyRow(r + count, r);
240 resizeVertically(nRows - count);
241}
242
243template <typename T>
244void Matrix<T>::copyRow(unsigned sourceRow, unsigned targetRow) {
245 if (sourceRow == targetRow)
246 return;
247 for (unsigned c = 0; c < nColumns; ++c)
248 at(targetRow, c) = at(sourceRow, c);
249}
250
251template <typename T>
252void Matrix<T>::fillRow(unsigned row, const T &value) {
253 for (unsigned col = 0; col < nColumns; ++col)
254 at(row, col) = value;
255}
256
257// moveColumns is implemented by moving the columns adjacent to the source range
258// to their final position.
259template <typename T>
260void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
261 if (num == 0)
262 return;
263
264 if (dstPos == srcPos)
265 return;
266
267 assert(srcPos + num <= getNumColumns() &&
268 "move source range exceeds matrix columns");
269 assert(dstPos + num <= getNumColumns() &&
270 "move destination range exceeds matrix columns");
271
272 unsigned numRows = getNumRows();
273 // std::rotate(start, middle, end) permutes the elements of [start, end] to
274 // [middle, end) + [start, middle). NOTE: &at(i, srcPos + num) will trigger an
275 // assert.
276 if (dstPos > srcPos) {
277 for (unsigned i = 0; i < numRows; ++i) {
278 std::rotate(&at(i, srcPos), &at(i, srcPos) + num, &at(i, dstPos) + num);
279 }
280 return;
281 }
282 for (unsigned i = 0; i < numRows; ++i) {
283 std::rotate(&at(i, dstPos), &at(i, srcPos), &at(i, srcPos) + num);
284 }
285}
286
287template <typename T>
289 assert(getNumColumns() == other.getNumRows());
290 unsigned n = getNumRows();
291 unsigned m = other.getNumRows();
292 unsigned p = other.getNumColumns();
293 Matrix<T> result(n, p);
294
295 for (unsigned i = 0; i < n; i++) {
296 for (unsigned j = 0; j < m; j++) {
297 for (unsigned k = 0; k < p; k++) {
298 result.at(i, k) += at(i, j) * other.at(j, k);
299 }
300 }
301 }
302 return result;
303}
304
305template <typename T>
306void Matrix<T>::addToRow(unsigned sourceRow, unsigned targetRow,
307 const T &scale) {
308 addToRow(targetRow, getRow(sourceRow), scale);
309}
310
311template <typename T>
312void Matrix<T>::addToRow(unsigned row, ArrayRef<T> rowVec, const T &scale) {
313 if (scale == 0)
314 return;
315 for (unsigned col = 0; col < nColumns; ++col)
316 at(row, col) += scale * rowVec[col];
317}
318
319template <typename T>
320void Matrix<T>::scaleRow(unsigned row, const T &scale) {
321 for (unsigned col = 0; col < nColumns; ++col)
322 at(row, col) *= scale;
323}
324
325template <typename T>
326void Matrix<T>::addToColumn(unsigned sourceColumn, unsigned targetColumn,
327 const T &scale) {
328 if (scale == 0)
329 return;
330 for (unsigned row = 0, e = getNumRows(); row < e; ++row)
331 at(row, targetColumn) += scale * at(row, sourceColumn);
332}
333
334template <typename T>
335void Matrix<T>::negateColumn(unsigned column) {
336 for (unsigned row = 0, e = getNumRows(); row < e; ++row)
337 at(row, column) = -at(row, column);
338}
339
340template <typename T>
341void Matrix<T>::negateRow(unsigned row) {
342 for (unsigned column = 0, e = getNumColumns(); column < e; ++column)
343 at(row, column) = -at(row, column);
344}
345
346template <typename T>
348 for (unsigned row = 0; row < nRows; ++row)
349 negateRow(row);
350}
351
352template <typename T>
354 assert(rowVec.size() == getNumRows() && "Invalid row vector dimension!");
355
357 for (unsigned col = 0, e = getNumColumns(); col < e; ++col)
358 for (unsigned i = 0, e = getNumRows(); i < e; ++i)
359 result[col] += rowVec[i] * at(i, col);
360 return result;
361}
362
363template <typename T>
365 assert(getNumColumns() == colVec.size() &&
366 "Invalid column vector dimension!");
367
369 for (unsigned row = 0, e = getNumRows(); row < e; row++)
370 for (unsigned i = 0, e = getNumColumns(); i < e; i++)
371 result[row] += at(row, i) * colVec[i];
372 return result;
373}
374
375/// Set M(row, targetCol) to its remainder on division by M(row, sourceCol)
376/// by subtracting from column targetCol an appropriate integer multiple of
377/// sourceCol. This brings M(row, targetCol) to the range [0, M(row,
378/// sourceCol)). Apply the same column operation to otherMatrix, with the same
379/// integer multiple.
381 unsigned sourceCol, unsigned targetCol,
382 Matrix<DynamicAPInt> &otherMatrix) {
383 assert(m(row, sourceCol) != 0 && "Cannot divide by zero!");
384 assert(m(row, sourceCol) > 0 && "Source must be positive!");
385 DynamicAPInt ratio = -floorDiv(m(row, targetCol), m(row, sourceCol));
386 m.addToColumn(sourceCol, targetCol, ratio);
387 otherMatrix.addToColumn(sourceCol, targetCol, ratio);
388}
389
390template <typename T>
391Matrix<T> Matrix<T>::getSubMatrix(unsigned fromRow, unsigned toRow,
392 unsigned fromColumn,
393 unsigned toColumn) const {
394 assert(fromRow <= toRow && "end of row range must be after beginning!");
395 assert(toRow <= nRows && "end of row range out of bounds!");
396 assert(fromColumn <= toColumn &&
397 "end of column range must be after beginning!");
398 assert(toColumn <= nColumns && "end of column range out of bounds!");
399 Matrix<T> subMatrix(toRow - fromRow, toColumn - fromColumn);
400 for (unsigned i = fromRow; i < toRow; ++i)
401 for (unsigned j = fromColumn; j < toColumn; ++j)
402 subMatrix(i - fromRow, j - fromColumn) = at(i, j);
403 return subMatrix;
404}
405
406template <typename T>
408 PrintTableMetrics ptm = {0, 0, "-"};
409 for (unsigned row = 0; row < nRows; ++row)
410 for (unsigned column = 0; column < nColumns; ++column)
411 updatePrintMetrics<T>(at(row, column), ptm);
412 unsigned minSpacing = 1;
413 for (unsigned row = 0; row < nRows; ++row) {
414 for (unsigned column = 0; column < nColumns; ++column) {
415 printWithPrintMetrics<T>(os, at(row, column), minSpacing, ptm);
416 }
417 os << "\n";
418 }
419}
420
421/// We iterate over the `indicator` bitset, checking each bit. If a bit is 1,
422/// we append it to one matrix, and if it is zero, we append it to the other.
423template <typename T>
424std::pair<Matrix<T>, Matrix<T>>
426 Matrix<T> rowsForOne(0, nColumns), rowsForZero(0, nColumns);
427 for (unsigned i = 0; i < nRows; i++) {
428 if (indicator[i] == 1)
429 rowsForOne.appendExtraRow(getRow(i));
430 else
431 rowsForZero.appendExtraRow(getRow(i));
432 }
433 return {rowsForOne, rowsForZero};
434}
435
436template <typename T>
437void Matrix<T>::dump() const {
438 print(llvm::errs());
439}
440
441template <typename T>
443 if (data.size() != nRows * nReservedColumns)
444 return false;
446 return false;
447#ifdef EXPENSIVE_CHECKS
448 for (unsigned r = 0; r < nRows; ++r)
449 for (unsigned c = nColumns; c < nReservedColumns; ++c)
450 if (data[r * nReservedColumns + c] != 0)
451 return false;
452#endif
453 return true;
454}
455
456namespace mlir {
457namespace presburger {
458template class Matrix<DynamicAPInt>;
459template class Matrix<Fraction>;
460} // namespace presburger
461} // namespace mlir
462
463IntMatrix IntMatrix::identity(unsigned dimension) {
464 IntMatrix matrix(dimension, dimension);
465 for (unsigned i = 0; i < dimension; ++i)
466 matrix(i, i) = 1;
467 return matrix;
468}
469
472 for (unsigned i = 0; i < nRows; i++)
473 for (unsigned j = 0; j < nColumns; j++)
474 mat(i, j) = at(i, j);
475
476 return mat;
477}
478
479std::pair<IntMatrix, IntMatrix> IntMatrix::computeHermiteNormalForm() const {
480 // We start with u as an identity matrix and perform operations on h until h
481 // is in hermite normal form. We apply the same sequence of operations on u to
482 // obtain a transform that takes h to hermite normal form.
483 IntMatrix h = *this;
485
486 unsigned echelonCol = 0;
487 // Invariant: in all rows above row, all columns from echelonCol onwards
488 // are all zero elements. In an iteration, if the curent row has any non-zero
489 // elements echelonCol onwards, we bring one to echelonCol and use it to
490 // make all elements echelonCol + 1 onwards zero.
491 for (unsigned row = 0; row < h.getNumRows(); ++row) {
492 // Search row for a non-empty entry, starting at echelonCol.
493 unsigned nonZeroCol = echelonCol;
494 for (unsigned e = h.getNumColumns(); nonZeroCol < e; ++nonZeroCol) {
495 if (h(row, nonZeroCol) == 0)
496 continue;
497 break;
498 }
499
500 // Continue to the next row with the same echelonCol if this row is all
501 // zeros from echelonCol onwards.
502 if (nonZeroCol == h.getNumColumns())
503 continue;
504
505 // Bring the non-zero column to echelonCol. This doesn't affect rows
506 // above since they are all zero at these columns.
507 if (nonZeroCol != echelonCol) {
508 h.swapColumns(nonZeroCol, echelonCol);
509 u.swapColumns(nonZeroCol, echelonCol);
510 }
511
512 // Make h(row, echelonCol) non-negative.
513 if (h(row, echelonCol) < 0) {
514 h.negateColumn(echelonCol);
515 u.negateColumn(echelonCol);
516 }
517
518 // Make all the entries in row after echelonCol zero.
519 for (unsigned i = echelonCol + 1, e = h.getNumColumns(); i < e; ++i) {
520 // We make h(row, i) non-negative, and then apply the Euclidean GCD
521 // algorithm to (row, i) and (row, echelonCol). At the end, one of them
522 // has value equal to the gcd of the two entries, and the other is zero.
523
524 if (h(row, i) < 0) {
525 h.negateColumn(i);
526 u.negateColumn(i);
527 }
528
529 unsigned targetCol = i, sourceCol = echelonCol;
530 // At every step, we set h(row, targetCol) %= h(row, sourceCol), and
531 // swap the indices sourceCol and targetCol. (not the columns themselves)
532 // This modulo is implemented as a subtraction
533 // h(row, targetCol) -= quotient * h(row, sourceCol),
534 // where quotient = floor(h(row, targetCol) / h(row, sourceCol)),
535 // which brings h(row, targetCol) to the range [0, h(row, sourceCol)).
536 //
537 // We are only allowed column operations; we perform the above
538 // for every row, i.e., the above subtraction is done as a column
539 // operation. This does not affect any rows above us since they are
540 // guaranteed to be zero at these columns.
541 while (h(row, targetCol) != 0 && h(row, sourceCol) != 0) {
542 modEntryColumnOperation(h, row, sourceCol, targetCol, u);
543 std::swap(targetCol, sourceCol);
544 }
545
546 // One of (row, echelonCol) and (row, i) is zero and the other is the gcd.
547 // Make it so that (row, echelonCol) holds the non-zero value.
548 if (h(row, echelonCol) == 0) {
549 h.swapColumns(i, echelonCol);
550 u.swapColumns(i, echelonCol);
551 }
552 }
553
554 // Make all entries before echelonCol non-negative and strictly smaller
555 // than the pivot entry.
556 for (unsigned i = 0; i < echelonCol; ++i)
557 modEntryColumnOperation(h, row, echelonCol, i, u);
558
559 ++echelonCol;
560 }
561
562 return {h, u};
563}
564
565// In the submatrix `mat(from:, from:)`, the function finds the position (row,
566// col) of the element with smallest non-zero absolute value. When all elements
567// in the submatrix are zero, returns std::nullopt.
568static std::optional<std::pair<unsigned, unsigned>>
569findNonZeroMinInSubmatrix(const IntMatrix &mat, unsigned from) {
570 unsigned numRows = mat.getNumRows();
571 unsigned numCols = mat.getNumColumns();
572 unsigned minRow = from, minCol = from;
573
574 std::optional<DynamicAPInt> minVal;
575 for (unsigned r = from; r < numRows; r++) {
576 for (unsigned c = from; c < numCols; c++) {
577 DynamicAPInt val = llvm::abs(mat(r, c));
578 if (val == 0 || (minVal && val >= *minVal))
579 continue;
580
581 minVal = val;
582 minRow = r;
583 minCol = c;
584 }
585 }
586
587 if (!minVal)
588 return std::nullopt;
589
590 return std::make_pair(minRow, minCol);
591}
592
593// Finds the first row in submatrix `mat(from:, from:)` that contains an element
594// `d` such that `d` is not a multiple of `divisor`. When there is no such row,
595// returns std::nullopt.
596static std::optional<unsigned> findNonMultipleRow(const IntMatrix &mat,
597 unsigned from,
598 const DynamicAPInt &divisor) {
599 unsigned numRows = mat.getNumRows();
600 unsigned numCols = mat.getNumColumns();
601 for (unsigned row = from; row < numRows; ++row) {
602 for (unsigned col = from; col < numCols; ++col) {
603 if (mat(row, col) % divisor != 0)
604 return row;
605 }
606 }
607 return std::nullopt;
608}
609
610std::tuple<IntMatrix, IntMatrix, IntMatrix>
612 IntMatrix d = *this;
613 // We put D into diagonal form by applying row and columns operations to it.
614 // The matrix U records row operations applied in the process, and V records
615 // column operations.
618
619 unsigned numRows = d.getNumRows();
620 unsigned numCols = d.getNumColumns();
621 for (unsigned i = 0, e = std::min(numRows, numCols); i < e; i++) {
622 // We first put D into diagonal form, and then ensure the divisibility
623 // condition. The latter step is better illustrated with an example:
624 //
625 // [6 0 ] ---(1)--> [6 10] ---(2)--> [2 0 ]
626 // [0 10] [0 10] [0 10]
627 //
628 // (1) adds the element violating the divisibility constraint to the same
629 // column in row i;
630 // (2) does an elimination of the column.
631 //
632 // There can be many elements that violate the constraint, hence the loop.
633 bool changed;
634 do {
635 changed = false;
636
637 // Find the entry in the submatrix d(i:, i:) with the smallest non-zero
638 // absolute value.
639 // The element is the pivot, and we record its current row and column.
640 auto pivotPos = findNonZeroMinInSubmatrix(d, i);
641 if (!pivotPos)
642 break;
643 auto [pvtRow, pvtCol] = *pivotPos;
644
645 // The remaining submatrix is zero.
646 if (d(pvtRow, pvtCol) == 0)
647 break;
648
649 // Bring pivot to d(i, i). Record the operation in u, v respectively.
650 if (pvtRow != i) {
651 d.swapRows(pvtRow, i);
652 u.swapRows(pvtRow, i);
653 }
654 if (pvtCol != i) {
655 d.swapColumns(pvtCol, i);
656 v.swapColumns(pvtCol, i);
657 }
658
659 // Ensure the pivot is positive.
660 if (d(i, i) < 0) {
661 d.negateRow(i);
662 u.negateRow(i);
663 }
664
665 // Clear other entries in row i and column i with Euclid's algorithm.
666 for (unsigned r = i + 1; r < numRows; ++r) {
667 while (d(r, i) != 0) {
668 DynamicAPInt quotient = d(r, i) / d(i, i);
669 d.addToRow(i, r, -quotient);
670 u.addToRow(i, r, -quotient);
671
672 if (d(r, i) != 0) {
673 d.swapRows(r, i);
674 u.swapRows(r, i);
675 changed = true;
676 }
677 }
678 }
679 // Similar to the rows operations, this time it works on columns.
680 for (unsigned c = i + 1; c < numCols; ++c) {
681 while (d(i, c) != 0) {
682 DynamicAPInt quotient = d(i, c) / d(i, i);
683 d.addToColumn(i, c, -quotient);
684 v.addToColumn(i, c, -quotient);
685
686 if (d(i, c) != 0) {
687 d.swapColumns(c, i);
688 v.swapColumns(c, i);
689 changed = true;
690 }
691 }
692 }
693
694 if (auto row = findNonMultipleRow(d, i + 1, d(i, i))) {
695 // Add the row (r) to row i. This brings d(r, c) into the i-th row,
696 // creating a new value at d(i, c) that will be used to reduce the
697 // pivot size.
698 d.addToRow(*row, i, 1);
699 u.addToRow(*row, i, 1);
700 changed = true;
701 }
702 } while (changed);
703 }
704
705 return {u, d, v};
706}
707
708DynamicAPInt IntMatrix::normalizeRow(unsigned row, unsigned cols) {
709 return normalizeRange(getRow(row).slice(0, cols));
710}
711
712DynamicAPInt IntMatrix::normalizeRow(unsigned row) {
713 return normalizeRow(row, getNumColumns());
714}
715
716DynamicAPInt IntMatrix::determinant(IntMatrix *inverse) const {
717 assert(nRows == nColumns &&
718 "determinant can only be calculated for square matrices!");
719
720 FracMatrix m(*this);
721
722 FracMatrix fracInverse(nRows, nColumns);
723 DynamicAPInt detM = m.determinant(&fracInverse).getAsInteger();
724
725 if (detM == 0)
726 return DynamicAPInt(0);
727
728 if (!inverse)
729 return detM;
730
731 *inverse = IntMatrix(nRows, nColumns);
732 for (unsigned i = 0; i < nRows; i++)
733 for (unsigned j = 0; j < nColumns; j++)
734 inverse->at(i, j) = (fracInverse.at(i, j) * detM).getAsInteger();
735
736 return detM;
737}
738
739FracMatrix FracMatrix::identity(unsigned dimension) {
740 return Matrix::identity(dimension);
741}
742
745 for (unsigned i = 0; i < nRows; i++)
746 for (unsigned j = 0; j < nColumns; j++)
747 mat(i, j) = at(i, j).getAsInteger();
748
749 return mat;
750}
751
754 for (unsigned i = 0, r = m.getNumRows(); i < r; i++)
755 for (unsigned j = 0, c = m.getNumColumns(); j < c; j++)
756 this->at(i, j) = m.at(i, j);
757}
758
760 assert(nRows == nColumns &&
761 "determinant can only be calculated for square matrices!");
762
763 FracMatrix m(*this);
764 FracMatrix tempInv(nRows, nColumns);
765 if (inverse)
766 tempInv = FracMatrix::identity(nRows);
767
768 Fraction a, b;
769 // Make the matrix into upper triangular form using
770 // gaussian elimination with row operations.
771 // If inverse is required, we apply more operations
772 // to turn the matrix into diagonal form. We apply
773 // the same operations to the inverse matrix,
774 // which is initially identity.
775 // Either way, the product of the diagonal elements
776 // is then the determinant.
777 for (unsigned i = 0; i < nRows; i++) {
778 if (m(i, i) == 0)
779 // First ensure that the diagonal
780 // element is nonzero, by swapping
781 // it with a nonzero row.
782 for (unsigned j = i + 1; j < nRows; j++) {
783 if (m(j, i) != 0) {
784 m.swapRows(j, i);
785 if (inverse)
786 tempInv.swapRows(j, i);
787 break;
788 }
789 }
790
791 b = m.at(i, i);
792 if (b == 0)
793 return 0;
794
795 // Set all elements above the
796 // diagonal to zero.
797 if (inverse) {
798 for (unsigned j = 0; j < i; j++) {
799 if (m.at(j, i) == 0)
800 continue;
801 a = m.at(j, i);
802 // Set element (j, i) to zero
803 // by subtracting the ith row,
804 // appropriately scaled.
805 m.addToRow(i, j, -a / b);
806 tempInv.addToRow(i, j, -a / b);
807 }
808 }
809
810 // Set all elements below the
811 // diagonal to zero.
812 for (unsigned j = i + 1; j < nRows; j++) {
813 if (m.at(j, i) == 0)
814 continue;
815 a = m.at(j, i);
816 // Set element (j, i) to zero
817 // by subtracting the ith row,
818 // appropriately scaled.
819 m.addToRow(i, j, -a / b);
820 if (inverse)
821 tempInv.addToRow(i, j, -a / b);
822 }
823 }
824
825 // Now only diagonal elements of m are nonzero, but they are
826 // not necessarily 1. To get the true inverse, we should
827 // normalize them and apply the same scale to the inverse matrix.
828 // For efficiency we skip scaling m and just scale tempInv appropriately.
829 if (inverse) {
830 for (unsigned i = 0; i < nRows; i++)
831 for (unsigned j = 0; j < nRows; j++)
832 tempInv.at(i, j) = tempInv.at(i, j) / m(i, i);
833
834 *inverse = std::move(tempInv);
835 }
836
838 for (unsigned i = 0; i < nRows; i++)
839 determinant *= m.at(i, i);
840
841 return determinant;
842}
843
845 // Create a copy of the argument to store
846 // the orthogonalised version.
847 FracMatrix orth(*this);
848
849 // For each vector (row) in the matrix, subtract its unit
850 // projection along each of the previous vectors.
851 // This ensures that it has no component in the direction
852 // of any of the previous vectors.
853 for (unsigned i = 1, e = getNumRows(); i < e; i++) {
854 for (unsigned j = 0; j < i; j++) {
855 Fraction jNormSquared = dotProduct(orth.getRow(j), orth.getRow(j));
856 assert(jNormSquared != 0 && "some row became zero! Inputs to this "
857 "function must be linearly independent.");
858 Fraction projectionScale =
859 dotProduct(orth.getRow(i), orth.getRow(j)) / jNormSquared;
860 orth.addToRow(j, i, -projectionScale);
861 }
862 }
863 return orth;
864}
865
866// Convert the matrix, interpreted (row-wise) as a basis
867// to an LLL-reduced basis.
868//
869// This is an implementation of the algorithm described in
870// "Factoring polynomials with rational coefficients" by
871// A. K. Lenstra, H. W. Lenstra Jr., L. Lovasz.
872//
873// Let {b_1, ..., b_n} be the current basis and
874// {b_1*, ..., b_n*} be the Gram-Schmidt orthogonalised
875// basis (unnormalized).
876// Define the Gram-Schmidt coefficients μ_ij as
877// (b_i • b_j*) / (b_j* • b_j*), where (•) represents the inner product.
878//
879// We iterate starting from the second row to the last row.
880//
881// For the kth row, we first check μ_kj for all rows j < k.
882// We subtract b_j (scaled by the integer nearest to μ_kj)
883// from b_k.
884//
885// Now, we update k.
886// If b_k and b_{k-1} satisfy the Lovasz condition
887// |b_k|^2 ≥ (δ - μ_k{k-1}^2) |b_{k-1}|^2,
888// we are done and we increment k.
889// Otherwise, we swap b_k and b_{k-1} and decrement k.
890//
891// We repeat this until k = n and return.
892void FracMatrix::LLL(const Fraction &delta) {
893 DynamicAPInt nearest;
894 Fraction mu;
895
896 // `gsOrth` holds the Gram-Schmidt orthogonalisation
897 // of the matrix at all times. It is recomputed every
898 // time the matrix is modified during the algorithm.
899 // This is naive and can be optimised.
900 FracMatrix gsOrth = gramSchmidt();
901
902 // We start from the second row.
903 unsigned k = 1;
904 while (k < getNumRows()) {
905 for (unsigned j = k - 1; j < k; j--) {
906 // Compute the Gram-Schmidt coefficient μ_jk.
907 mu = dotProduct(getRow(k), gsOrth.getRow(j)) /
908 dotProduct(gsOrth.getRow(j), gsOrth.getRow(j));
909 nearest = round(mu);
910 // Subtract b_j scaled by the integer nearest to μ_jk from b_k.
911 addToRow(k, getRow(j), -Fraction(nearest, 1));
912 gsOrth = gramSchmidt(); // Update orthogonalization.
913 }
914 mu = dotProduct(getRow(k), gsOrth.getRow(k - 1)) /
915 dotProduct(gsOrth.getRow(k - 1), gsOrth.getRow(k - 1));
916 // Check the Lovasz condition for b_k and b_{k-1}.
917 if (dotProduct(gsOrth.getRow(k), gsOrth.getRow(k)) >
918 (delta - mu * mu) *
919 dotProduct(gsOrth.getRow(k - 1), gsOrth.getRow(k - 1))) {
920 // If it is satisfied, proceed to the next k.
921 k += 1;
922 } else {
923 // If it is not satisfied, decrement k (without
924 // going beyond the second row).
925 swapRows(k, k - 1);
926 gsOrth = gramSchmidt(); // Update orthogonalization.
927 k = k > 1 ? k - 1 : 1;
928 }
929 }
930}
931
933 unsigned numRows = getNumRows();
934 unsigned numColumns = getNumColumns();
935 IntMatrix normalized(numRows, numColumns);
936
937 DynamicAPInt lcmDenoms = DynamicAPInt(1);
938 for (unsigned i = 0; i < numRows; i++) {
939 // For a row, first compute the LCM of the denominators.
940 for (unsigned j = 0; j < numColumns; j++)
941 lcmDenoms = lcm(lcmDenoms, at(i, j).den);
942 // Then, multiply by it throughout and convert to integers.
943 for (unsigned j = 0; j < numColumns; j++)
944 normalized(i, j) = (at(i, j) * lcmDenoms).getAsInteger();
945 }
946 return normalized;
947}
for(Operation *op :ops)
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
static std::optional< std::pair< unsigned, unsigned > > findNonZeroMinInSubmatrix(const IntMatrix &mat, unsigned from)
Definition Matrix.cpp:569
static void modEntryColumnOperation(Matrix< DynamicAPInt > &m, unsigned row, unsigned sourceCol, unsigned targetCol, Matrix< DynamicAPInt > &otherMatrix)
Set M(row, targetCol) to its remainder on division by M(row, sourceCol) by subtracting from column ta...
Definition Matrix.cpp:380
static std::optional< unsigned > findNonMultipleRow(const IntMatrix &mat, unsigned from, const DynamicAPInt &divisor)
Definition Matrix.cpp:596
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
FracMatrix gramSchmidt() const
Definition Matrix.cpp:844
IntMatrix normalizeRows() const
Definition Matrix.cpp:932
static FracMatrix identity(unsigned dimension)
Return the identity matrix of the specified dimension.
Definition Matrix.cpp:739
FracMatrix(unsigned rows, unsigned columns, unsigned reservedRows=0, unsigned reservedColumns=0)
Definition Matrix.h:315
Fraction determinant(FracMatrix *inverse=nullptr) const
Definition Matrix.cpp:759
void LLL(const Fraction &delta)
Definition Matrix.cpp:892
IntMatrix asIntMatrix() const
Definition Matrix.cpp:743
static IntMatrix identity(unsigned dimension)
Return the identity matrix of the specified dimension.
Definition Matrix.cpp:463
std::pair< IntMatrix, IntMatrix > computeHermiteNormalForm() const
Given the current matrix M, returns the matrices H, U such that H is the column hermite normal form o...
Definition Matrix.cpp:479
DynamicAPInt determinant(IntMatrix *inverse=nullptr) const
Definition Matrix.cpp:716
FracMatrix asFracMatrix() const
Definition Matrix.cpp:470
IntMatrix(unsigned rows, unsigned columns, unsigned reservedRows=0, unsigned reservedColumns=0)
Definition Matrix.h:261
DynamicAPInt normalizeRow(unsigned row, unsigned nCols)
Divide the first nCols of the specified row by their GCD.
Definition Matrix.cpp:708
std::tuple< IntMatrix, IntMatrix, IntMatrix > computeSmithNormalForm() const
Given the current matrix M, returns the matrices U, D, V such that UMV = D, where D is called the Smi...
Definition Matrix.cpp:611
This is a class to represent a resizable matrix.
Definition Matrix.h:42
void moveColumns(unsigned srcPos, unsigned num, unsigned dstPos)
Move the columns in the source range [srcPos, srcPos + num) to the specified destination [dstPos,...
Definition Matrix.cpp:260
bool hasConsistentState() const
Return whether the Matrix is in a consistent state with all its invariants satisfied.
Definition Matrix.cpp:442
void insertRows(unsigned pos, unsigned count)
Definition Matrix.cpp:216
unsigned getNumRows() const
Definition Matrix.h:86
void swapColumns(unsigned column, unsigned otherColumn)
Swap the given columns.
Definition Matrix.cpp:120
unsigned nRows
The current number of rows, columns, and reserved columns.
Definition Matrix.h:245
void removeColumn(unsigned pos)
Definition Matrix.cpp:194
unsigned appendExtraRow()
Add an extra row at the bottom of the matrix and return its position.
Definition Matrix.cpp:65
unsigned nReservedColumns
Definition Matrix.h:245
void addToColumn(unsigned sourceColumn, unsigned targetColumn, const T &scale)
Add scale multiples of the source column to the target column.
Definition Matrix.cpp:326
Matrix< T > getSubMatrix(unsigned fromRow, unsigned toRow, unsigned fromColumn, unsigned toColumn) const
Definition Matrix.cpp:391
void print(raw_ostream &os) const
Print the matrix.
Definition Matrix.cpp:407
void copyRow(unsigned sourceRow, unsigned targetRow)
Definition Matrix.cpp:244
void scaleRow(unsigned row, const T &scale)
Multiply the specified row by a factor of scale.
Definition Matrix.cpp:320
void insertColumn(unsigned pos)
Definition Matrix.cpp:148
MutableArrayRef< T > getRow(unsigned row)
Get a [Mutable]ArrayRef corresponding to the specified row.
Definition Matrix.cpp:130
void removeColumns(unsigned pos, unsigned count)
Definition Matrix.cpp:198
void insertColumns(unsigned pos, unsigned count)
Definition Matrix.cpp:152
void setRow(unsigned row, ArrayRef< T > elems)
Set the specified row to elems.
Definition Matrix.cpp:140
std::pair< Matrix< T >, Matrix< T > > splitByBitset(ArrayRef< int > indicator)
Split the rows of a matrix into two matrices according to which bits are 1 and which are 0 in a given...
Definition Matrix.cpp:425
void removeRow(unsigned pos)
Definition Matrix.cpp:230
bool operator==(const Matrix< T > &m) const
We cannot use the default implementation of operator== as it compares fields like reservedColumns etc...
Definition Matrix.cpp:33
SmallVector< T, 16 > data
Stores the data.
Definition Matrix.h:249
unsigned getNumColumns() const
Definition Matrix.h:88
T & at(unsigned row, unsigned column)
Access the element at the specified row and column.
Definition Matrix.h:62
void resizeVertically(unsigned newNRows)
Definition Matrix.cpp:104
unsigned getNumReservedRows() const
Return the maximum number of rows/columns that can be added without incurring a reallocation.
Definition Matrix.cpp:55
Matrix< T > transpose() const
Definition Matrix.cpp:80
SmallVector< T, 8 > preMultiplyWithRow(ArrayRef< T > rowVec) const
The given vector is interpreted as a row vector v.
Definition Matrix.cpp:353
static Matrix identity(unsigned dimension)
Return the identity matrix of the specified dimension.
Definition Matrix.cpp:47
void insertRow(unsigned pos)
Definition Matrix.cpp:212
SmallVector< T, 8 > postMultiplyWithColumn(ArrayRef< T > colVec) const
The given vector is interpreted as a column vector v.
Definition Matrix.cpp:364
void negateMatrix()
Negate the entire matrix.
Definition Matrix.cpp:347
void swapRows(unsigned row, unsigned otherRow)
Swap the given rows.
Definition Matrix.cpp:110
void resizeHorizontally(unsigned newNColumns)
Definition Matrix.cpp:90
void reserveRows(unsigned rows)
Reserve enough space to resize to the specified number of rows without reallocations.
Definition Matrix.cpp:60
void negateColumn(unsigned column)
Negate the specified column.
Definition Matrix.cpp:335
void resize(unsigned newNRows, unsigned newNColumns)
Resize the matrix to the specified dimensions.
Definition Matrix.cpp:98
void fillRow(unsigned row, const T &value)
Definition Matrix.cpp:252
void addToRow(unsigned sourceRow, unsigned targetRow, const T &scale)
Add scale multiples of the source row to the target row.
Definition Matrix.cpp:306
void negateRow(unsigned row)
Negate the specified row.
Definition Matrix.cpp:341
Matrix< T > postMultiply(const Matrix< T > &other) const
Returns the matrix right-multiplied with other.
Definition Matrix.cpp:288
void removeRows(unsigned pos, unsigned count)
Remove the rows having positions pos, pos + 1, ... pos + count - 1.
Definition Matrix.cpp:234
DynamicAPInt round(const Fraction &f)
Definition Fraction.h:136
Fraction dotProduct(ArrayRef< Fraction > a, ArrayRef< Fraction > b)
Compute the dot product of two vectors.
Definition Utils.cpp:537
void printWithPrintMetrics(raw_ostream &os, T val, unsigned minSpacing, const PrintTableMetrics &m)
Print val in the table with metrics specified in 'm'.
Definition Utils.h:328
void updatePrintMetrics(T val, PrintTableMetrics &m)
Iterate over each val in the table and update 'm' where .maxPreIndent and .maxPostIndent are initiali...
Definition Utils.h:314
DynamicAPInt normalizeRange(MutableArrayRef< DynamicAPInt > range)
Divide the range by its gcd and return the gcd.
Definition Utils.cpp:350
Include the generated interface declarations.
A class to represent fractions.
Definition Fraction.h:29
DynamicAPInt getAsInteger() const
Definition Fraction.h:51
Example usage: Print .12, 3.4, 56.7 preAlign = ".", minSpacing = 1, .12 .12 3.4 3....
Definition Utils.h:303
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.