The ChainCon Software (Release 0.03)
linmap.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A linear map for coefficients in an arbitrary commutative ring.
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: April 12, 2013.
26 
27 
28 #ifndef _CHAINCON_LINMAP_H_
29 #define _CHAINCON_LINMAP_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/chain.h"
43 #include "chaincon/tensor.h"
44 
45 
46 // --------------------------------------------------
47 // ------------------- linear map -------------------
48 // --------------------------------------------------
49 
50 /// A linear map.
51 /// This is in fact a finite set of cells mapped to a chain each.
52 /// It corresponds to a linear map on the vector space
53 /// generated by the cells over the coefficients field F_2 (a.k.a. Z_2).
54 template <class CellDomT, class CellImgT, class CoefT>
55 class tLinMap
56 {
57 public:
58  /// The type of cells in the domain of the linear map.
59  typedef CellDomT CellDomType;
60 
61  /// The type of cells in the images of the linear map.
62  typedef CellImgT CellImgType;
63 
64  /// The type of coefficients in the chain.
65  typedef CoefT CoefType;
66 
67  /// The type of the corresponding domain chain.
69 
70  /// The type of the corresponding image chain.
72 
73  /// The type of the corresponding domain chain.
75 
76  /// The type of the corresponding image chain.
78 
79  /// The default constructor.
80  tLinMap ();
81 
82  /// Adds a cell to the domain of the map, with zero image,
83  /// and returns its number.
84  int_t add (const CellDomT &c);
85 
86  /// Adds a cell to the image of the given cell.
87  void add (const CellDomT &c, const CellImgT &d, const CoefT &coef);
88 
89  /// Adds a chain to the image of the given cell.
90  void add (const CellDomT &c, const tChain<CellImgT,CoefT> &ch);
91 
92  // Swaps the image of the given cell with the provided chain.
93  // This method is much faster than using "add".
94 // void swapImage (const CellDomT &c, tChain<CellImgT,CoefT> &ch);
95 
96  /// Adds another map to the given map.
99 
100  /// Replaces all the coefficients in the map with their negation.
102 
103  /// Multiplies all the coefficients in the map by the given number.
105 
106  /// Removes the given cell from the domain of the map.
107  /// Note that this may change the order of cells in the domain.
108  void removenum (int_t n);
109 
110  /// Removes the given cell from the domain of the map.
111  /// Note that this may change the order of cells in the domain.
112  void remove (const CellDomT &c);
113 
114  /// Returns the image of the given cell.
115  const tChain<CellImgT,CoefT> &operator () (int_t n) const;
116 
117  /// Returns the image of the given cell.
118  tChain<CellImgT,CoefT> operator () (const CellDomT &c) const;
119 
120  /// Computes the image of the given chain.
121  tChain<CellImgT,CoefT> operator ()
122  (const tChain<CellDomT,CoefT> &ch) const;
123 
124  /// Computes the image of the given tensor.
126  (const tTensor<CellDomT,CellDomT,CoefT> &ch) const;
127 
128  /// Returns the number of elements in the domain
129  /// of the map. Note that the images
130  /// of some of the elements might be zero.
131  int_t size () const;
132 
133  /// Returns the n-th element of the domain of the map.
134  const CellDomT &operator [] (int_t n) const;
135 
136  /// Returns the domain of the map.
137  const chomp::homology::hashedset<CellDomT> &getDomain () const;
138 
139  /// Returns the image of the given cell for editing.
140  tChain<CellImgT,CoefT> &getImage (int_t n);
141 
142  /// Returns the image of the given cell for editing.
143  tChain<CellImgT,CoefT> &getImage (const CellDomT &c);
144 
145  /// Verifies if the two linear maps are the same.
146  bool operator == (const tLinMap<CellDomT,CellImgT,CoefT> &f) const;
147 
148  /// Swaps the data with another linear map.
150 
151 private:
152  /// The domain of the map.
153  chomp::homology::hashedset<CellDomT> domain;
154 
155  /// The chains in the images of each domain element.
156  chomp::homology::multitable<tChain<CellImgT,CoefT> > images;
157 
158 }; /* class tLinMap */
159 
160 // --------------------------------------------------
161 
162 template <class CellDomT, class CellImgT, class CoefT>
164  domain (1000), images (256)
165 {
166  return;
167 } /* tLinMap::tLinMap */
168 
169 template <class CellDomT, class CellImgT, class CoefT>
170 inline int_t tLinMap<CellDomT,CellImgT,CoefT>::add (const CellDomT &c)
171 {
172  return domain. add (c);
173 } /* tLinMap::add */
174 
175 template <class CellDomT, class CellImgT, class CoefT>
177  (const CellDomT &c, const CellImgT &d, const CoefT &coef)
178 {
179  int_t n = domain. getnumber (c);
180  if (n < 0)
181  {
182  n = domain. size ();
183  domain. add (c);
184  }
185  images [n]. add (d, coef);
186  return;
187 } /* tLinMap::add */
188 
189 template <class CellDomT, class CellImgT, class CoefT>
191  (const CellDomT &c, const tChain<CellImgT,CoefT> &ch)
192 {
193  int_t n = domain. getnumber (c);
194  if (n < 0)
195  {
196  n = domain. size ();
197  domain. add (c);
198  }
199  images [n] += ch;
200  return;
201 } /* tLinMap::add */
202 
203 /*
204 template <class CellDomT, class CellImgT, class CoefT>
205 inline void tLinMap<CellDomT,CellImgT,CoefT>::swapImage
206  (const CellDomT &c, tChain<CellImgT,CoefT> &ch)
207 {
208  int_t n = domain. getnumber (c);
209  if (n < 0)
210  {
211  n = domain. size ();
212  domain. add (c);
213  }
214  images [n]. swap (ch);
215  return;
216 }*/ /* tLinMap::swapImage */
217 
218 template <class CellDomT, class CellImgT, class CoefT>
222 {
223  int_t size = f. domain. size ();
224  for (int_t i = 0; i < size; ++ i)
225  {
226  if (f. images [i]. empty ())
227  continue;
228  add (f. domain [i], f. images [i]);
229  }
230  return *this;
231 } /* tLinMap::operator += */
232 
233 template <class CellDomT, class CellImgT, class CoefT>
236 {
237  int_t size = domain. size ();
238  for (int_t i = 0; i < size; ++ i)
239  images [i]. negate ();
240  return *this;
241 } /* tLinMap::negate */
242 
243 template <class CellDomT, class CellImgT, class CoefT>
246 {
247  int_t size = domain. size ();
248  for (int_t i = 0; i < size; ++ i)
249  images [i] *= c;
250  return *this;
251 } /* tLinMap::operator *= */
252 
253 template <class CellDomT, class CellImgT, class CoefT>
255 {
256  domain. removenum (n);
258  images [n] = empty;
259  if (n != domain. size ())
260  images [n]. swap (images [domain. size ()]);
261  return;
262 } /* tLinMap::removenum */
263 
264 template <class CellDomT, class CellImgT, class CoefT>
265 inline void tLinMap<CellDomT,CellImgT,CoefT>::remove (const CellDomT &c)
266 {
267  int_t n = domain. getnumber (c);
268  if (n >= 0)
269  removenum (n);
270  return;
271 } /* tLinMap::remove */
272 
273 template <class CellDomT, class CellImgT, class CoefT>
274 inline const tChain<CellImgT,CoefT> &
276 {
277  return images [n];
278 } /* tLinMap::operator () */
279 
280 template <class CellDomT, class CellImgT, class CoefT>
282  (const CellDomT &c) const
283 {
284  int_t n = domain. getnumber (c);
285  if (n >= 0)
286  return images [n];
287  else
288  return tChain<CellImgT,CoefT> ();
289 } /* tLinMap::operator () */
290 
291 template <class CellDomT, class CellImgT, class CoefT>
293  (const tChain<CellDomT,CoefT> &ch) const
294 {
296  int_t size = ch. size ();
297  for (int_t i = 0; i < size; ++ i)
298  {
299  int_t n = domain. getnumber (ch. getCell (i));
300  if (n < 0)
301  continue;
302  const CoefT &coef (ch. getCoef (i));
303  if (coef == 1)
304  image += images [n];
305  else
306  {
307  tChain<CellImgT,CoefT> img (images [n]);
308  img *= coef;
309  image += img;
310  }
311  }
312  return image;
313 } /* tLinMap::operator () */
314 
315 template <class CellDomT, class CellImgT, class CoefT>
319 {
321  int_t size = ch. size ();
322  for (int_t i = 0; i < size; ++ i)
323  {
324  int_t n1 = domain. getnumber (ch. left (i));
325  if (n1 < 0)
326  continue;
327  int_t n2 = domain. getnumber (ch. right (i));
328  if (n2 < 0)
329  continue;
330  if (ch. coef (i) == 1)
331  image. add (images [n1], images [n2]);
332  else
333  {
334  tChain<CellImgT,CoefT> img1 (images [n1]);
335  img1 *= ch. coef (i);
336  image. add (img1, images [n2]);
337  }
338  }
339  return image;
340 } /* tLinMap::operator () */
341 
342 template <class CellDomT, class CellImgT, class CoefT>
344 {
345  return domain. size ();
346 } /* tLinMap::size */
347 
348 template <class CellDomT, class CellImgT, class CoefT>
349 inline const CellDomT &tLinMap<CellDomT,CellImgT,CoefT>::operator []
350  (int_t n) const
351 {
352  return domain [n];
353 } /* tLinMap::operator [] */
354 
355 template <class CellDomT, class CellImgT, class CoefT>
356 inline const chomp::homology::hashedset<CellDomT> &
358 {
359  return domain;
360 } /* tLinMap::getDomain */
361 
362 template <class CellDomT, class CellImgT, class CoefT>
364  (int_t n)
365 {
366  return images [n];
367 } /* tLinMap::getImage */
368 
369 template <class CellDomT, class CellImgT, class CoefT>
371  (const CellDomT &c)
372 {
373  int_t n = domain. getnumber (c);
374  if (n < 0)
375  {
376  n = domain. size ();
377  domain. add (c);
378  }
379  return images [n];
380 } /* tLinMap::getImage */
381 
382 template <class CellDomT, class CellImgT, class CoefT>
385 {
386  int_t thisSize = domain. size ();
387  for (int_t i = 0; i < thisSize; ++ i)
388  {
389  const CellDomT &x = domain [i];
390  const tChain<CellImgT,CoefT> &y = images [i];
391  if (y. empty ())
392  continue;
393  int_t n = f. domain. getnumber (x);
394  if (n < 0)
395  return false;
396  if (!(y == f. images [n]))
397  return false;
398  }
399  int_t fSize = f. domain. size ();
400  for (int_t i = 0; i < fSize; ++ i)
401  {
402  const CellDomT &x = f. domain [i];
403  const tChain<CellImgT,CoefT> &y = f. images [i];
404  if (y. empty ())
405  continue;
406  int_t n = domain. getnumber (x);
407  if (n < 0)
408  return false;
409  if (!(y == images [n]))
410  return false;
411  }
412  return true;
413 } /* tLinMap::operator == */
414 
415 template <class CellDomT, class CellImgT, class CoefT>
418 {
419  domain. swap (f. domain);
420  images. swap (f. images);
421  return;
422 } /* tLinMap::swap */
423 
424 // --------------------------------------------------
425 
426 /// Writes a linear map to an output stream in the text mode.
427 template <class CellDomT, class CellImgT, class CoefT>
428 inline std::ostream &operator << (std::ostream &out,
430 {
431  bool trivial = true;
432  int_t size = f. size ();
433  for (int_t n = 0; n < size; ++ n)
434  {
435  const tChain<CellImgT,CoefT> &ch = f (n);
436  if (ch. empty ())
437  continue;
438  trivial = false;
439  const CellDomT &c = f [n];
440  out << c << " -> " << ch << "\n";
441  }
442  if (trivial)
443  out << '0';
444  return out;
445 } /* operator << */
446 
447 /// Reads a linear map from an input stream.
448 template <class CellDomT, class CellImgT, class CoefT>
449 inline std::istream &operator >> (std::istream &in,
451 {
452  throw "Operator >> not implemented for tLinMap.";
453  return in;
454 } /* operator >> */
455 
456 // --------------------------------------------------
457 
458 /// Adds the identity map on the given domain to the map 'f'.
459 template <class CellArray, class CellT, class CoefT>
460 inline void addIdentity (const CellArray &domain,
462 {
463  int_t size = domain. size ();
464  for (int_t i = 0; i < size; ++ i)
465  {
466  const CellT &c = domain [i];
467  f. add (c, c, CoefT (1));
468  }
469  return;
470 } /* addIdentity */
471 
472 /// Returns the identity map on the given domain.
473 template <class CellArray, class CellT, class CoefT>
474 inline tLinMap<CellT,CellT,CoefT> identity (const CellArray &domain)
475 {
477  addIdentity (domain, f);
478  return f;
479 } /* identity */
480 
481 /// Computes the composition of the two given maps.
482 /// More precisely, for each x in the domain of g,
483 /// the new map on x is defined as f(g(x)).
484 template <class CellXT, class CellYT, class CellZT, class CoefT>
485 inline tLinMap<CellXT,CellZT,CoefT> operator *
488 {
490  int_t size = g. size ();
491  for (int_t i = 0; i < size; ++ i)
492  {
493  const CellXT &x = g [i];
494  const tChain<CellYT,CoefT> &y = g (i);
495  const tChain<CellZT,CoefT> z = f (y);
496  if (z. empty ())
497  continue;
498  result. add (x, z);
499  }
500  return result;
501 } /* operator * */
502 
503 
504 #endif // _CHAINCON_LINMAP_H_
505 
void swap(tLinMap< CellDomT, CellImgT, CoefT > &f)
Swaps the data with another linear map.
Definition: linmap.h:417
std::istream & operator>>(std::istream &in, tLinMap< CellDomT, CellImgT, CoefT > &f)
Reads a linear map from an input stream.
Definition: linmap.h:449
std::ostream & operator<<(std::ostream &out, const tLinMap< CellDomT, CellImgT, CoefT > &f)
Writes a linear map to an output stream in the text mode.
Definition: linmap.h:428
CellImgT CellImgType
The type of cells in the images of the linear map.
Definition: linmap.h:62
void remove(const CellDomT &c)
Removes the given cell from the domain of the map.
Definition: linmap.h:265
int_t add(const CellDomT &c)
Adds a cell to the domain of the map, with zero image, and returns its number.
Definition: linmap.h:170
tChain< CellImgT, CoefT > & getImage(int_t n)
Returns the image of the given cell for editing.
Definition: linmap.h:364
tTensor< CellDomT, CellDomT, CoefT > TensorDomType
The type of the corresponding domain chain.
Definition: linmap.h:74
void removenum(int_t n)
Removes the given cell from the domain of the map.
Definition: linmap.h:254
tLinMap< CellDomT, CellImgT, CoefT > & operator*=(const CoefT &c)
Multiplies all the coefficients in the map by the given number.
Definition: linmap.h:245
tTensor< CellImgT, CellImgT, CoefT > TensorImgType
The type of the corresponding image chain.
Definition: linmap.h:77
tLinMap< CellT, CellT, CoefT > identity(const CellArray &domain)
Returns the identity map on the given domain.
Definition: linmap.h:474
A chain with coefficients in an arbitrary ring.
Definition: chain.h:50
A tensor of chains with coefficients in an arbitrary commutative ring.
bool operator==(const tLinMap< CellDomT, CellImgT, CoefT > &f) const
Verifies if the two linear maps are the same.
Definition: linmap.h:384
A linear map.
Definition: linmap.h:55
Tensor of chains.
Definition: tensor.h:52
const CellDomT & operator[](int_t n) const
Returns the n-th element of the domain of the map.
Definition: linmap.h:350
chomp::homology::hashedset< CellDomT > domain
The domain of the map.
Definition: linmap.h:153
CellDomT CellDomType
The type of cells in the domain of the linear map.
Definition: linmap.h:59
const tChain< CellImgT, CoefT > & operator()(int_t n) const
Returns the image of the given cell.
Definition: linmap.h:275
CoefT CoefType
The type of coefficients in the chain.
Definition: linmap.h:65
int_t size() const
Returns the number of elements in the domain of the map.
Definition: linmap.h:343
A chain with coefficients in an arbitrary commutative ring.
chomp::homology::multitable< tChain< CellImgT, CoefT > > images
The chains in the images of each domain element.
Definition: linmap.h:156
const chomp::homology::hashedset< CellDomT > & getDomain() const
Returns the domain of the map.
Definition: linmap.h:357
void addIdentity(const CellArray &domain, tLinMap< CellT, CellT, CoefT > &f)
Adds the identity map on the given domain to the map &#39;f&#39;.
Definition: linmap.h:460
tChain< CellImgT, CoefT > ChainImgType
The type of the corresponding image chain.
Definition: linmap.h:71
tChain< CellDomT, CoefT > ChainDomType
The type of the corresponding domain chain.
Definition: linmap.h:68
tLinMap()
The default constructor.
Definition: linmap.h:163
tLinMap< CellDomT, CellImgT, CoefT > & negate()
Replaces all the coefficients in the map with their negation.
Definition: linmap.h:235