Coding style cleanup
[libstick.git] / include / libstick-0.1 / simplicialcomplex.h
1 #ifndef simplicialcomplex_h_nealaezeojeeChuh
2 #define simplicialcomplex_h_nealaezeojeeChuh
3
4 #include <stdint.h>
5 #include <cstdlib>
6 #include <cstring>
7 #include <algorithm>
8 #include <vector>
9 #include <limits>
10
11 #include <iostream>
12
13 #include <libstick-0.1/booleanmatrix.h>
14
15
16 namespace libstick {
17
18 /** A simplicial complex is a std::vector of simplices such that each face is
19 * also part of the complex. Every simplex has dimension at most MAXDIM. The
20 * indices of simplices resp. their faces are of type IT. To each simplex a
21 * value is assigend, which is of type VT. When a simplicial_complex is
22 * instantiated, a single (-1) dimensional simplex is automatically created.
23 * Each 0-dimensional simplex automatically has this simplex as its face.
24 * Consequently, the innner class simplex_order gives the extended boundary
25 * matrix. */
26 template<int MAXDIM, class IT=uint32_t, class VT=double>
27 class simplicial_complex {
28
29 public:
30 /** The type of this class. */
31 typedef simplicial_complex<MAXDIM, IT, VT> simplcompltype;
32 /** Type of indices of simplices. */
33 typedef IT index_type;
34 /** To every simplex a function value is assigned according to which a
35 * filtration is considered. This is the value type of the function. */
36 typedef VT value_type;
37
38 /** A simplex of the complex. */
39 struct Simplex {
40 /** Dimension of the simplex. */
41 int dim;
42 /** The indices of the faces of the simplex. */
43 index_type faces[MAXDIM+1];
44 /** The value of the simplex. */
45 value_type value;
46
47 /** Create a new simplex with dimension 'dim', (dim+1)-faces and
48 * its value. If simpley is 0-dimensional, its face is
49 * automatically set to one (-1)-dimensional simplex. */
50 static Simplex create(int dim, index_type* faces, value_type value) {
51 assert(0 <= dim && dim <= MAXDIM);
52
53 Simplex s;
54 s.dim = dim;
55 s.value = value;
56
57 if (dim > 0)
58 memcpy(s.faces, faces, face_count_bydim(dim)*sizeof(index_type));
59 else
60 s.faces[0] = 0;
61
62 return s;
63 }
64
65 /** Create a (-1)-dimensional simplex. It has the lowest possible value. */
66 static Simplex create_minusonedim_simplex() {
67 Simplex s;
68
69 s.dim = -1;
70 s.faces[0] = 0;
71 s.value = std::numeric_limits<VT>::has_infinity
72 ? -std::numeric_limits<VT>::infinity()
73 : std::numeric_limits<VT>::min();
74
75 return s;
76 }
77
78 /** Get number of faces. */
79 size_t face_count() const {
80 return face_count_bydim(dim);
81 }
82
83 /** Get number of faces of a dim-dimensional simplex. */
84 static size_t face_count_bydim(int dim) {
85 assert(-1 <= dim && dim <= MAXDIM);
86 return dim + 1;
87 }
88 };
89
90 /** An order of the simplices of complex c. An order can be interpreted
91 * as a permuation of the complex's std::vector of simplices. */
92 class simplex_order {
93
94 public:
95 typedef boolean_colrowmatrix<IT> boundary_matrix;
96
97 /** Create a standard order of the complex c, i.e., the identity permutation. */
98 simplex_order(const simplcompltype &c) :
99 c(c)
100 {
101 reset();
102 }
103
104 /** Reset order to the identity permutation of the complex's simplices. */
105 void reset() {
106 order.clear();
107 for (unsigned i=0; i < c.size(); ++i)
108 order.push_back(i);
109 revorder = order;
110 }
111
112 /** Return number of simplices. */
113 size_t size() const {
114 assert(order.size() == revorder.size());
115 return order.size();
116 }
117
118 /** Get i-th simplex in the simplex order. */
119 const Simplex& get_simplex(size_t i) const {
120 assert(i < size());
121 return c.simplices[order.at(i)];
122 }
123
124 /** Returns true iff the faces of simplex i are before i in this order. */
125 bool is_filtration() const {
126 assert(size() == c.size());
127
128 for (unsigned i=0; i < size(); ++i)
129 for (unsigned f=0; f < get_simplex(i).face_count(); ++f)
130 if (revorder[get_simplex(i).faces[f]] >= i)
131 return false;
132
133 return true;
134 }
135
136 /** Returns true iff is_filtration() gives true and values of simplices
137 * are monotone w.r.t. this order of simplices. */
138 bool is_monotone() const {
139 assert(size() == c.size());
140
141 for (unsigned i=1; i < size(); ++i)
142 if (get_simplex(i-1).value > get_simplex(i).value)
143 return false;
144
145 return is_filtration();
146 }
147
148 /** Sort simplices such that is_monotone() gives true. This
149 * requires that the complex's is_monotone() gave true
150 * beforehand.*/
151 void make_monotone_filtration() {
152 assert(c.is_monotone());
153
154 sort(order.begin(), order.end(), cmp_monotone_filtration(c));
155 restore_revorder_from_order();
156
157 assert(c.is_monotone());
158 assert(is_filtration());
159 assert(is_monotone());
160 }
161
162 /** Get the boundary matrix of the complex according to this order. */
163 boundary_matrix get_boundary_matrix() const {
164 boundary_matrix mat(size());
165
166 for (unsigned c=0; c < size(); ++c)
167 for(unsigned r=0; r < get_simplex(c).face_count(); ++r)
168 mat.set(revorder[get_simplex(c).faces[r]], c, true);
169
170 return mat;
171 }
172
173 private:
174 /** Reconstruct 'revorder' by inverting the permutation given by 'order'. */
175 void restore_revorder_from_order() {
176 // Make revorder * order the identity permutation
177 for (unsigned i=0; i < size(); ++i)
178 revorder[order[i]] = i;
179 }
180
181 /** The complex of which we consider a simplex order. */
182 const simplcompltype &c;
183
184 /** The i-th simplex in order is the order[i]-th simplex of the
185 * complex. 'order' can be seen as a permutation of the
186 * simplices saved in 'c'. */
187 std::vector<index_type> order;
188
189 /** The i-th simplex in the complex is the revorder[i]-th
190 * simplex in order. 'revorder' can be seen as the inverse
191 * permutation saved in 'order'. */
192 std::vector<index_type> revorder;
193 };
194
195 public:
196 simplicial_complex() {
197 // Add the one minus-one dimensional simplex
198 add_simplex(Simplex::create_minusonedim_simplex());
199 }
200
201 /** Return number of simplices. */
202 size_t size() const {
203 return simplices.size();
204 }
205
206 /** Add a simplex to the complex. The dimension of the faces must be
207 * dim-1, and they must already be part of the complex. */
208 void add_simplex(int dim, index_type* faces, value_type value) {
209 add_simplex(Simplex::create(dim, faces, value));
210 }
211
212 /** Add a simplex to the complex. The dimension of the faces must be
213 * dim-1, and they must already be part of the complex. */
214 void add_simplex(Simplex s) {
215 // Check requirements for faces
216 for (unsigned i=0; i < s.face_count(); ++i) {
217 // Faces are already in complex.
218 assert(s.faces[i] < size());
219 // Faces have dimension dim-1
220 assert(simplices[s.faces[i]].dim == s.dim-1);
221 }
222
223 simplices.push_back(s);
224 }
225
226 /** Return true iff for each simplex i with dimension dim it holds that
227 * the faces of i are contained in the complex and have dimension dim-1. */
228 bool is_complex() const {
229 for (unsigned i=0; i < size(); ++i) {
230
231 const Simplex &s = simplices[i];
232 for (unsigned f=0; f < s.face_count(); ++f) {
233
234 if (s.faces[f] >= size())
235 return false;
236
237 const Simplex &face = simplices[s.faces[f]];
238 if (face.dim != s.dim-1)
239 return false;
240 }
241 }
242 return true;
243 }
244
245 /** Returns true iff simplex's values are monotone w.r.t.
246 * face-inclusion, i.e., for each simplex its value is not smaller than
247 * the values of its faces. Requires that is_complex() gives true. */
248 bool is_monotone() const {
249 assert(is_complex());
250
251 typename std::vector<Simplex>::const_iterator it = ++simplices.begin();
252 for (; it != simplices.end(); ++it)
253 for (unsigned f=0; f < it->face_count(); ++f)
254 if (simplices[it->faces[f]].value > it->value)
255 return false;
256
257 return true;
258 }
259
260 private:
261 /** Compares (operator<) two simplices (i.e. indices) in a
262 * simplex_order w.r.t. lexicographical order on (value,
263 * dimension)-tuples. */
264 struct cmp_monotone_filtration {
265 const simplicial_complex &c;
266
267 cmp_monotone_filtration(const simplicial_complex &c) :
268 c(c){
269 }
270
271 bool operator()(index_type i, index_type j) {
272 const Simplex& si = c.simplices[i];
273 const Simplex& sj = c.simplices[j];
274
275 if (si.value < sj.value)
276 return true;
277 else if (si.value == sj.value)
278 return si.dim < sj.dim;
279 else
280 return false;
281 }
282 };
283
284 public:
285 /** A list of simplices */
286 std::vector<Simplex> simplices;
287 };
288
289 }
290
291
292 #endif