The ChainCon Software (Release 0.03)
ssqmain.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// The main function of a program for computing the Steenrod squares
6 /// of a simplicial complex or a cubical complex.
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 July 4, 2013. Last revision: December 11, 2015.
27 
28 
29 #ifndef _SSQMAIN_H_
30 #define _SSQMAIN_H_
31 
32 
33 // the choice of cubes or simplices
34 #ifndef CUBES_NOT_SIMPLICES
35 #define CUBES_NOT_SIMPLICES 0
36 #endif
37 
38 // is space wrapping relevant for the chosen type of cells?
39 #if CUBES_NOT_SIMPLICES
40 #define SPACE_WRAPPING
41 #endif
42 
43 // should the direct formula by Marek Krcal be used for cubes?
44 #if CUBES_NOT_SIMPLICES
45 #ifndef DIRECT_CUBICAL_FORMULA
46 #define DIRECT_CUBICAL_FORMULA 1
47 #endif
48 #endif
49 
50 // include some standard C++ header files
51 #include <istream>
52 #include <ostream>
53 #include <vector>
54 #include <string>
55 
56 // include the string hashing definitions (before including "hashsets.h")
57 #include "chaincon/stringhash.h"
58 
59 // include selected header files from the CHomP library
60 #include "chomp/system/config.h"
61 #include "chomp/system/textfile.h"
62 #include "chomp/system/timeused.h"
63 #include "chomp/system/arg.h"
64 #include "chomp/struct/hashsets.h"
65 
66 // include relevant local header files
67 #include "chaincon/cubcell.h"
68 #include "chaincon/cubproduct.h"
69 #include "chaincon/awdiagcub.h"
70 #include "chaincon/simplex.h"
71 #include "chaincon/simplset.h"
72 #include "chaincon/awdiagsim.h"
73 #include "chaincon/emptycell.h"
74 #include "chaincon/chain.h"
75 #include "chaincon/linmap.h"
76 #include "chaincon/comblinmap.h"
77 #include "chaincon/ringzp.h"
78 #include "chaincon/cellnames.h"
79 #include "chaincon/awdiag.h"
80 #include "chaincon/boundary.h"
81 #include "chaincon/ez_aw.h"
82 #include "chaincon/ez_eml.h"
83 #include "chaincon/ez_shi.h"
84 #include "chaincon/readfcompl.h"
85 #include "chaincon/setfilter.h"
86 #ifdef SPACE_WRAPPING
87 #include "chaincon/wrapping.h"
88 #endif
89 #include "atmodcomp.h"
90 #include "ssqcomp.h"
91 
92 
93 // --------------------------------------------------
94 // ----------------- mainProcedure ------------------
95 // --------------------------------------------------
96 
97 /// Runs the experimental calculations.
98 //template <class SetT, class CellT, class CoefT, class CellNames>
99 template <class CellT, class SimCellT, class CoefT>
100 inline void computeSteenrodSquares (const char *Xname, const char *Aname,
101  int algorithmVersion, int ssquareVersion,
102  bool displayPi, bool displayIncl, bool displayPhi,
103  bool displayD, bool displayRepr, bool verify)
104 {
105  using chomp::homology::timeused;
106  using chomp::homology::sout;
107  using chomp::homology::sbug;
108  using chomp::homology::fileerror;
109  using chomp::homology::ignorecomments;
110  using chomp::homology::hashedset;
111  using chomp::homology::multitable;
112 
113  // prepare
114  timeused compTime;
115  compTime = 0;
116 
117 #if CUBES_NOT_SIMPLICES
118  sout << "Working with CUBICAL cells. "
119  "(Please, re-compile for simplices.)\n";
120 #if DIRECT_CUBICAL_FORMULA
121  sout << "Using a direct formula for cubical cells. "
122  "(Recompile to change.)\n";
123 #else
124  sout << "Using simplicial subdivision of cubical cells. "
125  "(Recompile to change.)\n";
126 #endif
127 #else
128  sout << "Working with SIMPLICIAL cells. "
129  "(Please, re-compile for cubical sets.)\n";
130 #endif
131 
132  // compute the homology groups and generators
133  sout << "Carrying out computations in the ring " <<
134  CoefT::ringsymbol () << ".\n";
135 
136  // read the cubical or simplicial set
137  sout << "Reading the cells from '" << Xname << "'...\n";
138  hashedset<CellT> Xcells;
139  std::ifstream in (Xname);
140  if (!in)
141  fileerror (Xname);
142  in >> Xcells;
143  in. close ();
144  if (Xcells. empty ())
145  throw "No cells read from the input file.";
146  sout << Xcells. size () << " cells read.\n";
147 
148  // show a warning that relative complexes are not handled
149  if (Aname && *Aname)
150  {
151  sout << "WARNING: Relative complexes cannot yet be handled "
152  "by this program.\nThe contents of '" <<
153  Aname << "' will be ignored.\n";
154  }
155 
156 #if CUBES_NOT_SIMPLICES && DIRECT_CUBICAL_FORMULA
157  hashedset<SimCellT> &Xsimpl (Xcells);
158 
159  // determine the highest dimension of cubical cells in the set
160  int dimension (-2);
161  int_t size (Xsimpl. size ());
162  for (int_t i = 0; i < size; ++ i)
163  {
164  int dim = Xsimpl [i]. dim ();
165  if (dimension < dim)
166  dimension = dim;
167  }
168 
169 // sbug << "DEBUG: The cubical set read from the file:\n" << Xsimpl;
170 
171  // transform the cubical set to a filtered complex
172  sout << "Transforming the cubical set "
173  "into a filtered complex...\n";
174 #else
175  // transform the data into the structure of a simplicial set
176  tSimplSet<SimCellT> Xsimpl;
177 
178 #if CUBES_NOT_SIMPLICES
179  // transform the cubes into products of simplices if necessary
180  sout << "Transforming the cubical cells into products "
181  "of simplices...\n";
182  for (int_t i = 0; i < Xcells. size (); ++ i)
183  cube2product (Xcells [i], Xsimpl);
184  sout << Xsimpl. size () << " products created.\n";
185 #else
186  sout << "Creating the structure of a simplicial set...\n";
187  for (int_t i = 0; i < Xcells. size (); ++ i)
188  Xsimpl. add (Xcells [i]);
189 
190  // add faces of all the cells in the product
191  sout << "Adding faces of cells in the product...\n";
192  int_t nAddedFaces = Xsimpl. addFaces ();
193  sout << nAddedFaces << " faces added, the complex has " <<
194  Xsimpl. size () << " elements.\n";
195 #endif
196 // sbug << "DEBUG: Xsimpl:\n" << Xsimpl;
197 
198  // normalize the constructed simplicial set
199  sout << "Normalizing the constructed simplicial set...\n";
200  int_t nDegenerate = Xsimpl. normalize ();
201  sout << nDegenerate << " degenerate cells removed, " <<
202  Xsimpl. size () << " cells remain.\n";
203 // sbug << "DEBUG: Xsimpl:\n" << Xsimpl;
204 
205  // determine the highest dimension of simplices in the simplicial set
206  int dimension = Xsimpl. dim ();
207 
208  // transform the simplicial set to a filtered complex
209  sout << "Transforming the simplicial set "
210  "into a filtered complex...\n";
211 #endif
213  set2filter (Xsimpl, K, dimension);
214 // sbug << "DEBUG: K:\n" << K;
215 
216 #if CUBES_NOT_SIMPLICES && DIRECT_CUBICAL_FORMULA
217  // add boundaries of cubical cells in the filtered complex
218  sout << "Adding boundaries of cells in the complex...\n";
219  addBoundaries (K, Unrestricted ());
220  sout << "The complex has " << K. size () << " cells.\n";
221 #endif
222 
223 // sbug << "DEBUG: The filtered complex:\n" << K;
224 
225  // prepare the data for storing the result of computations
226  chomp::homology::hashedset<SimCellT> H, A, B;
227  std::vector<CoefT> Q;
228 
229  // compute the homology groups and generators
230  sout << "Carrying out computations in the ring " <<
231  CoefT::ringsymbol () << ".\n";
232  tLinMap<SimCellT,SimCellT,CoefT> pi, incl, phi;
233  tCellNames<SimCellT> homCellNames;
234  tCellNames<SimCellT> cohomCellNames;
235 // homCellNames. setPrefix ("g");
236 // cohomCellNames. setPrefix ("g");
237 
238  // show the time used so far
239  sout << "The computations up to now took " << compTime << ".\n";
240 
241  sout << "=== Computing an AT model ===\n";
242 // computeAlgMinModel (K, false, H, A, B, Q, pi, incl, phi,
243 // homCellNames, cohomCellNames,
244 // displayPi, displayIncl, displayPhi, displayD, verify);
245  computeAlgTopModel (K, false,
246  H, pi, incl, phi, homCellNames,
247  displayPi, displayIncl, displayPhi,
248  displayRepr, verify, algorithmVersion);
249 
250  // show the time used so far
251  sout << "The computations up to now took " << compTime << ".\n";
252 
253  // compute the Steenrod squares
254  computeSteenrodSquares (H, A, B, Q, pi, incl, cohomCellNames,
255  ssquareVersion, verify);
256 
257  return;
258 } /* computeSteenrodSquares */
259 
260 
261 // --------------------------------------------------
262 // ---------------------- main ----------------------
263 // --------------------------------------------------
264 
265 /// The main procedure of the program.
266 /// Returns: 0 = Ok, -1 = Error, 1 = Help displayed, 2 = Wrong arguments.
267 inline int steenrodSquaresMain (int argc, char *argv [],
268  const char *title, const char *helpinfo)
269 {
270  // prepare the types for coordinates, cells, etc.
271  typedef short int CoordT;
272  typedef tZp<short> CoefT;
273 
274 #if CUBES_NOT_SIMPLICES
276  CellT;
277 #if DIRECT_CUBICAL_FORMULA
278  typedef CellT SimCellT;
279 #else
281 #endif
282 #else
284  typedef tSimplex<CoordT,SettableEmptyCell> SimCellT;
285 #endif
286 
287  using namespace chomp::homology;
288 
289  // turn on a message that will appear if the program does not finish
290  program_time = "Aborted after";
291 
292  // prepare user-configurable data
293  char *Xname = 0;
294  char *Aname = 0;
295  int p = 2;
296  bool reduced = false;
297  bool displayPi = false;
298  bool displayIncl = false;
299  bool displayPhi = false;
300  bool displayD = false;
301  bool displayRepr = false;
302  bool verify = false;
303  int algorithmVersion = 2;
304  int ssquareVersion = 2;
305 #ifdef SPACE_WRAPPING
306  const int maxWrapping = 100;
307  char *wrapping [maxWrapping];
308  int nWrapping = 0;
309 #endif
310 
311  // analyze the command line
312  arguments a;
313  arg (a, NULL, Xname);
314  arg (a, NULL, Aname);
315  arg (a, "a", algorithmVersion);
316  arg (a, "s", ssquareVersion);
317  arg (a, "p", p);
318 #ifdef SPACE_WRAPPING
319  arg (a, "w", wrapping, nWrapping, maxWrapping);
320 #endif
321  argswitch (a, "r", reduced, true);
322  argswitch (a, "dpi", displayPi, true);
323  argswitch (a, "dincl", displayIncl, true);
324  argswitch (a, "dphi", displayPhi, true);
325  argswitch (a, "dd", displayD, true);
326  argswitch (a, "drepr", displayRepr, true);
327  argswitch (a, "-verify", verify, true);
328  arghelp (a);
329 
330  argstreamprepare (a);
331  int argresult = a. analyze (argc, argv);
332  argstreamset ();
333 
334  // show the program's title
335  if (argresult >= 0)
336  sout << title << '\n';
337 
338  // if something was incorrect, show an additional message and exit
339  if (argresult < 0)
340  {
341  sout << "Call with '--help' for help.\n";
342  return 2;
343  }
344 
345  // if help requested or no filename present, show help information
346  if ((argresult > 0) || !Xname || (p < 0))
347  {
348  sout << helpinfo << '\n';
349  return 1;
350  }
351 
352  // try running the main function and catch an error message if thrown
353  try
354  {
355  // set up the ring of coefficients and make a correction to p
356  CoefT::setp (p);
357  p = CoefT::getp ();
358 
359  // set the existence of the empty cell if desired
360  if (reduced)
361  CellT::EmptyType::setExistence (true);
362 
363 #ifdef SPACE_WRAPPING
364  // set wrapping as desired
365  for (int i = 0; i < nWrapping; ++ i)
366  {
367  setWrapping<typename CellT::WrapType>
368  (std::string (wrapping [i]));
369  }
370 #endif
371 
372  // verify the experimental formulas
373  computeSteenrodSquares<CellT, SimCellT, CoefT> (Xname, Aname,
374  algorithmVersion, ssquareVersion,
375  displayPi, displayIncl, displayPhi,
376  displayD, displayRepr, verify);
377 
378  program_time = "Total time used:";
379  program_time = 1;
380  return 0;
381  }
382  catch (const char *msg)
383  {
384  sout << "ERROR: " << msg << '\n';
385  return -1;
386  }
387  catch (const std::exception &e)
388  {
389  sout << "ERROR: " << e. what () << '\n';
390  return -1;
391  }
392  catch (...)
393  {
394  sout << "ABORT: An unknown error occurred.\n";
395  return -1;
396  }
397 
398 } /* steenrodSquaresMain */
399 
400 
401 #endif // _SSQMAIN_H_
402 
Elements of the ring Z_p.
Boundary computation at the level of chains of cells.
A function that creates a filter from a set of cells of different dimensions.
A cubical version of the Alexander-Whitney diagonal.
A function that converts a cubical cell into a simplicial set that corresponds to the cartesian produ...
A linear map for coefficients in an arbitrary commutative ring.
A simplex with vertices of arbitrary type.
Definition: simplex.h:59
const char * helpinfo
Brief help information on the program&#39;s usage.
Definition: ammodcub.cpp:53
The Shih operator as a component of the Eilenberg-Zilber chain contraction.
void cube2product(const CubCellT &q, SetT &cells)
Transforms an elementary cube into a simplicial set that represents the Cartesian product of the simp...
Definition: cubproduct.h:57
A class whose instances can be used to generate names of cells with subsequent numbers, in each dimension separately.
Definition: cellnames.h:61
A combinatorial linear map (for coefficients in Z_2).
Tools for coordinate wrapping, a.k.a.
void set2filter(const SetT &set, FilterT &filter, int maxDim)
Transforms a collection of cells to a filter by adding them in the order of decreasing dimension...
Definition: setfilter.h:49
A linear map.
Definition: linmap.h:55
A cubical cell.
A simplicial set.
A filtered complex.
Definition: filtcomplex.h:53
void addBoundaries(tFilteredComplex< CellT > &K, const CellRestrT &restr)
Adds boundaries to all the cells in the given filtered complex.
Definition: boundary.h:459
A class for naming cells for nice text data output.
Hashing keys for std::string.
A chain with coefficients in an arbitrary commutative ring.
A generic procedure for the computation of the Steenrod squares for an algebraic topological model...
A simplex class with arbitrary vertices.
The Alexander-Whitney operator as a component of the Eilenberg-Zilber chain contraction.
const char * title
The title of the program and licensing information.
Definition: ammodcub.cpp:47
The decision on whether the empty cell should be used as a valid cell of dimension -1...
A generic procedure for reading a filtered cell complex or a pair of filtered cell complexes from tex...
Alexander-Whitney diagonal of a chain.
A Cartesian product of simplicial cells as a simplicial cell.
Definition: prodcell.h:48
An empty cell existence decision class with settable global flag.
Definition: emptycell.h:70
An element of the ring Z_p, where p is globally set.
Definition: ringzp.h:179
void computeAlgTopModel(const tFilteredComplex< CellT > &K, bool relativeComplex, chomp::homology::hashedset< CellT > &H, LinMap &pi, LinMap &incl, LinMap &phi, CellNames &cellNames, bool displayPi, bool displayIncl, bool displayPhi, bool displayRepr, bool verify, int algorithmVersion)
Computes an AT model and the homology of a cellular complex.
Definition: atmodcomp.h:65
A simplicial set.
Definition: simplset.h:51
A simplicial version of the Alexander-Whitney diagonal.
A template of a simple class for unrestricted cell selection in the boundary computation procedures...
Definition: boundary.h:56
The Eilenberg-Mac Lane operator as a component of the Eilenberg-Zilber chain contraction.
int steenrodSquaresMain(int argc, char *argv [], const char *title, const char *helpinfo)
The main procedure of the program.
Definition: ssqmain.h:267
An elementary cubical cell with vertex coordinates of integer type.
Definition: cubcell.h:63
void computeSteenrodSquares(const char *Xname, const char *Aname, int algorithmVersion, int ssquareVersion, bool displayPi, bool displayIncl, bool displayPhi, bool displayD, bool displayRepr, bool verify)
Runs the experimental calculations.
Definition: ssqmain.h:100
A generic procedure for the computation of chain contraction of a filtered cell complex stored in a f...