| 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 | TMatrix sq_mat ( A ); |
|---|
| 23 | CHECK_EQUAL ( sz, sq_mat.rows() ); |
|---|
| 24 | CHECK_EQUAL ( sz, sq_mat.cols() ); |
|---|
| 25 | |
|---|
| 26 | mat res = sq_mat.to_mat(); |
|---|
| 27 | CHECK_CLOSE ( A, res, epsilon ); |
|---|
| 28 | |
|---|
| 29 | vec v = randu ( sz ); |
|---|
| 30 | double w = randu(); |
|---|
| 31 | TMatrix sq_mat2 = sq_mat; |
|---|
| 32 | sq_mat2.opupdt ( v, w ); |
|---|
| 33 | |
|---|
| 34 | res = A + w * outer_product ( v, v ); |
|---|
| 35 | CHECK_CLOSE ( res, sq_mat2.to_mat(), epsilon ); |
|---|
| 36 | |
|---|
| 37 | TMatrix invmat ( sz ); |
|---|
| 38 | sq_mat.inv ( invmat ); |
|---|
| 39 | mat invA = inv ( A ); |
|---|
| 40 | CHECK_CLOSE ( invA, invmat.to_mat(), epsilon ); |
|---|
| 41 | |
|---|
| 42 | double d = det ( A ); |
|---|
| 43 | CHECK_CLOSE ( log ( d ), sq_mat.logdet(), epsilon ); |
|---|
| 44 | |
|---|
| 45 | double q = sq_mat.qform ( ones ( sz ) ); |
|---|
| 46 | CHECK_CLOSE ( sumsum ( A ), q, epsilon ); |
|---|
| 47 | |
|---|
| 48 | q = sq_mat.qform ( v ); |
|---|
| 49 | double r = ( A * v ) * v; |
|---|
| 50 | CHECK_CLOSE ( r, q, epsilon ); |
|---|
| 51 | |
|---|
| 52 | q = sq_mat.invqform ( v ); |
|---|
| 53 | r = ( invA * v ) * v; |
|---|
| 54 | CHECK_CLOSE ( r, q, epsilon ); |
|---|
| 55 | |
|---|
| 56 | sq_mat2 = sq_mat; |
|---|
| 57 | sq_mat2.clear(); |
|---|
| 58 | CHECK_EQUAL ( 0, sq_mat2.qform ( ones ( sz ) ) ); |
|---|
| 59 | |
|---|
| 60 | TMatrix twice = sq_mat; |
|---|
| 61 | twice += sq_mat; |
|---|
| 62 | res = 2 * A; |
|---|
| 63 | CHECK_CLOSE ( res, twice.to_mat(), epsilon ); |
|---|
| 64 | |
|---|
| 65 | twice = sq_mat; |
|---|
| 66 | twice *= 2; |
|---|
| 67 | CHECK_CLOSE ( res, twice.to_mat(), epsilon ); |
|---|
| 68 | |
|---|
| 69 | sq_mat2 = sq_mat; |
|---|
| 70 | mat B = randu ( sz, sz ); |
|---|
| 71 | sq_mat2.mult_sym ( B ); |
|---|
| 72 | res = ( B * A ) * B.T(); |
|---|
| 73 | CHECK_CLOSE ( res, sq_mat2.to_mat(), epsilon ); |
|---|
| 74 | |
|---|
| 75 | mat C = randu ( sz, sz - 1 ); |
|---|
| 76 | TMatrix CAC ( sz - 1 ); |
|---|
| 77 | sq_mat.mult_sym_t ( C, CAC ); |
|---|
| 78 | res = ( C.T() * A ) * C; |
|---|
| 79 | CHECK_CLOSE ( res, CAC.to_mat(), epsilon ); |
|---|
| 80 | |
|---|
| 81 | sq_mat2 = sq_mat; |
|---|
| 82 | sq_mat2.mult_sym_t ( B ); |
|---|
| 83 | res = ( B.T() * A ) * B; |
|---|
| 84 | CHECK_CLOSE ( res, sq_mat2.to_mat(), epsilon ); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | TEST ( ldmat_test ) { |
|---|
| 88 | test_square_matrix<ldmat> ( epsilon ); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | TEST ( fsqmat_test ) { |
|---|
| 92 | test_square_matrix<fsqmat> ( epsilon ); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | TEST ( chmat_test ) { |
|---|
| 96 | test_square_matrix<chmat> ( epsilon ); |
|---|
| 97 | } |
|---|