The ChainCon Software (Release 0.03)
ammodcomp.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A generic procedure for the computation of an algebraic minimal model
6 /// of a filtered cell complex stored in a file.
7 /// This is a complete procedure good for simplicial, cubical,
8 /// or other complexes.
9 /// It computes a chain contraction onto a cellular complex
10 /// with the boundary operator containing only the torsion part.
11 ///
12 /////////////////////////////////////////////////////////////////////////////
13 
14 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
15 //
16 // This file is part of my research software package. This is free software:
17 // you can redistribute it and/or modify it under the terms of the GNU
18 // General Public License as published by the Free Software Foundation,
19 // either version 3 of the License, or (at your option) any later version.
20 //
21 // This software is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this software; see the file "license.txt". If not,
28 // please, see <http://www.gnu.org/licenses/>.
29 
30 // Started on March 24, 2009. Last revision: August 21, 2013.
31 
32 
33 #ifndef _AMMODCOMP_H_
34 #define _AMMODCOMP_H_
35 
36 
37 // include relevant local header files
38 #include "chaincon/cellnames.h"
39 #include "chaincon/filtcomplex.h"
40 #include "chaincon/readfcompl.h"
41 #include "chaincon/linmap.h"
42 #include "chaincon/boundary.h"
43 #include "chaincon/ammodel.h"
44 #include "chaincon/homcohom.h"
45 
46 // include selected header files from the CHomP library
47 #include "chomp/system/config.h"
48 #include "chomp/system/textfile.h"
49 #include "chomp/system/timeused.h"
50 #include "chomp/struct/hashsets.h"
51 
52 // include some standard C++ header files
53 #include <istream>
54 #include <ostream>
55 #include <vector>
56 
57 
58 // --------------------------------------------------
59 // ---------------- compute homology ----------------
60 // --------------------------------------------------
61 
62 /// Computes the homology of a cellular complex.
63 /// The cellular complex is read from the given file.
64 /// Only top-dimensional cells should be listed in the file,
65 /// or otherwise the filtration may not be generated correctly.
66 template <class CellT, class CoefT, class CellArray1, class CellArray2,
67  class CellArray3, class CoefArray, class CellNames>
69  bool relativeComplex,
70  CellArray1 &H, CellArray2 &A, CellArray3 &B, CoefArray &Q,
74  CellNames &homCellNames, CellNames &cohomCellNames,
75  bool displayPi, bool displayIncl, bool displayPhi,
76  bool displayD, bool verify)
77 {
78  using chomp::homology::sout;
79 
80 // typedef typename LinMap::CellDomType CellT;
81  typedef tLinMap<CellT,CellT,CoefT> LinMap;
82 
83  // compute the minimal model together with the chain contraction
84  algMinModel (K, H, A, B, Q, pi, incl, phi);
85 
86  // show the computed homology groups
87  std::vector<std::vector<CellT> > representants;
88  std::vector<int> bettiNumbers;
89  std::vector<std::vector<CoefT> > torsion;
90  getHomCohom (H, A, B, Q, &representants, &bettiNumbers, &torsion,
91  false);
92  sout << "Homology groups:\n";
93  sout << homcohom2text (bettiNumbers, torsion, "H_", false);
94 
95  // show the computed cohomology groups
96  std::vector<std::vector<CellT> > cohomRepresentants;
97  std::vector<int> cohomBettiNumbers;
98  std::vector<std::vector<CoefT> > cohomTorsion;
99  getHomCohom (H, A, B, Q, &cohomRepresentants, &cohomBettiNumbers,
100  &cohomTorsion, true);
101  sout << "Cohomology groups:\n";
102  sout << homcohom2text (cohomBettiNumbers, cohomTorsion, "H^", false);
103 
104  // prepare correct cell names for homology representants
105  int homReprSize = representants. size ();
106  for (int dim = 0; dim < homReprSize; ++ dim)
107  {
108  int size = representants [dim]. size ();
109  for (int i = 0; i < size; ++ i)
110  homCellNames (representants [dim] [i]);
111  }
112 
113  // prepare correct cell names for cohomology representants
114  int cohomReprSize = cohomRepresentants. size ();
115  for (int dim = 0; dim < cohomReprSize; ++ dim)
116  {
117  int size = cohomRepresentants [dim]. size ();
118  for (int i = 0; i < size; ++ i)
119  cohomCellNames (cohomRepresentants [dim] [i]);
120  }
121 
122  // show the computed maps if requested to
123  tCellOwnNames<CellT> cellOwnNames;
124  if (displayPi)
125  {
126  sout << "The projection map pi:\n" <<
127  cells2names (pi, cellOwnNames, homCellNames);
128  }
129  if (displayIncl)
130  {
131  sout << "The inclusion map incl:\n" <<
132  cells2names (incl, homCellNames, cellOwnNames);
133  }
134  if (displayPhi)
135  {
136  sout << "The chain contraction phi:\n" << phi;
137  }
138  if (displayD)
139  {
140  sout << "The boundary map in the minimal model:\n";
141  for (int_t i = 0; i < A. size (); ++ i)
142  {
143  sout << homCellNames (A [i]) << " -> " << Q [i] <<
144  " * " << homCellNames (B [i]) << "\n";
145  }
146  }
147 
148  // prepare the boundary map, to be used for verifications
149  LinMap boundaryMap;
150 
151  // do several verifications to make sure the computed maps are good
152  if (verify)
153  {
154  if (relativeComplex)
155  computeBoundaryMap (K, boundaryMap, K);
156  else
157  computeBoundaryMap (K, boundaryMap, Unrestricted ());
158  if (phi * boundaryMap * phi == phi)
159  sout << "Verified: 'phi bd phi = phi'.\n";
160  else
161  sout << "Failed to verify that "
162  "'phi pd phi = phi'.\n";
163 
164  if (boundaryMap * phi * boundaryMap == boundaryMap)
165  sout << "Verified: 'bd phi bd = bd'.\n";
166  else
167  {
168  sout << "Failed to verify that 'bd phi bd = bd', "
169  "which is OK if there is torsion.\n";
170  }
171 
172  LinMap zeroMap;
173  if (phi * phi == zeroMap)
174  sout << "Verified: 'phi phi = 0'.\n";
175  else
176  sout << "Failed to verify that 'phi phi = 0'.\n";
177 
178  LinMap composition (boundaryMap * phi);
179  composition += phi * boundaryMap;
180  composition. negate ();
181  addIdentity (K, composition);
182  if (incl * pi == composition)
183  sout << "Verified: 'i pi = id - bd phi - phi bd'.\n";
184  else
185  sout << "Failed to verify that "
186  "'i pi = id - bd phi - phi bd'.\n";
187 
188  LinMap idHAB;
189  addIdentity (H, idHAB);
190  addIdentity (A, idHAB);
191  addIdentity (B, idHAB);
192  if (pi * incl == idHAB)
193  sout << "Verified: 'pi i = id_H+A+B'.\n";
194  else
195  sout << "Failed to verify that 'pi i = id_H+A+B'.\n";
196 
197  if (pi * phi == zeroMap)
198  sout << "Verified: 'pi phi = 0'.\n";
199  else
200  sout << "Failed to verify that 'pi phi = 0'.\n";
201 
202  if (phi * incl == zeroMap)
203  sout << "Verified: 'phi i = 0'.\n";
204  else
205  sout << "Failed to verify that 'phi i = 0'.\n";
206  }
207 
208  return;
209 } /* computeAlgMinModel */
210 
211 
212 #endif // _AMMODCOMP_H_
213 
Boundary computation at the level of chains of cells.
std::string homcohom2text(int bettiNumber, const std::vector< CoefT > &torsion, bool torsion_subscript)
Returns a text description of one (co)homology group.
Definition: homcohom.h:148
A linear map for coefficients in an arbitrary commutative ring.
Computation of the algebraic minimal model using the SNF.
void computeBoundaryMap(const CellArray &cells, tCombLinMap< CellT, CellT > &f, const CellRestrT &restr)
Computes the full boundary map for a cellular complex that contains the cells in a provided set and a...
Definition: boundary.h:323
A filtered cell complex.
A linear map.
Definition: linmap.h:55
A class for providing the cells as their names.
Definition: cellnames.h:238
A filtered complex.
Definition: filtcomplex.h:53
void getHomCohom(const CellArray1 &H, const CellArray2 &A, const CellArray3 &B, const CoefArray &Q, std::vector< std::vector< CellT > > *representants, std::vector< int > *bettiNumbers, std::vector< std::vector< CoefT > > *torsion, bool cohomology)
Computes Betti numbers and torsion coefficients for (co)homology from an algebraic minimal model (set...
Definition: homcohom.h:57
A class for naming cells for nice text data output.
NamesT::NameType cells2names(const CellT &in, NamesT &names)
Returns the name of a cell according to the given naming object.
Definition: cellnames.h:343
void computeAlgMinModel(const tFilteredComplex< CellT > &K, bool relativeComplex, CellArray1 &H, CellArray2 &A, CellArray3 &B, CoefArray &Q, tLinMap< CellT, CellT, CoefT > &pi, tLinMap< CellT, CellT, CoefT > &incl, tLinMap< CellT, CellT, CoefT > &phi, CellNames &homCellNames, CellNames &cohomCellNames, bool displayPi, bool displayIncl, bool displayPhi, bool displayD, bool verify)
Computes the homology of a cellular complex.
Definition: ammodcomp.h:68
A generic procedure for reading a filtered cell complex or a pair of filtered cell complexes from tex...
void algMinModel(const tFilteredComplex< CellT > &K, CellArray1 &H, CellArray2 &A, CellArray3 &B, CoefArray &Q, tLinMap< CellT, CellT, CoefT > &pi, tLinMap< CellT, CellT, CoefT > &incl, tLinMap< CellT, CellT, CoefT > &phi)
Computes a minimal model for a given filtered finite cell complex "K".
Definition: ammodel.h:75
void addIdentity(const CellArray &domain, tCombLinMap< CellT, CellT > &f)
Adds the identity map on the given domain to the map &#39;f&#39;.
Definition: comblinmap.h:416
A template of a simple class for unrestricted cell selection in the boundary computation procedures...
Definition: boundary.h:56
Computation of the (co)homology groups of an algebraic minimal model.