Add transpose operation to Matrix3x3
[gromacs.git] / src / gromacs / math / tests / matrix.cpp
blob92fb5c00a2ed2c61c08cbe6767d9aac71144c01a
1 /*
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.
35 /*! \internal \file
36 * \brief
37 * Tests matrices
39 * \author Christian Blau <cblau@gwdg.de>
40 * \ingroup module_math
42 #include "gmxpre.h"
44 #include "gromacs/math/matrix.h"
46 #include <string>
47 #include <vector>
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
52 #include "testutils/testasserts.h"
54 namespace gmx
57 namespace test
60 namespace
63 class MatrixTest : public ::testing::Test
65 public:
66 MatrixTest()
68 std::fill(begin(matrix_), end(matrix_), testNumber_ - 1);
70 protected:
71 Matrix3x3 matrix_;
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)
120 Matrix3x3 other;
121 other = matrix_;
122 using ::testing::Pointwise;
123 using ::testing::Eq;
124 EXPECT_THAT(other.toArrayRef(), Pointwise(Eq(), matrix_.toArrayRef()));
127 TEST_F(MatrixTest, canSwap)
129 Matrix3x3 other;
130 matrix_(0, 0) = testNumber_;
131 other.swap(matrix_);
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,
148 0.0, 1.0, 4.0,
149 5.0, 6.0, 0.0}};
150 EXPECT_EQ(determinant(mat), 1);
153 TEST_F(MatrixTest, noninvertableDeterminantIsZero)
155 const Matrix3x3 mat = {{1, 0, 0, 0, 1, 0, 0, 0, 0}};
156 EXPECT_EQ(determinant(mat), 0);
159 TEST_F(MatrixTest, determinantOfDiagonalMatrix)
161 const Matrix3x3 mat = {{2, 0, 0, 0, 3, 0, 0, 0, 4}};
162 EXPECT_EQ(determinant(mat), 24);
165 TEST_F(MatrixTest, traceWorks)
167 const Matrix3x3 mat = {{1.5, 9, 9, 9, 2.0, 9, 9, 9, 0.25}};
168 EXPECT_EQ(trace(mat), 3.75);
171 TEST_F(MatrixTest, transposeWorks)
173 const Matrix3x3 asymmetricMat = {{1, 2, 3,
174 4, 5, 6,
175 7, 8, 9}};
177 const Matrix3x3 transposedAsymmetricMat = transpose(asymmetricMat);
178 EXPECT_EQ(asymmetricMat(0, 0), transposedAsymmetricMat(0, 0));
179 EXPECT_EQ(asymmetricMat(0, 1), transposedAsymmetricMat(1, 0));
180 EXPECT_EQ(asymmetricMat(0, 2), transposedAsymmetricMat(2, 0));
181 EXPECT_EQ(asymmetricMat(1, 0), transposedAsymmetricMat(0, 1));
182 EXPECT_EQ(asymmetricMat(1, 1), transposedAsymmetricMat(1, 1));
183 EXPECT_EQ(asymmetricMat(1, 2), transposedAsymmetricMat(2, 1));
184 EXPECT_EQ(asymmetricMat(2, 0), transposedAsymmetricMat(0, 2));
185 EXPECT_EQ(asymmetricMat(2, 1), transposedAsymmetricMat(1, 2));
186 EXPECT_EQ(asymmetricMat(2, 2), transposedAsymmetricMat(2, 2));
190 TEST_F(MatrixTest, transposeOfSymmetricMatrix)
192 const Matrix3x3 symmetricMat = {{ 1, 2, 3,
193 2, 5, 6,
194 3, 6, 9}};
195 const Matrix3x3 transposedSymmetricMat = transpose(symmetricMat);
196 for (int i = 0; i < 3; i++)
198 for (int j = 0; j < 3; j++)
200 EXPECT_EQ(symmetricMat(i, j), transposedSymmetricMat(i, j));
205 } // namespace
207 } // namespace test
209 } // namespace gmx