The ChainCon Software (Release 0.03)
experiment1.cpp
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A program for experimentation with the formula for the A-W diagonal.
6 ///
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
10 //
11 // This file is part of my research software package. This is free software:
12 // you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation,
14 // either version 3 of the License, or (at your option) any later version.
15 //
16 // This software is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this software; see the file "license.txt". If not,
23 // please, see <http://www.gnu.org/licenses/>.
24 
25 // Started on July 4, 2013. Last revision: October 21, 2013.
26 
27 
28 // the choice of cubes or simplices
29 #define CUBES_NOT_SIMPLICES
30 #ifdef CUBES_NOT_SIMPLICES
31 #define SPACE_WRAPPING
32 #endif
33 
34 // include some standard C++ header files
35 #include <istream>
36 #include <ostream>
37 #include <string>
38 
39 // include the string hashing definitions (before including "hashsets.h")
40 #include "chaincon/stringhash.h"
41 
42 // include selected header files from the CHomP library
43 #include "chomp/system/config.h"
44 #include "chomp/system/textfile.h"
45 #include "chomp/system/timeused.h"
46 #include "chomp/system/arg.h"
47 #include "chomp/struct/hashsets.h"
48 
49 // include relevant local header files
50 #include "chaincon/cubcell.h"
51 #include "chaincon/awdiagcub.h"
52 #include "chaincon/simplex.h"
53 #include "chaincon/awdiagsim.h"
54 #include "chaincon/emptycell.h"
55 #include "chaincon/chain.h"
56 #include "chaincon/linmap.h"
57 #include "chaincon/comblinmap.h"
58 #include "chaincon/ringzp.h"
59 #include "chaincon/cellnames.h"
60 #include "chaincon/awdiag.h"
61 #include "chaincon/boundary.h"
62 #ifdef SPACE_WRAPPING
63 #include "chaincon/wrapping.h"
64 #endif
65 #include "atmodcomp.h"
66 
67 
68 // --------------------------------------------------
69 // -------------------- OVERTURE --------------------
70 // --------------------------------------------------
71 
72 /// The title of the program and licensing information.
73 const char *title = "\
74 Experimentation with the formula for the A-W diagonal.\n\
75 Version 0.00 (Sep 24, 2015). Copyright (C) 1997-2016 by Pawel Pilarczyk.\n\
76 This is free software. No warranty. Consult 'license.txt' for details.";
77 
78 /// Brief help information on the program's usage.
79 const char *helpinfo = "\
80 This program does some computations to experiment with the A-W diagonal.\n\
81 The ring of coefficients is Z or Z_p (the integers modulo prime number p).\n\
82 Call with:\n\
83 filename - the name of a file that contains a list of cubical cells,\n\
84 Switches and additional arguments:\n\
85 filename2 - the name of an additional file for relative (co)homology;\n\
86 -pN - selection of the coefficients: p=0 for Z, p=1 unsupported in this\n\
87 \tprogram, p > 1 for Z_p (default: p=2); please, use p <= 181,\n\
88 -r - compute reduced homology (with the empty set as a cell of dim -1),\n\
89 -b - don't add boundary cells (use -bx and -ba for X and A selectively),\n\
90 -w N1,..,Nk - set space wrapping, a.k.a. periodic boundary conditions;\n\
91 \trepeat the n-tuple for each dimension n; use 0 for no wrapping,\n\
92 -dpi, -dincl, -dphi - display the computed maps: pi, incl, phi,\n\
93 -drepr - display homology representants (meaningful for -aN with N < 3),\n\
94 -aN - homology algorithm: 0 = old (very slow), 1 = new without additional\n\
95 optimization (relatively fast), 2 = new (default), 3,4 = using the SNF,\n\
96 --verify - do additional verification of the computed maps,\n\
97 --log filename - save the output to a file (without progress indicators),\n\
98 --quiet - suppress data output to the screen (whcih can be still logged),\n\
99 --help - display this brief help information only and exit.\n\
100 For more information please consult the accompanying documentation\n\
101 or ask the program's author at http://www.PawelPilarczyk.com/.";
102 
103 
104 // --------------------------------------------------
105 // ------------------- experiment -------------------
106 // --------------------------------------------------
107 
108 /// Runs the experimental calculations.
109 template <class SetT, class CellT, class CoefT, class CellNames>
110 inline void experiment (const SetT &K,
111  const chomp::homology::hashedset<CellT> &H,
112  const tLinMap<CellT,CellT,CoefT> &pi,
113  const tLinMap<CellT,CellT,CoefT> &incl,
114  const tLinMap<CellT,CellT,CoefT> &phi,
115  CellNames &cellNames)
116 {
117  using chomp::homology::sout;
118  typedef tChain<CellT,CoefT> ChainT;
119  typedef tTensor<CellT,CellT,CoefT> TensorT;
120 
121  sout << "Checking the experimental formula on " <<
122  K. size () << " cells...\n";
123 
124  // go through all the cells in the cellular complex
125  int_t Ksize (K. size ());
126  for (int_t n = 0; n < Ksize; ++ n)
127  {
128  // retrieve the n-th element of the cellular complex
129  const CellT &c (K [n]);
130 
131  // compute the projection of c onto the homology module
132  ChainT hom_c (pi (c));
133  if (hom_c. empty ())
134  continue;
135  // else if (hom_c. size () > 1)
136  // {
137  // sout << "Size of f(" << c << ") is " <<
138  // hom_c. size ();
139  // continue;
140  // }
141 
142  // check the experimental formula for the AW diag in homology
143  ChainT bd_phi_c (boundary (phi (c), K));
144  ChainT neg_bd_phi_c (bd_phi_c);
145  neg_bd_phi_c. negate ();
146  CoefT one (1);
147  ChainT one_etc;
148  one_etc. add (c, one);
149  one_etc += neg_bd_phi_c;
150  TensorT aw1;
151  AWdiagonal (one_etc, aw1);
152  TensorT result1 (pi (aw1));
153 
154  // compute the result of the AW diag on the hom generators
155  TensorT aw2;
156  AWdiagonal (incl (hom_c), aw2);
157  TensorT result2 (pi (aw2));
158 
159  // compare the results and show the information
160  bool good = (result1 == result2);
161  sout << "Cell " << c << ", size " << hom_c. size () <<
162  (good ? ", formula OK" : ", WRONG RESULT") << ".\n";
163  if (!good)
164  {
165  sout << " result 1: " << cells2names (result1,
166  cellNames, cellNames) << ".\n";
167  sout << " result 2: " << cells2names (result2,
168  cellNames, cellNames) << ".\n";
169  }
170  }
171  return;
172 } /* experiment */
173 
174 
175 // --------------------------------------------------
176 // ---------------------- main ----------------------
177 // --------------------------------------------------
178 
179 /// The main procedure of the program.
180 /// Returns: 0 = Ok, -1 = Error, 1 = Help displayed, 2 = Wrong arguments.
181 int main (int argc, char *argv [])
182 {
183 #ifdef CUBES_NOT_SIMPLICES
185  SettableEmptyCell> CellT;
186 #else
187  typedef tSimplex<int_t,SettableEmptyCell> CellT;
188 #endif
189  typedef tZp<short> CoefT;
190  typedef tLinMap<CellT,CellT,CoefT> MapT;
191 
192  using namespace chomp::homology;
193 
194  // turn on a message that will appear if the program does not finish
195  program_time = "Aborted after";
196 
197  // prepare user-configurable data
198  char *Xname = 0;
199  char *Aname = 0;
200  int p = 2;
201  bool reduced = false;
202  bool addBoundariesX = true;
203  bool addBoundariesA = true;
204  bool addNoBoundaries = false;
205  bool displayPi = false;
206  bool displayIncl = false;
207  bool displayPhi = false;
208  bool displayRepr = false;
209  bool verify = false;
210  int algorithmVersion = 2;
211 #ifdef SPACE_WRAPPING
212  const int maxWrapping = 100;
213  char *wrapping [maxWrapping];
214  int nWrapping = 0;
215 #endif
216 
217  // analyze the command line
218  arguments a;
219  arg (a, NULL, Xname);
220  arg (a, NULL, Aname);
221  arg (a, "p", p);
222  arg (a, "a", algorithmVersion);
223 #ifdef SPACE_WRAPPING
224  arg (a, "w", wrapping, nWrapping, maxWrapping);
225 #endif
226  argswitch (a, "r", reduced, true);
227  argswitch (a, "bx", addBoundariesX, false);
228  argswitch (a, "ba", addBoundariesA, false);
229  argswitch (a, "b", addNoBoundaries, true);
230  argswitch (a, "dpi", displayPi, true);
231  argswitch (a, "dincl", displayIncl, true);
232  argswitch (a, "dphi", displayPhi, true);
233  argswitch (a, "drepr", displayRepr, true);
234  argswitch (a, "-verify", verify, true);
235  arghelp (a);
236 
237  argstreamprepare (a);
238  int argresult = a. analyze (argc, argv);
239  argstreamset ();
240 
241  // show the program's title
242  if (argresult >= 0)
243  sout << title << '\n';
244 
245  // if something was incorrect, show an additional message and exit
246  if (argresult < 0)
247  {
248  sout << "Call with '--help' for help.\n";
249  return 2;
250  }
251 
252  // if help requested or no filename present, show help information
253  if ((argresult > 0) || !Xname || (p < 0))
254  {
255  sout << helpinfo << '\n';
256  return 1;
257  }
258 
259  // try running the main function and catch an error message if thrown
260  try
261  {
262  // set up the ring of coefficients and make a correction to p
263  CoefT::setp (p);
264  p = CoefT::getp ();
265 
266  // set the existence of the empty cell if desired
267  if (reduced)
268  CellT::EmptyType::setExistence (true);
269 
270 #ifdef SPACE_WRAPPING
271  // set wrapping as desired
272  for (int i = 0; i < nWrapping; ++ i)
273  {
274  setWrapping<CellT::WrapType>
275  (std::string (wrapping [i]));
276  }
277 #endif
278 
279  // read the filtered cell complexes
282  readFilteredComplexes (Xname, Aname, K, L,
283  addBoundariesX && !addNoBoundaries,
284  addBoundariesA && !addNoBoundaries);
285  bool relativeComplex = !L. empty ();
286 
287  // prepare the data for storing the result of computation
288  chomp::homology::hashedset<CellT> H;
289  MapT pi, incl, phi;
290  tCellNames<CellT> cellNames;
291  // cellNames. setPrefix ("g");
292 
293  // compute an AT model
294  sout << "Carrying out computations in the ring " <<
295  CoefT::ringsymbol () << ".\n";
296  computeAlgTopModel (K, relativeComplex,
297  H, pi, incl, phi, cellNames,
298  displayPi, displayIncl, displayPhi,
299  displayRepr, verify, algorithmVersion);
300 
301  // verify the experimental formulas
302  experiment (K, H, pi, incl, phi, cellNames);
303 
304  program_time = "Total time used:";
305  program_time = 1;
306  return 0;
307  }
308  catch (const char *msg)
309  {
310  sout << "ERROR: " << msg << '\n';
311  return -1;
312  }
313  catch (const std::exception &e)
314  {
315  sout << "ERROR: " << e. what () << '\n';
316  return -1;
317  }
318  catch (...)
319  {
320  sout << "ABORT: An unknown error occurred.\n";
321  return -1;
322  }
323 
324 } /* main */
325 
Elements of the ring Z_p.
Boundary computation at the level of chains of cells.
A cubical version of the Alexander-Whitney diagonal.
A linear map for coefficients in an arbitrary commutative ring.
A simplex with vertices of arbitrary type.
Definition: simplex.h:59
const char * title
The title of the program and licensing information.
Definition: experiment1.cpp:73
const char * helpinfo
Brief help information on the program&#39;s usage.
Definition: experiment1.cpp:79
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 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.
A linear map.
Definition: linmap.h:55
A cubical cell.
A filtered complex.
Definition: filtcomplex.h:53
void readFilteredComplexes(const char *Xname, const char *Aname, tFilteredComplex< CellT > &K, tFilteredComplex< CellT > &L, bool addBoundariesX, bool addBoundariesA)
Reads a filtered cell complex or a pair of filtered cell complexes from text files.
Definition: readfcompl.h:56
Tensor of chains.
Definition: tensor.h:52
int main(int argc, char *argv [])
The main procedure of the program.
A class for naming cells for nice text data output.
Hashing keys for std::string.
NamesT::NameType cells2names(const CellT &in, NamesT &names)
Returns the name of a cell according to the given naming object.
Definition: cellnames.h:343
A chain with coefficients in an arbitrary commutative ring.
A simplex class with arbitrary vertices.
void AWdiagonal(const tCombChain< CellT > &ch, tCombTensor< CellT, CellT > &t)
Computes the Alexander-Whitney diagonal of a chain, using the procedure defined for individual cells...
Definition: awdiag.h:51
The decision on whether the empty cell should be used as a valid cell of dimension -1...
Alexander-Whitney diagonal of a chain.
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 version of the Alexander-Whitney diagonal.
An elementary cubical cell with vertex coordinates of integer type.
Definition: cubcell.h:63
void experiment(const SetT &K, const chomp::homology::hashedset< CellT > &H, const tLinMap< CellT, CellT, CoefT > &pi, const tLinMap< CellT, CellT, CoefT > &incl, const tLinMap< CellT, CellT, CoefT > &phi, CellNames &cellNames)
Runs the experimental calculations.
A generic procedure for the computation of chain contraction of a filtered cell complex stored in a f...