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

Arjun P via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 13 03:36:34 PDT 2023


================
@@ -283,12 +326,43 @@ template <typename T> bool Matrix<T>::hasConsistentState() const {
   return true;
 }
 
+template <typename T>
+T Matrix<T>::determinant() {
+  unsigned r = getNumRows();
+  unsigned c = getNumColumns();
+  if (r == 1)
+    return at(0, 0);
+  if (r == 2)
+    return (at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0));
+
+  T sign(-1), determinant(0);
+  Matrix<T> cofactor(r - 1, c - 1);
+
+  // Cofactor matrix consists of all columns other than the
+  // current one, and all rows except the first.
+  for (unsigned i = 0; i < c; i++) {
+    sign = -sign;
+    unsigned n = 0;
+    for (unsigned j = 0; j < c; j++) {
+      if (j == i)
+        continue;
+      for (unsigned k = 0; k < r - 1; k++)
+        cofactor(k, n) = at(k + 1, j);
+      n++;
+    }
+
+    determinant = determinant + at(0, i) * sign * cofactor.determinant();
----------------
Superty wrote:

please don't use this exponential recursive implementation. it's going to be pretty slow and unbounded recursion is does not conform to LLVM standards.  do you need it for both integers and fractions? try to use the gaussian elimination for this.

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


More information about the cfe-commits mailing list