Add tests for matrix reduction of complex examples
[libstick.git] / tests / persistence.h
1 #ifndef persistence_h_Fahlaewahgaebaqu
2 #define persistence_h_Fahlaewahgaebaqu
3
4 #include <cpptest.h>
5 #include <cpptest-suite.h>
6
7 #include <libstick-0.1/persistence.h>
8
9 using namespace libstick;
10
11
12 class persistence_TestSuite: public Test::Suite {
13
14 private:
15 typedef simplicial_complex<3, uint32_t, double> scomplex;
16 typedef persistence<3, uint32_t, double> pers;
17 typedef pers::boundary_matrix bm;
18 typedef pers::transformation_matrix tm;
19
20 bool setupcalled;
21 scomplex ball, ring, torus;
22 scomplex::simplex_order oball, oring, otorus;
23
24 public:
25 persistence_TestSuite() :
26 setupcalled(false),
27 oball(ball),
28 oring(ring),
29 otorus(torus)
30 {
31 TEST_ADD(persistence_TestSuite::test_matrix_reduction);
32 }
33
34 protected:
35 virtual void setup() {
36 if (setupcalled)
37 return;
38 setupcalled = true;
39
40 // 7 ------------------------------------- 8
41 // \ /
42 // \ 4 ------------------------- 5 /
43 // \ \ / /
44 // \ \ 1 ------- 2 / /
45 // \ \ \ / / /
46 // \ \ \ / / /
47 // \ \ \ / / /
48 // \ \ \ / / /
49 // \ \ 3 / /
50 // \ \ / /
51 // \ \ / /
52 // \ \ / /
53 // \ \ / /
54 // \ \ / /
55 // \ \ / /
56 // \ 6 /
57 // \ /
58 // \ /
59 // \ /
60 // 9
61
62 scomplex::Simplex ssring[] = {
63 // dimension, faces, value...
64 /* 1 */ {0, {}, 1},
65 /* 2 */ {0, {}, 2},
66 /* 3 */ {0, {}, 3},
67 /* 4 */ {0, {}, 4},
68 /* 5 */ {0, {}, 5},
69 /* 6 */ {0, {}, 6},
70 /* 7 */ {1, {2, 3}, 6.01},
71 /* 8 */ {1, {3, 1}, 6.02},
72 /* 9 */ {1, {1, 2}, 6.03},
73 /* 10 */ {1, {4, 5}, 6.04},
74 /* 11 */ {1, {5, 6}, 6.05},
75 /* 12 */ {1, {6, 4}, 6.06},
76 /* 13 */ {1, {1, 4}, 6.07},
77 /* 14 */ {1, {1, 5}, 6.08},
78 /* 15 */ {2, {13, 14, 10}, 6.0801},
79 /* 16 */ {1, {3, 6}, 6.09},
80 /* 17 */ {1, {2, 5}, 6.10},
81 /* 18 */ {1, {2, 6}, 6.11},
82 /* 19 */ {2, {9, 14, 17}, 6.1101},
83 /* 20 */ {2, {7, 16, 18}, 6.1102},
84 /* 21 */ {2, {11, 17, 18}, 6.1103},
85 /* 22 */ {1, {1, 6}, 6.12},
86 /* 23 */ {2, {12, 13, 22}, 6.1201},
87 /* 24 */ {2, {8, 16, 22}, 6.1202}
88 };
89 const size_t cntssring = sizeof(ssring)/sizeof(scomplex::Simplex);
90 ring.add_simplices(ssring, cntssring);
91 oring.reset();
92
93 scomplex::Simplex sstorus[] = {
94 // dimension, faces, value...
95 /* 25 */ {0, {}, 7},
96 /* 26 */ {0, {}, 8},
97 /* 27 */ {0, {}, 9},
98 /* 28 */ {1, {25, 26}, 9.01},
99 /* 29 */ {1, {26, 27}, 9.02},
100 /* 30 */ {1, {25, 27}, 9.03},
101 /* 31 */ {1, {25, 1}, 9.04},
102 /* 32 */ {1, {26, 1}, 9.05},
103 /* 33 */ {2, {28, 31, 32}, 9.0501},
104 /* 34 */ {1, {27, 1}, 9.06},
105 /* 35 */ {2, {30, 31, 34}, 9.0601},
106 /* 36 */ {1, {27, 3}, 9.07},
107 /* 37 */ {2, {36, 34, 8}, 9.0701},
108 /* 38 */ {1, {27, 2}, 9.08},
109 /* 39 */ {1, {26, 2}, 9.09},
110 /* 40 */ {2, {9, 32, 39}, 9.0901},
111 /* 41 */ {2, {29, 38, 39}, 9.0902},
112 /* 42 */ {2, {7, 38, 36}, 9.0903},
113 /* 43 */ {1, {4, 25}, 9.10},
114 /* 44 */ {1, {5, 26}, 9.11},
115 /* 45 */ {1, {6, 27}, 9.12},
116 /* 46 */ {1, {4, 26}, 9.13},
117 /* 47 */ {1, {4, 27}, 9.14},
118 /* 48 */ {1, {5, 27}, 9.15},
119 /* 49 */ {2, {43, 47, 30}, 9.1501},
120 /* 50 */ {2, {12, 45, 47}, 9.1502},
121 /* 51 */ {2, {29, 44, 48}, 9.1503},
122 /* 52 */ {2, {48, 11, 45}, 9.1504},
123 /* 53 */ {2, {10, 46, 44}, 9.1505},
124 /* 54 */ {2, {43, 46, 28}, 9.1506},
125 };
126 const size_t cntsstorus = sizeof(sstorus)/sizeof(scomplex::Simplex);
127 torus = ring;
128 torus.add_simplices(sstorus, cntsstorus);
129 otorus.reset();
130
131 scomplex::Simplex ssball[] = {
132 // dimension, faces, value...
133 {0, {}, 1},
134 {0, {}, 2},
135 {0, {}, 3},
136 {0, {}, 4},
137 {0, {}, 5},
138 {1, {1, 2}, 6},
139 {1, {2, 3}, 7},
140 {1, {3, 4}, 8},
141 {1, {4, 1}, 9},
142 {1, {1, 5}, 10},
143 {1, {2, 5}, 11},
144 {1, {3, 5}, 12},
145 {1, {4, 5}, 13},
146 {2, {6, 10, 11}, 14},
147 {2, {7, 11, 12}, 15},
148 {2, {8, 12, 13}, 16},
149 {2, {9, 13, 10}, 17},
150 {1, {1, 3}, 18},
151 {2, {18, 6, 7}, 19},
152 {2, {18, 8, 9}, 20},
153 {2, {18, 10, 12}, 21},
154 {3, {21, 14, 15, 19}, 22},
155 {3, {21, 16, 17, 20}, 23},
156 };
157 const size_t cntssball = sizeof(ssball)/sizeof(scomplex::Simplex);
158 ball.add_simplices(ssball, cntssball);
159 oball.reset();
160 }
161
162 virtual void tear_down() {
163 }
164
165 void test_matrix_reduction() {
166 pers ringp(oring);
167 const bm &ringbm = ringp.get_boundary_matrix();
168 const bm &ringrbm = ringp.get_reduced_boundary_matrix();
169 const tm &ringtm = ringp.get_transformation_matrix();
170 TEST_ASSERT(ringrbm == ringbm * ringtm);
171
172 pers torusp(otorus);
173 const bm &torusbm = torusp.get_boundary_matrix();
174 const bm &torusrbm = torusp.get_reduced_boundary_matrix();
175 const tm &torustm = torusp.get_transformation_matrix();
176 TEST_ASSERT(torusrbm == torusbm * torustm);
177
178 uint32_t torusrbme_coords[][2] = {
179 {0, 1}, {2, 7}, {3, 7}, {1, 8}, {2, 8}, {4, 10}, {5, 10}, {4, 11}, {6, 11}, {1, 13},
180 {4, 13}, {10, 15}, {13, 15}, {14, 15}, {9, 19}, {10, 19}, {13, 19}, {17, 19}, {7, 20},
181 {16, 20}, {18, 20}, {7, 21}, {9, 21}, {10, 21}, {11, 21}, {13, 21}, {16, 21}, {12, 23},
182 {13, 23}, {22, 23}, {7, 24}, {8, 24}, {9, 24}, {10, 24}, {11, 24}, {12, 24}, {25, 28},
183 {26, 28}, {25, 29}, {27, 29}, {1, 31}, {25, 31}, {28, 33}, {31, 33}, {32, 33}, {30, 35},
184 {31, 35}, {34, 35}, {8, 37}, {30, 37}, {31, 37}, {36, 37}, {9, 40}, {28, 40}, {31, 40},
185 {39, 40}, {9, 41}, {28, 41}, {29, 41}, {31, 41}, {38, 41}, {7, 42}, {8, 42}, {9, 42},
186 {28, 42}, {29, 42}, {30, 42}, {7, 49}, {8, 49}, {9, 49}, {28, 49}, {29, 49}, {43, 49},
187 {47, 49}, {10, 50}, {11, 50}, {28, 50}, {29, 50}, {43, 50}, {45, 50}, {29, 51}, {44, 51},
188 {48, 51}, {10, 52}, {28, 52}, {43, 52}, {44, 52}, {28, 53}, {43, 53}, {46, 53 }
189 };
190 bm torusrbme(torusbm.size());
191 torusrbme.set_all(torusrbme_coords, sizeof(torusrbme_coords)/(2*sizeof(uint32_t)), true);
192 TEST_ASSERT(torusrbme == torusrbm);
193
194
195 pers ballp(oball);
196 const bm &ballbm = ballp.get_boundary_matrix();
197 const bm &ballrbm = ballp.get_reduced_boundary_matrix();
198 const tm &balltm = ballp.get_transformation_matrix();
199 TEST_ASSERT(ballrbm == ballbm * balltm);
200
201 uint32_t ballrbme_coords[][2] = {
202 {0, 1}, {1, 6}, {2, 6}, {1, 7}, {3, 7}, {1, 8}, {4, 8}, {1, 10}, {5, 10}, {6, 14},
203 {10, 14}, {11, 14}, {6, 15}, {7, 15}, {10, 15}, {12, 15}, {6, 16}, {7, 16}, {8, 16},
204 {10, 16}, {13, 16}, {6, 17}, {7, 17}, {8, 17}, {9, 17}, {6, 19}, {7, 19}, {18, 19},
205 {14, 22}, {15, 22}, {19, 22}, {21, 22}, {14, 23}, {15, 23}, {16, 23}, {17, 23}, {19, 23},
206 {20, 23 }
207 };
208 bm ballrbme(ballrbm.size());
209 ballrbme.set_all(ballrbme_coords, sizeof(ballrbme_coords)/(2*sizeof(uint32_t)), true);
210 TEST_ASSERT(ballrbme == ballrbm);
211
212 //std::cout << std::endl;
213 //ballp.interpret_reduction(std::cout);
214 }
215 };
216
217 #endif