afc9fab1c8f649603b4044f7c602bfd1e0b4a22b
[libstick.git] / tests / simplicialcomplex.h
1 #ifndef simplicialcomplex_h_ooDeimaexieghaev
2 #define simplicialcomplex_h_ooDeimaexieghaev
3
4 #include <cpptest.h>
5 #include <cpptest-suite.h>
6
7 #include <libstick-0.1/simplicialcomplex.h>
8 #include <libstick-0.1/matrixreduction.h>
9
10 using namespace libstick;
11
12
13 class SimplicialComplexTestSuite: public Test::Suite {
14
15 private:
16 typedef SimplicialComplex<2, uint32_t, double> scomplex;
17 typedef scomplex::SimplexOrder::BoundaryMatrix bm;
18
19 bool setupcalled;
20 scomplex c1, c2, c3;
21 scomplex::SimplexOrder o1, o2, o3, o3b;
22
23 public:
24 SimplicialComplexTestSuite() :
25 setupcalled(false),
26 o1(c1),
27 o2(c2),
28 o3(c3),
29 o3b(c3)
30 {
31 TEST_ADD(SimplicialComplexTestSuite::test_isComplex);
32 TEST_ADD(SimplicialComplexTestSuite::test_isMonotoneComplex);
33 TEST_ADD(SimplicialComplexTestSuite::test_isOrderFiltration);
34 TEST_ADD(SimplicialComplexTestSuite::test_isOrderMonotone);
35 TEST_ADD(SimplicialComplexTestSuite::test_boundaryMatrix);
36 TEST_ADD(SimplicialComplexTestSuite::test_matrixreduction);
37 }
38
39 protected:
40 virtual void setup() {
41 if (setupcalled)
42 return;
43 setupcalled = true;
44
45 const unsigned num = 11;
46 scomplex::Simplex ss[num] = {
47 // dimension, faces, value...
48 {0, {}, 1},
49 {0, {}, 2},
50 {0, {}, 3},
51 {0, {}, 4},
52 {1, {1, 2}, 5},
53 {1, {2, 3}, 6},
54 {1, {3, 4}, 7},
55 {1, {4, 1}, 8},
56 {1, {1, 3}, 9},
57 {2, {7, 8, 9}, 10},
58 {2, {5, 6, 9}, 11}
59 };
60
61 // This is o1 This is o2 This is o3(b)
62 // (value = index) (values) (values)
63 //
64 // 1 ----5---- 2 1 ----5---- 2 1 ----5---- 2
65 // |\ | |\ | |\ |
66 // | \ 11 | | \ 11 | | \ 13 |
67 // | \ | | \ | | \ |
68 // | \ | | \ | | \ |
69 // 8 9 6 8 9 12 8 9 12
70 // | \ | | \ | | \ |
71 // | 10 \ | | 10 \ | | 10 \ |
72 // | \ | | \ | | \ |
73 // | \| | \| | \|
74 // 4 ----7---- 3 4 ----7---- 3 4 ----7---- 3
75
76 // Build the complex
77 for (unsigned i=0; i<num; ++i)
78 c1.addSimplex(ss[i]);
79
80 c2 = c1;
81 c2.simplices[6].value = 12;
82
83 c3 = c2;
84 c3.simplices[11].value = 13;
85
86 o1.reset();
87 o2.reset();
88 o3.reset();
89 o3b.reset();
90 o3b.makeMonotoneFiltration();
91 }
92
93 virtual void tear_down() {
94 }
95
96 void test_isComplex() {
97 TEST_ASSERT(c1.isComplex());
98 TEST_ASSERT(c2.isComplex());
99 TEST_ASSERT(c3.isComplex());
100 }
101
102 void test_isMonotoneComplex() {
103 TEST_ASSERT(c1.isMonotone());
104 TEST_ASSERT(!c2.isMonotone());
105 TEST_ASSERT(c3.isMonotone());
106 }
107
108 void test_isOrderFiltration() {
109 TEST_ASSERT(o1.isFiltration());
110 TEST_ASSERT(o2.isFiltration());
111 TEST_ASSERT(o3.isFiltration());
112 TEST_ASSERT(o3b.isFiltration());
113 }
114
115 void test_isOrderMonotone() {
116 TEST_ASSERT(o1.isMonotone());
117 TEST_ASSERT(!o2.isMonotone());
118 TEST_ASSERT(!o3.isMonotone());
119 TEST_ASSERT(o3b.isMonotone());
120 }
121
122 void test_boundaryMatrix() {
123 bm mat1 = o1.getBoundaryMatrix();
124 bm mat1e(c1.size());
125 mat1e.set(0, 1, true);
126 mat1e.set(0, 2, true);
127 mat1e.set(0, 3, true);
128 mat1e.set(0, 4, true);
129 mat1e.set(1, 5, true);
130 mat1e.set(2, 5, true);
131 mat1e.set(2, 6, true);
132 mat1e.set(3, 6, true);
133 mat1e.set(3, 7, true);
134 mat1e.set(4, 7, true);
135 mat1e.set(4, 8, true);
136 mat1e.set(1, 8, true);
137 mat1e.set(1, 9, true);
138 mat1e.set(3, 9, true);
139 mat1e.set(7, 10, true);
140 mat1e.set(8, 10, true);
141 mat1e.set(9, 10, true);
142 mat1e.set(5, 11, true);
143 mat1e.set(6, 11, true);
144 mat1e.set(9, 11, true);
145 TEST_ASSERT(mat1 == mat1e);
146
147 bm mat3b = o3b.getBoundaryMatrix();
148 bm mat3be(c1.size());
149 mat3be.set(0, 1, true);
150 mat3be.set(0, 2, true);
151 mat3be.set(0, 3, true);
152 mat3be.set(0, 4, true);
153 mat3be.set(1, 5, true);
154 mat3be.set(2, 5, true);
155 mat3be.set(3, 6, true);
156 mat3be.set(4, 6, true);
157 mat3be.set(4, 7, true);
158 mat3be.set(1, 7, true);
159 mat3be.set(1, 8, true);
160 mat3be.set(3, 8, true);
161 mat3be.set(6, 9, true);
162 mat3be.set(7, 9, true);
163 mat3be.set(8, 9, true);
164 mat3be.set(2, 10, true);
165 mat3be.set(3, 10, true);
166 mat3be.set(5, 11, true);
167 mat3be.set(8, 11, true);
168 mat3be.set(10, 11, true);
169 TEST_ASSERT(mat3b == mat3be);
170
171 //std::cout << mat3b << std::endl << std::endl;
172 //std::cout << mat3be << std::endl << std::endl;
173 //std::cout << ((bm::colbase) mat3b) << std::endl << std::endl;
174 //std::cout << ((bm::rowbase) mat3b) << std::endl << std::endl;
175 }
176
177 void test_matrixreduction() {
178 bm b = o3b.getBoundaryMatrix();
179 bm v = unitMatrix<bm>(b.size());
180
181 bm b_orig = b;
182 reduceBoundaryMatrix(b, v);
183 TEST_ASSERT(b == b_orig*v);
184
185 //std::cout << std::endl << "after reduce: " << std::endl;
186 //std::cout << std::endl << b << std::endl;
187 //std::cout << std::endl << v << std::endl;
188 //std::cout << std::endl << (b_orig*v) << std::endl;
189 }
190 };
191
192 #endif