The ChainCon Software (Release 0.03)
atmodel0.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// Algebraic topological model computation: Algorithm 0, very slow;
6 /// combinatorial version - for coefficients in Z_2.
7 ///
8 /////////////////////////////////////////////////////////////////////////////
9 
10 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
11 //
12 // This file is part of my research software package. This is free software:
13 // you can redistribute it and/or modify it under the terms of the GNU
14 // General Public License as published by the Free Software Foundation,
15 // either version 3 of the License, or (at your option) any later version.
16 //
17 // This software is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this software; see the file "license.txt". If not,
24 // please, see <http://www.gnu.org/licenses/>.
25 
26 // Started on March 24, 2009. Last revision: March 24, 2011.
27 
28 
29 #ifndef _CHAINCON_ATMODEL0_H_
30 #define _CHAINCON_ATMODEL0_H_
31 
32 
33 // include some standard C++ header files
34 #include <istream>
35 #include <ostream>
36 
37 // include selected header files from the CHomP library
38 #include "chomp/system/config.h"
39 #include "chomp/system/textfile.h"
40 #include "chomp/system/timeused.h"
41 #include "chomp/struct/hashsets.h"
42 
43 // include relevant local header files
44 #include "chaincon/combchain.h"
45 #include "chaincon/comblinmap.h"
46 
47 
48 // --------------------------------------------------
49 // -------------------- AT model --------------------
50 // --------------------------------------------------
51 
52 /// Computes an algebraic topological model for a given
53 /// filtered finite cell complex "K".
54 /// This is an old version of the algorithm, very slow.
55 /// Cells that represent homology generators are appended to the vector "H".
56 /// The projection map "pi", the inclusion from "H" to the complex "K",
57 /// and the homology gradient vector field "phi"
58 /// are assumed to be initially zero and are constructed.
59 template <class CellT, class CellArray1, class CellArray2, class CellRestrT>
60 void algTopModel0 (const CellArray1 &K, CellArray2 &H,
64  const CellRestrT &restr)
65 {
66  using chomp::homology::sout;
67  using chomp::homology::scon;
68  using chomp::homology::sbug;
69 
70  // don't do anything for an empty complex
71  if (K. empty ())
72  return;
73 
74  // show a message indicating what is being computed
75  sbug << "[AT Model Algorithm 0.]\n";
76  sout << "Computing an algebraic topological model...\n";
77 
78  // start measuring computation time
79  chomp::homology::timeused compTime;
80  compTime = 0;
81 
82  // prepare a set of cells that represent homology generators
83  chomp::homology::hashedset<CellT> homGener;
84 
85  // process all the cells in the filter
86  int_t KSize = K. size ();
87  for (int_t i = 0; i < KSize; ++ i)
88  {
89  // show progress indicator
90  if (!(i % 17))
91  scon << i << " out of " << KSize << ", " <<
92  (i * 100 / KSize) << "% done.\r";
93 
94  // retrieve the i-th cell in the filter
95  const CellT &c = K [i];
96 
97  // compute the modified cell
98  tCombChain<CellT> cChain;
99  cChain. add (c);
100  tCombChain<CellT> ch (phi (boundary (cChain, restr)));
101  ch. add (c);
102 
103  // compute the boundary of the modified cell
104  tCombChain<CellT> bd_ch (boundary (ch, restr));
105 
106  // if the boundary is zero
107  if (bd_ch. empty ())
108  {
109  // add this cell to the set of homology representants
110  homGener. add (c);
111 
112  // define the inclusion for this cell
113  incl. add (c, ch);
114 
115  // define the projection on this cell
116  pi. add (c, c);
117 
118  // skip the rest
119  continue;
120  }
121 
122  // project the boundary onto the homology generators
123  tCombChain<CellT> d (pi (bd_ch));
124 
125  // make sure that this projection is nontrivial
126  if (d. empty ())
127  {
128  sout << "Debug: c = " << c << ".\n";
129  sout << "Debug: bd_c = " <<
130  boundary (cChain, restr) << ".\n";
131  sout << "Debug: ch = " << ch << ".\n";
132  sout << "Debug: bd_ch = " << bd_ch << ".\n";
133  throw "Empty boundary in the homology algorithm.";
134  }
135 
136  // choose any element of this boundary
137  const CellT &u = d. getCell (0);
138 
139  // prepare a new phi
141 
142  // prepare a new projection pi
144 
145  // go through all the previously processed cells
146  for (int_t j = 0; j < i; ++ j)
147  {
148  // retrieve the j-th cell
149  const CellT &cj = K [j];
150 
151  // compute the chain pi (cj + phi bd cj + bd phi cj)
152  tCombChain<CellT> cjChain;
153  cjChain. add (cj);
155  (phi (boundary (cjChain, restr)));
156  cjh += boundary (phi (cj), restr);
157  cjh. add (cj);
158  tCombChain<CellT> xi (pi (cjh));
159 
160  // if u does not appear in this chain
161  // then skip the remaining computations for cj
162  int_t pos = xi. position (u);
163  if (pos < 0)
164  continue;
165 
166  // compute new phi (cj) by adding ch to it
167  phiAdd. add (cj, ch);
168 
169  // compute the new image of cj by the projection
170  tCombChain<CellT> combination
171  (phi (boundary (cjChain, restr)));
172  combination += boundary (phi (cj), restr);
173  combination += phiAdd (boundary (cjChain, restr));
174  combination += boundary (phiAdd (cj), restr);
175  piAdd. add (cj, pi (combination));
176  }
177 
178  // === DEBUG VERIFICATION BEGIN ===
179  // prepare a new projection pi - to test if this is the same
180  tCombLinMap<CellT,CellT> piAddTest;
181 
182  // go through all the cells again and compute new pi
183  for (int_t j = 0; j < i; ++ j)
184  {
185  // retrieve the j-th cell
186  const CellT &cj = K [j];
187 
188  // compute xi = pi (cj + phi bd cj + bd phi cj)
189  tCombChain<CellT> cjh (boundary (phi (cj), restr));
190  tCombChain<CellT> cjChain;
191  cjChain. add (cj);
192  cjh += phi (boundary (cjChain, restr));
193  cjh. add (cj);
194  tCombChain<CellT> xi (pi (cjh));
195 
196  // if u does not appear in this chain
197  // then skip the remaining computations for cj
198  int_t pos = xi. position (u);
199  if (pos < 0)
200  continue;
201 
202  // compute the new image of cj by the projection
203  tCombChain<CellT> combination
204  (phi (boundary (cjChain, restr)));
205  combination += boundary (phi (cj), restr);
206  combination += phiAdd (boundary (cjChain, restr));
207  combination += boundary (phiAdd (cj), restr);
208  piAddTest. add (cj, pi (combination));
209  }
210 
211  // check if both versions of the new pi are the same
212  if (!(piAdd == piAddTest))
213  {
214  sout << "ERROR: The computed two versions "
215  "of the map pi are not the same!\n";
216  sout << "i = " << i << ", c_i = " << c <<
217  ", u = " << u << ".\n";
218  sout << "The wrong map piAdd:\n" << piAdd;
219  sout << "The correct map piAdd:\n" << piAddTest;
220  throw "Wrong map pi detected.\n";
221  }
222  // === DEBUG VERIFICATION END ===
223 
224  // replace the old phi with the new one
225  phi += phiAdd;
226 
227  // replace the old pi with the new one
228  pi += piAdd;
229 
230  // remove the cell from the set of homology representants
231  homGener. remove (u);
232  incl. remove (u);
233  }
234 
235  // show information on the computation time
236  sout << "AT model computed in " << compTime << ".\n";
237 
238  // copy the computed homology generators
239  int_t genSize = homGener. size ();
240  for (int_t i = 0; i < genSize; ++ i)
241  H. push_back (homGener [i]);
242 
243  return;
244 } /* algTopModel0 */
245 
246 
247 #endif // _CHAINCON_ATMODEL0_H_
248 
void algTopModel0(const CellArray1 &K, CellArray2 &H, tCombLinMap< CellT, CellT > &pi, tCombLinMap< CellT, CellT > &incl, tCombLinMap< CellT, CellT > &phi, const CellRestrT &restr)
Computes an algebraic topological model for a given filtered finite cell complex "K".
Definition: atmodel0.h:60
A combinatorial chain, that is, a chain with Z_2 coefficients.
tCombChain< CellT > boundary(const tCombChain< CellT > &c, const CellRestrT &restr)
Returns the boundary of a given chain (takes boundary cells restricted by the given object...
Definition: boundary.h:156
A combinatorial linear map (for coefficients in Z_2).
A combinatorial chain.
Definition: combchain.h:49
A combinatorial linear map.
Definition: comblinmap.h:56