Nektar++
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
42namespace Nektar
43{
44
45// Note - All tests should excercise both the blas and normal code.
46// The easiest way to do this is to perform one test with integers and
47// one with doubles.
48namespace MatrixMultiplicationTests
49{
50BOOST_AUTO_TEST_CASE(TestStandardFullTimesStandardFull)
51{
52
53 {
54 // double buf1[] = {1, 2, 3,
55 // 4, 5, 6,
56 // 7, 8, 9};
57 // double buf2[] = { 10, 11, 12,
58 // 15, 16, 17,
59 // 19, 20, 21 };
60 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
61 double buf2[] = {10, 15, 19, 11, 16, 20, 12, 17, 21};
62
63 NekMatrix<double> lhs(3, 3, buf1);
64 NekMatrix<double> rhs(3, 3, buf2);
65
66 NekMatrix<double> result = lhs * rhs;
67
68 BOOST_CHECK(result.GetRows() == 3);
69 BOOST_CHECK(result.GetColumns() == 3);
70
71 double epsilon = 1e-12;
72 BOOST_CHECK_CLOSE(*result(0, 0), 97.0, epsilon);
73 BOOST_CHECK_CLOSE(*result(0, 1), 103.0, epsilon);
74 BOOST_CHECK_CLOSE(*result(0, 2), 109.0, epsilon);
75
76 BOOST_CHECK_CLOSE(*result(1, 0), 229.0, epsilon);
77 BOOST_CHECK_CLOSE(*result(1, 1), 244.0, epsilon);
78 BOOST_CHECK_CLOSE(*result(1, 2), 259.0, epsilon);
79
80 BOOST_CHECK_CLOSE(*result(2, 0), 361.0, epsilon);
81 BOOST_CHECK_CLOSE(*result(2, 1), 385.0, epsilon);
82 BOOST_CHECK_CLOSE(*result(2, 2), 409.0, epsilon);
83 }
84}
85
86BOOST_AUTO_TEST_CASE(TestStandardFullTimesVector)
87{
88
89 {
90 // double buf1[] = {1, 2, 3,
91 // 4, 5, 6,
92 // 7, 8, 9};
93 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
94 double buf2[] = {10, 11, 12};
95
96 NekMatrix<double> lhs(3, 3, buf1);
97 NekVector<double> rhs(3, buf2);
98 NekVector<double> result = lhs * rhs;
99
100 BOOST_CHECK(result.GetRows() == 3);
101
102 double epsilon = 1e-12;
103 BOOST_CHECK_CLOSE(result[0], 68.0, epsilon);
104 BOOST_CHECK_CLOSE(result[1], 167.0, epsilon);
105 BOOST_CHECK_CLOSE(result[2], 266.0, epsilon);
106 }
107
108 {
109 // double buf1[] = {1, 2, 3,
110 // 4, 5, 6,
111 // 7, 8, 9,
112 // 10, 11, 12};
113 double buf1[] = {1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12};
114 double buf2[] = {10, 11, 12};
115
116 NekMatrix<double> lhs(4, 3, buf1);
117 NekVector<double> rhs(3, buf2);
118 NekVector<double> result = lhs * rhs;
119
120 BOOST_CHECK(result.GetRows() == 4);
121
122 double epsilon = 1e-12;
123 BOOST_CHECK_CLOSE(result[0], 68.0, epsilon);
124 BOOST_CHECK_CLOSE(result[1], 167.0, epsilon);
125 BOOST_CHECK_CLOSE(result[2], 266.0, epsilon);
126 BOOST_CHECK_CLOSE(result[3], 365.0, epsilon);
127 }
128}
129
130BOOST_AUTO_TEST_CASE(TestScaledFullTimesScaledFull)
131{
132
133 {
134 // double buf1[] = {1, 2, 3,
135 // 4, 5, 6,
136 // 7, 8, 9};
137 // double buf2[] = { 10, 11, 12,
138 // 15, 16, 17,
139 // 19, 20, 21 };
140 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
141 double buf2[] = {10, 15, 19, 11, 16, 20, 12, 17, 21};
142
143 std::shared_ptr<NekMatrix<double>> lhsInnerMatrix(
144 new NekMatrix<double>(3, 3, buf1));
145 std::shared_ptr<NekMatrix<double>> rhsInnerMatrix(
146 new NekMatrix<double>(3, 3, buf2));
147
148 NekMatrix<NekMatrix<double>, ScaledMatrixTag> lhs(2.0, lhsInnerMatrix);
149 NekMatrix<NekMatrix<double>, ScaledMatrixTag> rhs(3.0, rhsInnerMatrix);
150
151 NekMatrix<double> result = lhs * rhs;
152
153 BOOST_CHECK(result.GetRows() == 3);
154 BOOST_CHECK(result.GetColumns() == 3);
155
156 double epsilon = 1e-12;
157 BOOST_CHECK_CLOSE(*result(0, 0), 582.0, epsilon);
158 BOOST_CHECK_CLOSE(*result(0, 1), 618.0, epsilon);
159 BOOST_CHECK_CLOSE(*result(0, 2), 654.0, epsilon);
160
161 BOOST_CHECK_CLOSE(*result(1, 0), 1374.0, epsilon);
162 BOOST_CHECK_CLOSE(*result(1, 1), 1464.0, epsilon);
163 BOOST_CHECK_CLOSE(*result(1, 2), 1554.0, epsilon);
164
165 BOOST_CHECK_CLOSE(*result(2, 0), 2166.0, epsilon);
166 BOOST_CHECK_CLOSE(*result(2, 1), 2310.0, epsilon);
167 BOOST_CHECK_CLOSE(*result(2, 2), 2454.0, epsilon);
168 }
169}
170
171BOOST_AUTO_TEST_CASE(TestScaledFullTimesVector)
172{
173
174 {
175 // double buf1[] = {1, 2, 3,
176 // 4, 5, 6,
177 // 7, 8, 9};
178 double buf1[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
179 double buf2[] = {10, 11, 12};
180
181 std::shared_ptr<NekMatrix<double>> innerMatrix(
182 new NekMatrix<double>(3, 3, buf1));
183 NekMatrix<NekMatrix<double>, ScaledMatrixTag> lhs(2.0, innerMatrix);
184 NekVector<double> rhs(3, buf2);
185 NekVector<double> result = lhs * rhs;
186
187 BOOST_CHECK(result.GetRows() == 3);
188
189 double epsilon = 1e-12;
190 BOOST_CHECK_CLOSE(result[0], 136.0, epsilon);
191 BOOST_CHECK_CLOSE(result[1], 334.0, epsilon);
192 BOOST_CHECK_CLOSE(result[2], 532.0, epsilon);
193 }
194
195 {
196 // double buf1[] = {1, 2, 3,
197 // 4, 5, 6,
198 // 7, 8, 9,
199 // 10, 11, 12};
200 double buf1[] = {1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12};
201 double buf2[] = {10, 11, 12};
202
203 std::shared_ptr<NekMatrix<double>> innerMatrix(
204 new NekMatrix<double>(4, 3, buf1));
205 NekMatrix<NekMatrix<double>, ScaledMatrixTag> lhs(3.0, innerMatrix);
206 NekVector<double> rhs(3, buf2);
207 NekVector<double> result = lhs * rhs;
208
209 BOOST_CHECK(result.GetRows() == 4);
210
211 double epsilon = 1e-12;
212 BOOST_CHECK_CLOSE(result[0], 204.0, epsilon);
213 BOOST_CHECK_CLOSE(result[1], 501.0, epsilon);
214 BOOST_CHECK_CLOSE(result[2], 798.0, epsilon);
215 BOOST_CHECK_CLOSE(result[3], 1095.0, epsilon);
216 }
217}
218} // namespace MatrixMultiplicationTests
219} // namespace Nektar
unsigned int GetRows() const
Definition: NekVector.cpp:206
BOOST_AUTO_TEST_CASE(TestStandardFullTimesStandardFull)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2