Nektar++
TestNekMatrixMultiplication.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestNekMatrixMultiplication.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Tests NekMatrix functionality.
32//
33///////////////////////////////////////////////////////////////////////////////
34
36#include <boost/bind.hpp>
37#include <boost/test/tools/floating_point_comparison.hpp>
38#include <boost/test/unit_test.hpp>
39#include <functional>
40#include <iostream>
41
43{
44BOOST_AUTO_TEST_CASE(TestStandardFullTimesStandardFull)
45{
46
47 {
48 // double buf1[] = {1, 2, 3,
49 // 4, 5, 6,
50 // 7, 8, 9};
51 // double buf2[] = { 10, 11, 12,
52 // 15, 16, 17,
53 // 19, 20, 21 };
54 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
55 double buf2[] = {10, 15, 19, 11, 16, 20, 12, 17, 21};
56
57 NekMatrix<double> lhs(3, 3, buf1);
58 NekMatrix<double> rhs(3, 3, buf2);
59
60 NekMatrix<double> result = lhs * rhs;
61
62 BOOST_CHECK(result.GetRows() == 3);
63 BOOST_CHECK(result.GetColumns() == 3);
64
65 double epsilon = 1e-12;
66 BOOST_CHECK_CLOSE(*result(0, 0), 97.0, epsilon);
67 BOOST_CHECK_CLOSE(*result(0, 1), 103.0, epsilon);
68 BOOST_CHECK_CLOSE(*result(0, 2), 109.0, epsilon);
69
70 BOOST_CHECK_CLOSE(*result(1, 0), 229.0, epsilon);
71 BOOST_CHECK_CLOSE(*result(1, 1), 244.0, epsilon);
72 BOOST_CHECK_CLOSE(*result(1, 2), 259.0, epsilon);
73
74 BOOST_CHECK_CLOSE(*result(2, 0), 361.0, epsilon);
75 BOOST_CHECK_CLOSE(*result(2, 1), 385.0, epsilon);
76 BOOST_CHECK_CLOSE(*result(2, 2), 409.0, epsilon);
77 }
78}
79
80BOOST_AUTO_TEST_CASE(TestStandardFullTimesVector)
81{
82
83 {
84 // double buf1[] = {1, 2, 3,
85 // 4, 5, 6,
86 // 7, 8, 9};
87 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
88 double buf2[] = {10, 11, 12};
89
90 NekMatrix<double> lhs(3, 3, buf1);
91 NekVector<double> rhs(3, buf2);
92 NekVector<double> result = lhs * rhs;
93
94 BOOST_CHECK(result.GetRows() == 3);
95
96 double epsilon = 1e-12;
97 BOOST_CHECK_CLOSE(result[0], 68.0, epsilon);
98 BOOST_CHECK_CLOSE(result[1], 167.0, epsilon);
99 BOOST_CHECK_CLOSE(result[2], 266.0, epsilon);
100 }
101
102 {
103 // double buf1[] = {1, 2, 3,
104 // 4, 5, 6,
105 // 7, 8, 9,
106 // 10, 11, 12};
107 double buf1[] = {1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12};
108 double buf2[] = {10, 11, 12};
109
110 NekMatrix<double> lhs(4, 3, buf1);
111 NekVector<double> rhs(3, buf2);
112 NekVector<double> result = lhs * rhs;
113
114 BOOST_CHECK(result.GetRows() == 4);
115
116 double epsilon = 1e-12;
117 BOOST_CHECK_CLOSE(result[0], 68.0, epsilon);
118 BOOST_CHECK_CLOSE(result[1], 167.0, epsilon);
119 BOOST_CHECK_CLOSE(result[2], 266.0, epsilon);
120 BOOST_CHECK_CLOSE(result[3], 365.0, epsilon);
121 }
122}
123
124BOOST_AUTO_TEST_CASE(TestScaledFullTimesScaledFull)
125{
126
127 {
128 // double buf1[] = {1, 2, 3,
129 // 4, 5, 6,
130 // 7, 8, 9};
131 // double buf2[] = { 10, 11, 12,
132 // 15, 16, 17,
133 // 19, 20, 21 };
134 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
135 double buf2[] = {10, 15, 19, 11, 16, 20, 12, 17, 21};
136
137 std::shared_ptr<NekMatrix<double>> lhsInnerMatrix(
138 new NekMatrix<double>(3, 3, buf1));
139 std::shared_ptr<NekMatrix<double>> rhsInnerMatrix(
140 new NekMatrix<double>(3, 3, buf2));
141
142 NekMatrix<NekMatrix<double>, ScaledMatrixTag> lhs(2.0, lhsInnerMatrix);
143 NekMatrix<NekMatrix<double>, ScaledMatrixTag> rhs(3.0, rhsInnerMatrix);
144
145 NekMatrix<double> result = lhs * rhs;
146
147 BOOST_CHECK(result.GetRows() == 3);
148 BOOST_CHECK(result.GetColumns() == 3);
149
150 double epsilon = 1e-12;
151 BOOST_CHECK_CLOSE(*result(0, 0), 582.0, epsilon);
152 BOOST_CHECK_CLOSE(*result(0, 1), 618.0, epsilon);
153 BOOST_CHECK_CLOSE(*result(0, 2), 654.0, epsilon);
154
155 BOOST_CHECK_CLOSE(*result(1, 0), 1374.0, epsilon);
156 BOOST_CHECK_CLOSE(*result(1, 1), 1464.0, epsilon);
157 BOOST_CHECK_CLOSE(*result(1, 2), 1554.0, epsilon);
158
159 BOOST_CHECK_CLOSE(*result(2, 0), 2166.0, epsilon);
160 BOOST_CHECK_CLOSE(*result(2, 1), 2310.0, epsilon);
161 BOOST_CHECK_CLOSE(*result(2, 2), 2454.0, epsilon);
162 }
163}
164
165BOOST_AUTO_TEST_CASE(TestScaledFullTimesVector)
166{
167
168 {
169 // double buf1[] = {1, 2, 3,
170 // 4, 5, 6,
171 // 7, 8, 9};
172 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
173 double buf2[] = {10, 11, 12};
174
175 std::shared_ptr<NekMatrix<double>> innerMatrix(
176 new NekMatrix<double>(3, 3, buf1));
177 NekMatrix<NekMatrix<double>, ScaledMatrixTag> lhs(2.0, innerMatrix);
178 NekVector<double> rhs(3, buf2);
179 NekVector<double> result = lhs * rhs;
180
181 BOOST_CHECK(result.GetRows() == 3);
182
183 double epsilon = 1e-12;
184 BOOST_CHECK_CLOSE(result[0], 136.0, epsilon);
185 BOOST_CHECK_CLOSE(result[1], 334.0, epsilon);
186 BOOST_CHECK_CLOSE(result[2], 532.0, epsilon);
187 }
188
189 {
190 // double buf1[] = {1, 2, 3,
191 // 4, 5, 6,
192 // 7, 8, 9,
193 // 10, 11, 12};
194 double buf1[] = {1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12};
195 double buf2[] = {10, 11, 12};
196
197 std::shared_ptr<NekMatrix<double>> innerMatrix(
198 new NekMatrix<double>(4, 3, buf1));
199 NekMatrix<NekMatrix<double>, ScaledMatrixTag> lhs(3.0, innerMatrix);
200 NekVector<double> rhs(3, buf2);
201 NekVector<double> result = lhs * rhs;
202
203 BOOST_CHECK(result.GetRows() == 4);
204
205 double epsilon = 1e-12;
206 BOOST_CHECK_CLOSE(result[0], 204.0, epsilon);
207 BOOST_CHECK_CLOSE(result[1], 501.0, epsilon);
208 BOOST_CHECK_CLOSE(result[2], 798.0, epsilon);
209 BOOST_CHECK_CLOSE(result[3], 1095.0, epsilon);
210 }
211}
212} // namespace Nektar::MatrixMultiplicationTests
unsigned int GetRows() const
Definition: NekVector.cpp:206
BOOST_AUTO_TEST_CASE(TestStandardFullTimesStandardFull)