root/library/tests/square_mat_test.cpp @ 453

Revision 453, 2.2 kB (checked in by vbarta, 15 years ago)

moved square_mat children's tests to testsuite

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
9bool fast = false;
10
11namespace UnitTest
12{
13
14inline 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
27template<typename TMatrix>
28void test_square_matrix(double epsilon) {
29    int sz = 3;
30    mat A0 = randu(sz, sz);
31    mat A = A0 * A0.T();
32       
33    TMatrix sqmat(A);
34    CHECK_EQUAL(sz, sqmat.rows());
35    CHECK_EQUAL(sz, sqmat.cols());
36
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
48    TMatrix invmat(sz);
49    sqmat.inv(invmat);
50    mat invA = inv(A);
51    CHECK_CLOSE(invA, invmat.to_mat(), epsilon);
52
53    double d = det(A);
54    CHECK_CLOSE(log(d), sqmat.logdet(), epsilon);
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
63    q = sqmat.invqform(v);
64    r = (invA * v) * v;
65    CHECK_CLOSE(r, q, epsilon);
66
67    sqmat2 = sqmat;
68    sqmat2.clear();
69    CHECK_EQUAL(0, sqmat2.qform(ones(sz)));
70
71    TMatrix twice = sqmat;
72    twice += sqmat;
73    res = 2 * A;
74    CHECK_CLOSE(res, twice.to_mat(), epsilon);
75
76    twice = sqmat;
77    twice *= 2;
78    CHECK_CLOSE(res, twice.to_mat(), epsilon);
79
80    sqmat2 = sqmat;
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
92TEST(test_ldmat) {
93    test_square_matrix<ldmat>(epsilon);
94}
95
96TEST(test_fsqmat) {
97    test_square_matrix<fsqmat>(epsilon);
98}
99
100TEST(test_chmat) {
101    test_square_matrix<chmat>(epsilon);
102}
Note: See TracBrowser for help on using the browser.