root/library/tests/square_mat_test.cpp @ 456

Revision 456, 1.9 kB (checked in by vbarta, 15 years ago)

custom test location for harness tests (extended UnitTest?++), configurable tolerance - all tests pass (most of the time)

Line 
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
7const double epsilon = 0.00001;
8
9template<typename TMatrix>
10void test_square_matrix(double epsilon) {
11    int sz = 3;
12    mat A0 = randu(sz, sz);
13    mat A = A0 * A0.T();
14       
15    TMatrix sqmat(A);
16    CHECK_EQUAL(sz, sqmat.rows());
17    CHECK_EQUAL(sz, sqmat.cols());
18
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
30    TMatrix invmat(sz);
31    sqmat.inv(invmat);
32    mat invA = inv(A);
33    CHECK_CLOSE(invA, invmat.to_mat(), epsilon);
34
35    double d = det(A);
36    CHECK_CLOSE(log(d), sqmat.logdet(), epsilon);
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
45    q = sqmat.invqform(v);
46    r = (invA * v) * v;
47    CHECK_CLOSE(r, q, epsilon);
48
49    sqmat2 = sqmat;
50    sqmat2.clear();
51    CHECK_EQUAL(0, sqmat2.qform(ones(sz)));
52
53    TMatrix twice = sqmat;
54    twice += sqmat;
55    res = 2 * A;
56    CHECK_CLOSE(res, twice.to_mat(), epsilon);
57
58    twice = sqmat;
59    twice *= 2;
60    CHECK_CLOSE(res, twice.to_mat(), epsilon);
61
62    sqmat2 = sqmat;
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
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
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
80TEST(test_ldmat) {
81    test_square_matrix<ldmat>(epsilon);
82}
83
84TEST(test_fsqmat) {
85    test_square_matrix<fsqmat>(epsilon);
86}
87
88TEST(test_chmat) {
89    test_square_matrix<chmat>(epsilon);
90}
Note: See TracBrowser for help on using the browser.