Anonymous avatar Anonymous committed e570a1a

Started working on inverse support, unfortunately broken.

Comments (0)

Files changed (2)

neglect/cpplib/include/neglect/matrix.hpp

             return m_cols[row];
         }
 
+        /* create a new matrix by exluding a row and column */
         matrix<T, M - 1, N - 1> exclude(size_t row, size_t column) const
         {
             assert(row < M);
             return rv;
         }
 
-        T minor(size_t row, size_t column) const
-        {
-            return det(exclude(row, column));
-        }
-
-        T cofactor(size_t row, size_t column) const
-        {
-            return math::pow(T(-1), (row + column)) * minor(row, column);
-        }
-
-        T determinant() const
-        {
-            T sum = T();
-            for (size_t i = 0; i < N; i++)
-                sum += m_cols[i][0] * cofactor(i, 0);
-            return sum;
-        }
-
     private:
         neglect::vector<T, M> m_cols[N];
     };
         return rv;
     }
 
+    /* determinant of a square matrix */
+
+    template <typename T, size_t N>
+    T determinant(const matrix<T, N, N> &mat)
+    {
+        T sum = T();
+        for (size_t i = 0; i < N; i++)
+            sum += mat[i][0] * math::pow(T(-1), static_cast<T>(i))
+                * determinant(mat.exclude(i, 0));
+        return sum;
+    }
+
+    template <typename T>
+    T determinant(const matrix<T, 2, 2> &mat)
+    {
+        return mat(0, 0) * mat(1, 1) - mat(0, 1) * mat(1, 0);
+    }
+
     /* creates a scale matrix */
     template <typename T>
     matrix<T, 4, 4> scale_matrix(T sx, T zy, T sz)

neglect/tests/src/test_matrix.cpp

         m1x(2, 2) = 100.0f;
         NUT_CHECK_NOT_EQUAL(m1x, expected);
     }
+
+    NUT_TESTCASE(matrix_determinant)
+    {
+        mat4 m1;
+        m1[0] = vec4( 1,  2,  3,  4);
+        m1[1] = vec4( 5,  6,  7,  8);
+        m1[2] = vec4( 9, 10, 11, 12);
+        m1[3] = vec4(13, 14, 15, 16);
+        NUT_CHECK_EQUAL(determinant(m1), 0);
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.