The ChainCon Software (Release 0.03)
experiment2.cpp
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A program for experimentation with the formulas
6 /// for the E-Z chain contraction: AW, EML, SHI.
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 4, 2014.
27 
28 
29 // the choice of cubes or simplices
30 //#define CUBES_NOT_SIMPLICES
31 #ifdef CUBES_NOT_SIMPLICES
32 #define SPACE_WRAPPING
33 #endif
34 
35 // include some standard C++ header files
36 #include <istream>
37 #include <ostream>
38 #include <string>
39 
40 // include the string hashing definitions (before including "hashsets.h")
41 #include "chaincon/stringhash.h"
42 
43 // include selected header files from the CHomP library
44 #include "chomp/system/config.h"
45 #include "chomp/system/textfile.h"
46 #include "chomp/system/timeused.h"
47 #include "chomp/system/arg.h"
48 #include "chomp/struct/hashsets.h"
49 
50 // include relevant local header files
51 #include "chaincon/cubcell.h"
52 #include "chaincon/awdiagcub.h"
53 #include "chaincon/simplex.h"
54 #include "chaincon/awdiagsim.h"
55 #include "chaincon/emptycell.h"
56 #include "chaincon/chain.h"
57 #include "chaincon/linmap.h"
58 #include "chaincon/comblinmap.h"
59 #include "chaincon/ringzp.h"
60 #include "chaincon/cellnames.h"
61 #include "chaincon/awdiag.h"
62 #include "chaincon/boundary.h"
63 #include "chaincon/ez_aw.h"
64 #include "chaincon/ez_eml.h"
65 #include "chaincon/ez_shi.h"
66 #ifdef SPACE_WRAPPING
67 #include "chaincon/wrapping.h"
68 #endif
69 
70 
71 // --------------------------------------------------
72 // -------------------- OVERTURE --------------------
73 // --------------------------------------------------
74 
75 /// The title of the program and licensing information.
76 const char *title = "\
77 Experimentation with the formulas for the E-Z chain contraction.\n\
78 Version 0.00 (Dec 4, 2014). Copyright (C) 1997-2016 by Pawel Pilarczyk.\n\
79 This is free software. No warranty. Consult 'license.txt' for details.";
80 
81 /// Brief help information on the program's usage.
82 const char *helpinfo = "\
83 This program does some computations to experiment with the Eilenberg-Zilber\n\
84 chain contraction.\n\
85 The ring of coefficients is Z or Z_p (the integers modulo prime number p).\n\
86 Call with:\n\
87 filename - the name of a file that contains a list of cells for the test,\n\
88 Switches and additional arguments:\n\
89 -pN - selection of the coefficients: p=0 for Z, p=1 unsupported in this\n\
90 \tprogram, p > 1 for Z_p (default: p=2); please, use p <= 181,\n\
91 -r - compute reduced homology (with the empty set as a cell of dim -1),\n\
92 --log filename - save the output to a file (without progress indicators),\n\
93 --quiet - suppress data output to the screen (whcih can be still logged),\n\
94 --help - display this brief help information only and exit.\n\
95 For more information please consult the accompanying documentation\n\
96 or ask the program's author at http://www.PawelPilarczyk.com/.";
97 
98 
99 // --------------------------------------------------
100 // ------------------- experiment -------------------
101 // --------------------------------------------------
102 
103 /// Runs the experimental calculations.
104 template <class CellT, class CoefT>
105 inline void experiment (const char *Xname)
106 {
107  using chomp::homology::sout;
108  using chomp::homology::ignorecomments;
109  typedef tProdCell<CellT> ProdT;
110  typedef tChain<tProdCell<CellT>,CoefT> ChainT;
111  typedef tTensor<tProdCell<CellT>,tProdCell<CellT>,CoefT> TensorProdT;
112  typedef tTensor<CellT,CellT,CoefT> TensorCellT;
113 
114  sout << "Checking the experimental formulas...\n";
115 
116  // open the input file
117  std::ifstream in (Xname);
118  if (!in)
119  throw "Can't open the input file.";
120 
121  // go through all the cells in the input file
122  int counter (0);
123  ignorecomments (in);
124  while (!in. eof ())
125  {
126  // read the two cells
127  CellT x;
128  in >> x;
129  ignorecomments (in);
130  if (in. eof ())
131  break;
132  CellT y;
133  in >> y;
134  ignorecomments (in);
135  ++ counter;
136 
137  // prepare the product of the two cells
138  ProdT product (x, y);
139 
140  // prepare the tensor of the two cells
141  TensorCellT tensor;
142  tensor. add (x, y, CoefT (1));
143  TensorProdT tensorProd;
144  tensorProd. add (ProdT (x), ProdT (y), CoefT (1));
145 
146  // show the operators applied to the two cells
147  TensorCellT resultAW;
148  EZ_AW (x, y, resultAW);
149  sout << "AW (" << x << " X " << y << ") = " <<
150  resultAW << ".\n";
151  ChainT resultEML;
152  EZ_EML (x, y, resultEML);
153  sout << "EML (" << x << " x " << y << ") = " <<
154  resultEML << ".\n";
155  ChainT resultSHI;
156  EZ_SHI (x, y, resultSHI);
157  sout << "SHI (" << x << " X " << y << ") = " <<
158  resultSHI << ".\n";
159 
160  // check if SHI EML (x, y) = 0
161  ChainT result1a;
162  EZ_EML (tensor, result1a);
163  ChainT result1b;
164  EZ_SHI (result1a, result1b);
165  if (result1b. empty ())
166  {
167  sout << "Verified: SHI EML (" << x << " x " << y <<
168  ") = 0.\n";
169  }
170  else
171  {
172  sout << "PROBLEM: SHI EML (" << x << " x " << y <<
173  ") is nonzero: " << result1b << ".\n";
174  }
175 
176  // check if SHI SHI (x, y) = 0
177  ChainT result2a;
178  EZ_SHI (product, result2a);
179  ChainT result2b;
180  EZ_SHI (result2a, result2b);
181  if (result2b. empty ())
182  {
183  sout << "Verified: SHI SHI (" << x << " X " << y <<
184  ") = 0.\n";
185  }
186  else
187  {
188  sout << "PROBLEM: SHI SHI (" << x << " X " << y <<
189  ") is nonzero: " << result2b << ".\n";
190  }
191 
192  // check if AW SHI (x, y) = 0
193  ChainT result3a;
194  EZ_SHI (product, result3a);
195  TensorProdT result3b;
196  EZ_AW (result3a, result3b);
197  if (result3b. empty ())
198  {
199  sout << "Verified: AW SHI (" << x << " X " << y <<
200  ") = 0.\n";
201  }
202  else
203  {
204  sout << "PROBLEM: AW SHI (" << x << " X " << y <<
205  ") is nonzero: " << result3b << ".\n";
206  }
207 
208  // check if AW EML (x, y) = 0
209  ChainT result4a;
210  EZ_EML (tensor, result4a);
211  TensorProdT result4b;
212  EZ_AW (result4a, result4b);
213  if (result4b == tensorProd)
214  {
215  sout << "Verified: AW EML (" << x << " x " << y <<
216  ") = " << x << " x " << y << ".\n";
217  }
218  else
219  {
220  sout << "PROBLEM: AW EML (" << x << " x " << y <<
221  ") differs from " << x << " x " << y <<
222  ": " << result4b << ".\n";
223  }
224 
225  // compute a formula for I - d SHI
226  ChainT result5a;
227  EZ_SHI (product, result5a);
228  ChainT result5b;
229  computeBoundary (result5a, result5b, Unrestricted ());
230  result5b. negate ();
231  result5b. add (product, CoefT (1));
232  sout << "(I - d SHI) (" << x << " X " << y << ") = " <<
233  result5b << ".\n";
234 
235  // compute a formula for I - SHI d
236  ChainT prodChain;
237  prodChain. add (product, CoefT (1));
238  ChainT result6a;
239  computeBoundary (prodChain, result6a, Unrestricted ());
240  ChainT result6b;
241  EZ_SHI (result6a, result6b);
242  result6b. negate ();
243  result6b += prodChain;
244  sout << "(I - SHI d) (" << x << " X " << y << ") = " <<
245  result6b << ".\n";
246 
247  // check 1 - d SHI - SHI d
248  // TODO
249  }
250  in. close ();
251  sout << counter << " pair" << ((counter == 1) ? "" : "s") <<
252  " of cells have been tested.\n";
253  return;
254 } /* experiment */
255 
256 
257 // --------------------------------------------------
258 // ---------------------- main ----------------------
259 // --------------------------------------------------
260 
261 /// The main procedure of the program.
262 /// Returns: 0 = Ok, -1 = Error, 1 = Help displayed, 2 = Wrong arguments.
263 int main (int argc, char *argv [])
264 {
265 #ifdef CUBES_NOT_SIMPLICES
267  SettableEmptyCell> CellT;
268 #else
269  typedef tSimplex<int_t,SettableEmptyCell> CellT;
270 #endif
271  typedef tZp<short> CoefT;
272 // typedef tLinMap<CellT,CellT,CoefT> MapT;
273 
274  using namespace chomp::homology;
275 
276  // turn on a message that will appear if the program does not finish
277  program_time = "Aborted after";
278 
279  // prepare user-configurable data
280  char *Xname = 0;
281  int p = 0;
282  bool reduced = false;
283 #ifdef SPACE_WRAPPING
284  const int maxWrapping = 100;
285  char *wrapping [maxWrapping];
286  int nWrapping = 0;
287 #endif
288 
289  // analyze the command line
290  arguments a;
291  arg (a, NULL, Xname);
292  arg (a, "p", p);
293 #ifdef SPACE_WRAPPING
294  arg (a, "w", wrapping, nWrapping, maxWrapping);
295 #endif
296  argswitch (a, "r", reduced, true);
297  arghelp (a);
298 
299  argstreamprepare (a);
300  int argresult = a. analyze (argc, argv);
301  argstreamset ();
302 
303  // show the program's title
304  if (argresult >= 0)
305  sout << title << '\n';
306 
307  // if something was incorrect, show an additional message and exit
308  if (argresult < 0)
309  {
310  sout << "Call with '--help' for help.\n";
311  return 2;
312  }
313 
314  // if help requested or no filename present, show help information
315  if ((argresult > 0) || !Xname || (p < 0))
316  {
317  sout << helpinfo << '\n';
318  return 1;
319  }
320 
321  // try running the main function and catch an error message if thrown
322  try
323  {
324  // set up the ring of coefficients and make a correction to p
325  CoefT::setp (p);
326  p = CoefT::getp ();
327  sout << "Carrying out computations in the ring " <<
328  CoefT::ringsymbol () << ".\n";
329 
330  // set the existence of the empty cell if desired
331  if (reduced)
332  CellT::EmptyType::setExistence (true);
333 
334 #ifdef SPACE_WRAPPING
335  // set wrapping as desired
336  for (int i = 0; i < nWrapping; ++ i)
337  {
338  setWrapping<typename CellT::WrapType>
339  (std::string (wrapping [i]));
340  }
341 #endif
342 
343  // verify the experimental formulas
344  experiment<CellT,CoefT> (Xname);
345 
346  program_time = "Total time used:";
347  program_time = 1;
348  return 0;
349  }
350  catch (const char *msg)
351  {
352  sout << "ERROR: " << msg << '\n';
353  return -1;
354  }
355  catch (const std::exception &e)
356  {
357  sout << "ERROR: " << e. what () << '\n';
358  return -1;
359  }
360  catch (...)
361  {
362  sout << "ABORT: An unknown error occurred.\n";
363  return -1;
364  }
365 
366 } /* main */
367 
Elements of the ring Z_p.
Boundary computation at the level of chains of cells.
A cubical version of the Alexander-Whitney diagonal.
int main(int argc, char *argv [])
The main procedure of the program.
A linear map for coefficients in an arbitrary commutative ring.
A simplex with vertices of arbitrary type.
Definition: simplex.h:59
void experiment(const char *Xname)
Runs the experimental calculations.
The Shih operator as a component of the Eilenberg-Zilber chain contraction.
A chain with coefficients in an arbitrary ring.
Definition: chain.h:50
A combinatorial linear map (for coefficients in Z_2).
Tools for coordinate wrapping, a.k.a.
void EZ_SHI(const SimCellT &s1, const SimCellT &s2, tCombChain< ProdCellT > &t)
Computes the Shih operator on a product of two simplicial cells.
Definition: ez_shi.h:55
A cubical cell.
Tensor of chains.
Definition: tensor.h:52
void computeBoundary(const CellT &c, tCombChain< CellT > &b, const CellRestrT &restr)
Adds the boundary of a given cell to the provided chain (takes boundary cells restricted by the given...
Definition: boundary.h:118
void EZ_EML(const SimCellT &s1, const SimCellT &s2, tCombChain< ProdCellT > &t)
Computes the Eilenberg-Mac Lane operator on a tensor product of two simplicial cells.
Definition: ez_eml.h:56
A class for naming cells for nice text data output.
void EZ_AW(const SimCellT &s1, const SimCellT &s2, tCombTensor< SimCellT, SimCellT > &t)
Computes the Alexander-Whitney operator on a product of simplicial cells.
Definition: ez_aw.h:76
Hashing keys for std::string.
A chain with coefficients in an arbitrary commutative ring.
A simplex class with arbitrary vertices.
The Alexander-Whitney operator as a component of the Eilenberg-Zilber chain contraction.
const char * helpinfo
Brief help information on the program&#39;s usage.
Definition: experiment2.cpp:82
The decision on whether the empty cell should be used as a valid cell of dimension -1...
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
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.
An elementary cubical cell with vertex coordinates of integer type.
Definition: cubcell.h:63
const char * title
The title of the program and licensing information.
Definition: experiment2.cpp:76