2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
39 * \author Christian Blau <cblau@gwdg.de>
40 * \ingroup module_math
44 #include "gromacs/math/matrix.h"
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
52 #include "gromacs/math/vec.h"
54 #include "testutils/testasserts.h"
65 class MatrixTest : public ::testing::Test
68 MatrixTest() { std::fill(begin(matrix_), end(matrix_), testNumber_ - 1); }
72 real testNumber_ = 42;
75 TEST_F(MatrixTest, canSetFromArray)
77 std::array<real, 3 * 3> arr = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
78 Matrix3x3 newMatrix(arr);
79 EXPECT_EQ(newMatrix(0, 0), 1);
80 EXPECT_EQ(newMatrix(0, 1), 2);
81 EXPECT_EQ(newMatrix(0, 2), 3);
82 EXPECT_EQ(newMatrix(1, 0), 4);
83 EXPECT_EQ(newMatrix(1, 1), 5);
84 EXPECT_EQ(newMatrix(1, 2), 6);
85 EXPECT_EQ(newMatrix(2, 0), 7);
86 EXPECT_EQ(newMatrix(2, 1), 8);
87 EXPECT_EQ(newMatrix(2, 2), 9);
90 TEST_F(MatrixTest, canSetStaticallyFromList)
92 Matrix3x3 newMatrix = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
93 EXPECT_EQ(newMatrix(0, 0), 1);
94 EXPECT_EQ(newMatrix(0, 1), 2);
95 EXPECT_EQ(newMatrix(0, 2), 3);
96 EXPECT_EQ(newMatrix(1, 0), 4);
97 EXPECT_EQ(newMatrix(1, 1), 5);
98 EXPECT_EQ(newMatrix(1, 2), 6);
99 EXPECT_EQ(newMatrix(2, 0), 7);
100 EXPECT_EQ(newMatrix(2, 1), 8);
101 EXPECT_EQ(newMatrix(2, 2), 9);
104 TEST_F(MatrixTest, canConstructAndFill)
106 for (const auto& x : matrix_)
108 EXPECT_EQ(testNumber_ - 1, x);
112 TEST_F(MatrixTest, canSetValues)
114 matrix_(1, 1) = testNumber_;
115 EXPECT_EQ(testNumber_, matrix_(1, 1));
118 TEST_F(MatrixTest, canCopyAssign)
123 using ::testing::Pointwise;
124 EXPECT_THAT(other.toArrayRef(), Pointwise(Eq(), matrix_.toArrayRef()));
127 TEST_F(MatrixTest, canSwap)
130 matrix_(0, 0) = testNumber_;
132 EXPECT_EQ(testNumber_, other(0, 0));
133 for (auto x = begin(other) + 1; x != end(other); ++x)
135 EXPECT_EQ(testNumber_ - 1, *x);
139 TEST_F(MatrixTest, staticMultiDimArrayExtent)
141 EXPECT_EQ(matrix_.extent(0), 3);
142 EXPECT_EQ(matrix_.extent(1), 3);
145 TEST_F(MatrixTest, determinantWorks)
147 const Matrix3x3 mat = { { 1.0, 2.0, 3.0, 0.0, 1.0, 4.0, 5.0, 6.0, 0.0 } };
148 EXPECT_EQ(determinant(mat), 1);
151 TEST_F(MatrixTest, noninvertableDeterminantIsZero)
153 const Matrix3x3 mat = { { 1, 0, 0, 0, 1, 0, 0, 0, 0 } };
154 EXPECT_EQ(determinant(mat), 0);
157 TEST_F(MatrixTest, determinantOfDiagonalMatrix)
159 const Matrix3x3 mat = { { 2, 0, 0, 0, 3, 0, 0, 0, 4 } };
160 EXPECT_EQ(determinant(mat), 24);
163 TEST_F(MatrixTest, traceWorks)
165 const Matrix3x3 mat = { { 1.5, 9, 9, 9, 2.0, 9, 9, 9, 0.25 } };
166 EXPECT_EQ(trace(mat), 3.75);
169 TEST_F(MatrixTest, transposeWorks)
171 const Matrix3x3 asymmetricMat = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
173 const Matrix3x3 transposedAsymmetricMat = transpose(asymmetricMat);
174 EXPECT_EQ(asymmetricMat(0, 0), transposedAsymmetricMat(0, 0));
175 EXPECT_EQ(asymmetricMat(0, 1), transposedAsymmetricMat(1, 0));
176 EXPECT_EQ(asymmetricMat(0, 2), transposedAsymmetricMat(2, 0));
177 EXPECT_EQ(asymmetricMat(1, 0), transposedAsymmetricMat(0, 1));
178 EXPECT_EQ(asymmetricMat(1, 1), transposedAsymmetricMat(1, 1));
179 EXPECT_EQ(asymmetricMat(1, 2), transposedAsymmetricMat(2, 1));
180 EXPECT_EQ(asymmetricMat(2, 0), transposedAsymmetricMat(0, 2));
181 EXPECT_EQ(asymmetricMat(2, 1), transposedAsymmetricMat(1, 2));
182 EXPECT_EQ(asymmetricMat(2, 2), transposedAsymmetricMat(2, 2));
185 TEST_F(MatrixTest, transposeOfSymmetricMatrix)
187 const Matrix3x3 symmetricMat = { { 1, 2, 3, 2, 5, 6, 3, 6, 9 } };
188 const Matrix3x3 transposedSymmetricMat = transpose(symmetricMat);
189 for (int i = 0; i < 3; i++)
191 for (int j = 0; j < 3; j++)
193 EXPECT_EQ(symmetricMat(i, j), transposedSymmetricMat(i, j));
198 TEST_F(MatrixTest, canCreateFromLegacyMatrix)
200 matrix legacyMatrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
201 const Matrix3x3 fromLegacy = createMatrix3x3FromLegacyMatrix(legacyMatrix);
202 for (int i = 0; i < 3; i++)
204 for (int j = 0; j < 3; j++)
206 EXPECT_EQ(fromLegacy(i, j), legacyMatrix[i][j]);
211 TEST_F(MatrixTest, canFillLegacyMatrix)
213 matrix legacyMatrix = { { -2 } };
214 fillLegacyMatrix(matrix_, legacyMatrix);
215 for (int i = 0; i < 3; i++)
217 for (int j = 0; j < 3; j++)
219 EXPECT_EQ(legacyMatrix[i][j], matrix_(i, j));