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

chaincon/homgvf0.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// \file
00004 ///
00005 /// Homology gradient vector field computation: Algorithm 0, very slow.
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: March 24, 2011.
00026 
00027 
00028 #ifndef _CHAINCON_HOMGVF0_H_
00029 #define _CHAINCON_HOMGVF0_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 #include "chomp/system/timeused.h"
00039 #include "chomp/system/textfile.h"
00040 #include "chomp/struct/hashsets.h"
00041 
00042 // include relevant local header files
00043 #include "chaincon/combchain.h"
00044 #include "chaincon/comblinmap.h"
00045 
00046 
00047 // --------------------------------------------------
00048 // ------------------ Homology GVF ------------------
00049 // --------------------------------------------------
00050 
00051 /// Computes the homology gradient vector field for a given
00052 /// filtered finite cell complex "K".
00053 /// This is an old version of the algorithm, very slow.
00054 /// Cells that represent homology generators are appended to the vector "H".
00055 /// The projection map "pi", the inclusion from "H" to the complex "K",
00056 /// and the homology gradient vector field "phi"
00057 /// are assumed to be initially zero and are constructed.
00058 template <class CellT, class CellArray1, class CellArray2>
00059 void homologyGVF0 (const CellArray1 &K, CellArray2 &H,
00060         tCombLinMap<CellT,CellT> &pi,
00061         tCombLinMap<CellT,CellT> &incl,
00062         tCombLinMap<CellT,CellT> &phi)
00063 {
00064         using chomp::homology::sout;
00065         using chomp::homology::scon;
00066 
00067         // don't do anything for an empty complex
00068         if (K. empty ())
00069                 return;
00070 
00071         // show a message indicating what is being computed
00072         sout << "Computing a homology gradient vector field...\n";
00073 
00074         // start measuring computation time
00075         chomp::homology::timeused compTime;
00076         compTime = 0;
00077 
00078         // prepare a set of cells that represent homology generators
00079         chomp::homology::hashedset<CellT> homGener;
00080 
00081         // process all the cells in the filter
00082         int_t KSize = K. size ();
00083         for (int_t i = 0; i < KSize; ++ i)
00084         {
00085                 // show progress indicator
00086                 if (!(i % 17))
00087                         scon << i << " out of " << KSize << ", " <<
00088                                 (i * 100 / KSize) << "% done.\r";
00089 
00090                 // retrieve the i-th cell in the filter
00091                 const CellT &c = K [i];
00092 
00093                 // compute the modified cell
00094                 tCombChain<CellT> ch (c);
00095                 ch. add (phi (boundary (c)));
00096 
00097                 // compute the boundary of the modified cell
00098                 tCombChain<CellT> bd_ch (boundary (ch));
00099 
00100                 // if the boundary is zero
00101                 if (bd_ch. empty ())
00102                 {
00103                         // add this cell to the set of homology representants
00104                         homGener. add (c);
00105 
00106                         // define the inclusion for this cell
00107                         incl. add (c, ch);
00108 
00109                         // define the projection on this cell
00110                         pi. add (c, c);
00111 
00112                         // skip the rest
00113                         continue;
00114                 }
00115 
00116                 // project the boundary onto the homology generators
00117                 tCombChain<CellT> d (pi (bd_ch));
00118 
00119                 // make sure that this projection is nontrivial
00120                 if (d. empty ())
00121                 {
00122                         sout << "Debug: c = " << c << ".\n";
00123                         sout << "Debug: bd_c = " << boundary (c) <<
00124                                 ".\n";
00125                         sout << "Debug: ch = " << ch << ".\n";
00126                         sout << "Debug: bd_ch = " << bd_ch << ".\n";
00127                         throw "Empty boundary in the homology algorithm.";
00128                 }
00129 
00130                 // choose any element of this boundary
00131                 const CellT &u = d [0];
00132 
00133                 // prepare a new phi
00134                 tCombLinMap<CellT,CellT> phiAdd;
00135 
00136                 // prepare a new projection pi
00137                 tCombLinMap<CellT,CellT> piAdd;
00138 
00139                 // go through all the previously processed cells
00140                 for (int_t j = 0; j < i; ++ j)
00141                 {
00142                         // retrieve the j-th cell
00143                         const CellT &cj = K [j];
00144 
00145                         // compute the chain pi (cj + phi bd cj + bd phi cj)
00146                         tCombChain<CellT> cjh;
00147                         cjh. add (cj);
00148                         cjh. add (phi (boundary (cj)));
00149                         cjh. add (boundary (phi (cj)));
00150                         tCombChain<CellT> xi (pi (cjh));
00151 
00152                         // if u does not appear in this chain
00153                         // then skip the remaining computations for cj
00154                         if (!xi. contains (u))
00155                                 continue;
00156 
00157                         // compute new phi (cj) by adding ch to it
00158                         phiAdd. add (cj, ch);
00159 
00160                         // compute the new image of cj by the projection
00161                         tCombChain<CellT> combination;
00162                         combination. add (phi (boundary (cj)));
00163                         combination. add (boundary (phi (cj)));
00164                         combination. add (phiAdd (boundary (cj)));
00165                         combination. add (boundary (phiAdd (cj)));
00166                         piAdd. add (cj, pi (combination));
00167                 }
00168 
00169                 // === DEBUG VERIFICATION BEGIN ===
00170                 // prepare a new projection pi - to test if this is the same
00171                 tCombLinMap<CellT,CellT> piAddTest;
00172 
00173                 // go through all the cells again and compute new pi
00174                 for (int_t j = 0; j < i; ++ j)
00175                 {
00176                         // retrieve the j-th cell
00177                         const CellT &cj = K [j];
00178 
00179                         // compute xi = pi (cj + phi bd cj + bd phi cj)
00180                         tCombChain<CellT> cjh (boundary (phi (cj)));
00181                         cjh. add (phi (boundary (cj)));
00182                         cjh. add (cj);
00183                         tCombChain<CellT> xi (pi (cjh));
00184 
00185                         // if u does not appear in this chain
00186                         // then skip the remaining computations for cj
00187                         if (!xi. contains (u))
00188                                 continue;
00189 
00190                         // compute the new image of cj by the projection
00191                         tCombChain<CellT> combination;
00192                         combination. add (phi (boundary (cj)));
00193                         combination. add (boundary (phi (cj)));
00194                         combination. add (phiAdd (boundary (cj)));
00195                         combination. add (boundary (phiAdd (cj)));
00196                         piAddTest. add (cj, pi (combination));
00197                 }
00198 
00199                 // check if both versions of the new pi are the same
00200                 if (!(piAdd == piAddTest))
00201                 {
00202                         sout << "ERROR: The computed two versions "
00203                                 "of the map pi are not the same!\n";
00204                         sout << "i = " << i << ", c_i = " << c <<
00205                                 ", u = " << u << ".\n";
00206                         sout << "The wrong map piAdd:\n" << piAdd;
00207                         sout << "The correct map piAdd:\n" << piAddTest;
00208                         throw "Wrong map pi detected.\n";
00209                 }
00210                 // === DEBUG VERIFICATION END ===
00211 
00212                 // replace the old phi with the new one
00213                 phi. add (phiAdd);
00214 
00215                 // replace the old pi with the new one
00216                 pi. add (piAdd);
00217 
00218                 // remove the cell from the set of homology representants
00219                 homGener. remove (u);
00220                 incl. remove (u);
00221         }
00222 
00223         // show information on the computation time
00224         sout << "Homology gvf computed in " << compTime << ".\n";
00225 
00226         // copy the computed homology generators
00227         int_t genSize = homGener. size ();
00228         for (int_t i = 0; i < genSize; ++ i)
00229                 H. push_back (homGener [i]);
00230 
00231         return;
00232 } /* homologyGVF0 */
00233 
00234 
00235 #endif // _CHAINCON_HOMGVF0_H_
00236 

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