Add function for calculating 3x3 matrix determinant
[alexxy/gromacs.git] / src / gromacs / math / matrix.h
index 9d3a9dfcf640bb8194292a801348a7798d36297f..a725e69ef63e0aaefe370c4964896088e9e47fed 100644 (file)
@@ -34,7 +34,7 @@
  */
 /*! \libinternal
  * \file
- * \brief Declares special case of 3x3 matrix frequently used.
+ * \brief Declares special case of 3x3 matrix frequently used, and associated functions.
  *
  * \author Christian Blau <cblau@gwdg.de>
  * \ingroup module_math
@@ -63,6 +63,15 @@ using BasicMatrix3x3 = MultiDimArray<std::array<ElementType, 3*3>, extents <3, 3
  */
 using Matrix3x3 = BasicMatrix3x3<real>;
 
+//! Determinant of a 3x3 matrix
+template <class ElementType>
+ElementType determinant(const BasicMatrix3x3<ElementType> matrix)
+{
+    return (  matrix(0, 0)*(matrix(1, 1)*matrix(2, 2)-matrix(2, 1)*matrix(1, 2))
+              -matrix(1, 0)*(matrix(0, 1)*matrix(2, 2)-matrix(2, 1)*matrix(0, 2))
+              +matrix(2, 0)*(matrix(0, 1)*matrix(1, 2)-matrix(1, 1)*matrix(0, 2)));
+}
+
 } // namespace gmx
 
 #endif