The Conley-Morse Graphs Software
mapdist.h
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2///
3/// @file mapdist.h
4///
5/// Map computation distance checker.
6/// This file contains the definition of routines for the verification
7/// of whether the interval images of cubes computed while computing
8/// the combinatorial map get closer to or farther from some subspaces.
9/// Please, note that the routines defined here should only be used
10/// from "cmsingle", not from "cmgraphs" itself. This is only a temporary
11/// hack introduced into the code to gather the data in question,
12/// don't rely on it to be efficient or user-friendly. Sorry about this!
13///
14/// @author Pawel Pilarczyk
15///
16/////////////////////////////////////////////////////////////////////////////
17
18// Copyright (C) 1997-2014 by Pawel Pilarczyk.
19//
20// This file is part of my research software package. This is free software:
21// you can redistribute it and/or modify it under the terms of the GNU
22// General Public License as published by the Free Software Foundation,
23// either version 3 of the License, or (at your option) any later version.
24//
25// This software is distributed in the hope that it will be useful,
26// but WITHOUT ANY WARRANTY; without even the implied warranty of
27// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28// GNU General Public License for more details.
29//
30// You should have received a copy of the GNU General Public License
31// along with this software; see the file "license.txt". If not,
32// please, see <https://www.gnu.org/licenses/>.
33
34// Started on September 16, 2010. Last revision: January 12, 2011.
35
36
37#ifndef _CMGRAPHS_MAPDIST_H_
38#define _CMGRAPHS_MAPDIST_H_
39
40
41// include some standard C++ header files
42#include <iostream>
43#include <fstream>
44#include <sstream>
45#include <algorithm>
46#include <memory>
47#include <new>
48#include <cmath>
49
50// include selected header files from the CHomP library
51#include "chomp/system/textfile.h"
52#include "chomp/system/timeused.h"
53#include "chomp/homology/homology.h"
54
55// include local header files
56#include "config.h"
57#include "typedefs.h"
58#include "typeintv.h"
59
60
61// --------------------------------------------------
62// ------------------ MapDistance -------------------
63// --------------------------------------------------
64
65/// The map distance change tracker.
66/// Note: This class is not thread-safe.
67template <class cubetype,
68 class cubsettype = chomp::homology::hashedset<cubetype> >
70{
71public:
72 /// The default constructor.
74
75 /// The destructor that saves all the collected information to files.
77
78 /// The operator for gathering the information on a given cube.
79 void operator () (const typename cubetype::CoordType *coord, int dim,
80 const IntervalType *x, const IntervalType *y) const;
81
82private:
83 /// Should the distances be really checked? Please, set this
84 /// to 'true' only if you know what you are doing. Otherwise,
85 /// leave it as 'false'.
86 static const bool checkDistanceChanges = false;
87
88 /// Are the X and Y coordinates rotated? If so then the distance
89 /// in this direction will be measured vertically,
90 /// just towards the X axis.
91 static const bool rotatedXY = false;
92
93 /// The sets of boxes with the distance checked:
94 /// closer, farther, undetermined,
95 /// for each hyperplane separately.
96 mutable chomp::homology::multitable<cubsettype> distCubes;
97
98 /// The dimension of the phase space.
99 mutable int dim;
100
101 /// The consecutive number of sets for file naming purposes.
102 static int number;
103
104 /// Computes the number of subspaces, depending on the dimension.
105 static int countSubspaces (int dim);
106
107 /// Computes the lower and upper bounds for the distance
108 /// of the interval box from the given subspace.
109 /// Note: It can actually compute a multiple of the distance
110 /// or the squared distance, but this is OK for the purpose
111 /// of comparing the distances if they get smaller or larger.
112 static void distance (int subspace, const IntervalType *box, int dim,
113 double &distMin, double &distMax);
114
115}; /* class MapDistance */
116
117// --------------------------------------------------
118
119template <class cubetype, class cubsettype>
121
122// --------------------------------------------------
123
124template <class cubetype, class cubsettype>
126{
127 return;
128} /* MapDistance::MapDistance */
129
130template <class cubetype, class cubsettype>
132{
133 if (!checkDistanceChanges)
134 return;
135
136 int_t countCubes = 0;
137 int nSubspaces = countSubspaces (dim);
138 for (int s = 0; s < nSubspaces; ++ s)
139 {
140 const char *changeFile [] =
141 {"c", "f", "u"};
142 const char *changeDesc [] =
143 {"is closer to ", "is farther from ",
144 "is neither closer to nor farther from "};
145 for (int n = 0; n < 3; ++ n)
146 {
147 const cubsettype &cubes = distCubes [3 * s + n];
148 if (cubes. empty ())
149 continue;
150 countCubes += cubes. size ();
151 std::ostringstream filename;
152 filename << "_" << number << "_" << s <<
153 changeFile [n] << ".cub";
154 std::ofstream f (filename. str (). c_str ());
155 chomp::homology::sbug << "* Saving " <<
156 cubes. size () << " cubes to " <<
157 filename. str () << "... ";
158 f << "; Cubes whose image " << changeDesc [n] <<
159 " the subspace no. " << s << "\n"
160 "; in comparison to the distance of the "
161 " box itself from that subspace.\n";
162 f << cubes;
163 chomp::homology::sbug << "Done.\n";
164 }
165 }
166 if (countCubes)
167 ++ number;
168 return;
169} /* MapDistance::~MapDistance */
170
171template <class cubetype, class cubsettype>
173{
174 // return 2^d - d - 1, which is the number of possible subsets
175 // that have at least 2 elements each, and which correspond
176 // to the subspaces of interest
177 int subspaces = (1 << dim) - dim - 1;
178
179 // add one more computation in dim > 2: the minimal distance
180 // from the codimension-1 hyperplanes
181 if (dim > 2)
182 ++ subspaces;
183 return subspaces;
184} /* MapDistance::countSubspaces */
185
186template <class cubetype, class cubsettype>
188 const IntervalType *box, int dim, double &distMin, double &distMax)
189{
190 // distance from the diagonal or from the X axis in the 2D case
191 if ((subspace == 0) && (dim == 2))
192 {
193 double xLeft;
194 double xRight;
195 if (rotatedXY)
196 {
197 xLeft = box [1]. leftBound ();
198 xRight = box [1]. rightBound ();
199 }
200 else
201 {
202 IntervalType boxDist = box [0] - box [1];
203 xLeft = boxDist. leftBound ();
204 xRight = boxDist. rightBound ();
205 }
206 double xLeftAbs = std::abs (xLeft);
207 double xRightAbs = std::abs (xRight);
208 distMax = std::max (xLeftAbs, xRightAbs);
209 if ((xLeft <= 0) && (xRight >= 0))
210 distMin = 0;
211 else
212 distMin = std::min (xLeftAbs, xRightAbs);
213 }
214 // distance from the x=y=z line in the 3D case
215 else if ((subspace == 3) && (dim == 3))
216 {
217 // the line has the direction vector (a,b,c)=(1,1,1);
218 // the formula for the projected point is:
219 // \frac{(a,b,c) \dot (x,y,z)}{a^2+b^2+c^2} (a,b,c),
220 // so we need to compute the distance from this point
221 // to (x,y,z); this formula was given by Hiroe;
222 // in order to speed up the computations, let's compute
223 // 9 times the square of the distance
224 const IntervalType &x = box [0];
225 const IntervalType &y = box [1];
226 const IntervalType &z = box [2];
227 IntervalType dist = sqr (-2 * x + y + z) +
228 sqr (x - 2 * y + z) + sqr (x + y - 2 * z);
229 distMin = dist. leftBound ();
230 if (distMin < 0)
231 distMin = 0;
232 distMax = dist. rightBound ();
233 }
234 // the minimal distance from the three codim-1 subspaces
235 else if ((subspace == 4) && (dim == 3))
236 {
237 for (int sub = 0; sub < dim; ++ sub)
238 {
239 double subMin = 0;
240 double subMax = 0;
241 distance (sub, box, dim, subMin, subMax);
242 if (!sub || (distMin > subMin))
243 distMin = subMin;
244 if (!sub || (distMax < subMax))
245 distMax = subMax;
246 }
247 }
248 // distance from the 2D subspaces in the 3D case
249 else if (dim == 3)
250 {
251 // determine the coordinates: 0 = yz, 1 = xz, 2 = xy
252 int coord0 = (subspace > 0) ? 0 : 1;
253 int coord1 = (subspace < 2) ? 2 : 1;
254 IntervalType boxDist = box [coord0] - box [coord1];
255 double xLeft = boxDist. leftBound ();
256 double xRight = boxDist. rightBound ();
257 double xLeftAbs = std::abs (xLeft);
258 double xRightAbs = std::abs (xRight);
259 distMax = std::max (xLeftAbs, xRightAbs);
260 if ((xLeft <= 0) && (xRight >= 0))
261 distMin = 0;
262 else
263 distMin = std::min (xLeftAbs, xRightAbs);
264 }
265 // the remaining cases are not yet implemented
266 else
267 {
268 throw "Distance measuring not implemented for the "
269 "requested case.\n"
270 "Please, set 'checkDistanceChanges' in 'mapdist.h' "
271 "to 'false'\nand re-compile your program.";
272 }
273 return;
274} /* MapDistance::distance */
275
276// --------------------------------------------------
277
278template <class cubetype, class cubsettype>
280 (const typename cubetype::CoordType *coord, int dim,
281 const IntervalType *x, const IntervalType *y) const
282{
283 if (!checkDistanceChanges)
284 return;
285
286 // update the dimension
287 if (!(this -> dim))
288 this -> dim = dim;
289
290 // check the distance changes from all the subspaces of interest
291 int nSubspaces = countSubspaces (dim);
292 for (int s = 0; s < nSubspaces; ++ s)
293 {
294 // determine the distance bounds for the original box
295 double xDistMin = 0;
296 double xDistMax = 0;
297 distance (s, x, dim, xDistMin, xDistMax);
298
299 // determine the distance bounds for its image
300 double yDistMin = 0;
301 double yDistMax = 0;
302 distance (s, y, dim, yDistMin, yDistMax);
303
304 // if the image is closer
305 if (xDistMin >= yDistMax)
306 {
307 distCubes [3 * s + 0]. add (cubetype (coord, dim));
308 }
309 // if the image is farther
310 else if (xDistMax <= yDistMin)
311 {
312 distCubes [3 * s + 1]. add (cubetype (coord, dim));
313 }
314 // if the distance change is undertermined
315 else
316 {
317 distCubes [3 * s + 2]. add (cubetype (coord, dim));
318 }
319 }
320 // swich the rounding direction to the neutral one
321 resetRounding ();
322 return;
323} /* MapDistance::operator () */
324
325
326#endif // _CMGRAPHS_MAPDIST_H_
327
328
The map distance change tracker.
Definition: mapdist.h:70
static const bool rotatedXY
Are the X and Y coordinates rotated? If so then the distance in this direction will be measured verti...
Definition: mapdist.h:91
static const bool checkDistanceChanges
Should the distances be really checked? Please, set this to 'true' only if you know what you are doin...
Definition: mapdist.h:86
void operator()(const typename cubetype::CoordType *coord, int dim, const IntervalType *x, const IntervalType *y) const
The operator for gathering the information on a given cube.
Definition: mapdist.h:280
chomp::homology::multitable< cubsettype > distCubes
The sets of boxes with the distance checked: closer, farther, undetermined, for each hyperplane separ...
Definition: mapdist.h:96
static int countSubspaces(int dim)
Computes the number of subspaces, depending on the dimension.
Definition: mapdist.h:172
MapDistance()
The default constructor.
Definition: mapdist.h:125
~MapDistance()
The destructor that saves all the collected information to files.
Definition: mapdist.h:131
static void distance(int subspace, const IntervalType *box, int dim, double &distMin, double &distMax)
Computes the lower and upper bounds for the distance of the interval box from the given subspace.
Definition: mapdist.h:187
int dim
The dimension of the phase space.
Definition: mapdist.h:99
static int number
The consecutive number of sets for file naming purposes.
Definition: mapdist.h:102
Choice of configuration settings.
Customizable data types for the Conley-Morse graphs computation program.
Data types for interval arithmetic.
void resetRounding()
This function resets rounding switches of the processor and sets rounding to the nearest.
Definition: typeintv.h:65
capd::DInterval IntervalType
The type of an interval (from the CAPD library 2.9/3.0 beta).
Definition: typeintv.h:49