1 | #include "../bdm/math/square_mat.h" |
---|
2 | #include "../bdm/math/chmat.h" |
---|
3 | #include "itpp_ext.h" |
---|
4 | #include "../mat_checks.h" |
---|
5 | #include "UnitTest++.h" |
---|
6 | #include <math.h> |
---|
7 | |
---|
8 | const double epsilon = 0.00001; |
---|
9 | |
---|
10 | using namespace itpp; |
---|
11 | |
---|
12 | using bdm::fsqmat; |
---|
13 | using bdm::chmat; |
---|
14 | using bdm::ldmat; |
---|
15 | |
---|
16 | template<typename TMatrix> |
---|
17 | void test_square_matrix ( double epsilon ) { |
---|
18 | int sz = 3; |
---|
19 | mat A0 = randu ( sz, sz ); |
---|
20 | mat A = A0 * A0.T(); |
---|
21 | |
---|
22 | // ----------- SIZES --------- |
---|
23 | TMatrix sq_mat ( A ); |
---|
24 | CHECK_EQUAL ( sz, sq_mat.rows() ); |
---|
25 | CHECK_EQUAL ( sz, sq_mat.cols() ); |
---|
26 | |
---|
27 | // ----------- FULL MAT --------- |
---|
28 | mat res = sq_mat.to_mat(); |
---|
29 | CHECK_CLOSE ( A, res, epsilon ); |
---|
30 | |
---|
31 | // ----------- OUTER PRODUCT UPDATE --------- |
---|
32 | vec v = randu ( sz ); |
---|
33 | double w = randu(); |
---|
34 | TMatrix sq_mat2 = sq_mat; |
---|
35 | sq_mat2.opupdt ( v, w ); |
---|
36 | |
---|
37 | res = A + w * outer_product ( v, v ); |
---|
38 | CHECK_CLOSE ( res, sq_mat2.to_mat(), epsilon ); |
---|
39 | |
---|
40 | // ----------- INVERSION --------- |
---|
41 | TMatrix invmat ( sz ); |
---|
42 | sq_mat.inv ( invmat ); |
---|
43 | mat invA = inv ( A ); |
---|
44 | CHECK_CLOSE ( invA, invmat.to_mat(), epsilon ); |
---|
45 | |
---|
46 | // ----------- DETERMINANT --------- |
---|
47 | double d = det ( A ); |
---|
48 | CHECK_CLOSE ( log ( d ), sq_mat.logdet(), epsilon ); |
---|
49 | |
---|
50 | // ----------- QUADRATIC FORM --------- |
---|
51 | double q = sq_mat.qform ( ones ( sz ) ); |
---|
52 | CHECK_CLOSE ( sumsum ( A ), q, epsilon ); |
---|
53 | |
---|
54 | q = sq_mat.qform ( v ); |
---|
55 | double r = ( A * v ) * v; |
---|
56 | CHECK_CLOSE ( r, q, epsilon ); |
---|
57 | |
---|
58 | q = sq_mat.invqform ( v ); |
---|
59 | r = ( invA * v ) * v; |
---|
60 | CHECK_CLOSE ( r, q, epsilon ); |
---|
61 | |
---|
62 | sq_mat2 = sq_mat; |
---|
63 | sq_mat2.clear(); |
---|
64 | CHECK_EQUAL ( 0, sq_mat2.qform ( ones ( sz ) ) ); |
---|
65 | |
---|
66 | // ----------- + operator --------- |
---|
67 | TMatrix twice = sq_mat; |
---|
68 | twice += sq_mat; |
---|
69 | res = 2 * A; |
---|
70 | CHECK_CLOSE ( res, twice.to_mat(), epsilon ); |
---|
71 | |
---|
72 | // ----------- * operator --------- |
---|
73 | twice = sq_mat; |
---|
74 | twice *= 2; |
---|
75 | CHECK_CLOSE ( res, twice.to_mat(), epsilon ); |
---|
76 | |
---|
77 | // ----------- MULTIPLICATION --------- |
---|
78 | sq_mat2 = sq_mat; |
---|
79 | mat B = randu ( sz, sz ); |
---|
80 | sq_mat2.mult_sym ( B ); |
---|
81 | res = ( B * A ) * B.T(); |
---|
82 | CHECK_CLOSE ( res, sq_mat2.to_mat(), epsilon ); |
---|
83 | |
---|
84 | mat C = randu ( sz, sz - 1 ); |
---|
85 | TMatrix CAC ( sz - 1 ); |
---|
86 | sq_mat.mult_sym_t ( C, CAC ); |
---|
87 | res = ( C.T() * A ) * C; |
---|
88 | CHECK_CLOSE ( res, CAC.to_mat(), epsilon ); |
---|
89 | |
---|
90 | sq_mat2 = sq_mat; |
---|
91 | sq_mat2.mult_sym_t ( B ); |
---|
92 | res = ( B.T() * A ) * B; |
---|
93 | CHECK_CLOSE ( res, sq_mat2.to_mat(), epsilon ); |
---|
94 | |
---|
95 | // ----------- PERMUTATION --------- |
---|
96 | mat M1 = randu (sz,sz); |
---|
97 | mat M = M1*M1.T(); |
---|
98 | vec perm_v_rand = randu(sz); |
---|
99 | ivec perm_v_ids = sort_index(perm_v_rand); |
---|
100 | |
---|
101 | mat Mperm_c=M.get_cols(perm_v_ids); |
---|
102 | mat Mperm=Mperm_c.get_rows(perm_v_ids); |
---|
103 | |
---|
104 | TMatrix T(M); |
---|
105 | TMatrix Tperm(T,perm_v_ids); |
---|
106 | |
---|
107 | CHECK_CLOSE(Tperm.to_mat(), Mperm, epsilon); |
---|
108 | } |
---|
109 | |
---|
110 | TEST ( ldmat_test ) { |
---|
111 | test_square_matrix<ldmat> ( epsilon ); |
---|
112 | } |
---|
113 | |
---|
114 | TEST ( fsqmat_test ) { |
---|
115 | test_square_matrix<fsqmat> ( epsilon ); |
---|
116 | } |
---|
117 | |
---|
118 | TEST ( chmat_test ) { |
---|
119 | test_square_matrix<chmat> ( epsilon ); |
---|
120 | } |
---|