[Mlir-commits] [mlir] [MLIR][Presburger] Add Gram-Schmidt (PR #70843)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Dec 4 01:09:47 PST 2023
https://github.com/Abhinav271828 updated https://github.com/llvm/llvm-project/pull/70843
>From 060e3c8f7f76e083723e4c303ec396447e6a1df3 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 31 Oct 2023 17:55:43 +0000
Subject: [PATCH 01/11] Add gram-schmidt and tests
---
.../include/mlir/Analysis/Presburger/Matrix.h | 5 ++++
mlir/include/mlir/Analysis/Presburger/Utils.h | 5 ++++
mlir/lib/Analysis/Presburger/Matrix.cpp | 23 +++++++++++++++++++
mlir/lib/Analysis/Presburger/Utils.cpp | 8 +++++++
.../Analysis/Presburger/MatrixTest.cpp | 15 ++++++++++++
5 files changed, 56 insertions(+)
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 4d9f13832e069..b591b0b4fdad1 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -265,6 +265,11 @@ class FracMatrix : public Matrix<Fraction> {
// does not exist, which happens iff det = 0.
// Assert-fails if the matrix is not square.
Fraction determinant(FracMatrix *inverse = nullptr) const;
+
+ // Computes the Gram-Schmidt orthogonalisation
+ // of the matrix (cubic time).
+ FracMatrix gramSchmidt() const;
+
};
} // namespace presburger
diff --git a/mlir/include/mlir/Analysis/Presburger/Utils.h b/mlir/include/mlir/Analysis/Presburger/Utils.h
index a451ae8bf5572..639683cea0d9f 100644
--- a/mlir/include/mlir/Analysis/Presburger/Utils.h
+++ b/mlir/include/mlir/Analysis/Presburger/Utils.h
@@ -276,6 +276,11 @@ SmallVector<MPInt, 8> getNegatedCoeffs(ArrayRef<MPInt> coeffs);
/// a_1 x_1 + ... + a_n x_ + c < 0, i.e., -a_1 x_1 - ... - a_n x_ - c - 1 >= 0,
/// since all the variables are constrained to be integers.
SmallVector<MPInt, 8> getComplementIneq(ArrayRef<MPInt> ineq);
+
+/// Compute the dot product of two vectors.
+/// Assumes that the vectors have the same sizes.
+Fraction dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b);
+
} // namespace presburger
} // namespace mlir
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index ae97e456d9820..7669e461f0794 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -548,4 +548,27 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
determinant *= m.at(i, i);
return determinant;
+}
+
+FracMatrix FracMatrix::gramSchmidt() const {
+
+ // Create a copy of the argument to store
+ // the orthogonalised version.
+ FracMatrix orth(*this);
+ Fraction projectionScale;
+
+ // For each vector (row) in the matrix,
+ // subtract its unit projection along
+ // each of the previous vectors.
+ // This ensures that it has no component
+ // in the direction of any of the
+ // previous vectors.
+ for (unsigned i = 1; i < getNumRows(); i++) {
+ for (unsigned j = 0; j < i; j++) {
+ projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
+ dotProduct(orth.getRow(j), orth.getRow(j));
+ orth.addToRow(j, i, -projectionScale);
+ }
+ }
+ return orth;
}
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/Utils.cpp b/mlir/lib/Analysis/Presburger/Utils.cpp
index 9aef2f5de1093..30f01f5458191 100644
--- a/mlir/lib/Analysis/Presburger/Utils.cpp
+++ b/mlir/lib/Analysis/Presburger/Utils.cpp
@@ -520,3 +520,11 @@ SmallVector<int64_t, 8> presburger::getInt64Vec(ArrayRef<MPInt> range) {
std::transform(range.begin(), range.end(), result.begin(), int64FromMPInt);
return result;
}
+
+Fraction presburger::dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b) {
+ assert(a.size() == b.size() && "Dot product of two unequal vectors!");
+ Fraction sum = 0;
+ for (unsigned i = 0; i < a.size(); i++)
+ sum += a[i] * b[i];
+ return sum;
+}
\ No newline at end of file
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index d05b05e004c5c..b7cc8e07a54e1 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -310,3 +310,18 @@ TEST(MatrixTest, intInverse) {
EXPECT_EQ(det, 0);
}
+
+TEST(MatrixTest, gramSchmidt) {
+ FracMatrix mat = makeFracMatrix(3, 5, {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1), Fraction(19, 1)},
+ {Fraction(4, 1), Fraction(5, 1), Fraction(6, 1), Fraction(13, 1), Fraction(20, 1)},
+ {Fraction(7, 1), Fraction(8, 1), Fraction(9, 1), Fraction(16, 1), Fraction(24, 1)}});
+
+ FracMatrix gramSchmidt = makeFracMatrix(3, 5,
+ {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1), Fraction(19, 1)},
+ {Fraction(142, 185), Fraction(383, 555), Fraction(68, 111), Fraction(13, 185), Fraction(-262, 555)},
+ {Fraction(53, 463), Fraction(27, 463), Fraction(1, 463), Fraction(-181, 463), Fraction(100, 463)}});
+
+ FracMatrix gs = mat.gramSchmidt();
+
+ EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
+}
\ No newline at end of file
>From b42b51c0c1fc8599fbec422f262672cad1bc41d9 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 10:39:47 +0000
Subject: [PATCH 02/11] Minor fixes
---
mlir/include/mlir/Analysis/Presburger/Utils.h | 2 +-
mlir/lib/Analysis/Presburger/Matrix.cpp | 6 ++----
2 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Utils.h b/mlir/include/mlir/Analysis/Presburger/Utils.h
index 639683cea0d9f..3fb978e4faf6d 100644
--- a/mlir/include/mlir/Analysis/Presburger/Utils.h
+++ b/mlir/include/mlir/Analysis/Presburger/Utils.h
@@ -278,7 +278,7 @@ SmallVector<MPInt, 8> getNegatedCoeffs(ArrayRef<MPInt> coeffs);
SmallVector<MPInt, 8> getComplementIneq(ArrayRef<MPInt> ineq);
/// Compute the dot product of two vectors.
-/// Assumes that the vectors have the same sizes.
+/// The vectors must have the same sizes.
Fraction dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b);
} // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 7669e461f0794..896a45cef7361 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -551,11 +551,9 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
}
FracMatrix FracMatrix::gramSchmidt() const {
-
// Create a copy of the argument to store
// the orthogonalised version.
FracMatrix orth(*this);
- Fraction projectionScale;
// For each vector (row) in the matrix,
// subtract its unit projection along
@@ -565,8 +563,8 @@ FracMatrix FracMatrix::gramSchmidt() const {
// previous vectors.
for (unsigned i = 1; i < getNumRows(); i++) {
for (unsigned j = 0; j < i; j++) {
- projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
- dotProduct(orth.getRow(j), orth.getRow(j));
+ Fraction projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
+ dotProduct(orth.getRow(j), orth.getRow(j));
orth.addToRow(j, i, -projectionScale);
}
}
>From bd5be3b1712eb5408ea1b2f07b4b6ae526155292 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 10:50:28 +0000
Subject: [PATCH 03/11] Test for zero dot product
---
mlir/unittests/Analysis/Presburger/MatrixTest.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index b7cc8e07a54e1..7e94c717a985b 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -324,4 +324,7 @@ TEST(MatrixTest, gramSchmidt) {
FracMatrix gs = mat.gramSchmidt();
EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
+ for (unsigned i = 0; i < 3u; i++)
+ for (unsigned j = i+1; j < 3u; j++)
+ EXPECT_EQ(dotProduct(gramSchmidt.getRow(i), gramSchmidt.getRow(j)), 0);
}
\ No newline at end of file
>From 94beea95b63abf17a96d6beca7afb43c4f89760d Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 13:44:36 +0000
Subject: [PATCH 04/11] Add assert for linear independence
---
mlir/include/mlir/Analysis/Presburger/Matrix.h | 3 ++-
mlir/lib/Analysis/Presburger/Matrix.cpp | 3 +++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index b591b0b4fdad1..d0df429e22594 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -267,7 +267,8 @@ class FracMatrix : public Matrix<Fraction> {
Fraction determinant(FracMatrix *inverse = nullptr) const;
// Computes the Gram-Schmidt orthogonalisation
- // of the matrix (cubic time).
+ // of the rows of matrix (cubic time).
+ // The rows of the matrix must be linearly independent.
FracMatrix gramSchmidt() const;
};
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 896a45cef7361..2bfc07fa8402e 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -551,6 +551,9 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
}
FracMatrix FracMatrix::gramSchmidt() const {
+ bool linIndep = (nRows < nColumns) || (nRows == nColumns && determinant(nullptr) != 0);
+ assert(linIndep && "the vectors must be linearly independent!");
+
// Create a copy of the argument to store
// the orthogonalised version.
FracMatrix orth(*this);
>From 6769effdb2a8d27530a1a537f6a8598527256ceb Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 13:46:26 +0000
Subject: [PATCH 05/11] Formatting
---
.../include/mlir/Analysis/Presburger/Matrix.h | 1 -
mlir/lib/Analysis/Presburger/Matrix.cpp | 41 ++++++++++---------
.../Analysis/Presburger/MatrixTest.cpp | 27 ++++++++----
3 files changed, 39 insertions(+), 30 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index d0df429e22594..89fad85c0c337 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -270,7 +270,6 @@ class FracMatrix : public Matrix<Fraction> {
// of the rows of matrix (cubic time).
// The rows of the matrix must be linearly independent.
FracMatrix gramSchmidt() const;
-
};
} // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 2bfc07fa8402e..c2adea94216b1 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -551,25 +551,26 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
}
FracMatrix FracMatrix::gramSchmidt() const {
- bool linIndep = (nRows < nColumns) || (nRows == nColumns && determinant(nullptr) != 0);
- assert(linIndep && "the vectors must be linearly independent!");
-
- // Create a copy of the argument to store
- // the orthogonalised version.
- FracMatrix orth(*this);
-
- // For each vector (row) in the matrix,
- // subtract its unit projection along
- // each of the previous vectors.
- // This ensures that it has no component
- // in the direction of any of the
- // previous vectors.
- for (unsigned i = 1; i < getNumRows(); i++) {
- for (unsigned j = 0; j < i; j++) {
- Fraction projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
- dotProduct(orth.getRow(j), orth.getRow(j));
- orth.addToRow(j, i, -projectionScale);
- }
+ bool linIndep =
+ (nRows < nColumns) || (nRows == nColumns && determinant(nullptr) != 0);
+ assert(linIndep && "the vectors must be linearly independent!");
+
+ // Create a copy of the argument to store
+ // the orthogonalised version.
+ FracMatrix orth(*this);
+
+ // For each vector (row) in the matrix,
+ // subtract its unit projection along
+ // each of the previous vectors.
+ // This ensures that it has no component
+ // in the direction of any of the
+ // previous vectors.
+ for (unsigned i = 1; i < getNumRows(); i++) {
+ for (unsigned j = 0; j < i; j++) {
+ Fraction projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
+ dotProduct(orth.getRow(j), orth.getRow(j));
+ orth.addToRow(j, i, -projectionScale);
}
- return orth;
+ }
+ return orth;
}
\ No newline at end of file
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 7e94c717a985b..1c4f91884b346 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -312,19 +312,28 @@ TEST(MatrixTest, intInverse) {
}
TEST(MatrixTest, gramSchmidt) {
- FracMatrix mat = makeFracMatrix(3, 5, {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1), Fraction(19, 1)},
- {Fraction(4, 1), Fraction(5, 1), Fraction(6, 1), Fraction(13, 1), Fraction(20, 1)},
- {Fraction(7, 1), Fraction(8, 1), Fraction(9, 1), Fraction(16, 1), Fraction(24, 1)}});
-
- FracMatrix gramSchmidt = makeFracMatrix(3, 5,
- {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1), Fraction(19, 1)},
- {Fraction(142, 185), Fraction(383, 555), Fraction(68, 111), Fraction(13, 185), Fraction(-262, 555)},
- {Fraction(53, 463), Fraction(27, 463), Fraction(1, 463), Fraction(-181, 463), Fraction(100, 463)}});
+ FracMatrix mat =
+ makeFracMatrix(3, 5,
+ {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1),
+ Fraction(12, 1), Fraction(19, 1)},
+ {Fraction(4, 1), Fraction(5, 1), Fraction(6, 1),
+ Fraction(13, 1), Fraction(20, 1)},
+ {Fraction(7, 1), Fraction(8, 1), Fraction(9, 1),
+ Fraction(16, 1), Fraction(24, 1)}});
+
+ FracMatrix gramSchmidt = makeFracMatrix(
+ 3, 5,
+ {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1),
+ Fraction(19, 1)},
+ {Fraction(142, 185), Fraction(383, 555), Fraction(68, 111),
+ Fraction(13, 185), Fraction(-262, 555)},
+ {Fraction(53, 463), Fraction(27, 463), Fraction(1, 463),
+ Fraction(-181, 463), Fraction(100, 463)}});
FracMatrix gs = mat.gramSchmidt();
EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
for (unsigned i = 0; i < 3u; i++)
- for (unsigned j = i+1; j < 3u; j++)
+ for (unsigned j = i + 1; j < 3u; j++)
EXPECT_EQ(dotProduct(gramSchmidt.getRow(i), gramSchmidt.getRow(j)), 0);
}
\ No newline at end of file
>From e352ed9772e0665f114f47098d29d66053aca37f Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 14:07:29 +0000
Subject: [PATCH 06/11] Formatting
---
mlir/lib/Analysis/Presburger/Utils.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Analysis/Presburger/Utils.cpp b/mlir/lib/Analysis/Presburger/Utils.cpp
index 30f01f5458191..a11bd9db00585 100644
--- a/mlir/lib/Analysis/Presburger/Utils.cpp
+++ b/mlir/lib/Analysis/Presburger/Utils.cpp
@@ -521,7 +521,8 @@ SmallVector<int64_t, 8> presburger::getInt64Vec(ArrayRef<MPInt> range) {
return result;
}
-Fraction presburger::dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b) {
+Fraction presburger::dotProduct(MutableArrayRef<Fraction> a,
+ MutableArrayRef<Fraction> b) {
assert(a.size() == b.size() && "Dot product of two unequal vectors!");
Fraction sum = 0;
for (unsigned i = 0; i < a.size(); i++)
>From 14d1b11bcae11311f1737522acd07a086ac5800d Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Mon, 6 Nov 2023 13:19:47 +0000
Subject: [PATCH 07/11] Style fixes
---
mlir/include/mlir/Analysis/Presburger/Utils.h | 2 +-
mlir/lib/Analysis/Presburger/Matrix.cpp | 18 ++++++++----------
mlir/lib/Analysis/Presburger/Utils.cpp | 8 ++++----
3 files changed, 13 insertions(+), 15 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Utils.h b/mlir/include/mlir/Analysis/Presburger/Utils.h
index 3fb978e4faf6d..20af0bfcd62ba 100644
--- a/mlir/include/mlir/Analysis/Presburger/Utils.h
+++ b/mlir/include/mlir/Analysis/Presburger/Utils.h
@@ -279,7 +279,7 @@ SmallVector<MPInt, 8> getComplementIneq(ArrayRef<MPInt> ineq);
/// Compute the dot product of two vectors.
/// The vectors must have the same sizes.
-Fraction dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b);
+Fraction dotProduct(ArrayRef<Fraction> a, ArrayRef<Fraction> b);
} // namespace presburger
} // namespace mlir
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index c2adea94216b1..9a35a31eb5c46 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -460,8 +460,8 @@ FracMatrix FracMatrix::identity(unsigned dimension) {
FracMatrix::FracMatrix(IntMatrix m)
: FracMatrix(m.getNumRows(), m.getNumColumns()) {
- for (unsigned i = 0; i < m.getNumRows(); i++)
- for (unsigned j = 0; j < m.getNumColumns(); j++)
+ for (unsigned i = 0, r = m.getNumRows(); i < r; i++)
+ for (unsigned j = 0, c = m.getNumColumns(); j < c; j++)
this->at(i, j) = m.at(i, j);
}
@@ -552,20 +552,18 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
FracMatrix FracMatrix::gramSchmidt() const {
bool linIndep =
- (nRows < nColumns) || (nRows == nColumns && determinant(nullptr) != 0);
+ (nRows < nColumns) || (nRows == nColumns && determinant() != 0);
assert(linIndep && "the vectors must be linearly independent!");
// Create a copy of the argument to store
// the orthogonalised version.
FracMatrix orth(*this);
- // For each vector (row) in the matrix,
- // subtract its unit projection along
- // each of the previous vectors.
- // This ensures that it has no component
- // in the direction of any of the
- // previous vectors.
- for (unsigned i = 1; i < getNumRows(); i++) {
+ // For each vector (row) in the matrix, subtract its unit
+ // projection along each of the previous vectors.
+ // This ensures that it has no component in the direction
+ // of any of the previous vectors.
+ for (unsigned i = 1, e = getNumRows(); i < e; i++) {
for (unsigned j = 0; j < i; j++) {
Fraction projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
dotProduct(orth.getRow(j), orth.getRow(j));
diff --git a/mlir/lib/Analysis/Presburger/Utils.cpp b/mlir/lib/Analysis/Presburger/Utils.cpp
index a11bd9db00585..5f5c024f6a6e5 100644
--- a/mlir/lib/Analysis/Presburger/Utils.cpp
+++ b/mlir/lib/Analysis/Presburger/Utils.cpp
@@ -521,11 +521,11 @@ SmallVector<int64_t, 8> presburger::getInt64Vec(ArrayRef<MPInt> range) {
return result;
}
-Fraction presburger::dotProduct(MutableArrayRef<Fraction> a,
- MutableArrayRef<Fraction> b) {
- assert(a.size() == b.size() && "Dot product of two unequal vectors!");
+Fraction presburger::dotProduct(ArrayRef<Fraction> a,
+ ArrayRef<Fraction> b) {
+ assert(a.size() == b.size() && "dot product is only valid for vectors of equal sizes!");
Fraction sum = 0;
- for (unsigned i = 0; i < a.size(); i++)
+ for (unsigned i = 0, e = a.size(); i < e; i++)
sum += a[i] * b[i];
return sum;
}
\ No newline at end of file
>From 0c62c2608456746c3abcd1e74085cfec2d0a497e Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Mon, 6 Nov 2023 13:51:46 +0000
Subject: [PATCH 08/11] Formatting
---
.../Analysis/Presburger/MatrixTest.cpp | 35 ++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 1c4f91884b346..071a3a77f9dd6 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -335,5 +335,38 @@ TEST(MatrixTest, gramSchmidt) {
EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
for (unsigned i = 0; i < 3u; i++)
for (unsigned j = i + 1; j < 3u; j++)
- EXPECT_EQ(dotProduct(gramSchmidt.getRow(i), gramSchmidt.getRow(j)), 0);
+ EXPECT_EQ(dotProduct(gs.getRow(i), gs.getRow(j)), 0);
+
+ mat = makeFracMatrix(3, 3,
+ {{Fraction(20, 1), Fraction(17, 1), Fraction(10, 1)},
+ {Fraction(20, 1), Fraction(18, 1), Fraction(6, 1)},
+ {Fraction(15, 1), Fraction(14, 1), Fraction(10, 1)}});
+
+ gramSchmidt = makeFracMatrix(3, 3, {{20, 17, 10}, {Fraction(460, 789), Fraction(1180, 789), Fraction(-2926, 789)}, {Fraction(-2925, 3221), Fraction(3000, 3221), Fraction(750, 3221)}});
+
+ gs = mat.gramSchmidt();
+
+ EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
+ for (unsigned i = 0; i < 3u; i++)
+ for (unsigned j = i + 1; j < 3u; j++)
+ EXPECT_EQ(dotProduct(gs.getRow(i), gs.getRow(j)), 0);
+
+ mat = makeFracMatrix(4, 4,
+ {{Fraction(1, 26), Fraction(13, 12), Fraction(34, 13), Fraction(7, 10)},
+ {Fraction(40, 23), Fraction(34, 1), Fraction(11, 19), Fraction(15, 1)},
+ {Fraction(21, 22), Fraction(10, 9), Fraction(4, 11), Fraction(14, 11)},
+ {Fraction(35, 22), Fraction(1, 15) , Fraction(5, 8), Fraction(30, 1)}});
+
+ gs = mat.gramSchmidt();
+
+ // The integers involved are too big to construct the actual matrix.
+ for (unsigned i = 0; i < 4u; i++)
+ for (unsigned j = i+1; j < 4u; j++)
+ EXPECT_EQ(dotProduct(gs.getRow(i), gs.getRow(j)), 0);
+
+ mat = FracMatrix::identity(/*dim=*/10);
+
+ gs = mat.gramSchmidt();
+
+ EXPECT_EQ_FRAC_MATRIX(gs, FracMatrix::identity(10));
}
\ No newline at end of file
>From d47ea5c7490a8d3357ddeb64cec8a52299ca823c Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Mon, 6 Nov 2023 13:53:53 +0000
Subject: [PATCH 09/11] Formatting
---
mlir/lib/Analysis/Presburger/Utils.cpp | 6 ++--
.../Analysis/Presburger/MatrixTest.cpp | 31 +++++++++++--------
2 files changed, 21 insertions(+), 16 deletions(-)
diff --git a/mlir/lib/Analysis/Presburger/Utils.cpp b/mlir/lib/Analysis/Presburger/Utils.cpp
index 5f5c024f6a6e5..7d0fa43571e9b 100644
--- a/mlir/lib/Analysis/Presburger/Utils.cpp
+++ b/mlir/lib/Analysis/Presburger/Utils.cpp
@@ -521,9 +521,9 @@ SmallVector<int64_t, 8> presburger::getInt64Vec(ArrayRef<MPInt> range) {
return result;
}
-Fraction presburger::dotProduct(ArrayRef<Fraction> a,
- ArrayRef<Fraction> b) {
- assert(a.size() == b.size() && "dot product is only valid for vectors of equal sizes!");
+Fraction presburger::dotProduct(ArrayRef<Fraction> a, ArrayRef<Fraction> b) {
+ assert(a.size() == b.size() &&
+ "dot product is only valid for vectors of equal sizes!");
Fraction sum = 0;
for (unsigned i = 0, e = a.size(); i < e; i++)
sum += a[i] * b[i];
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 071a3a77f9dd6..8e2515f5f3a91 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -336,13 +336,17 @@ TEST(MatrixTest, gramSchmidt) {
for (unsigned i = 0; i < 3u; i++)
for (unsigned j = i + 1; j < 3u; j++)
EXPECT_EQ(dotProduct(gs.getRow(i), gs.getRow(j)), 0);
-
+
mat = makeFracMatrix(3, 3,
- {{Fraction(20, 1), Fraction(17, 1), Fraction(10, 1)},
- {Fraction(20, 1), Fraction(18, 1), Fraction(6, 1)},
- {Fraction(15, 1), Fraction(14, 1), Fraction(10, 1)}});
+ {{Fraction(20, 1), Fraction(17, 1), Fraction(10, 1)},
+ {Fraction(20, 1), Fraction(18, 1), Fraction(6, 1)},
+ {Fraction(15, 1), Fraction(14, 1), Fraction(10, 1)}});
- gramSchmidt = makeFracMatrix(3, 3, {{20, 17, 10}, {Fraction(460, 789), Fraction(1180, 789), Fraction(-2926, 789)}, {Fraction(-2925, 3221), Fraction(3000, 3221), Fraction(750, 3221)}});
+ gramSchmidt = makeFracMatrix(
+ 3, 3,
+ {{Fraction(20, 1), Fraction(17, 1), Fraction(10, 1)},
+ {Fraction(460, 789), Fraction(1180, 789), Fraction(-2926, 789)},
+ {Fraction(-2925, 3221), Fraction(3000, 3221), Fraction(750, 3221)}});
gs = mat.gramSchmidt();
@@ -351,19 +355,20 @@ TEST(MatrixTest, gramSchmidt) {
for (unsigned j = i + 1; j < 3u; j++)
EXPECT_EQ(dotProduct(gs.getRow(i), gs.getRow(j)), 0);
- mat = makeFracMatrix(4, 4,
- {{Fraction(1, 26), Fraction(13, 12), Fraction(34, 13), Fraction(7, 10)},
- {Fraction(40, 23), Fraction(34, 1), Fraction(11, 19), Fraction(15, 1)},
- {Fraction(21, 22), Fraction(10, 9), Fraction(4, 11), Fraction(14, 11)},
- {Fraction(35, 22), Fraction(1, 15) , Fraction(5, 8), Fraction(30, 1)}});
-
+ mat = makeFracMatrix(
+ 4, 4,
+ {{Fraction(1, 26), Fraction(13, 12), Fraction(34, 13), Fraction(7, 10)},
+ {Fraction(40, 23), Fraction(34, 1), Fraction(11, 19), Fraction(15, 1)},
+ {Fraction(21, 22), Fraction(10, 9), Fraction(4, 11), Fraction(14, 11)},
+ {Fraction(35, 22), Fraction(1, 15), Fraction(5, 8), Fraction(30, 1)}});
+
gs = mat.gramSchmidt();
// The integers involved are too big to construct the actual matrix.
for (unsigned i = 0; i < 4u; i++)
- for (unsigned j = i+1; j < 4u; j++)
+ for (unsigned j = i + 1; j < 4u; j++)
EXPECT_EQ(dotProduct(gs.getRow(i), gs.getRow(j)), 0);
-
+
mat = FracMatrix::identity(/*dim=*/10);
gs = mat.gramSchmidt();
>From 019c84efc8284bf1e8f4b7276f4a1bbd1089f242 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Mon, 6 Nov 2023 14:07:47 +0000
Subject: [PATCH 10/11] Formatting
---
mlir/unittests/Analysis/Presburger/MatrixTest.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 8e2515f5f3a91..8fa88cc504c6a 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -344,8 +344,8 @@ TEST(MatrixTest, gramSchmidt) {
gramSchmidt = makeFracMatrix(
3, 3,
- {{Fraction(20, 1), Fraction(17, 1), Fraction(10, 1)},
- {Fraction(460, 789), Fraction(1180, 789), Fraction(-2926, 789)},
+ {{Fraction(20, 1), Fraction(17, 1), Fraction(10, 1)},
+ {Fraction(460, 789), Fraction(1180, 789), Fraction(-2926, 789)},
{Fraction(-2925, 3221), Fraction(3000, 3221), Fraction(750, 3221)}});
gs = mat.gramSchmidt();
@@ -357,10 +357,10 @@ TEST(MatrixTest, gramSchmidt) {
mat = makeFracMatrix(
4, 4,
- {{Fraction(1, 26), Fraction(13, 12), Fraction(34, 13), Fraction(7, 10)},
- {Fraction(40, 23), Fraction(34, 1), Fraction(11, 19), Fraction(15, 1)},
- {Fraction(21, 22), Fraction(10, 9), Fraction(4, 11), Fraction(14, 11)},
- {Fraction(35, 22), Fraction(1, 15), Fraction(5, 8), Fraction(30, 1)}});
+ {{Fraction(1, 26), Fraction(13, 12), Fraction(34, 13), Fraction(7, 10)},
+ {Fraction(40, 23), Fraction(34, 1), Fraction(11, 19), Fraction(15, 1)},
+ {Fraction(21, 22), Fraction(10, 9), Fraction(4, 11), Fraction(14, 11)},
+ {Fraction(35, 22), Fraction(1, 15), Fraction(5, 8), Fraction(30, 1)}});
gs = mat.gramSchmidt();
>From 3df8609bc57c67668139128e22862b71b78f2dbc Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Mon, 4 Dec 2023 14:39:28 +0530
Subject: [PATCH 11/11] Check for linear independence
---
mlir/unittests/Analysis/Presburger/MatrixTest.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 8fa88cc504c6a..508d4fa369c14 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -365,6 +365,9 @@ TEST(MatrixTest, gramSchmidt) {
gs = mat.gramSchmidt();
// The integers involved are too big to construct the actual matrix.
+ // but we can check that the result is linearly independent.
+ ASSERT_FALSE(mat.determinant(nullptr) == 0);
+
for (unsigned i = 0; i < 4u; i++)
for (unsigned j = i + 1; j < 4u; j++)
EXPECT_EQ(dotProduct(gs.getRow(i), gs.getRow(j)), 0);
More information about the Mlir-commits
mailing list