1 #ifndef simplicialcomplex_h_nealaezeojeeChuh
2 #define simplicialcomplex_h_nealaezeojeeChuh
13 #include <libstick-0.1/booleanmatrix.h>
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 SimplicialComplex 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 SimplexOrder gives the extended boundary
26 template<int MAXDIM
, class IT
=uint32_t, class VT
=double>
27 class SimplicialComplex
{
30 /** The type of this class. */
31 typedef SimplicialComplex
<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
;
38 /** A simplex of the complex. */
40 /** Dimension of the simplex. */
42 /** The indices of the faces of the simplex. */
43 index_type faces
[MAXDIM
+1];
44 /** The value of the simplex. */
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
);
58 memcpy(s
.faces
, faces
, faceCountByDim(dim
)*sizeof(index_type
));
65 /** Create a (-1)-dimensional simplex. It has the lowest possible value. */
66 static Simplex
create_minusonedim_simplex() {
71 s
.value
= std::numeric_limits
<VT
>::has_infinity
72 ? -std::numeric_limits
<VT
>::infinity()
73 : std::numeric_limits
<VT
>::min();
78 /** Get number of faces. */
79 size_t faceCount() const {
80 return faceCountByDim(dim
);
83 /** Get number of faces of a dim-dimensional simplex. */
84 static size_t faceCountByDim(int dim
) {
85 assert(-1 <= dim
&& dim
<= MAXDIM
);
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. */
95 typedef BooleanColRowMatrix
<IT
> BoundaryMatrix
;
97 /** Create a standard order of the complex c, i.e., the identity permutation. */
98 SimplexOrder(const simplcompltype
&c
) :
104 /** Reset order to the identity permutation of the complex's simplices. */
107 for (unsigned i
=0; i
< c
.size(); ++i
)
112 /** Return number of simplices. */
113 size_t size() const {
114 assert(order
.size() == revorder
.size());
118 /** Get i-th simplex in the simplex order. */
119 const Simplex
& getSimplex(size_t i
) const {
121 return c
.simplices
[order
.at(i
)];
124 /** Returns true iff the faces of simplex i are before i in this order. */
125 bool isFiltration() const {
126 assert(size() == c
.size());
128 for (unsigned i
=0; i
< size(); ++i
)
129 for (unsigned f
=0; f
< getSimplex(i
).faceCount(); ++f
)
130 if (revorder
[getSimplex(i
).faces
[f
]] >= i
)
136 /** Returns true iff isFiltration() gives true and values of simplices
137 * are monotone w.r.t. this order of simplices. */
138 bool isMonotone() const {
139 assert(size() == c
.size());
141 for (unsigned i
=1; i
< size(); ++i
)
142 if (getSimplex(i
-1).value
> getSimplex(i
).value
)
145 return isFiltration();
148 /** Sort simplices such that isMonotone() gives true. This
149 * requires that the complex's isMonotone() gave true
151 void makeMonotoneFiltration() {
152 assert(c
.isMonotone());
154 sort(order
.begin(), order
.end(), cmpMonotoneFiltration(c
));
155 restoreRevorderFromOrder();
157 assert(c
.isMonotone());
158 assert(isFiltration());
159 assert(isMonotone());
162 /** Get the boundary matrix of the complex according to this order. */
163 BoundaryMatrix
getBoundaryMatrix() const {
164 BoundaryMatrix
mat(size());
166 for (unsigned c
=0; c
< size(); ++c
)
167 for(unsigned r
=0; r
< getSimplex(c
).faceCount(); ++r
)
168 mat
.set(revorder
[getSimplex(c
).faces
[r
]], c
, true);
174 /** Reconstruct 'revorder' by inverting the permutation given by 'order'. */
175 void restoreRevorderFromOrder() {
176 // Make revorder * order the identity permutation
177 for (unsigned i
=0; i
< size(); ++i
)
178 revorder
[order
[i
]] = i
;
181 /** The complex of which we consider a simplex order. */
182 const simplcompltype
&c
;
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
;
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
;
196 SimplicialComplex() {
197 // Add the one minus-one dimensional simplex
198 addSimplex(Simplex::create_minusonedim_simplex());
201 /** Return number of simplices. */
202 size_t size() const {
203 return simplices
.size();
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 addSimplex(int dim
, index_type
* faces
, value_type value
) {
209 addSimplex(Simplex::create(dim
, faces
, value
));
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 addSimplex(Simplex s
) {
215 // Check requirements for faces
216 for (unsigned i
=0; i
< s
.faceCount(); ++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);
223 simplices
.push_back(s
);
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 isComplex() const {
229 for (unsigned i
=0; i
< size(); ++i
) {
231 const Simplex
&s
= simplices
[i
];
232 for (unsigned f
=0; f
< s
.faceCount(); ++f
) {
234 if (s
.faces
[f
] >= size())
237 const Simplex
&face
= simplices
[s
.faces
[f
]];
238 if (face
.dim
!= s
.dim
-1)
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 isComplex() gives true. */
248 bool isMonotone() const {
251 typename
std::vector
<Simplex
>::const_iterator it
= ++simplices
.begin();
252 for (; it
!= simplices
.end(); ++it
)
253 for (unsigned f
=0; f
< it
->faceCount(); ++f
)
254 if (simplices
[it
->faces
[f
]].value
> it
->value
)
261 /** Compares (operator<) two simplices (i.e. indices) in a
262 * SimplexOrder w.r.t. lexicographical order on (value,
263 * dimension)-tuples. */
264 struct cmpMonotoneFiltration
{
265 const SimplicialComplex
&c
;
267 cmpMonotoneFiltration(const SimplicialComplex
&c
) :
271 bool operator()(index_type i
, index_type j
) {
272 const Simplex
& si
= c
.simplices
[i
];
273 const Simplex
& sj
= c
.simplices
[j
];
275 if (si
.value
< sj
.value
)
277 else if (si
.value
== sj
.value
)
278 return si
.dim
< sj
.dim
;
285 /** A list of simplices */
286 std::vector
<Simplex
> simplices
;