Commits

Anonymous committed c279d45

Fixed determinant, moved exclude to a global function

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);
-            assert(column < N);
-            matrix<T, M - 1, N - 1> rv;
-            for (size_t i = 0; i < M; i++) {
-                if (i == row)
-                    continue;
-                for (size_t j = 0; j < N; j++)
-                    if (j != column)
-                        rv[j - (j > column)][i - (i > row)] = m_cols[j][i];
-            }
-            return rv;
-        }
-
     private:
         neglect::vector<T, M> m_cols[N];
     };
         return rv;
     }
 
+    /* create a smaller matrix by excluding a row and column */
+
+    template <typename T, size_t N>
+    matrix<T, N - 1, N - 1> exclude(const matrix<T, N, N> &mat,
+                                    size_t row, size_t column)
+    {
+        assert(row < N && column < N);
+        matrix<T, N - 1, N - 1> rv;
+        for (size_t i = 0; i < N; i++) {
+            if (i == row)
+                continue;
+            for (size_t j = 0; j < N; j++)
+                if (j != column)
+                    rv[j - (j > column)][i - (i > row)] = mat[j][i];
+        }
+        return rv;
+    }
+
+    template <typename T>
+    T exclude(const matrix<T, 2, 2> &mat, size_t row, size_t column)
+    {
+        assert(row < 2 && column < 2);
+        return mat[1 - column][1 - row];
+    }
+
     /* determinant of a square matrix */
 
     template <typename T, size_t N>
     {
         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));
+            sum += mat[0][i] * math::pow(T(-1), static_cast<T>(i))
+                * determinant(exclude(mat, 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);
+    }
+
+    template <typename T>
     T determinant(const matrix<T, 3, 3> &mat)
     {
         return (
         );
     }
 
-    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 sy, T sz)

neglect/tests/src/test_matrix.cpp

         expected[1] = vec3( 5,  7,  8);
         expected[2] = vec3(13, 15, 16);
 
-        mat3 m1x = m1.exclude(1, 2);
+        mat3 m1x = exclude(m1, 1, 2);
         NUT_CHECK_EQUAL(m1x, expected);
         m1x(2, 2) = 100.0f;
         NUT_CHECK_NOT_EQUAL(m1x, expected);
+
+        mat2 m2;
+        m2[0] = vec2(1, 2);
+        m2[1] = vec2(3, 4);
+        NUT_CHECK_EQUAL(exclude(m2, 1, 1), 1);
+        NUT_CHECK_EQUAL(exclude(m2, 0, 1), 2);
     }
 
     NUT_TESTCASE(matrix_determinant)