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