[clang-tools-extra] [MLIR][Presburger] Define matrix inverse for rational matrices (PR #67382)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 25 16:32:33 PDT 2023
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff c74398649f28ab78c74bacfe82e69224377008e7 69cce307842ab6bfbca10f44d3826623379a0385 -- mlir/include/mlir/Analysis/Presburger/Fraction.h mlir/include/mlir/Analysis/Presburger/Matrix.h mlir/lib/Analysis/Presburger/Matrix.cpp mlir/unittests/Analysis/Presburger/MatrixTest.cpp mlir/unittests/Analysis/Presburger/Utils.h
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index b9048334c39f..74edddd6cb7d 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -247,27 +247,25 @@ public:
// An inherited class for rational matrices, with no new data attributes.
// This is only used for the matrix-related method which apply only
// to fractions (inverse).
-class FracMatrix : public Matrix<Fraction>
-{
+class FracMatrix : public Matrix<Fraction> {
public:
FracMatrix(unsigned rows, unsigned columns, unsigned reservedRows = 0,
- unsigned reservedColumns = 0) :
- Matrix<Fraction>(rows, columns, reservedRows, reservedColumns) {};
+ unsigned reservedColumns = 0)
+ : Matrix<Fraction>(rows, columns, reservedRows, reservedColumns){};
- FracMatrix(Matrix<Fraction> m) :
- Matrix<Fraction>(m.getNumRows(), m.getNumColumns(), m.getNumReservedRows(), m.getNumReservedColumns())
- {
+ FracMatrix(Matrix<Fraction> m)
+ : Matrix<Fraction>(m.getNumRows(), m.getNumColumns(),
+ m.getNumReservedRows(), m.getNumReservedColumns()) {
for (unsigned i = 0; i < m.getNumRows(); i++)
for (unsigned j = 0; j < m.getNumColumns(); j++)
at(i, j) = m(i, j);
};
-
+
/// Return the identity matrix of the specified dimension.
static FracMatrix identity(unsigned dimension);
// Return the inverse of the matrix, leaving the calling object unmodified.
FracMatrix inverse();
-
};
} // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index c0fbc688be2e..de5e2bbe285a 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -392,7 +392,6 @@ MPInt IntMatrix::normalizeRow(unsigned row) {
return normalizeRow(row, getNumColumns());
}
-
FracMatrix FracMatrix::identity(unsigned dimension) {
FracMatrix matrix(dimension, dimension);
for (unsigned i = 0; i < dimension; ++i)
@@ -400,60 +399,55 @@ FracMatrix FracMatrix::identity(unsigned dimension) {
return matrix;
}
-FracMatrix FracMatrix::inverse()
-{
- // We use Gaussian elimination on the rows of [M | I]
- // to find the integer inverse. We proceed left-to-right,
- // top-to-bottom. M is assumed to be a dim x dim matrix.
+FracMatrix FracMatrix::inverse() {
+ // We use Gaussian elimination on the rows of [M | I]
+ // to find the integer inverse. We proceed left-to-right,
+ // top-to-bottom. M is assumed to be a dim x dim matrix.
- unsigned dim = getNumRows();
+ unsigned dim = getNumRows();
- // Construct the augmented matrix [M | I]
- FracMatrix augmented(dim, dim + dim);
- for (unsigned i = 0; i < dim; i++)
- {
- augmented.fillRow(i, 0);
- for (unsigned j = 0; j < dim; j++)
- augmented(i, j) = at(i, j);
- augmented(i, dim+i).num = 1;
- augmented(i, dim+i).den = 1;
- }
- Fraction a, b;
- for (unsigned i = 0; i < dim; i++)
- {
- if (augmented(i, i) == Fraction(0, 1))
- for (unsigned j = i+1; j < dim; j++)
- if (augmented(j, i) != Fraction(0, 1))
- {
- augmented.addToRow(i, augmented.getRow(j), Fraction(1, 1));
- break;
- }
-
- b = augmented(i, i);
- for (unsigned j = 0; j < dim; j++)
- {
- if (i == j || augmented(j, i) == 0) continue;
- a = augmented(j, i);
- // Rj -> Rj - (b/a)Ri
- augmented.addToRow(j, augmented.getRow(i), - a / b);
- // Now (Rj)i = 0
+ // Construct the augmented matrix [M | I]
+ FracMatrix augmented(dim, dim + dim);
+ for (unsigned i = 0; i < dim; i++) {
+ augmented.fillRow(i, 0);
+ for (unsigned j = 0; j < dim; j++)
+ augmented(i, j) = at(i, j);
+ augmented(i, dim + i).num = 1;
+ augmented(i, dim + i).den = 1;
+ }
+ Fraction a, b;
+ for (unsigned i = 0; i < dim; i++) {
+ if (augmented(i, i) == Fraction(0, 1))
+ for (unsigned j = i + 1; j < dim; j++)
+ if (augmented(j, i) != Fraction(0, 1)) {
+ augmented.addToRow(i, augmented.getRow(j), Fraction(1, 1));
+ break;
}
+
+ b = augmented(i, i);
+ for (unsigned j = 0; j < dim; j++) {
+ if (i == j || augmented(j, i) == 0)
+ continue;
+ a = augmented(j, i);
+ // Rj -> Rj - (b/a)Ri
+ augmented.addToRow(j, augmented.getRow(i), -a / b);
+ // Now (Rj)i = 0
}
-
- // Now only diagonal elements are nonzero, but they are
- // not necessarily 1.
- for (unsigned i = 0; i < dim; i++)
- {
- a = augmented(i, i);
- for (unsigned j = dim; j < dim + dim; j++)
- augmented(i, j) = augmented(i, j) / a;
- }
+ }
+
+ // Now only diagonal elements are nonzero, but they are
+ // not necessarily 1.
+ for (unsigned i = 0; i < dim; i++) {
+ a = augmented(i, i);
+ for (unsigned j = dim; j < dim + dim; j++)
+ augmented(i, j) = augmented(i, j) / a;
+ }
- // Copy the right half of the augmented matrix.
- FracMatrix inverse(dim, dim);
- for (unsigned i = 0; i < dim; i++)
- for (unsigned j = 0; j < dim; j++)
- inverse(i, j) = augmented(i, j+dim);
+ // Copy the right half of the augmented matrix.
+ FracMatrix inverse(dim, dim);
+ for (unsigned i = 0; i < dim; i++)
+ for (unsigned j = 0; j < dim; j++)
+ inverse(i, j) = augmented(i, j + dim);
- return inverse;
+ return inverse;
}
\ No newline at end of file
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index d07318617e1a..efa6d4507d16 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -250,12 +250,16 @@ TEST(MatrixTest, computeHermiteNormalForm) {
}
TEST(MatrixTest, inverse) {
- FracMatrix mat = makeFracMatrix(2, 2, {{Fraction(2, 1), Fraction(1, 1)}, {Fraction(7, 1), Fraction(0, 1)}});
- FracMatrix inverse = makeFracMatrix(2, 2, {{Fraction(0, 1), Fraction(1, 7)}, {Fraction(1, 1), Fraction(-2, 7)}});
-
- FracMatrix inv = mat.inverse();
-
- for (unsigned row = 0; row < 2; row++)
- for (unsigned col = 0; col < 2; col++)
- EXPECT_EQ(inv(row, col), inverse(row, col));
+ FracMatrix mat = makeFracMatrix(
+ 2, 2,
+ {{Fraction(2, 1), Fraction(1, 1)}, {Fraction(7, 1), Fraction(0, 1)}});
+ FracMatrix inverse = makeFracMatrix(
+ 2, 2,
+ {{Fraction(0, 1), Fraction(1, 7)}, {Fraction(1, 1), Fraction(-2, 7)}});
+
+ FracMatrix inv = mat.inverse();
+
+ for (unsigned row = 0; row < 2; row++)
+ for (unsigned col = 0; col < 2; col++)
+ EXPECT_EQ(inv(row, col), inverse(row, col));
}
diff --git a/mlir/unittests/Analysis/Presburger/Utils.h b/mlir/unittests/Analysis/Presburger/Utils.h
index c652daa583a8..ce149c32ffa3 100644
--- a/mlir/unittests/Analysis/Presburger/Utils.h
+++ b/mlir/unittests/Analysis/Presburger/Utils.h
@@ -41,7 +41,7 @@ inline IntMatrix makeIntMatrix(unsigned numRow, unsigned numColumns,
}
inline FracMatrix makeFracMatrix(unsigned numRow, unsigned numColumns,
- ArrayRef<SmallVector<Fraction, 8>> matrix) {
+ ArrayRef<SmallVector<Fraction, 8>> matrix) {
FracMatrix results(numRow, numColumns);
assert(matrix.size() == numRow);
for (unsigned i = 0; i < numRow; ++i) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/67382
More information about the cfe-commits
mailing list