1 | #include "../bdm/math/square_mat.h" |
---|
2 | #include "../bdm/math/chmat.h" |
---|
3 | #include "mat_checks.h" |
---|
4 | #include "UnitTest++.h" |
---|
5 | #include <math.h> |
---|
6 | |
---|
7 | const double epsilon = 0.00001; |
---|
8 | |
---|
9 | bool fast = false; |
---|
10 | |
---|
11 | namespace UnitTest |
---|
12 | { |
---|
13 | |
---|
14 | inline void CheckClose(TestResults &results, const itpp::mat &expected, |
---|
15 | const itpp::mat &actual, double tolerance, |
---|
16 | TestDetails const& details) { |
---|
17 | if (!AreClose(expected, actual, tolerance)) { |
---|
18 | MemoryOutStream stream; |
---|
19 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
20 | |
---|
21 | results.OnTestFailure(details, stream.GetText()); |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | } |
---|
26 | |
---|
27 | template<typename TMatrix> |
---|
28 | void test_square_matrix_minimum(double epsilon) { |
---|
29 | int sz = 3; |
---|
30 | mat A0 = randu(sz, sz); |
---|
31 | mat A = A0 * A0.T(); |
---|
32 | |
---|
33 | TMatrix sqmat(A); |
---|
34 | CHECK_EQUAL(sz, sqmat.rows()); |
---|
35 | CHECK_EQUAL(sz, sqmat.cols()); |
---|
36 | |
---|
37 | mat res = sqmat.to_mat(); |
---|
38 | CHECK_CLOSE(A, res, epsilon); |
---|
39 | |
---|
40 | vec v = randu(sz); |
---|
41 | double w = randu(); |
---|
42 | TMatrix sqmat2 = sqmat; |
---|
43 | sqmat2.opupdt(v, w); |
---|
44 | |
---|
45 | res = A + w * outer_product(v, v); |
---|
46 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
47 | |
---|
48 | double d = det(A); |
---|
49 | CHECK_CLOSE(log(d), sqmat.logdet(), epsilon); |
---|
50 | |
---|
51 | double q = sqmat.qform(ones(sz)); |
---|
52 | CHECK_CLOSE(sumsum(A), q, epsilon); |
---|
53 | |
---|
54 | q = sqmat.qform(v); |
---|
55 | double r = (A * v) * v; |
---|
56 | CHECK_CLOSE(r, q, epsilon); |
---|
57 | |
---|
58 | sqmat2 = sqmat; |
---|
59 | sqmat2.clear(); |
---|
60 | CHECK_EQUAL(0, sqmat2.qform(ones(sz))); |
---|
61 | } |
---|
62 | |
---|
63 | template<typename TMatrix> |
---|
64 | void test_square_matrix(double epsilon) { |
---|
65 | test_square_matrix_minimum<TMatrix>(epsilon); |
---|
66 | |
---|
67 | int sz = 3; |
---|
68 | mat A0 = randu(sz, sz); |
---|
69 | mat A = A0 * A0.T(); |
---|
70 | |
---|
71 | TMatrix sqmat(A); |
---|
72 | TMatrix twice = sqmat; |
---|
73 | twice += sqmat; |
---|
74 | mat res(2 * A); |
---|
75 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
76 | |
---|
77 | twice = sqmat; |
---|
78 | twice *= 2; |
---|
79 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
80 | |
---|
81 | TMatrix sqmat2 = sqmat; |
---|
82 | mat B = randu(sz, sz); |
---|
83 | sqmat2.mult_sym(B); |
---|
84 | res = (B * A) * B.T(); |
---|
85 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
86 | |
---|
87 | sqmat2 = sqmat; |
---|
88 | sqmat2.mult_sym_t(B); |
---|
89 | res = (B.T() * A) * B; |
---|
90 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
91 | } |
---|
92 | |
---|
93 | TEST(test_ldmat) { |
---|
94 | test_square_matrix<ldmat>(epsilon); |
---|
95 | } |
---|
96 | |
---|
97 | TEST(test_fsqmat) { |
---|
98 | test_square_matrix<fsqmat>(epsilon); |
---|
99 | } |
---|
100 | |
---|
101 | TEST(test_chmat) { |
---|
102 | test_square_matrix_minimum<chmat>(epsilon); |
---|
103 | } |
---|