The ChainCon Software (Release 0.03)
homcohom.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// Computation of the (co)homology groups of an algebraic minimal model.
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 March 24, 2009. Last revision: January 13, 2013.
26 
27 
28 #ifndef _CHAINCON_HOMCOHOM_H_
29 #define _CHAINCON_HOMCOHOM_H_
30 
31 
32 // include some standard C++ header files
33 //#include <istream>
34 #include <ostream>
35 #include <string>
36 #include <sstream>
37 #include <vector>
38 //#include <algorithm>
39 
40 // include selected header files from the CHomP library
41 #include "chomp/system/config.h"
42 //#include "chomp/struct/multitab.h"
43 //#include "chomp/struct/hashsets.h"
44 
45 
46 // --------------------------------------------------
47 // -------------------- homology --------------------
48 // --------------------------------------------------
49 
50 /// Computes Betti numbers and torsion coefficients for (co)homology
51 /// from an algebraic minimal model (set "cohomology" to "false"
52 /// for homology and to "true" for cohomology).
53 /// Saves representant cells (first for the free part, then for torsion)
54 /// unless the pointer is NULL.
55 template <class CellT, class CoefT, class CellArray1, class CellArray2,
56  class CellArray3, class CoefArray>
57 void getHomCohom (const CellArray1 &H, const CellArray2 &A,
58  const CellArray3 &B, const CoefArray &Q,
59  std::vector<std::vector<CellT> > *representants,
60  std::vector<int> *bettiNumbers,
61  std::vector<std::vector<CoefT> > *torsion,
62  bool cohomology)
63 {
64  // analyze the homology generators not related to torsion first
65  int_t Hsize = H. size ();
66  for (int_t i = 0; i < Hsize; ++ i)
67  {
68  // determine the dimension of the representant cell
69  int dim = H [i]. dim ();
70 
71  if (representants)
72  {
73  // extend the vector for representants if necessary
74  while (static_cast<int> ((*representants). size ())
75  <= dim)
76  {
77  std::vector<CellT> empty;
78  (*representants). push_back (empty);
79  }
80 
81  // append the (co)homology generator representant
82  (*representants) [dim]. push_back (H [i]);
83  }
84 
85  if (bettiNumbers)
86  {
87  // extend the array of Betti numbers if necessary
88  while (static_cast<int> ((*bettiNumbers). size ())
89  <= dim)
90  {
91  (*bettiNumbers). push_back (0);
92  }
93 
94  // increase the relevant Betti number
95  ++ (*bettiNumbers) [dim];
96  }
97  }
98 
99  // gather the torsion information
100  int_t Bsize = B. size ();
101  if ((Bsize != static_cast<int_t> (A. size ())) ||
102  (Bsize != static_cast<int_t> (Q. size ())))
103  {
104  throw "Mismatched sizes of torsion arrays.";
105  }
106  for (int_t i = 0; i < Bsize; ++ i)
107  {
108  // determine the dimension of the representant cell
109  int dim = cohomology ? A [i]. dim () : B [i]. dim ();
110 
111  if (representants)
112  {
113  // extend the vector for representants if necessary
114  while (static_cast<int> ((*representants). size ())
115  <= dim)
116  {
117  std::vector<CellT> empty;
118  (*representants). push_back (empty);
119  }
120 
121  // append the homology generator representant
122  (*representants) [dim]. push_back
123  (cohomology ? A [i] : B [i]);
124  }
125 
126  if (torsion)
127  {
128  // extend the vector for torsion if necessary
129  while (static_cast<int> ((*torsion). size ()) <= dim)
130  {
131  std::vector<CoefT> empty;
132  (*torsion). push_back (empty);
133  }
134 
135  // append the torsion coefficient
136  (*torsion) [dim]. push_back (Q [i]);
137  }
138  }
139 
140  return;
141 } /* getHomCohom */
142 
143 /// Returns a text description of one (co)homology group.
144 /// Torsion coefficients can be either shown using subscripts
145 /// after the ring symbol, e.g. Z_p, or as quotient rings, e.g. Z/pZ,
146 /// depending on the option selected.
147 template <class CoefT>
148 std::string homcohom2text (int bettiNumber,
149  const std::vector<CoefT> &torsion, bool torsion_subscript)
150 {
151  // return the zero group if this is the case
152  if (!bettiNumber && !torsion. size ())
153  return std::string ("0");
154 
155  // prepare variables
156  std::ostringstream s;
157  std::string ring (CoefT::ringsymbol ());
158 
159  // output the free part
160  if (bettiNumber == 1)
161  s << ring;
162  else if (bettiNumber > 1)
163  s << ring << "^" << bettiNumber;
164 
165  // output the torsion part
166  if (torsion. size ())
167  {
168  int size = torsion. size ();
169  for (int i = 0; i < size; ++ i)
170  {
171  if (bettiNumber || i)
172  s << " + ";
173  if (torsion_subscript)
174  s << ring << "_" << torsion [i];
175  else
176  s << ring << "/" << torsion [i] << ring;
177  }
178  }
179 
180  return s. str ();
181 } /* homcohom2text */
182 
183 /// Returns a text description of all the homology groups
184 /// of an algebraic minimal model.
185 /// Torsion coefficients can be either shown as subscript of the ring symbol,
186 /// e.g. Z_p, or as a quotient, e.g. Z/pZ, depending on the option selected.
187 template <class CoefT>
188 std::string homcohom2text (const std::vector<int> &bettiNumbers,
189  const std::vector<std::vector<CoefT> > &torsion,
190  const std::string &symbol, bool torsion_subscript)
191 {
192  std::ostringstream s;
193  int maxCounter = std::max (bettiNumbers. size (), torsion. size ());
194  std::vector<CoefT> empty;
195  for (int dim = 0; dim < maxCounter; ++ dim)
196  {
197  int bettiNumber ((static_cast<int> (bettiNumbers. size ())
198  <= dim) ? 0 : bettiNumbers [dim]);
199  const std::vector<CoefT> &tor
200  ((static_cast<int> (torsion. size ()) <= dim) ?
201  empty : torsion [dim]);
202  s << symbol << dim << " = " << homcohom2text
203  (bettiNumber, tor, torsion_subscript) << "\n";
204  }
205  return s. str ();
206 } /* homcohom2text */
207 
208 
209 #endif // _CHAINCON_HOMCOHOM_H_
210 
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
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