[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 | template<typename TMatrix> |
---|
[453] | 10 | void test_square_matrix(double epsilon) { |
---|
[426] | 11 | int sz = 3; |
---|
| 12 | mat A0 = randu(sz, sz); |
---|
| 13 | mat A = A0 * A0.T(); |
---|
[453] | 14 | |
---|
[426] | 15 | TMatrix sqmat(A); |
---|
[427] | 16 | CHECK_EQUAL(sz, sqmat.rows()); |
---|
| 17 | CHECK_EQUAL(sz, sqmat.cols()); |
---|
| 18 | |
---|
[426] | 19 | mat res = sqmat.to_mat(); |
---|
| 20 | CHECK_CLOSE(A, res, epsilon); |
---|
| 21 | |
---|
| 22 | vec v = randu(sz); |
---|
| 23 | double w = randu(); |
---|
| 24 | TMatrix sqmat2 = sqmat; |
---|
| 25 | sqmat2.opupdt(v, w); |
---|
| 26 | |
---|
| 27 | res = A + w * outer_product(v, v); |
---|
| 28 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 29 | |
---|
[438] | 30 | TMatrix invmat(sz); |
---|
| 31 | sqmat.inv(invmat); |
---|
| 32 | mat invA = inv(A); |
---|
| 33 | CHECK_CLOSE(invA, invmat.to_mat(), epsilon); |
---|
| 34 | |
---|
[426] | 35 | double d = det(A); |
---|
| 36 | CHECK_CLOSE(log(d), sqmat.logdet(), epsilon); |
---|
[427] | 37 | |
---|
| 38 | double q = sqmat.qform(ones(sz)); |
---|
| 39 | CHECK_CLOSE(sumsum(A), q, epsilon); |
---|
| 40 | |
---|
| 41 | q = sqmat.qform(v); |
---|
| 42 | double r = (A * v) * v; |
---|
| 43 | CHECK_CLOSE(r, q, epsilon); |
---|
| 44 | |
---|
[453] | 45 | q = sqmat.invqform(v); |
---|
| 46 | r = (invA * v) * v; |
---|
| 47 | CHECK_CLOSE(r, q, epsilon); |
---|
| 48 | |
---|
[427] | 49 | sqmat2 = sqmat; |
---|
| 50 | sqmat2.clear(); |
---|
| 51 | CHECK_EQUAL(0, sqmat2.qform(ones(sz))); |
---|
[426] | 52 | |
---|
| 53 | TMatrix twice = sqmat; |
---|
| 54 | twice += sqmat; |
---|
[453] | 55 | res = 2 * A; |
---|
[426] | 56 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
| 57 | |
---|
| 58 | twice = sqmat; |
---|
| 59 | twice *= 2; |
---|
| 60 | CHECK_CLOSE(res, twice.to_mat(), epsilon); |
---|
| 61 | |
---|
[453] | 62 | sqmat2 = sqmat; |
---|
[426] | 63 | mat B = randu(sz, sz); |
---|
| 64 | sqmat2.mult_sym(B); |
---|
| 65 | res = (B * A) * B.T(); |
---|
| 66 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 67 | |
---|
[455] | 68 | mat C = randu(sz, sz-1); |
---|
| 69 | TMatrix CAC(sz-1); |
---|
| 70 | sqmat.mult_sym_t(C,CAC); |
---|
| 71 | res = (C.T() * A) * C; |
---|
| 72 | CHECK_CLOSE(res, CAC.to_mat(), epsilon); |
---|
| 73 | |
---|
[426] | 74 | sqmat2 = sqmat; |
---|
| 75 | sqmat2.mult_sym_t(B); |
---|
| 76 | res = (B.T() * A) * B; |
---|
| 77 | CHECK_CLOSE(res, sqmat2.to_mat(), epsilon); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | TEST(test_ldmat) { |
---|
| 81 | test_square_matrix<ldmat>(epsilon); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | TEST(test_fsqmat) { |
---|
| 85 | test_square_matrix<fsqmat>(epsilon); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | TEST(test_chmat) { |
---|
[437] | 89 | test_square_matrix<chmat>(epsilon); |
---|
[426] | 90 | } |
---|