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
70 std::fill(begin(matrix_
), end(matrix_
), testNumber_
- 1);
74 real testNumber_
= 42;
77 TEST_F(MatrixTest
, canSetFromArray
)
79 std::array
<real
, 3*3> arr
= {{1, 2, 3, 4, 5, 6, 7, 8, 9}};
80 Matrix3x3
newMatrix(arr
);
81 EXPECT_EQ(newMatrix(0, 0), 1);
82 EXPECT_EQ(newMatrix(0, 1), 2);
83 EXPECT_EQ(newMatrix(0, 2), 3);
84 EXPECT_EQ(newMatrix(1, 0), 4);
85 EXPECT_EQ(newMatrix(1, 1), 5);
86 EXPECT_EQ(newMatrix(1, 2), 6);
87 EXPECT_EQ(newMatrix(2, 0), 7);
88 EXPECT_EQ(newMatrix(2, 1), 8);
89 EXPECT_EQ(newMatrix(2, 2), 9);
92 TEST_F(MatrixTest
, canSetStaticallyFromList
)
94 Matrix3x3 newMatrix
= {{1, 2, 3, 4, 5, 6, 7, 8, 9}};
95 EXPECT_EQ(newMatrix(0, 0), 1);
96 EXPECT_EQ(newMatrix(0, 1), 2);
97 EXPECT_EQ(newMatrix(0, 2), 3);
98 EXPECT_EQ(newMatrix(1, 0), 4);
99 EXPECT_EQ(newMatrix(1, 1), 5);
100 EXPECT_EQ(newMatrix(1, 2), 6);
101 EXPECT_EQ(newMatrix(2, 0), 7);
102 EXPECT_EQ(newMatrix(2, 1), 8);
103 EXPECT_EQ(newMatrix(2, 2), 9);
106 TEST_F(MatrixTest
, canConstructAndFill
)
108 for (const auto &x
: matrix_
)
110 EXPECT_EQ(testNumber_
- 1, x
);
114 TEST_F(MatrixTest
, canSetValues
)
116 matrix_(1, 1) = testNumber_
;
117 EXPECT_EQ(testNumber_
, matrix_(1, 1) );
120 TEST_F(MatrixTest
, canCopyAssign
)
124 using ::testing::Pointwise
;
126 EXPECT_THAT(other
.toArrayRef(), Pointwise(Eq(), matrix_
.toArrayRef()));
129 TEST_F(MatrixTest
, canSwap
)
132 matrix_(0, 0) = testNumber_
;
134 EXPECT_EQ(testNumber_
, other(0, 0));
135 for (auto x
= begin(other
) + 1; x
!= end(other
); ++x
)
137 EXPECT_EQ(testNumber_
- 1, *x
);
141 TEST_F(MatrixTest
, staticMultiDimArrayExtent
)
143 EXPECT_EQ(matrix_
.extent(0), 3);
144 EXPECT_EQ(matrix_
.extent(1), 3);
147 TEST_F(MatrixTest
, determinantWorks
)
149 const Matrix3x3 mat
= {{1.0, 2.0, 3.0,
152 EXPECT_EQ(determinant(mat
), 1);
155 TEST_F(MatrixTest
, noninvertableDeterminantIsZero
)
157 const Matrix3x3 mat
= {{1, 0, 0, 0, 1, 0, 0, 0, 0}};
158 EXPECT_EQ(determinant(mat
), 0);
161 TEST_F(MatrixTest
, determinantOfDiagonalMatrix
)
163 const Matrix3x3 mat
= {{2, 0, 0, 0, 3, 0, 0, 0, 4}};
164 EXPECT_EQ(determinant(mat
), 24);
167 TEST_F(MatrixTest
, traceWorks
)
169 const Matrix3x3 mat
= {{1.5, 9, 9, 9, 2.0, 9, 9, 9, 0.25}};
170 EXPECT_EQ(trace(mat
), 3.75);
173 TEST_F(MatrixTest
, transposeWorks
)
175 const Matrix3x3 asymmetricMat
= {{1, 2, 3,
179 const Matrix3x3 transposedAsymmetricMat
= transpose(asymmetricMat
);
180 EXPECT_EQ(asymmetricMat(0, 0), transposedAsymmetricMat(0, 0));
181 EXPECT_EQ(asymmetricMat(0, 1), transposedAsymmetricMat(1, 0));
182 EXPECT_EQ(asymmetricMat(0, 2), transposedAsymmetricMat(2, 0));
183 EXPECT_EQ(asymmetricMat(1, 0), transposedAsymmetricMat(0, 1));
184 EXPECT_EQ(asymmetricMat(1, 1), transposedAsymmetricMat(1, 1));
185 EXPECT_EQ(asymmetricMat(1, 2), transposedAsymmetricMat(2, 1));
186 EXPECT_EQ(asymmetricMat(2, 0), transposedAsymmetricMat(0, 2));
187 EXPECT_EQ(asymmetricMat(2, 1), transposedAsymmetricMat(1, 2));
188 EXPECT_EQ(asymmetricMat(2, 2), transposedAsymmetricMat(2, 2));
192 TEST_F(MatrixTest
, transposeOfSymmetricMatrix
)
194 const Matrix3x3 symmetricMat
= {{ 1, 2, 3,
197 const Matrix3x3 transposedSymmetricMat
= transpose(symmetricMat
);
198 for (int i
= 0; i
< 3; i
++)
200 for (int j
= 0; j
< 3; j
++)
202 EXPECT_EQ(symmetricMat(i
, j
), transposedSymmetricMat(i
, j
));
207 TEST_F(MatrixTest
, canCreateFromLegacyMatrix
)
209 matrix legacyMatrix
= { { 1, 2, 3}, { 4, 5, 6}, {7, 8, 9}};
210 const Matrix3x3 fromLegacy
= createMatrix3x3FromLegacyMatrix(legacyMatrix
);
211 for (int i
= 0; i
< 3; i
++)
213 for (int j
= 0; j
< 3; j
++)
215 EXPECT_EQ(fromLegacy(i
, j
), legacyMatrix
[i
][j
]);