The ChainCon Software (Release 0.03)
chain.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A chain with 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: December 3, 2014.
26 
27 
28 #ifndef _CHAINCON_CHAIN_H_
29 #define _CHAINCON_CHAIN_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 
40 // include relevant local header files
41 #include "chaincon/extarray.h"
42 
43 
44 // --------------------------------------------------
45 // --------------------- chain ----------------------
46 // --------------------------------------------------
47 
48 /// A chain with coefficients in an arbitrary ring.
49 template <class CellT, class CoefT>
50 class tChain
51 {
52 public:
53  /// The type of cells in the chain.
54  typedef CellT CellType;
55 
56  /// The type of coefficients in the chain.
57  typedef CoefT CoefType;
58 
59  /// The default constructor of an empty chain.
60  tChain ();
61 
62  /// Returns the number of elements in the chain.
63  int_t size () const;
64 
65  /// Returns true if and only if the chain is zero.
66  bool empty () const;
67 
68  /// Returns the n-th cell of the chain.
69  const CellT &getCell (int_t n) const;
70 
71  /// Returns the coefficient at the n-th cell of the chain.
72  const CoefT &getCoef (int_t n) const;
73 
74  /// Returns the position of the given cell in the chain
75  /// or -1 if the cell is not there.
76  int_t position (const CellT &c) const;
77 
78  /// Adds a given cell to the chain with the given coefficient.
79  void add (const CellT &cell, const CoefT &coef);
80 
81  /// Adds a given chain to the chain.
83 
84  /// Negates all the coefficients in the chain.
86 
87  /// Multiples the chain by a given coefficient.
88  tChain<CellT,CoefT> &operator *= (const CoefT &coef);
89 
90  /// Compares if the two chains are equal.
91  bool operator == (const tChain<CellT,CoefT> &ch) const;
92 
93  /// Swaps the data with another chain.
94  void swap (tChain<CellT,CoefT> &ch);
95 
96 private:
97  /// The set of elements in the chain.
98  chomp::homology::hashedset<CellT> cells;
99 
100  /// The coefficients that appear at the cells in the chain.
102 
103 }; /* class tChain */
104 
105 // --------------------------------------------------
106 
107 template <class CellT, class CoefT>
109  cells (1), coefs ()
110 {
111  return;
112 } /* tChain::tChain */
113 
114 template <class CellT, class CoefT>
115 inline int_t tChain<CellT,CoefT>::size () const
116 {
117  return cells. size ();
118 } /* tChain::size */
119 
120 template <class CellT, class CoefT>
121 inline bool tChain<CellT,CoefT>::empty () const
122 {
123  return cells. empty ();
124 } /* tChain::empty */
125 
126 template <class CellT, class CoefT>
127 inline const CellT &tChain<CellT,CoefT>::getCell (int_t n) const
128 {
129  return cells [n];
130 } /* tChain::operator [] */
131 
132 template <class CellT, class CoefT>
133 inline const CoefT &tChain<CellT,CoefT>::getCoef (int_t n) const
134 {
135  return coefs [n];
136 } /* tChain::operator [] */
137 
138 template <class CellT, class CoefT>
139 inline int_t tChain<CellT,CoefT>::position (const CellT &c) const
140 {
141  return cells. getnumber (c);
142 } /* tChain::position */
143 
144 template <class CellT, class CoefT>
145 inline void tChain<CellT,CoefT>::add (const CellT &cell, const CoefT &coef)
146 {
147  int_t pos = cells. getnumber (cell);
148  if (pos < 0)
149  {
150  pos = cells. size ();
151  cells. add (cell);
152  coefs [pos] = coef;
153  }
154  else
155  {
156  CoefT &c = coefs [pos];
157  c += coef;
158  if (c == 0)
159  {
160  cells. removenum (pos);
161  int_t size = cells. size ();
162  if (pos != size)
163  {
164  coefs [pos]. swap (coefs [size]);
165  }
166  }
167  }
168  return;
169 } /* tChain::add */
170 
171 template <class CellT, class CoefT>
174 {
175  if (empty ())
176  {
177  tChain<CellT,CoefT> chainCopy (ch);
178  swap (chainCopy);
179  }
180  else
181  {
182  int_t size = ch. size ();
183  for (int_t i = 0; i < size; ++ i)
184  add (ch. getCell (i), ch. getCoef (i));
185  }
186  return *this;
187 } /* tChain::operator += */
188 
189 template <class CellT, class CoefT>
191 {
192  int_t size = cells. size ();
193  for (int_t i = 0; i < size; ++ i)
194  coefs [i]. negate ();
195  return *this;
196 } /* tChain::negate */
197 
198 template <class CellT, class CoefT>
199 inline tChain<CellT,CoefT> &tChain<CellT,CoefT>::operator *=
200  (const CoefT &coef)
201 {
202  if (coef == 0)
203  {
204  chomp::homology::hashedset<CellT> emptyCells (1);
205  cells. swap (emptyCells);
206  extarray<CoefT> emptyCoefs;
207  coefs. swap (emptyCoefs);
208  }
209  else
210  {
211  int_t size = cells. size ();
212  for (int_t i = 0; i < size; ++ i)
213  coefs [i] *= coef;
214  }
215  return *this;
216 } /* tChain::operator *= */
217 
218 template <class CellT, class CoefT>
220  const
221 {
222  int_t size = cells. size ();
223  if (size != ch. cells. size ())
224  return false;
225  for (int_t pos = 0; pos < size; ++ pos)
226  {
227  int_t chPos = ch. cells. getnumber (cells [pos]);
228  if (chPos < 0)
229  return false;
230  if (!(coefs [pos] == ch. coefs [chPos]))
231  return false;
232  }
233  return true;
234 } /* tChain::operator == */
235 
236 template <class CellT, class CoefT>
238 {
239  cells. swap (ch. cells);
240  coefs. swap (ch. coefs);
241  return;
242 } /* tChain::swap */
243 
244 // --------------------------------------------------
245 
246 /// Writes a chain to an output stream.
247 template <class CellT, class CoefT>
248 std::ostream &operator << (std::ostream &out, const tChain<CellT,CoefT> &c)
249 {
250  int_t size = c. size ();
251  if (!size)
252  out << '0';
253  else
254  {
255  for (int_t i = 0; i < size; ++ i)
256  {
257  if (i)
258  out << " + ";
259  out << c. getCoef (i) << " * " << c. getCell (i);
260  }
261  }
262  return out;
263 } /* operator << */
264 
265 /// Reads a chain from an input stream.
266 template <class CellT, class CoefT>
267 std::istream &operator >> (std::istream &in, tChain<CellT,CoefT> &c)
268 {
269  throw "Operator >> not implemented for tChain.";
270  return in;
271 } /* operator >> */
272 
273 // --------------------------------------------------
274 
275 /// Computes the sum of two chains.
276 template <class CellT, class CoefT>
278  const tChain<CellT,CoefT> &b)
279 {
280  tChain<CellT,CoefT> c (a);
281  c += b;
282  return c;
283 } /* operator + */
284 
285 /// Computes the dot product of two chains.
286 template <class CellT, class CoefT>
288  const tChain<CellT,CoefT> &b)
289 {
290  // determine the shorter and the longer of the two chains
291  const tChain<CellT,CoefT> &shorter
292  ((a. size () < b. size ()) ? a : b);
293  const tChain<CellT,CoefT> &longer
294  ((a. size () < b. size ()) ? b : a);
295 
296  // prepare a ring element for the dot product
297  CoefT c (0);
298 
299  // scan the shorter chain
300  int_t length = shorter. length ();
301  for (int_t i = 0; i < length; ++ i)
302  {
303  // find the cell in the longer chain
304  int_t j = longer. position (a. getCell (i));
305  if (j < 0)
306  continue;
307 
308  // add the product of the coefficients to the result
309  CoefT x (a. getCoef (i));
310  x *= b. getCoef (j);
311  c += x;
312  }
313 
314  return c;
315 } /* operator * */
316 
317 
318 #endif // _CHAINCON_CHAIN_H_
319 
An extendible array.
chomp::homology::hashedset< CellT > cells
The set of elements in the chain.
Definition: chain.h:98
CoefT operator*(const tChain< CellT, CoefT > &a, const tChain< CellT, CoefT > &b)
Computes the dot product of two chains.
Definition: chain.h:287
CellT CellType
The type of cells in the chain.
Definition: chain.h:54
bool empty() const
Returns true if and only if the chain is zero.
Definition: chain.h:121
const CoefT & getCoef(int_t n) const
Returns the coefficient at the n-th cell of the chain.
Definition: chain.h:133
A chain with coefficients in an arbitrary ring.
Definition: chain.h:50
CoefT CoefType
The type of coefficients in the chain.
Definition: chain.h:57
void add(const CellT &cell, const CoefT &coef)
Adds a given cell to the chain with the given coefficient.
Definition: chain.h:145
tChain< CellT, CoefT > operator+(const tChain< CellT, CoefT > &a, const tChain< CellT, CoefT > &b)
Computes the sum of two chains.
Definition: chain.h:277
int_t position(const CellT &c) const
Returns the position of the given cell in the chain or -1 if the cell is not there.
Definition: chain.h:139
bool operator==(const tChain< CellT, CoefT > &ch) const
Compares if the two chains are equal.
Definition: chain.h:219
tChain< CellT, CoefT > & operator+=(const tChain< CellT, CoefT > &ch)
Adds a given chain to the chain.
Definition: chain.h:173
int_t size() const
Returns the number of elements in the chain.
Definition: chain.h:115
const CellT & getCell(int_t n) const
Returns the n-th cell of the chain.
Definition: chain.h:127
tChain< CellT, CoefT > & negate()
Negates all the coefficients in the chain.
Definition: chain.h:190
std::istream & operator>>(std::istream &in, tChain< CellT, CoefT > &c)
Reads a chain from an input stream.
Definition: chain.h:267
tChain()
The default constructor of an empty chain.
Definition: chain.h:108
tChain< CellT, CoefT > & operator*=(const CoefT &coef)
Multiples the chain by a given coefficient.
Definition: chain.h:200
void swap(tChain< CellT, CoefT > &ch)
Swaps the data with another chain.
Definition: chain.h:237
extarray< CoefT > coefs
The coefficients that appear at the cells in the chain.
Definition: chain.h:101