Nektar++
TestTriangularMatrixOperations.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestTriangularMatrixOperations.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:
32//
33///////////////////////////////////////////////////////////////////////////////
34
37
38#include <boost/test/tools/floating_point_comparison.hpp>
39#include <boost/test/unit_test.hpp>
40
41namespace Nektar
42{
43namespace TriangularMatrixVectorMultiplicationUnitTests
44{
45BOOST_AUTO_TEST_CASE(TestUpperTriangularMatrixVectorMultiplication)
46{
47 // [1 2 3 4]
48 // [0 5 6 7]
49 // [0 0 8 9]
50 // [0 0 0 10]
51
52 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
53 NekDouble x_buf[] = {10, 20, 30, 40};
54
56 NekVector<NekDouble> x(4, x_buf);
57
58 NekVector<NekDouble> result = m * x;
59
60 NekDouble expected_result_buf[] = {300, 560, 600, 400};
61 NekVector<NekDouble> expected_result(4, expected_result_buf);
62 BOOST_CHECK_EQUAL(expected_result, result);
63}
64
65BOOST_AUTO_TEST_CASE(TestScaledUpperTriangularMatrixVectorMultiplication)
66{
67 // [1 2 3 4]
68 // [0 5 6 7]
69 // [0 0 8 9]
70 // [0 0 0 10]
71
72 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
73 NekDouble x_buf[] = {10, 20, 30, 40};
74
75 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> m(
76 new NekMatrix<NekDouble>(4, 4, a_buf, eUPPER_TRIANGULAR));
77 NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag> scaled(2, m);
78 NekVector<NekDouble> x(4, x_buf);
79
80 NekVector<NekDouble> result = scaled * x;
81
82 NekDouble expected_result_buf[] = {600, 1120, 1200, 800};
83 NekVector<NekDouble> expected_result(4, expected_result_buf);
84 BOOST_CHECK_EQUAL(expected_result, result);
85}
86
87BOOST_AUTO_TEST_CASE(TestLowerTriangularMatrixVectorMultiplication)
88{
89 // [1 0 0 0]
90 // [2 6 0 0]
91 // [5 8 7 0]
92 // [3 4 9 10]
93
94 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
95 NekDouble x_buf[] = {10, 20, 30, 40};
96
98 NekVector<NekDouble> x(4, x_buf);
99
100 NekVector<NekDouble> result = m * x;
101
102 NekDouble expected_result_buf[] = {10, 140, 420, 780};
103 NekVector<NekDouble> expected_result(4, expected_result_buf);
104 BOOST_CHECK_EQUAL(expected_result, result);
105}
106
107BOOST_AUTO_TEST_CASE(TestScaledLowerTriangularMatrixVectorMultiplication)
108{
109 // [1 0 0 0]
110 // [2 6 0 0]
111 // [5 8 7 0]
112 // [3 4 9 10]
113
114 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
115 NekDouble x_buf[] = {10, 20, 30, 40};
116
117 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> m(
118 new NekMatrix<NekDouble>(4, 4, a_buf, eLOWER_TRIANGULAR));
119 NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag> scaled(3.0, m);
120 NekVector<NekDouble> x(4, x_buf);
121
122 NekVector<NekDouble> result = scaled * x;
123
124 NekDouble expected_result_buf[] = {30, 420, 1260, 2340};
125 NekVector<NekDouble> expected_result(4, expected_result_buf);
126 BOOST_CHECK_EQUAL(expected_result, result);
127}
128
129BOOST_AUTO_TEST_CASE(TestUpperTriangularSolve)
130{
131 // [1 2 3 4]
132 // [0 5 6 7]
133 // [0 0 8 9]
134 // [0 0 0 10]
135
136 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
138
139 NekDouble b_buf[] = {300, 560, 600, 400};
140 NekVector<NekDouble> b(4, b_buf);
141
142 LinearSystem sys(m);
143 NekVector<NekDouble> x = sys.Solve(b);
144
145 NekDouble expected_result_buf[] = {10, 20, 30, 40};
146 NekVector<NekDouble> expected_result(4, expected_result_buf);
147
148 BOOST_CHECK_EQUAL(expected_result, x);
149}
150
151BOOST_AUTO_TEST_CASE(TestUpperTriangularTransposeSolve)
152{
153 // [1 2 3 4]
154 // [0 5 6 7]
155 // [0 0 8 9]
156 // [0 0 0 10]
157
158 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
160
161 NekDouble b_buf[] = {10, 120, 390, 850};
162 NekVector<NekDouble> b(4, b_buf);
163
164 LinearSystem sys(m);
166
167 NekDouble expected_result_buf[] = {10, 20, 30, 40};
168 NekVector<NekDouble> expected_result(4, expected_result_buf);
169
170 BOOST_CHECK_EQUAL(expected_result, x);
171}
172
173BOOST_AUTO_TEST_CASE(TestLowerTriangularSolve)
174{
175 // [1 2 3 4]
176 // [0 5 6 7]
177 // [0 0 8 9]
178 // [0 0 0 10]
179
180 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
182
183 NekDouble b_buf[] = {10, 140, 420, 780};
184 NekVector<NekDouble> b(4, b_buf);
185
186 LinearSystem sys(m);
187 NekVector<NekDouble> x = sys.Solve(b);
188
189 NekDouble expected_result_buf[] = {10, 20, 30, 40};
190 NekVector<NekDouble> expected_result(4, expected_result_buf);
191
192 BOOST_CHECK_EQUAL(expected_result, x);
193}
194
195BOOST_AUTO_TEST_CASE(TestLowerTriangularTransposeSolve)
196{
197 // [1 2 3 4]
198 // [0 5 6 7]
199 // [0 0 8 9]
200 // [0 0 0 10]
201
202 NekDouble a_buf[] = {1, 2, 5, 3, 6, 8, 4, 7, 9, 10};
204
205 NekDouble b_buf[] = {320, 520, 570, 400};
206 NekVector<NekDouble> b(4, b_buf);
207
208 LinearSystem sys(m);
210
211 NekDouble expected_result_buf[] = {10, 20, 30, 40};
212 NekVector<NekDouble> expected_result(4, expected_result_buf);
213
214 BOOST_CHECK_EQUAL(expected_result, x);
215}
216} // namespace TriangularMatrixVectorMultiplicationUnitTests
217} // namespace Nektar
RawType_t< VectorType > SolveTranspose(const VectorType &b)
Definition: NekLinSys.hpp:475
RawType_t< VectorType > Solve(const VectorType &b)
Definition: NekLinSys.hpp:452
BOOST_AUTO_TEST_CASE(TestUpperTriangularMatrixVectorMultiplication)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble