The ChainCon Software (Release 0.03)
comblinmap.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A combinatorial linear map (for coefficients in Z_2).
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: June 20, 2012.
26 
27 
28 #ifndef _CHAINCON_COMBLINMAP_H_
29 #define _CHAINCON_COMBLINMAP_H_
30 
31 
32 // include some standard C++ header files
33 #include <istream>
34 #include <ostream>
35 
36 // include selected header files from the CHomP library
37 #include "chomp/system/config.h"
38 #include "chomp/struct/hashsets.h"
39 #include "chomp/struct/multitab.h"
40 
41 // include relevant local header files
42 #include "chaincon/combchain.h"
43 #include "chaincon/combtensor.h"
44 #include "chaincon/ringz2.h"
45 
46 
47 // --------------------------------------------------
48 // ------------ combinatorial linear map ------------
49 // --------------------------------------------------
50 
51 /// A combinatorial linear map.
52 /// This is in fact a finite set of cells mapped to a chain each.
53 /// It corresponds to a linear map on the vector space
54 /// generated by the cells over the coefficients field F_2 (a.k.a. Z_2).
55 template <class CellDomT, class CellImgT>
57 {
58 public:
59  /// The type of cells in the domain of the combinatorial linear map.
60  typedef CellDomT CellDomType;
61 
62  /// The type of cells in the images of the combinatorial linear map.
63  typedef CellImgT CellImgType;
64 
65  /// The type of coefficients in the chain.
66  typedef tZ2 CoefType;
67 
68  /// The type of the corresponding domain chain.
70 
71  /// The type of the corresponding image chain.
73 
74  /// The type of the corresponding domain chain.
76 
77  /// The type of the corresponding image chain.
79 
80  /// The default constructor.
81  tCombLinMap ();
82 
83  /// Adds a cell to the domain of the map, with zero image,
84  /// and returns its number.
85  int_t add (const CellDomT &c);
86 
87  /// Adds a cell to the image of the given cell.
88  void add (const CellDomT &c, const CellImgT &d);
89 
90  /// Adds a chain to the image of the given cell.
91  void add (const CellDomT &c, const tCombChain<CellImgT> &ch);
92 
93  // Swaps the image of the given cell with the provided chain.
94  // This method is much faster than using "add".
95 // void swapImage (const CellDomT &c, tCombChain<CellImgT> &ch);
96 
97  /// Adds another map to the given map.
100 
101  /// Replaces all the coefficients in the map with their negation.
103 
104  /// Removes the given cell from the domain of the map.
105  /// Note that this may change the order of cells in the domain.
106  void removenum (int_t n);
107 
108  /// Removes the given cell from the domain of the map.
109  /// Note that this may change the order of cells in the domain.
110  void remove (const CellDomT &c);
111 
112  /// Returns the image of the given cell.
113  const tCombChain<CellImgT> &operator () (int_t n) const;
114 
115  /// Returns the image of the given cell.
116  tCombChain<CellImgT> operator () (const CellDomT &c) const;
117 
118  /// Computes the image of the given chain.
119  tCombChain<CellImgT> operator ()
120  (const tCombChain<CellDomT> &ch) const;
121 
122  /// Computes the image of the given tensor.
124  (const tCombTensor<CellDomT,CellDomT> &ch) const;
125 
126  /// Returns the number of elements in the domain
127  /// of the combinatorial map. Note that the images
128  /// of some of the elements might be zero.
129  int_t size () const;
130 
131  /// Returns the n-th element of the domain of the combinatorial map.
132  const CellDomT &operator [] (int_t n) const;
133 
134  /// Returns the image of the given cell for editing.
135  tCombChain<CellImgT> &getImage (int_t n);
136 
137  /// Returns the image of the given cell for editing.
138  tCombChain<CellImgT> &getImage (const CellDomT &c);
139 
140  /// Verifies if the two combinatorial linear maps are the same.
141  bool operator == (const tCombLinMap<CellDomT,CellImgT> &f) const;
142 
143  /// Swaps the data with another combinatorial linear map.
145 
146 private:
147  /// The domain of the map.
148  chomp::homology::hashedset<CellDomT> domain;
149 
150  /// The chains in the images of each domain element.
151  chomp::homology::multitable<tCombChain<CellImgT> > images;
152 
153 }; /* class tCombLinMap */
154 
155 // --------------------------------------------------
156 
157 template <class CellDomT, class CellImgT>
159  domain (1000), images (256)
160 {
161  return;
162 } /* tCombLinMap::tCombLinMap */
163 
164 template <class CellDomT, class CellImgT>
165 inline int_t tCombLinMap<CellDomT,CellImgT>::add (const CellDomT &c)
166 {
167  return domain. add (c);
168 } /* tCombLinMap::add */
169 
170 template <class CellDomT, class CellImgT>
172  (const CellDomT &c, const CellImgT &d)
173 {
174  int_t n = domain. getnumber (c);
175  if (n < 0)
176  {
177  n = domain. size ();
178  domain. add (c);
179  }
180  images [n]. add (d);
181  return;
182 } /* tCombLinMap::add */
183 
184 template <class CellDomT, class CellImgT>
186  (const CellDomT &c, const tCombChain<CellImgT> &ch)
187 {
188  int_t n = domain. getnumber (c);
189  if (n < 0)
190  {
191  n = domain. size ();
192  domain. add (c);
193  }
194  images [n] += ch;
195  return;
196 } /* tCombLinMap::add */
197 
198 /*
199 template <class CellDomT, class CellImgT>
200 inline void tCombLinMap<CellDomT,CellImgT>::swapImage
201  (const CellDomT &c, tCombChain<CellImgT> &ch)
202 {
203  int_t n = domain. getnumber (c);
204  if (n < 0)
205  {
206  n = domain. size ();
207  domain. add (c);
208  }
209  images [n]. swap (ch);
210  return;
211 }*/ /* tCombLinMap::swapImage */
212 
213 template <class CellDomT, class CellImgT>
217 {
218  int_t size = f. domain. size ();
219  for (int_t i = 0; i < size; ++ i)
220  {
221  if (f. images [i]. empty ())
222  continue;
223  this -> add (f. domain [i], f. images [i]);
224  }
225  return *this;
226 } /* tCombLinMap::operator += */
227 
228 template <class CellDomT, class CellImgT>
231 {
232  return *this;
233 } /* tCombLinMap::negate */
234 
235 template <class CellDomT, class CellImgT>
237 {
238  domain. removenum (n);
239  tCombChain<CellImgT> empty;
240  images [n] = empty;
241  if (n != domain. size ())
242  images [n]. swap (images [domain. size ()]);
243  return;
244 } /* tCombLinMap::removenum */
245 
246 template <class CellDomT, class CellImgT>
247 inline void tCombLinMap<CellDomT,CellImgT>::remove (const CellDomT &c)
248 {
249  int_t n = domain. getnumber (c);
250  if (n >= 0)
251  removenum (n);
252  return;
253 } /* tCombLinMap::remove */
254 
255 template <class CellDomT, class CellImgT>
256 inline const tCombChain<CellImgT> &
258 {
259  return images [n];
260 } /* tCombLinMap::operator () */
261 
262 template <class CellDomT, class CellImgT>
264  (const CellDomT &c) const
265 {
266  int_t n = domain. getnumber (c);
267  if (n >= 0)
268  return images [n];
269  else
270  return tCombChain<CellImgT> ();
271 } /* tCombLinMap::operator () */
272 
273 template <class CellDomT, class CellImgT>
275  (const tCombChain<CellDomT> &ch) const
276 {
277  tCombChain<CellImgT> image;
278  int_t size = ch. size ();
279  for (int_t i = 0; i < size; ++ i)
280  {
281  int_t n = domain. getnumber (ch. getCell (i));
282  if (n >= 0)
283  image += images [n];
284  }
285  return image;
286 } /* tCombLinMap::operator () */
287 
288 template <class CellDomT, class CellImgT>
292 {
294  int_t size = ch. size ();
295  for (int_t i = 0; i < size; ++ i)
296  {
297  int_t n1 = domain. getnumber (ch. left (i));
298  int_t n2 = domain. getnumber (ch. right (i));
299  if ((n1 >= 0) && (n2 >= 0))
300  image. add (images [n1], images [n2]);
301  }
302  return image;
303 } /* tCombLinMap::operator () */
304 
305 template <class CellDomT, class CellImgT>
307 {
308  return domain. size ();
309 } /* tCombLinMap::size */
310 
311 template <class CellDomT, class CellImgT>
312 inline const CellDomT &tCombLinMap<CellDomT,CellImgT>::operator []
313  (int_t n) const
314 {
315  return domain [n];
316 } /* tCombLinMap::operator [] */
317 
318 template <class CellDomT, class CellImgT>
320  (int_t n)
321 {
322  return images [n];
323 } /* tCombLinMap::getImage */
324 
325 template <class CellDomT, class CellImgT>
327  (const CellDomT &c)
328 {
329  int_t n = domain. getnumber (c);
330  if (n < 0)
331  {
332  n = domain. size ();
333  domain. add (c);
334  }
335  return images [n];
336 } /* tCombLinMap::getImage */
337 
338 template <class CellDomT, class CellImgT>
341 {
342  int_t thisSize = domain. size ();
343  for (int_t i = 0; i < thisSize; ++ i)
344  {
345  const CellDomT &x = domain [i];
346  const tCombChain<CellImgT> &y = images [i];
347  if (y. empty ())
348  continue;
349  int_t n = f. domain. getnumber (x);
350  if (n < 0)
351  return false;
352  if (!(y == f. images [n]))
353  return false;
354  }
355  int_t fSize = f. domain. size ();
356  for (int_t i = 0; i < fSize; ++ i)
357  {
358  const CellDomT &x = f. domain [i];
359  const tCombChain<CellImgT> &y = f. images [i];
360  if (y. empty ())
361  continue;
362  int_t n = domain. getnumber (x);
363  if (n < 0)
364  return false;
365  if (!(y == images [n]))
366  return false;
367  }
368  return true;
369 } /* tCombLinMap::operator == */
370 
371 template <class CellDomT, class CellImgT>
374 {
375  domain. swap (f. domain);
376  images. swap (f. images);
377  return;
378 } /* tCombLinMap::swap */
379 
380 // --------------------------------------------------
381 
382 /// Writes a combinatorial linear map to an output stream in the text mode.
383 template <class CellDomT, class CellImgT>
384 inline std::ostream &operator << (std::ostream &out,
386 {
387  bool trivial = true;
388  int_t size = f. size ();
389  for (int_t n = 0; n < size; ++ n)
390  {
391  const tCombChain<CellImgT> &ch = f (n);
392  if (ch. empty ())
393  continue;
394  trivial = false;
395  const CellDomT &c = f [n];
396  out << c << " -> " << ch << "\n";
397  }
398  if (trivial)
399  out << '0';
400  return out;
401 } /* operator << */
402 
403 /// Reads a combinatorial linear map from an input stream.
404 template <class CellDomT, class CellImgT>
405 inline std::istream &operator >> (std::istream &in,
407 {
408  throw "Operator >> not implemented for tCombLinMap.";
409  return in;
410 } /* operator >> */
411 
412 // --------------------------------------------------
413 
414 /// Adds the identity map on the given domain to the map 'f'.
415 template <class CellArray, class CellT>
416 inline void addIdentity (const CellArray &domain,
418 {
419  int_t size = domain. size ();
420  for (int_t i = 0; i < size; ++ i)
421  {
422  const CellT &c = domain [i];
423  f. add (c, c);
424  }
425  return;
426 } /* addIdentity */
427 
428 /// Returns the identity map on the given domain.
429 template <class CellArray, class CellT>
430 inline tCombLinMap<CellT,CellT> identity (const CellArray &domain)
431 {
433  addIdentity (domain, f);
434  return f;
435 } /* identity */
436 
437 /// Computes the composition of the two given maps.
438 /// More precisely, for each x in the domain of g,
439 /// the new map on x is defined as f(g(x)).
440 template <class CellXT, class CellYT, class CellZT>
441 inline tCombLinMap<CellXT,CellZT> operator *
444 {
446  int_t size = g. size ();
447  for (int_t i = 0; i < size; ++ i)
448  {
449  const CellXT &x = g [i];
450  const tCombChain<CellYT> &y = g (i);
451  const tCombChain<CellZT> z = f (y);
452  if (z. empty ())
453  continue;
454  result. add (x, z);
455  }
456  return result;
457 } /* operator * */
458 
459 
460 #endif // _CHAINCON_COMBLINMAP_H_
461 
tCombChain< CellDomT > ChainDomType
The type of the corresponding domain chain.
Definition: comblinmap.h:69
tCombChain< CellImgT > ChainImgType
The type of the corresponding image chain.
Definition: comblinmap.h:72
tCombChain< CellImgT > & getImage(int_t n)
Returns the image of the given cell for editing.
Definition: comblinmap.h:320
tZ2 CoefType
The type of coefficients in the chain.
Definition: comblinmap.h:66
An element of the ring Z_2.
Definition: ringz2.h:47
void swap(tCombLinMap< CellDomT, CellImgT > &f)
Swaps the data with another combinatorial linear map.
Definition: comblinmap.h:373
int_t size() const
Returns the number of elements in the domain of the combinatorial map.
Definition: comblinmap.h:306
chomp::homology::multitable< tCombChain< CellImgT > > images
The chains in the images of each domain element.
Definition: comblinmap.h:151
A combinatorial chain, that is, a chain with Z_2 coefficients.
const CellDomT & operator[](int_t n) const
Returns the n-th element of the domain of the combinatorial map.
Definition: comblinmap.h:313
std::ostream & operator<<(std::ostream &out, const tCombLinMap< CellDomT, CellImgT > &f)
Writes a combinatorial linear map to an output stream in the text mode.
Definition: comblinmap.h:384
int_t add(const CellDomT &c)
Adds a cell to the domain of the map, with zero image, and returns its number.
Definition: comblinmap.h:165
const tCombChain< CellImgT > & operator()(int_t n) const
Returns the image of the given cell.
Definition: comblinmap.h:257
tCombTensor< CellImgT, CellImgT > TensorImgType
The type of the corresponding image chain.
Definition: comblinmap.h:78
bool operator==(const tCombLinMap< CellDomT, CellImgT > &f) const
Verifies if the two combinatorial linear maps are the same.
Definition: comblinmap.h:340
Combinatorial tensor of cells.
Definition: combtensor.h:53
A combinatorial chain.
Definition: combchain.h:49
A combinatorial tensor (for coefficients in Z_2).
tCombLinMap()
The default constructor.
Definition: comblinmap.h:158
tCombTensor< CellDomT, CellDomT > TensorDomType
The type of the corresponding domain chain.
Definition: comblinmap.h:75
Elements of the ring Z_2.
tCombLinMap< CellDomT, CellImgT > & negate()
Replaces all the coefficients in the map with their negation.
Definition: comblinmap.h:230
std::istream & operator>>(std::istream &in, tCombLinMap< CellDomT, CellImgT > &f)
Reads a combinatorial linear map from an input stream.
Definition: comblinmap.h:405
CellImgT CellImgType
The type of cells in the images of the combinatorial linear map.
Definition: comblinmap.h:63
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
chomp::homology::hashedset< CellDomT > domain
The domain of the map.
Definition: comblinmap.h:148
A combinatorial linear map.
Definition: comblinmap.h:56
tCombLinMap< CellT, CellT > identity(const CellArray &domain)
Returns the identity map on the given domain.
Definition: comblinmap.h:430
void remove(const CellDomT &c)
Removes the given cell from the domain of the map.
Definition: comblinmap.h:247
CellDomT CellDomType
The type of cells in the domain of the combinatorial linear map.
Definition: comblinmap.h:60
void removenum(int_t n)
Removes the given cell from the domain of the map.
Definition: comblinmap.h:236