[clang-tools-extra] [MLIR][Presburger] Implement matrix inverse (PR #67382)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 17 09:27:59 PDT 2023


================
@@ -432,4 +432,138 @@ MPInt IntMatrix::normalizeRow(unsigned row, unsigned cols) {
 
 MPInt IntMatrix::normalizeRow(unsigned row) {
   return normalizeRow(row, getNumColumns());
+}
+
+MPInt IntMatrix::determinant() {
+  unsigned r = getNumRows();
+  unsigned c = getNumColumns();
+  assert(r == c);
+
+  FracMatrix m(r, c);
+  for (unsigned i = 0; i < r; i++)
+    for (unsigned j = 0; j < c; j++)
+      m.at(i, j) = Fraction(at(i, j), 1);
+  
+  Fraction det = m.determinant();
+
+  return det.getAsInteger();
+}
+
+std::optional<IntMatrix> IntMatrix::integerInverse() {
+  Fraction det = Fraction(determinant(), 1);
+  FracMatrix newMat(getNumRows(), getNumColumns());
+  for (unsigned i = 0; i < getNumRows(); i++)
+    for (unsigned j = 0; j < getNumColumns(); j++)
+      newMat(i, j) = Fraction(at(i, j), 1);
----------------
Abhinav271828 wrote:

it doesn't work, we don't have a cast from IntMatrix to FracMatrix. shall I write one?

https://github.com/llvm/llvm-project/pull/67382


More information about the cfe-commits mailing list