[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 | |
---|
[428] | 14 | inline void CheckClose(TestResults &results, const itpp::mat &expected, |
---|
| 15 | const itpp::mat &actual, double tolerance, |
---|
| 16 | TestDetails const& details) { |
---|
[426] | 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> |
---|
[453] | 28 | void test_square_matrix(double epsilon) { |
---|
[426] | 29 | int sz = 3; |
---|
| 30 | mat A0 = randu(sz, sz); |
---|
| 31 | mat A = A0 * A0.T(); |
---|
[453] | 32 | |
---|
[426] | 33 | TMatrix sqmat(A); |
---|
[427] | 34 | CHECK_EQUAL(sz, sqmat.rows()); |
---|
| 35 | CHECK_EQUAL(sz, sqmat.cols()); |
---|
| 36 | |
---|
[426] | 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 | |
---|
[438] | 48 | TMatrix invmat(sz); |
---|
| 49 | sqmat.inv(invmat); |
---|
| 50 | mat invA = inv(A); |
---|
| 51 | CHECK_CLOSE(invA, invmat.to_mat(), epsilon); |
---|
| 52 | |
---|
[426] | 53 | double d = det(A); |
---|
| 54 | CHECK_CLOSE(log(d), sqmat.logdet(), epsilon); |
---|
[427] | 55 | |
---|
| 56 | double q = sqmat.qform(ones(sz)); |
---|
| 57 | CHECK_CLOSE(sumsum(A), q, epsilon); |
---|
| 58 | |
---|
| 59 | q = sqmat.qform(v); |
---|
| 60 | double r = (A * v) * v; |
---|
| 61 | CHECK_CLOSE(r, q, epsilon); |
---|
| 62 | |
---|
[453] | 63 | q = sqmat.invqform(v); |
---|
| 64 | r = (invA * v) * v; |
---|
| 65 | CHECK_CLOSE(r, q, epsilon); |
---|
| 66 | |
---|
[427] | 67 | sqmat2 = sqmat; |
---|
| 68 | sqmat2.clear(); |
---|
| 69 | CHECK_EQUAL(0, sqmat2.qform(ones(sz))); |
---|
[426] | 70 | |
---|
| 71 | TMatrix twice = sqmat; |
---|
| 72 | twice += sqmat; |
---|
[453] | 73 | res = 2 * A; |
---|
[426] | 74 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
| 75 | |
---|
| 76 | twice = sqmat; |
---|
| 77 | twice *= 2; |
---|
| 78 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
| 79 | |
---|
[453] | 80 | sqmat2 = sqmat; |
---|
[426] | 81 | mat B = randu(sz, sz); |
---|
| 82 | sqmat2.mult_sym(B); |
---|
| 83 | res = (B * A) * B.T(); |
---|
| 84 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 85 | |
---|
| 86 | sqmat2 = sqmat; |
---|
| 87 | sqmat2.mult_sym_t(B); |
---|
| 88 | res = (B.T() * A) * B; |
---|
| 89 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | TEST(test_ldmat) { |
---|
| 93 | test_square_matrix<ldmat>(epsilon); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | TEST(test_fsqmat) { |
---|
| 97 | test_square_matrix<fsqmat>(epsilon); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | TEST(test_chmat) { |
---|
[437] | 101 | test_square_matrix<chmat>(epsilon); |
---|
[426] | 102 | } |
---|