• Main Page
  • Classes
  • Files
  • File List
  • File Members

chaincon/combtensor.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// \file
00004 ///
00005 /// A combinatorial tensor (for coefficients in Z_2).
00006 ///
00007 /////////////////////////////////////////////////////////////////////////////
00008 
00009 // Copyright (C) 2009-2011 by Pawel Pilarczyk.
00010 //
00011 // This file is part of my research software package. This is free software:
00012 // you can redistribute it and/or modify it under the terms of the GNU
00013 // General Public License as published by the Free Software Foundation,
00014 // either version 3 of the License, or (at your option) any later version.
00015 //
00016 // This software is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU General Public License
00022 // along with this software; see the file "license.txt". If not,
00023 // please, see <http://www.gnu.org/licenses/>.
00024 
00025 // Started on March 24, 2009. Last revision: April 4, 2011.
00026 
00027 
00028 #ifndef _CHAINCON_COMBTENSOR_H_
00029 #define _CHAINCON_COMBTENSOR_H_
00030 
00031 
00032 // include some standard C++ header files
00033 #include <istream>
00034 #include <ostream>
00035 
00036 // include selected header files from the CHomP library
00037 #include "chomp/system/config.h"
00038 
00039 // include relevant local header files
00040 #include "chaincon/pair.h"
00041 #include "chaincon/combchain.h"
00042 
00043 
00044 // --------------------------------------------------
00045 // -------------- combinatorial tensor --------------
00046 // --------------------------------------------------
00047 
00048 /// Combinatorial tensor of cells. This is in fact a list of pairs of cells
00049 /// of the given type. One can add products of chains to the tensor,
00050 /// which is then decomposed into tensors of individual cells and reduced.
00051 template <class CellT>
00052 class tCombTensor
00053 {
00054 public:
00055         /// The type of cells in the combinatorial tensor.
00056         typedef CellT CellType;
00057 
00058         /// The default constructor of an empty combinatorial tensor.
00059         tCombTensor ();
00060 
00061         /// Returns the number of elements in the combinatorial tensor.
00062         int_t size () const;
00063 
00064         /// Returns true if and only if the tensor is zero (empty set).
00065         bool empty () const;
00066 
00067         /// Returns the n-th left element of the combinatorial tensor.
00068         const CellT &left (int_t n) const;
00069 
00070         /// Returns the n-th right element of the combinatorial tensor.
00071         const CellT &right (int_t n) const;
00072 
00073         /// Adds a pair of cells to the combinatorial tensor.
00074         void add (const CellT &c1, const CellT &c2);
00075 
00076         /// Adds a pair of chains to the combinatorial tensor.
00077         void add (const tCombChain<CellT> &c1, const tCombChain<CellT> &c2);
00078 
00079         /// Adds a given tensor to the combinatorial tensor.
00080         void add (const tCombTensor<CellT> &ch);
00081 
00082 //      /// Compares if the two tensors are equal.
00083 //      bool operator == (const tCombTensor<CellT> &ch) const;
00084 
00085         /// Swaps the data with another tensor.
00086         void swap (tCombTensor<CellT> &ch);
00087 
00088 private:
00089         /// The set of elements in the combinatorial tensor.
00090         tCombChain<tPair<CellT,CellT> > tensor;
00091 
00092 }; /* class tCombTensor */
00093 
00094 // --------------------------------------------------
00095 
00096 template <class CellT>
00097 inline tCombTensor<CellT>::tCombTensor ()
00098 {
00099         return;
00100 } /* tCombTensor::tCombTensor */
00101 
00102 template <class CellT>
00103 inline int_t tCombTensor<CellT>::size () const
00104 {
00105         return tensor. size ();
00106 } /* tCombTensor::size */
00107 
00108 template <class CellT>
00109 inline bool tCombTensor<CellT>::empty () const
00110 {
00111         return tensor. empty ();
00112 } /* tCombTensor::empty */
00113 
00114 template <class CellT>
00115 inline const CellT &tCombTensor<CellT>::left (int_t n) const
00116 {
00117         return tensor [n]. left;
00118 } /* tCombTensor::left */
00119 
00120 template <class CellT>
00121 inline const CellT &tCombTensor<CellT>::right (int_t n) const
00122 {
00123         return tensor [n]. right;
00124 } /* tCombTensor::right */
00125 
00126 template <class CellT>
00127 inline void tCombTensor<CellT>::add (const CellT &c1, const CellT &c2)
00128 {
00129         tensor. add (tPair<CellT,CellT> (c1, c2));
00130 } /* tCombTensor::add */
00131 
00132 template <class CellT>
00133 inline void tCombTensor<CellT>::add (const tCombChain<CellT> &ch1,
00134         const tCombChain<CellT> &ch2)
00135 {
00136         int_t ch1size = ch1. size ();
00137         int_t ch2size = ch2. size ();
00138         for (int_t i = 0; i < ch1size; ++ i)
00139         {
00140                 const CellT &c1 = ch1 [i];
00141                 for (int_t j = 0; j < ch2size; ++ j)
00142                 {
00143                         const CellT &c2 = ch2 [j];
00144                         tensor. add (tPair<CellT,CellT> (c1, c2));
00145                 }
00146         }
00147         return;
00148 } /* tCombTensor::add */
00149 
00150 template <class CellT>
00151 inline void tCombTensor<CellT>::add (const tCombTensor<CellT> &ch)
00152 {
00153         tensor. add (ch. tensor);
00154         return;
00155 } /* tCombTensor::add */
00156 
00157 
00158 template <class CellT>
00159 inline void tCombTensor<CellT>::swap (tCombTensor<CellT> &ch)
00160 {
00161         tensor. swap (ch. cells);
00162         return;
00163 } /* tCombTensor::swap */
00164 
00165 // --------------------------------------------------
00166 
00167 /// Writes a combinatorial tensor to an output stream.
00168 template <class CellT>
00169 std::ostream &operator << (std::ostream &out, const tCombTensor<CellT> &c)
00170 {
00171         int_t size = c. size ();
00172         for (int_t i = 0; i < size; ++ i)
00173         {
00174                 if (i)
00175                         out << " + ";
00176                 out << c. left (i) << " x " << c. right (i);
00177                 }
00178         return out;
00179 } /* operator << */
00180 
00181 /// Reads a combinatorial tensor from an input stream.
00182 template <class CellT>
00183 std::istream &operator >> (std::istream &in, tCombTensor<CellT> &c)
00184 {
00185         throw "Operator >> not implemented for tCombTensor.";
00186         return in;
00187 } /* operator >> */
00188 
00189 // --------------------------------------------------
00190 
00191 /// Adds the boundary of a given tensor to the provided tensor.
00192 template <class CellT>
00193 inline void computeBoundary (const tCombTensor<CellT> &c,
00194         tCombTensor<CellT> &b)
00195 {
00196         int_t size = c. size ();
00197         for (int_t i = 0; i < size; ++ i)
00198         {
00199                 tCombChain<CellT> left;
00200                 computeBoundary (c. left (i), left);
00201                 tCombChain<CellT> right;
00202                 computeBoundary (c. right (i), right);
00203                 b. add (left, right);
00204         }
00205         return;
00206 } /* computeBoundary */
00207 
00208 /// Returns the boundary of a given tensor.
00209 template <class CellT>
00210 inline tCombTensor<CellT> boundary (const tCombTensor<CellT> &c)
00211 {
00212         // prepare a tensor that is going to contain the boundary of c
00213         tCombTensor<CellT> b;
00214 
00215         // add the boundary of each pair in the tensor c to the tensor b
00216         computeBoundary (c, b);
00217 
00218         // return the computed boundary tensor
00219         return b;
00220 } /* boundary */
00221 
00222 
00223 #endif // _CHAINCON_COMBTENSOR_H_
00224 

Generated on Tue Apr 5 2011 00:06:32 for Chain Contraction Software by  doxygen 1.7.2