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