The ChainCon Software (Release 0.03)
combtensor.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A combinatorial tensor (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: January 3, 2013.
26 
27 
28 #ifndef _CHAINCON_COMBTENSOR_H_
29 #define _CHAINCON_COMBTENSOR_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 
39 // include relevant local header files
40 #include "chaincon/pair.h"
41 #include "chaincon/combchain.h"
42 #include "chaincon/ringz2.h"
43 
44 
45 // --------------------------------------------------
46 // -------------- combinatorial tensor --------------
47 // --------------------------------------------------
48 
49 /// Combinatorial tensor of cells. This is in fact a list of pairs of cells
50 /// of the given type. One can add products of chains to the tensor,
51 /// which is then decomposed into tensors of individual cells and reduced.
52 template <class Cell1T, class Cell2T>
54 {
55 public:
56  /// The type of cells on the left in the combinatorial tensor.
57  typedef Cell1T Cell1Type;
58 
59  /// The type of cells on the right in the combinatorial tensor.
60  typedef Cell2T Cell2Type;
61 
62  /// The type of coefficients in the chain.
63  typedef tZ2 CoefType;
64 
65  /// The default constructor of an empty combinatorial tensor.
66  tCombTensor ();
67 
68  /// Returns the number of elements in the combinatorial tensor.
69  int_t size () const;
70 
71  /// Returns true if and only if the tensor is zero (empty set).
72  bool empty () const;
73 
74  /// Returns the n-th left element of the combinatorial tensor.
75  const Cell1T &left (int_t n) const;
76 
77  /// Returns the n-th right element of the combinatorial tensor.
78  const Cell2T &right (int_t n) const;
79 
80  /// Adds a pair of cells to the combinatorial tensor.
81  void add (const Cell1T &c1, const Cell2T &c2);
82 
83  /// Adds a pair of chains to the combinatorial tensor.
84  void add (const tCombChain<Cell1T> &c1,
85  const tCombChain<Cell2T> &c2);
86 
87  /// Adds a given tensor to the combinatorial tensor.
88  tCombTensor<Cell1T,Cell2T> &operator +=
89  (const tCombTensor<Cell1T,Cell2T> &ch);
90 
91  /// Compares if the two tensors are equal.
92  bool operator == (const tCombTensor<Cell1T,Cell2T> &ch) const;
93 
94  /// Swaps the data with another tensor.
96 
97 private:
98  /// The set of elements in the combinatorial tensor.
100 
101 }; /* class tCombTensor */
102 
103 // --------------------------------------------------
104 
105 template <class Cell1T, class Cell2T>
107 {
108  return;
109 } /* tCombTensor::tCombTensor */
110 
111 template <class Cell1T, class Cell2T>
112 inline int_t tCombTensor<Cell1T,Cell2T>::size () const
113 {
114  return tensor. size ();
115 } /* tCombTensor::size */
116 
117 template <class Cell1T, class Cell2T>
119 {
120  return tensor. empty ();
121 } /* tCombTensor::empty */
122 
123 template <class Cell1T, class Cell2T>
124 inline const Cell1T &tCombTensor<Cell1T,Cell2T>::left (int_t n) const
125 {
126  return tensor. getCell (n). left;
127 } /* tCombTensor::left */
128 
129 template <class Cell1T, class Cell2T>
130 inline const Cell2T &tCombTensor<Cell1T,Cell2T>::right (int_t n) const
131 {
132  return tensor. getCell (n). right;
133 } /* tCombTensor::right */
134 
135 template <class Cell1T, class Cell2T>
137  (const Cell1T &c1, const Cell2T &c2)
138 {
139  tensor. add (tPair<Cell1T,Cell2T> (c1, c2));
140 } /* tCombTensor::add */
141 
142 template <class Cell1T, class Cell2T>
144  (const tCombChain<Cell1T> &ch1, const tCombChain<Cell2T> &ch2)
145 {
146  int_t ch1size = ch1. size ();
147  int_t ch2size = ch2. size ();
148  for (int_t i = 0; i < ch1size; ++ i)
149  {
150  const Cell1T &c1 = ch1. getCell (i);
151  for (int_t j = 0; j < ch2size; ++ j)
152  {
153  const Cell2T &c2 = ch2. getCell (j);
154  tensor. add (tPair<Cell1T,Cell2T> (c1, c2));
155  }
156  }
157  return;
158 } /* tCombTensor::add */
159 
160 template <class Cell1T, class Cell2T>
163 {
164  tensor += ch. tensor;
165  return *this;
166 } /* tCombTensor::operator += */
167 
168 template <class Cell1T, class Cell2T>
169 inline bool tCombTensor<Cell1T,Cell2T>::operator ==
170  (const tCombTensor<Cell1T,Cell2T> &ch) const
171 {
172  return (tensor == ch. tensor);
173 } /* tCombTensor::operator == */
174 
175 template <class Cell1T, class Cell2T>
177 {
178  tensor. swap (ch. tensor);
179  return;
180 } /* tCombTensor::swap */
181 
182 // --------------------------------------------------
183 
184 /// Writes a combinatorial tensor to an output stream.
185 template <class Cell1T, class Cell2T>
186 std::ostream &operator << (std::ostream &out,
188 {
189  int_t size = c. size ();
190  if (!size)
191  out << '0';
192  else
193  {
194  for (int_t i = 0; i < size; ++ i)
195  {
196  if (i)
197  out << " + ";
198  out << c. left (i) << " x " << c. right (i);
199  }
200  }
201  return out;
202 } /* operator << */
203 
204 /// Reads a combinatorial tensor from an input stream.
205 template <class Cell1T, class Cell2T>
206 std::istream &operator >> (std::istream &in, tCombTensor<Cell1T,Cell2T> &c)
207 {
208  throw "Operator >> not implemented for tCombTensor.";
209  return in;
210 } /* operator >> */
211 
212 
213 #endif // _CHAINCON_COMBTENSOR_H_
214 
A pair of elements of two (possibly different) types.
Definition: pair.h:48
std::ostream & operator<<(std::ostream &out, const tCombTensor< Cell1T, Cell2T > &c)
Writes a combinatorial tensor to an output stream.
Definition: combtensor.h:186
tZ2 CoefType
The type of coefficients in the chain.
Definition: combtensor.h:63
An element of the ring Z_2.
Definition: ringz2.h:47
Cell1T Cell1Type
The type of cells on the left in the combinatorial tensor.
Definition: combtensor.h:57
tCombTensor()
The default constructor of an empty combinatorial tensor.
Definition: combtensor.h:106
void swap(tCombTensor< Cell1T, Cell2T > &ch)
Swaps the data with another tensor.
Definition: combtensor.h:176
A combinatorial chain, that is, a chain with Z_2 coefficients.
tCombChain< tPair< Cell1T, Cell2T > > tensor
The set of elements in the combinatorial tensor.
Definition: combtensor.h:99
Cell2T Cell2Type
The type of cells on the right in the combinatorial tensor.
Definition: combtensor.h:60
void add(const Cell1T &c1, const Cell2T &c2)
Adds a pair of cells to the combinatorial tensor.
Definition: combtensor.h:137
Combinatorial tensor of cells.
Definition: combtensor.h:53
A combinatorial chain.
Definition: combchain.h:49
bool empty() const
Returns true if and only if the tensor is zero (empty set).
Definition: combtensor.h:118
Elements of the ring Z_2.
A pair of elements.
int_t size() const
Returns the number of elements in the combinatorial tensor.
Definition: combtensor.h:112
bool operator==(const tCombTensor< Cell1T, Cell2T > &ch) const
Compares if the two tensors are equal.
Definition: combtensor.h:170
const Cell2T & right(int_t n) const
Returns the n-th right element of the combinatorial tensor.
Definition: combtensor.h:130
std::istream & operator>>(std::istream &in, tCombTensor< Cell1T, Cell2T > &c)
Reads a combinatorial tensor from an input stream.
Definition: combtensor.h:206
const Cell1T & left(int_t n) const
Returns the n-th left element of the combinatorial tensor.
Definition: combtensor.h:124