Add trace functionality to 3x3 matrices
[alexxy/gromacs.git] / src / gromacs / math / matrix.h
index a725e69ef63e0aaefe370c4964896088e9e47fed..ed225ad5854c9a65f7825ccf9c131d3c874853fd 100644 (file)
@@ -61,7 +61,11 @@ using BasicMatrix3x3 = MultiDimArray<std::array<ElementType, 3*3>, extents <3, 3
 /*! \brief Three-by-three real number matrix.
  * \note will replace the C-style real[3][3] "matrix"
  */
-using Matrix3x3 = BasicMatrix3x3<real>;
+using Matrix3x3          = BasicMatrix3x3<real>;
+//! Convenience alias for a matrix view
+using Matrix3x3Span      = Matrix3x3::view_type;
+//! Convenience alias for a const matrix view
+using Matrix3x3ConstSpan = Matrix3x3::const_view_type;
 
 //! Determinant of a 3x3 matrix
 template <class ElementType>
@@ -72,6 +76,11 @@ ElementType determinant(const BasicMatrix3x3<ElementType> matrix)
               +matrix(2, 0)*(matrix(0, 1)*matrix(1, 2)-matrix(1, 1)*matrix(0, 2)));
 }
 
+//! Calculates the trace of a 3x3 matrix view
+constexpr real trace(Matrix3x3ConstSpan matrixView)
+{
+    return matrixView(0, 0) + matrixView(1, 1) + matrixView(2, 2);
+}
 } // namespace gmx
 
 #endif