[426] | 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 | 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 | mat res = sqmat.to_mat(); |
---|
| 35 | CHECK_CLOSE(A, res, epsilon); |
---|
| 36 | |
---|
| 37 | vec v = randu(sz); |
---|
| 38 | double w = randu(); |
---|
| 39 | TMatrix sqmat2 = sqmat; |
---|
| 40 | sqmat2.opupdt(v, w); |
---|
| 41 | |
---|
| 42 | res = A + w * outer_product(v, v); |
---|
| 43 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 44 | |
---|
| 45 | double d = det(A); |
---|
| 46 | CHECK_CLOSE(log(d), sqmat.logdet(), epsilon); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | template<typename TMatrix> |
---|
| 50 | void test_square_matrix(double epsilon) { |
---|
| 51 | test_square_matrix_minimum<TMatrix>(epsilon); |
---|
| 52 | |
---|
| 53 | int sz = 3; |
---|
| 54 | mat A0 = randu(sz, sz); |
---|
| 55 | mat A = A0 * A0.T(); |
---|
| 56 | |
---|
| 57 | TMatrix sqmat(A); |
---|
| 58 | TMatrix twice = sqmat; |
---|
| 59 | twice += sqmat; |
---|
| 60 | mat res(2 * A); |
---|
| 61 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
| 62 | |
---|
| 63 | twice = sqmat; |
---|
| 64 | twice *= 2; |
---|
| 65 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
| 66 | |
---|
| 67 | TMatrix sqmat2 = sqmat; |
---|
| 68 | mat B = randu(sz, sz); |
---|
| 69 | sqmat2.mult_sym(B); |
---|
| 70 | res = (B * A) * B.T(); |
---|
| 71 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 72 | |
---|
| 73 | sqmat2 = sqmat; |
---|
| 74 | sqmat2.mult_sym_t(B); |
---|
| 75 | res = (B.T() * A) * B; |
---|
| 76 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | TEST(test_ldmat) { |
---|
| 80 | test_square_matrix<ldmat>(epsilon); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | TEST(test_fsqmat) { |
---|
| 84 | test_square_matrix<fsqmat>(epsilon); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | TEST(test_chmat) { |
---|
| 88 | test_square_matrix_minimum<chmat>(0.2); |
---|
| 89 | } |
---|