The ChainCon Software (Release 0.03)
atmodel2.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// Algebraic topological model computation: Algorithm 2, new version,
6 /// with computing the transpose of the projection (faster);
7 /// combinatorial version - for coefficients in Z_2.
8 ///
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
12 //
13 // This file is part of my research software package. This is free software:
14 // you can redistribute it and/or modify it under the terms of the GNU
15 // General Public License as published by the Free Software Foundation,
16 // either version 3 of the License, or (at your option) any later version.
17 //
18 // This software is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this software; see the file "license.txt". If not,
25 // please, see <http://www.gnu.org/licenses/>.
26 
27 // Started on March 24, 2009. Last revision: March 24, 2011.
28 
29 
30 #ifndef _CHAINCON_ATMODEL2_H_
31 #define _CHAINCON_ATMODEL2_H_
32 
33 
34 // include some standard C++ header files
35 #include <istream>
36 #include <ostream>
37 
38 // include selected header files from the CHomP library
39 #include "chomp/system/config.h"
40 #include "chomp/system/textfile.h"
41 #include "chomp/system/timeused.h"
42 #include "chomp/struct/hashsets.h"
43 
44 // include relevant local header files
45 #include "chaincon/combchain.h"
46 #include "chaincon/comblinmap.h"
47 
48 
49 // --------------------------------------------------
50 // -------------------- AT model --------------------
51 // --------------------------------------------------
52 
53 /// Computes an algebraic topological model for the given
54 /// filtered finite cell complex "K".
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 algTopModel2 (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 // using chomp::homology::sseq;
70 
71  // don't do anything for an empty complex
72  if (K. empty ())
73  return;
74 
75  // show a message indicating what is being computed
76  sbug << "[AT Model Algorithm 2.]\n";
77  sout << "Computing an algebraic topological model...\n";
78 
79  // start measuring computation time
80  chomp::homology::timeused compTime;
81  compTime = 0;
82 
83  // prepare a set of cells that represent homology generators
84  chomp::homology::hashedset<CellT> homGener;
85 
86  // prepare the transpose of the projection pi
88 
89  // process all the cells in the filter
90  int_t KSize = K. size ();
91  for (int_t i = 0; i < KSize; ++ i)
92  {
93  // show progress indicator
94  if (!(i % 17))
95  scon << i << " out of " << KSize << ", " <<
96  (i * 100 / KSize) << "% done.\r";
97 
98  // retrieve the i-th cell in the filter
99  const CellT &c = K [i];
100 
101  // compute the modified cell
102  tCombChain<CellT> cChain;
103  cChain. add (c);
105  ch. add (c);
106  ch += phi (boundary (cChain, restr));
107 
108  // compute the boundary of the modified cell
109  tCombChain<CellT> bd_ch (boundary (ch, restr));
110 
111  // if the boundary is zero
112  if (bd_ch. empty ())
113  {
114  // add this cell to the set of homology representants
115  homGener. add (c);
116 
117  // define the inclusion for this cell
118  incl. add (c, ch);
119 
120  // define the projection on this cell
121  pi. add (c, c);
122  piT. add (c, c);
123 
124  // add the information to the 'seq' stream
125  // (to produce a demo on how it works)
126  // if (sseq. show || sseq. log)
127  // sseq << "add " << c << "\n";
128 
129  // skip the rest
130  continue;
131  }
132 
133  // project the boundary onto the homology generators
134  tCombChain<CellT> d (pi (bd_ch));
135  int_t dsize = d. size ();
136 
137  // make sure that this projection is nontrivial
138  if (d. empty ())
139  {
140  sout << "Debug: c = " << c << ".\n";
141  sout << "Debug: bd_c = " <<
142  boundary (cChain, restr) << ".\n";
143  sout << "Debug: ch = " << ch << ".\n";
144  sout << "Debug: bd_ch = " << bd_ch << ".\n";
145  throw "Empty boundary in the homology algorithm.";
146  }
147 
148  // choose any element of this boundary
149  const CellT &u = d. getCell (0);
150 
151  // add the information to the 'seq' stream
152  // (to produce a demo on how it works)
153  // if (sseq. show || sseq. log)
154  // sseq << "reduce " << c << " and " << u << "\n";
155 
156  // go through all the cells which have this element
157  // in their image by pi
158  tCombChain<CellT> piTu (piT (u));
159  int_t size = piTu. size ();
160  for (int_t j = 0; j < size; ++ j)
161  {
162  // retrieve the j-th cell
163  const CellT &cj = piTu. getCell (j);
164 
165  // compute new phi (cj) by adding ch to it
166  phi. add (cj, ch);
167 
168  // retrieve the image of the cell cj for editing
169  tCombChain<CellT> &picj = pi. getImage (cj);
170 
171  // remember the previous image of cj by pi
172  // tCombChain<CellT> prev (picj);
173 
174  // compute the new image of cj by the projection
175  picj += d;
176 
177  // update the transpose of the projection
178  for (int_t k = 0; k < dsize; ++ k)
179  {
180  // if this cell is in the new pi (cj)
181  const CellT &dk (d. getCell (k));
182  // int_t pos = picj. position (dk);
183  // if (pos >= 0)
184  // {
185  // if it wasn't there before
186  // if (prev. position (dk) < 0)
187  // add it to the transposed matrix
188  piT. add (dk, cj);
189  // }
190  // if this cell disapeared from pi (cj)
191  // else
192  // {
193  // remove it from the transpose of pi
194  // piT. add (dk, cj);
195  // }
196  }
197  }
198 
199  // remove the cell from the set of homology representants
200  homGener. remove (u);
201  incl. remove (u);
202  piT. remove (u);
203  }
204 
205  // copy the computed homology generators
206  int_t genSize = homGener. size ();
207  for (int_t i = 0; i < genSize; ++ i)
208  H. push_back (homGener [i]);
209 
210  // show information on the computation time
211  sout << "AT model computed in " << compTime << ".\n";
212 
213  return;
214 } /* algTopModel2 */
215 
216 
217 #endif // _CHAINCON_ATMODEL2_H_
218 
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
void algTopModel2(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 the given filtered finite cell complex "K"...
Definition: atmodel2.h:60
A combinatorial linear map.
Definition: comblinmap.h:56