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 "testutils/testasserts.h"
63 class MatrixTest
: public ::testing::Test
68 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
)
122 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
, determinantWorksForInt
)
147 const BasicMatrix3x3
<int> mat
= {{6, 4, 2, 1, -2, 8, 1, 5, 7}};
148 EXPECT_EQ(determinant(mat
), -306);
151 TEST_F(MatrixTest
, determinantWorksForFloat
)
153 const BasicMatrix3x3
<float> mat
= {{1.0, 2.0, 3.0,
156 EXPECT_EQ(determinant(mat
), 1);
159 TEST_F(MatrixTest
, noninvertableDeterminantIsZero
)
161 const BasicMatrix3x3
<int> mat
= {{1, 0, 0, 0, 1, 0, 0, 0, 0}};
162 EXPECT_EQ(determinant(mat
), 0);
165 TEST_F(MatrixTest
, determinantOfDiagonalMatrix
)
167 const BasicMatrix3x3
<int> mat
= {{2, 0, 0, 0, 3, 0, 0, 0, 4}};
168 EXPECT_EQ(determinant(mat
), 24);
171 TEST_F(MatrixTest
, traceWorks
)
173 const Matrix3x3 mat
= {{1.5, 9, 9, 9, 2.0, 9, 9, 9, 0.25}};
174 EXPECT_EQ(trace(mat
), 3.75);