Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
blockmatrixinverse.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <qle/math/blockmatrixinverse.hpp>
#include "toplevelfixture.hpp"
#include <ql/math/comparison.hpp>
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
#include <boost/make_shared.hpp>
#include <boost/timer/timer.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testSingleBlock)
 
 BOOST_AUTO_TEST_CASE (testTwoBlocks)
 
 BOOST_AUTO_TEST_CASE (testThreeBlocks)
 
 BOOST_AUTO_TEST_CASE (testFourBlocksBigMatrix)
 
 BOOST_AUTO_TEST_CASE (testTenBlocksBigMatrix)
 
 BOOST_AUTO_TEST_CASE (testSparseMatrix)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/6]

BOOST_AUTO_TEST_CASE ( testSingleBlock  )

Definition at line 61 of file blockmatrixinverse.cpp.

61 {
62 BOOST_TEST_MESSAGE("Test block matrix inversion with single block matrix");
63
64 // clang-format off
65 std::vector<Real> data = { 1.0, 2.0, 2.0,
66 1.0, 1.0, 5.0,
67 -2.0, 0.5, 4.0 };
68 // clang-format on
69
70 Matrix m(3, 3);
71 std::copy(data.begin(), data.end(), m.begin());
72
73 std::vector<Size> indices = {3};
74
75 Matrix res = blockMatrixInverse(m, indices);
76 Matrix ex = inverse(m);
77 check(res, ex);
78} // testSingleBlock
QuantLib::SparseMatrix inverse(QuantLib::SparseMatrix m)
Matrix blockMatrixInverse(const Matrix &A, const std::vector< Size > &blockIndices)
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/6]

BOOST_AUTO_TEST_CASE ( testTwoBlocks  )

Definition at line 80 of file blockmatrixinverse.cpp.

80 {
81 BOOST_TEST_MESSAGE("Test block matrix inversion with two blocks");
82
83 // clang-format off
84 std::vector<Real> data = { 1.0, 2.0, 2.0, 3.0,
85 1.0, 1.0, 5.0, 1.0,
86 -2.0, 0.5, 4.0, -2.0,
87 3.0, 1.0, -1.0, -1.0};
88 // clang-format on
89
90 Matrix m(4, 4);
91 std::copy(data.begin(), data.end(), m.begin());
92
93 std::vector<Size> indices = {2, 4};
94
95 Matrix res = blockMatrixInverse(m, indices);
96 Matrix ex = inverse(m);
97 check(res, ex);
98} // testTwoBlocks
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/6]

BOOST_AUTO_TEST_CASE ( testThreeBlocks  )

Definition at line 100 of file blockmatrixinverse.cpp.

100 {
101 BOOST_TEST_MESSAGE("Test block matrix inversion with three blocks");
102
103 // clang-format off
104 std::vector<Real> data = { 1.0, 2.0, 2.0, 3.0,
105 1.0, 1.0, 5.0, 1.0,
106 -2.0, 0.5, 4.0, -2.0,
107 3.0, 1.0, -1.0, -1.0};
108 // clang-format on
109
110 Matrix m(4, 4);
111 std::copy(data.begin(), data.end(), m.begin());
112
113 std::vector<Size> indices = {1, 2, 4};
114
115 Matrix res = blockMatrixInverse(m, indices);
116 Matrix ex = inverse(m);
117 check(res, ex);
118} // testThreeBlocks
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/6]

BOOST_AUTO_TEST_CASE ( testFourBlocksBigMatrix  )

Definition at line 120 of file blockmatrixinverse.cpp.

120 {
121 BOOST_TEST_MESSAGE("Test block matrix inversion with four blocks big matrix");
122
123 Matrix m(300, 300, 0.0);
124
125 std::vector<Size> indices = {50, 100, 280, 300};
126
127 MersenneTwisterUniformRng mt(42);
128 for (Size i = 0; i < indices.size(); ++i) {
129 Size a0 = (i == 0 ? 0 : indices[i - 1]);
130 Size a1 = indices[i];
131 for (Size ii = a0; ii < a1; ++ii) {
132 for (Size jj = a0; jj < a1; ++jj) {
133 m[ii][jj] = mt.nextReal();
134 }
135 }
136 }
137
138 boost::timer::cpu_timer timer;
139 Matrix res = blockMatrixInverse(m, indices);
140 timer.stop();
141 BOOST_TEST_MESSAGE("block matrix inversion: " << timer.elapsed().wall * 1e-6 << " ms");
142 timer.start();
143 Matrix ex = inverse(m);
144 timer.stop();
145 BOOST_TEST_MESSAGE("plain matrix inversion: " << timer.elapsed().wall * 1e-6 << " ms");
146 check(res, ex);
147} // testFourBlocksBigMatrix
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [5/6]

BOOST_AUTO_TEST_CASE ( testTenBlocksBigMatrix  )

Definition at line 149 of file blockmatrixinverse.cpp.

149 {
150 BOOST_TEST_MESSAGE("Test block matrix inversion with ten blocks big matrix");
151
152 Matrix m(500, 500, 0.0);
153
154 std::vector<Size> indices = {30, 80, 130, 150, 200, 280, 370, 420, 430, 500};
155
156 MersenneTwisterUniformRng mt(42);
157 for (Size i = 0; i < indices.size(); ++i) {
158 Size a0 = (i == 0 ? 0 : indices[i - 1]);
159 Size a1 = indices[i];
160 for (Size ii = a0; ii < a1; ++ii) {
161 for (Size jj = a0; jj < a1; ++jj) {
162 m[ii][jj] = mt.nextReal();
163 }
164 }
165 }
166
167 boost::timer::cpu_timer timer;
168 Matrix res = blockMatrixInverse(m, indices);
169 timer.stop();
170 BOOST_TEST_MESSAGE("block matrix inversion: " << timer.elapsed().wall * 1e-6 << " ms");
171 timer.start();
172 Matrix ex = inverse(m);
173 timer.stop();
174 BOOST_TEST_MESSAGE("plain matrix inversion: " << timer.elapsed().wall * 1e-6 << " ms");
175 check(res, ex);
176} // testFourBlocksBigMatrix
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [6/6]

BOOST_AUTO_TEST_CASE ( testSparseMatrix  )

Definition at line 178 of file blockmatrixinverse.cpp.

178 {
179 BOOST_TEST_MESSAGE("Test sparse matrix with two blocks");
180
181 // clang-format off
182 std::vector<Real> data = { 1.0, 2.0, 0.0, 3.0,
183 0.0, 1.0, 5.0, 0.0,
184 -2.0, 0.0, 4.0, -2.0,
185 3.0, 1.0, -1.0, -1.0};
186 // clang-format on
187
188 Matrix m(4, 4);
189 std::copy(data.begin(), data.end(), m.begin());
190
191 SparseMatrix sm(4, 4);
192 for (Size i = 0; i < 4; ++i) {
193 for (Size j = 0; j < 4; ++j) {
194 Real tmp = data[i * 4 + j];
195 if (!close_enough(tmp, 0.0))
196 sm(i, j) = tmp;
197 }
198 }
199
200 std::vector<Size> indices = {2, 4};
201
202 SparseMatrix res = blockMatrixInverse(sm, indices);
203 Matrix ex = inverse(m);
204
205 Matrix res2(4, 4);
206 for (Size i = 0; i < 4; ++i) {
207 for (Size j = 0; j < 4; ++j) {
208 res2[i][j] = res(i, j);
209 }
210 }
211
212 check(res2, ex);
213} // testSingleBlock
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
+ Here is the call graph for this function: