The ChainCon Software (Release 0.03)
tensor.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A tensor of chains 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: April 12, 2013.
26 
27 
28 #ifndef _CHAINCON_TENSOR_H_
29 #define _CHAINCON_TENSOR_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/chain.h"
42 
43 
44 // --------------------------------------------------
45 // --------------------- tensor ---------------------
46 // --------------------------------------------------
47 
48 /// Tensor of chains. This is in fact a chain made up of pairs of cells
49 /// of the given type. One can add products of chains to the tensor,
50 /// which is then decomposed into tensors of individual cells and reduced.
51 template <class Cell1T, class Cell2T, class CoefT>
52 class tTensor
53 {
54 public:
55  /// The type of left cells in the tensor.
56  typedef Cell1T Cell1Type;
57 
58  /// The type of right cells in the tensor.
59  typedef Cell2T Cell2Type;
60 
61  /// The type of coefficients in the chain.
62  typedef CoefT CoefType;
63 
64  /// The default constructor of an empty tensor.
65  tTensor ();
66 
67  /// Returns the number of elements in the tensor.
68  int_t size () const;
69 
70  /// Returns true if and only if the tensor is zero (empty set).
71  bool empty () const;
72 
73  /// Returns the n-th left element of the tensor.
74  const Cell1T &left (int_t n) const;
75 
76  /// Returns the n-th right element of the tensor.
77  const Cell2T &right (int_t n) const;
78 
79  /// Returns the n-th coefficient in the tensor.
80  const CoefT &coef (int_t n) const;
81 
82  /// Adds a pair of cells to the tensor.
83  void add (const Cell1T &c1, const Cell2T &c2, const CoefT &coef);
84 
85  /// Adds a pair of chains to the tensor.
86  void add (const tChain<Cell1T,CoefT> &c1,
87  const tChain<Cell2T,CoefT> &c2);
88 
89  /// Adds a given tensor to the tensor.
90  tTensor<Cell1T,Cell2T,CoefT> &operator +=
91  (const tTensor<Cell1T,Cell2T,CoefT> &ch);
92 
93  /// Multiples the tensor by a given coefficient.
95 
96  /// Compares if the two tensors are equal.
97  bool operator == (const tTensor<Cell1T,Cell2T,CoefT> &ch) const;
98 
99  /// Swaps the data with another tensor.
101 
102 private:
103  /// The set of elements in the tensor.
105 
106 }; /* class tTensor */
107 
108 // --------------------------------------------------
109 
110 template <class Cell1T, class Cell2T, class CoefT>
112 {
113  return;
114 } /* tTensor::tTensor */
115 
116 template <class Cell1T, class Cell2T, class CoefT>
118 {
119  return tensor. size ();
120 } /* tTensor::size */
121 
122 template <class Cell1T, class Cell2T, class CoefT>
124 {
125  return tensor. empty ();
126 } /* tTensor::empty */
127 
128 template <class Cell1T, class Cell2T, class CoefT>
129 inline const Cell1T &tTensor<Cell1T,Cell2T,CoefT>::left (int_t n) const
130 {
131  return tensor. getCell (n). left;
132 } /* tTensor::left */
133 
134 template <class Cell1T, class Cell2T, class CoefT>
135 inline const Cell2T &tTensor<Cell1T,Cell2T,CoefT>::right (int_t n) const
136 {
137  return tensor. getCell (n). right;
138 } /* tTensor::right */
139 
140 template <class Cell1T, class Cell2T, class CoefT>
141 inline const CoefT &tTensor<Cell1T,Cell2T,CoefT>::coef (int_t n) const
142 {
143  return tensor. getCoef (n);
144 } /* tTensor::coef */
145 
146 template <class Cell1T, class Cell2T, class CoefT>
148  (const Cell1T &c1, const Cell2T &c2, const CoefT &coef)
149 {
150  tensor. add (tPair<Cell1T,Cell2T> (c1, c2), coef);
151 } /* tTensor::add */
152 
153 template <class Cell1T, class Cell2T, class CoefT>
156 {
157  int_t ch1size = ch1. size ();
158  int_t ch2size = ch2. size ();
159  for (int_t i = 0; i < ch1size; ++ i)
160  {
161  const Cell1T &cell1 = ch1. getCell (i);
162  const CoefT &coef1 = ch1. getCoef (i);
163  for (int_t j = 0; j < ch2size; ++ j)
164  {
165  const Cell2T &cell2 = ch2. getCell (j);
166  CoefT coef (ch2. getCoef (j));
167  coef *= coef1;
168  add (cell1, cell2, coef);
169  }
170  }
171  return;
172 } /* tTensor::add */
173 
174 template <class Cell1T, class Cell2T, class CoefT>
178 {
179  tensor += ch. tensor;
180  return *this;
181 } /* tTensor::operator += */
182 
183 template <class Cell1T, class Cell2T, class CoefT>
185  tTensor<Cell1T,Cell2T,CoefT>::operator *=
186  (const CoefT &coef)
187 {
188  tensor *= coef;
189  return *this;
190 } /* tTensor::operator *= */
191 
192 template <class Cell1T, class Cell2T, class CoefT>
193 inline bool tTensor<Cell1T,Cell2T,CoefT>::operator ==
195 {
196  return (tensor == ch. tensor);
197 } /* tTensor::operator == */
198 
199 template <class Cell1T, class Cell2T, class CoefT>
202 {
203  tensor. swap (ch. tensor);
204  return;
205 } /* tTensor::swap */
206 
207 // --------------------------------------------------
208 
209 /// Writes a tensor to an output stream.
210 template <class Cell1T, class Cell2T, class CoefT>
211 std::ostream &operator << (std::ostream &out,
213 {
214  int_t size = c. size ();
215  if (!size)
216  out << '0';
217  else
218  {
219  for (int_t i = 0; i < size; ++ i)
220  {
221  if (i)
222  out << " + ";
223  out << c. coef (i) << " * " <<
224  c. left (i) << " x " << c. right (i);
225  }
226  }
227  return out;
228 } /* operator << */
229 
230 /// Reads a tensor from an input stream.
231 template <class Cell1T, class Cell2T, class CoefT>
232 std::istream &operator >> (std::istream &in,
234 {
235  throw "Operator >> not implemented for tTensor.";
236  return in;
237 } /* operator >> */
238 
239 
240 #endif // _CHAINCON_TENSOR_H_
241 
tTensor()
The default constructor of an empty tensor.
Definition: tensor.h:111
A pair of elements of two (possibly different) types.
Definition: pair.h:48
int_t size() const
Returns the number of elements in the tensor.
Definition: tensor.h:117
Cell2T Cell2Type
The type of right cells in the tensor.
Definition: tensor.h:59
const Cell1T & left(int_t n) const
Returns the n-th left element of the tensor.
Definition: tensor.h:129
A chain with coefficients in an arbitrary ring.
Definition: chain.h:50
bool operator==(const tTensor< Cell1T, Cell2T, CoefT > &ch) const
Compares if the two tensors are equal.
Definition: tensor.h:194
bool empty() const
Returns true if and only if the tensor is zero (empty set).
Definition: tensor.h:123
Tensor of chains.
Definition: tensor.h:52
void add(const Cell1T &c1, const Cell2T &c2, const CoefT &coef)
Adds a pair of cells to the tensor.
Definition: tensor.h:148
CoefT CoefType
The type of coefficients in the chain.
Definition: tensor.h:62
tTensor< Cell1T, Cell2T, CoefT > & operator*=(const CoefT &coef)
Multiples the tensor by a given coefficient.
Definition: tensor.h:186
const CoefT & coef(int_t n) const
Returns the n-th coefficient in the tensor.
Definition: tensor.h:141
A chain with coefficients in an arbitrary commutative ring.
std::istream & operator>>(std::istream &in, tTensor< Cell1T, Cell2T, CoefT > &c)
Reads a tensor from an input stream.
Definition: tensor.h:232
std::ostream & operator<<(std::ostream &out, const tTensor< Cell1T, Cell2T, CoefT > &c)
Writes a tensor to an output stream.
Definition: tensor.h:211
A pair of elements.
tChain< tPair< Cell1T, Cell2T >, CoefT > tensor
The set of elements in the tensor.
Definition: tensor.h:104
void swap(tTensor< Cell1T, Cell2T, CoefT > &ch)
Swaps the data with another tensor.
Definition: tensor.h:201
Cell1T Cell1Type
The type of left cells in the tensor.
Definition: tensor.h:56
const Cell2T & right(int_t n) const
Returns the n-th right element of the tensor.
Definition: tensor.h:135