The ChainCon Software (Release 0.03)
atmodel2r.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 /// version for an arbitrary commutative ring of coefficients.
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_ATMODEL2R_H_
31 #define _CHAINCON_ATMODEL2R_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/chain.h"
46 #include "chaincon/linmap.h"
47 #include "chaincon/combchain.h"
48 #include "chaincon/comblinmap.h"
49 
50 
51 // --------------------------------------------------
52 // -------------------- AT model --------------------
53 // --------------------------------------------------
54 
55 /// Computes an algebraic topological model for the given
56 /// filtered finite cell complex "K".
57 /// Cells that represent homology generators are appended to the vector "H".
58 /// The projection map "pi", the inclusion from "H" to the complex "K",
59 /// and the homology gradient vector field "phi"
60 /// are assumed to be initially zero and are constructed.
61 template <class CellT, class CoefT, class CellArray1, class CellArray2,
62  class CellRestrT>
63 void algTopModel2 (const CellArray1 &K, CellArray2 &H,
67  const CellRestrT &restr)
68 {
69  using chomp::homology::sout;
70  using chomp::homology::scon;
71  using chomp::homology::sbug;
72 
73  // don't do anything for an empty complex
74  if (K. empty ())
75  return;
76 
77  // show a message indicating what is being computed
78  sbug << "[AT Model Algorithm 2r.]\n";
79  sout << "Computing an algebraic topological model...\n";
80 
81  // start measuring computation time
82  chomp::homology::timeused compTime;
83  compTime = 0;
84 
85  // prepare a set of cells that represent homology generators
86  chomp::homology::hashedset<CellT> homGener;
87 
88  // prepare a combinatorial version of the transpose
89  // of the projection pi, which is a matrix that stores
90  // all the cells that have a given cell in their images by pi
92 
93  // process all the remaining cells in the filter
94  int_t KSize = K. size ();
95  for (int_t i = 0; i < KSize; ++ i)
96  {
97  // show progress indicator
98  if (!(i % 17))
99  scon << i << " out of " << KSize << ", " <<
100  (i * 100 / KSize) << "% done.\r";
101 
102  // retrieve the i-th cell in the filter
103  const CellT &c = K [i];
104  // sout << "\nDEBUG: i = " << i << ", c = " << c << ".\n";
105 
106  // compute the modified cell
107  tChain<CellT,CoefT> cChain;
108  cChain. add (c, CoefT (1));
110  ch. add (c, CoefT (1));
111  ch. negate ();
112  ch += phi (boundary (cChain, restr));
113  // sout << "DEBUG: bd_c = " << boundary (cChain, restr) << ".\n";
114  ch. negate ();
115  // sout << "DEBUG: ch = c - phi (bd (c)) = " << ch << ".\n";
116 
117  // compute the boundary of the modified cell
118  tChain<CellT,CoefT> bd_ch (boundary (ch, restr));
119  // sout << "DEBUG: bd_ch = " << bd_ch << ".\n";
120 
121  // if the boundary is zero
122  if (bd_ch. empty ())
123  {
124  // add this cell to the set of homology representants
125  homGener. add (c);
126 
127  // define the inclusion for this cell
128  incl. add (c, ch);
129 
130  // define the projection on this cell
131  pi. add (c, c, CoefT (1));
132 
133  // add the cell to the list in the transpose
134  piT. add (c, c);
135 
136  // skip the rest
137  continue;
138  }
139 
140  // project the boundary onto the homology generators
141  tChain<CellT,CoefT> d (pi (bd_ch));
142  int_t dsize = d. size ();
143  // sout << "DEBUG: d = pi (bd_ch) = " << d << ".\n";
144 
145  // make sure that this projection is nontrivial
146  if (d. empty ())
147  {
148  sout << "Debug: c = " << c << ".\n";
149  sout << "Debug: bd_c = " <<
150  boundary (cChain, restr) << ".\n";
151  sout << "Debug: ch = " << ch << ".\n";
152  sout << "Debug: bd_ch = " << bd_ch << ".\n";
153  throw "Empty boundary in the homology algorithm 2r.";
154  }
155 
156  // choose any element of this boundary
157  const CellT &u = d. getCell (0);
158 
159  // compute the inverse of its coefficient
160  CoefT lambdaInv (d. getCoef (0));
161  lambdaInv. invert ();
162 
163  // go through all the cells which have this element
164  // in their image by pi
165  tCombChain<CellT> piTu (piT (u));
166  int_t size = piTu. size ();
167  for (int_t j = 0; j < size; ++ j)
168  {
169  // retrieve the j-th cell
170  const CellT &cj = piTu. getCell (j);
171 
172  // retrieve the image of the cell cj for editing
173  tChain<CellT,CoefT> &picj = pi. getImage (cj);
174 
175  // determine the coefficient to use
176  int_t pos = picj. position (u);
177  if (pos < 0)
178  throw "ATmodel: u not found in pi(cj).";
179  CoefT etaLambdaInv (picj. getCoef (pos));
180  etaLambdaInv *= lambdaInv;
181 
182  // compute new phi (cj)
183  tChain<CellT,CoefT> elich (ch);
184  elich *= etaLambdaInv;
185  phi. add (cj, elich);
186  // sout << "DEBUG: phi (" << cj << ") := " <<
187  // phi (cj) << ".\n";
188 
189  // remember the previous image of cj by pi
190  tChain<CellT,CoefT> prev (picj);
191 
192  // compute the new image of cj by the projection
193  tChain<CellT,CoefT> melid (d);
194  etaLambdaInv. negate ();
195  melid *= etaLambdaInv;
196  picj += melid;
197  // sout << "DEBUG: pi(" << cj << "): " << prev <<
198  // " -> " << picj << ".\n";
199 
200  // update the transpose of the projection
201  for (int_t k = 0; k < dsize; ++ k)
202  {
203  // if this cell is in the new pi (cj)
204  const CellT &dk (d. getCell (k));
205  bool newContains =
206  (picj. position (dk) >= 0);
207  bool prevContains =
208  (prev. position (dk) >= 0);
209  if (newContains != prevContains)
210  piT. add (dk, cj);
211  }
212  }
213 
214  // remove the cell from the set of homology representants
215  homGener. remove (u);
216  incl. remove (u);
217  piT. remove (u);
218  }
219 
220  // copy the computed homology generators
221  int_t genSize = homGener. size ();
222  for (int_t i = 0; i < genSize; ++ i)
223  H. push_back (homGener [i]);
224 
225  // show information on the computation time
226  sout << "AT model computed in " << compTime << ".\n";
227 
228  return;
229 } /* algTopModel2 */
230 
231 
232 #endif // _CHAINCON_ATMODEL2R_H_
233 
A linear map for coefficients in an arbitrary commutative ring.
void algTopModel2(const CellArray1 &K, CellArray2 &H, tLinMap< CellT, CellT, CoefT > &pi, tLinMap< CellT, CellT, CoefT > &incl, tLinMap< CellT, CellT, CoefT > &phi, const CellRestrT &restr)
Computes an algebraic topological model for the given filtered finite cell complex "K"...
Definition: atmodel2r.h:63
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 chain with coefficients in an arbitrary ring.
Definition: chain.h:50
A combinatorial linear map (for coefficients in Z_2).
A linear map.
Definition: linmap.h:55
A combinatorial chain.
Definition: combchain.h:49
A chain with coefficients in an arbitrary commutative ring.
A combinatorial linear map.
Definition: comblinmap.h:56