Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
fillemptymatrix.cpp File Reference
#include "toplevelfixture.hpp"
#include <boost/test/unit_test.hpp>
#include <qle/math/fillemptymatrix.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testBlankLineFill)
 
 BOOST_AUTO_TEST_CASE (testInterpolateOnly)
 
 BOOST_AUTO_TEST_CASE (testExtrapolateOnly)
 
 BOOST_AUTO_TEST_CASE (testInterpExtrap)
 
 BOOST_AUTO_TEST_CASE (testSingleEntry)
 
 BOOST_AUTO_TEST_CASE (testEmptyMatrix)
 
 BOOST_AUTO_TEST_CASE (testFullMatrix)
 
 BOOST_AUTO_TEST_CASE (testSingleRowCol)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/8]

BOOST_AUTO_TEST_CASE ( testBlankLineFill  )

Definition at line 36 of file fillemptymatrix.cpp.

36 {
37 BOOST_TEST_MESSAGE("Testing filling matrices with blank lines");
38
39 Real non_val = -1;
40
41 // empty row matrix & empty col matrix
42 Matrix empty_row, empty_col;
43 empty_row = Matrix(5, 5, Real(22.5));
44 empty_col = Matrix(5, 5, Real(22.5));
45
46 for (int i = 0; i < 5; i++) {
47 empty_row[2][i] = non_val;
48 empty_col[i][2] = non_val;
49 }
50
51 // check fails + successes
52 Matrix tmp_mat_r = empty_row;
53 Matrix tmp_mat_c = empty_col;
54 BOOST_CHECK_THROW(fillIncompleteMatrix(tmp_mat_r, true, non_val), QuantLib::Error);
55 BOOST_CHECK_THROW(fillIncompleteMatrix(tmp_mat_c, false, non_val), QuantLib::Error);
56
57 tmp_mat_r = empty_row;
58 tmp_mat_c = empty_col;
59 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_mat_r, false, non_val));
60 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_mat_c, true, non_val));
61
62 // check vals
63 Real tol = 1.0E-8;
64 for (int i = 0; i < 5; i++) {
65 BOOST_CHECK_CLOSE(tmp_mat_r[2][i], Real(22.5), tol);
66 BOOST_CHECK_CLOSE(tmp_mat_c[i][2], Real(22.5), tol);
67 }
68}
void fillIncompleteMatrix(Matrix &mat, bool interpRows=true, Real blank=QL_NULL_REAL)
function that fills a matrix
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/8]

BOOST_AUTO_TEST_CASE ( testInterpolateOnly  )

Definition at line 70 of file fillemptymatrix.cpp.

70 {
71 BOOST_TEST_MESSAGE("Testing interpolation only");
72
73 Real non_val = -1;
74 Matrix incomplete_m;
75 // incomplete matrix of the form:
76 /*
77 1 2 3 4 5
78 2 3 4 5 6
79 3 4 5 6 7
80 4 5 6 7 8
81 5 6 7 8 9
82 */
83 // But center block is non_values.
84
85 incomplete_m = Matrix(5, 5, non_val);
86 for (int i = 0; i < 5; i++) {
87 for (int j = 0; j < 5; j++) {
88 if (i == 0 || j == 0 || i == 4 || j == 4) {
89 incomplete_m[i][j] = j + i + 1;
90 }
91 }
92 }
93
94 // fill matrix
95 Matrix to_fill_row = incomplete_m;
96 Matrix to_fill_col = incomplete_m;
97 fillIncompleteMatrix(to_fill_row, true, non_val);
98 fillIncompleteMatrix(to_fill_col, false, non_val);
99
100 // check results
101 Real tol = 1.0E-8;
102 for (int i = 0; i < 5; i++) {
103 for (int j = 0; j < 5; j++) {
104 BOOST_CHECK_CLOSE(to_fill_row[i][j], j + i + 1, tol);
105 BOOST_CHECK_CLOSE(to_fill_col[i][j], j + i + 1, tol);
106 }
107 }
108}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/8]

BOOST_AUTO_TEST_CASE ( testExtrapolateOnly  )

Definition at line 110 of file fillemptymatrix.cpp.

110 {
111 BOOST_TEST_MESSAGE("Testing extrapolation of edges in filling the matrix");
112
113 Real non_val = -1;
114 Matrix missing_rows, missing_cols;
115 vector<vector<Real> > test_cases; // for different test cases, different missing lines
116 for (int i = 0; i < 4; i++) {
117 vector<Real> tmp;
118 for (int j = 0; j <= i; j++) {
119 tmp.push_back(j);
120 }
121 test_cases.push_back(tmp);
122 }
123
124 // incomplete matricies of the form:
125 /*
126 ' 2 3 4 5 ' ' ' ' '
127 ' 3 4 5 6 2 3 4 5 6
128 ' 4 5 6 7 3 4 5 6 7
129 ' 5 6 7 8 4 5 6 7 8
130 ' 6 7 8 9 5 6 7 8 9
131 */
132 // incomplete_rows: some rows at leading edge are missing.
133 // incomplete_cols: some cols at leading edge are missing.
134
135 // loop over cases with different missing rows/cols
136 vector<vector<Real> >::iterator cs;
137 for (cs = test_cases.begin(); cs != test_cases.end(); cs++) {
138
139 // set up empty matrices
140 missing_rows = Matrix(5, 5, non_val);
141 missing_cols = Matrix(5, 5, non_val);
142 for (int i = 0; i < 5; i++) {
143 for (int j = 0; j < 5; j++) {
144
145 // ignore empty lines
146 bool set_row = find(cs->begin(), cs->end(), i) == cs->end();
147 bool set_col = find(cs->begin(), cs->end(), j) == cs->end();
148 if (set_row) {
149 missing_rows[i][j] = j + i + 1;
150 }
151 if (set_col) {
152 missing_cols[i][j] = j + i + 1;
153 }
154 }
155 }
156
157 // fill matrices
158 Matrix to_fill_rows = missing_rows;
159 Matrix to_fill_cols = missing_cols;
160 fillIncompleteMatrix(to_fill_rows, false, non_val);
161 fillIncompleteMatrix(to_fill_cols, true, non_val);
162
163 // check results
164 for (int i = 0; i < 5; i++) {
165 for (int j = 0; j < 5; j++) {
166 int last_val = cs->size();
167 Real expectedVal_row;
168 Real expectedVal_col;
169 if (j < last_val) {
170 expectedVal_col = missing_cols[i][last_val];
171 } else {
172 expectedVal_col = missing_cols[i][j];
173 }
174 if (i < last_val) {
175 expectedVal_row = missing_rows[last_val][j];
176 } else {
177 expectedVal_row = missing_rows[i][j];
178 }
179 bool check_row = to_fill_rows[i][j] == expectedVal_row;
180 bool check_col = to_fill_cols[i][j] == expectedVal_col;
181
182 if (!check_row || !check_col) {
183 BOOST_FAIL("FillIncomplete matrix failed on extrapolation only tests.");
184 }
185 }
186 }
187 }
188}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/8]

BOOST_AUTO_TEST_CASE ( testInterpExtrap  )

Definition at line 190 of file fillemptymatrix.cpp.

190 {
191 BOOST_TEST_MESSAGE("Testing interpolation and extrapolation");
192
193 Real non_val = -1;
194 Matrix incomplete_m;
195 // incomplete matrix of the form:
196 /*
197 1 ' ' ' '
198 ' 2 ' ' '
199 ' ' 3 ' '
200 ' ' ' 4 '
201 ' ' ' ' 5
202 */
203
204 incomplete_m = Matrix(5, 5, non_val);
205 for (int i = 0; i < 5; i++) {
206 incomplete_m[i][i] = i;
207 }
208
209 // fill matrix
210 Matrix to_fill_rows = incomplete_m;
211 Matrix to_fill_cols = incomplete_m;
212 fillIncompleteMatrix(to_fill_rows, true, non_val);
213 fillIncompleteMatrix(to_fill_cols, false, non_val);
214
215 // check results
216 for (int i = 0; i < 5; i++) {
217 for (int j = 0; j < 5; j++) {
218 BOOST_CHECK_EQUAL(to_fill_rows[i][j], incomplete_m[i][i]);
219 BOOST_CHECK_EQUAL(to_fill_cols[i][j], incomplete_m[j][j]);
220 }
221 }
222}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [5/8]

BOOST_AUTO_TEST_CASE ( testSingleEntry  )

Definition at line 224 of file fillemptymatrix.cpp.

224 {
225
226 Matrix inc = Matrix(1, 1, 22.5);
227 Matrix tmp1 = inc, tmp2 = inc, tmp3 = inc, tmp4 = inc;
228
229 // non-blank value.
230 BOOST_TEST_MESSAGE("Testing single non-blank entry");
231 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp1, true, -1));
232 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp2, false, -1));
233 BOOST_CHECK_EQUAL(tmp1[0][0], inc[0][0]);
234 BOOST_CHECK_EQUAL(tmp2[0][0], inc[0][0]);
235
236 // blank value.
237 BOOST_TEST_MESSAGE("Testing single blank entry");
238 BOOST_CHECK_THROW(fillIncompleteMatrix(tmp3, true, 22.5), QuantLib::Error);
239 BOOST_CHECK_THROW(fillIncompleteMatrix(tmp4, false, 22.5), QuantLib::Error);
240}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [6/8]

BOOST_AUTO_TEST_CASE ( testEmptyMatrix  )

Definition at line 242 of file fillemptymatrix.cpp.

242 {
243 BOOST_TEST_MESSAGE("testing empty matrices");
244
245 Matrix m;
246 BOOST_CHECK_THROW(fillIncompleteMatrix(m, true, -1), QuantLib::Error);
247 BOOST_CHECK_THROW(fillIncompleteMatrix(m, false, -1), QuantLib::Error);
248}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [7/8]

BOOST_AUTO_TEST_CASE ( testFullMatrix  )

Definition at line 250 of file fillemptymatrix.cpp.

250 {
251 BOOST_TEST_MESSAGE("tesing full matrices");
252
253 // set up matrices
254 Matrix full_single = Matrix(1, 1, 22.5);
255 Matrix full = Matrix(5, 5, 22.5);
256 Matrix tmp_single_r = full_single;
257 Matrix tmp_single_c = full_single;
258 Matrix tmp_r = full;
259 Matrix tmp_c = full;
260
261 // "fill"
262 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_single_r, true, -1));
263 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_single_c, false, -1));
264 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_r, true, -1));
265 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_c, false, -1));
266
267 // check results
268 BOOST_CHECK_EQUAL(tmp_single_r[0][0], full_single[0][0]);
269 BOOST_CHECK_EQUAL(tmp_single_c[0][0], full_single[0][0]);
270 for (int i = 0; i < 5; i++) {
271 for (int j = 0; j < 5; j++) {
272 BOOST_CHECK_EQUAL(tmp_r[i][j], full[i][j]);
273 BOOST_CHECK_EQUAL(tmp_c[i][j], full[i][j]);
274 }
275 }
276}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [8/8]

BOOST_AUTO_TEST_CASE ( testSingleRowCol  )

Definition at line 278 of file fillemptymatrix.cpp.

278 {
279 Matrix single_row = Matrix(1, 5, 22.5);
280 Matrix single_col = Matrix(5, 1, 22.5);
281 single_row[0][3] = -1; // single blank entry
282 single_col[3][0] = -1; // single blank entry
283 Matrix tmp_row = single_row;
284 Matrix tmp_col = single_col;
285
286 BOOST_CHECK_THROW(fillIncompleteMatrix(tmp_row, false, -1), QuantLib::Error);
287 BOOST_CHECK_THROW(fillIncompleteMatrix(tmp_col, true, -1), QuantLib::Error);
288
289 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_row, true, -1));
290 BOOST_CHECK_NO_THROW(fillIncompleteMatrix(tmp_col, false, -1));
291}
+ Here is the call graph for this function: