32#ifndef _CMGRAPHS_UTILS_H_
33#define _CMGRAPHS_UTILS_H_
49#include "chomp/system/config.h"
50#include "chomp/system/textfile.h"
51#include "chomp/cubes/cube.h"
52#include "chomp/multiwork/mw.h"
53#include "chomp/struct/autoarray.h"
71 std::ifstream in (filename);
84 unsigned int pos = fileName. size ();
89 int ch = fileName [-- pos];
90 if ((pos == 0) || (ch ==
'/') || (ch ==
'\\') || (ch ==
':'))
93 return std::string (fileName, 0, pos);
136template <
class intType1,
class intType2,
class intType3>
138 const intType2 *left,
const intType2 *right,
139 const intType3 *limit,
int dim)
141 for (
int i = 0; i < dim; ++ i)
143 if ((coord [i] == left [i]) && (left [i] != 0))
147 else if ((coord [i] == right [i] - 1) &&
148 (right [i] != limit [i]))
159template <
class intType>
161 double *leftMapParam,
double *rightMapParam)
171#ifndef NONLINEAR_PARAMETERS
174 if (curCoord [j] > 0)
176#ifdef NONLINEAR_PARAMETERS
177 leftMapParam [i] = nonlinearParameter
181 curCoord [j] * paramWidth /
185#ifdef CONFIG_PARAMPOINTS
186 rightMapParam [i] = leftMapParam [i];
190#ifdef NONLINEAR_PARAMETERS
191 rightMapParam [i] = nonlinearParameter
192 (j, curCoord [j] + 1);
195 (curCoord [j] + 1) * paramWidth /
214 std::vector<parCoord> *subCoords,
int minCount)
216 using chomp::homology::sbug;
217 const bool debug =
true;
245 if ((minIndex < 0) ||
253 increasing [i] = minIndex;
254 taken [minIndex] =
true;
270 sbug <<
"Computing subdivisions - detailed information:\n";
271 sbug <<
" boxStep = " << boxStep <<
", minCount = " <<
272 minCount <<
", volume = " << volume <<
"\n";
280 int index = increasing [i];
281 int localBoxStep = (boxStep <
paramSubdiv [index]) ?
283 double side = volume ?
284 (std::exp (std::log (volume) / (
paramDim - i))) :
288 double delta = (volume && (i ==
paramDim - 1)) ?
290 maxIter [index] =
static_cast<parCoord>
294 if (maxIter [index] <= 0)
298 sbug <<
" index " << index <<
": remaining vol = " <<
299 volume <<
", side = " << side <<
", maxIter = " <<
300 maxIter [index] <<
"\n";
307 volume *= maxIter [index];
319 sbug <<
" dir " << i <<
":";
321 subCoords [i]. push_back (0);
322 for (
int j = 1; j < maxIter [i]; ++ j)
326 maxIter [i] - 1) * j + maxIter [i] - 1) /
328 subCoords [i]. push_back (coord);
331 parCoord width = subCoords [i] [j] -
332 subCoords [i] [j - 1] + 1;
333 if ((j == 1) || (width < minWidth))
335 if ((j == 1) || (maxWidth < width))
337 sbug <<
" " << coord;
343 parCoord lastWidth = subCoords [i] [maxIter [i]] -
344 subCoords [i] [maxIter [i] - 1];
346 minWidth <<
"-" << maxWidth <<
"; " <<
365template <
class intType>
368 intType tenPower = 10;
374 intType nextPower = tenPower * 10;
375 if (nextPower < tenPower)
377 tenPower = nextPower;
385template <
class intType1,
class intType2>
387 const intType2 *maxCoords,
int dim)
389 std::ostringstream str;
390 for (
int i = 0; i < dim; ++ i)
396 for (
int d = digits; d < maxDigits; ++ d)
404template <
class intType>
405inline std::string
coord2str (
const intType *coords,
int dim)
407 std::ostringstream str;
408 for (
int i = 0; i < dim; ++ i)
418template <
class cubeType>
421 using chomp::homology::auto_array;
426 return std::string (
"");
429 typedef typename cubeType::CoordType coordType;
430 auto_array<coordType> coords (
new coordType [dim]);
431 q. coord (coords. get ());
498 int nCubes = theSet. size ();
499 for (
int i = 0; i < nCubes; ++ i)
501 theSet [i]. coord (coord);
527 if (!range. defined ())
534 out << (i ?
" x [" :
"[") <<
535 range. coordLeftMin [i] <<
"," <<
536 range. coordRightMax [i] <<
"]";
542template <
class OutStream>
545 if (!range. defined ())
553 double rangeMin = range. coordLeftMin [i] *
spaceWidth [i] /
555 double rangeMax = range. coordRightMax [i] *
spaceWidth [i] /
557 out << (i ?
" x [" :
"[") << rangeMin <<
"," <<
568 int nCubes = theSet. size ();
569 spcCoord rightBound = (1 << depth) - 1;
570 for (
int i = 0; i < nCubes; ++ i)
572 theSet [i]. coord (coord);
575 if ((coord [j] <= 0) || (coord [j] >= rightBound))
589template <
class typeCubes>
592 typedef typename typeCubes::value_type tCube;
593 typedef typename typeCubes::value_type::CoordType tCoord;
597 int dim = X [0]. dim ();
599 tCoord cmin [tCube::MaxDim];
600 tCoord cmax [tCube::MaxDim];
602 for (
int i = 0; i < n; ++ i)
605 for (
int j = 0; j < dim; ++ j)
608 cmax [j] = cmin [j] + 2;
610 chomp::homology::tRectangle<tCoord> r (cmin, cmax, dim);
612 while ((c = r. get ()) != 0)
613 result. add (tCube (c, dim));
619template <
class typeCube,
class typeCubes>
622 typedef typename typeCubes::value_type::CoordType tCoord;
626 tCoord cmin [typeCube::MaxDim];
627 tCoord cmax [typeCube::MaxDim];
629 for (
int j = 0; j < dim; ++ j)
632 cmax [j] = cmin [j] + 2;
634 chomp::homology::tRectangle<tCoord> r (cmin, cmax, dim);
636 while ((c = r. get ()) != 0)
637 result. add (typeCube (c, dim));
643template <
class typeCubes>
646 typedef typename typeCubes::value_type tCube;
647 typedef typename typeCubes::value_type::CoordType tCoord;
651 int dim = X [0]. dim ();
653 tCoord coord [tCube::MaxDim];
655 for (
int i = 0; i < n; ++ i)
657 X [i]. coord (coord);
658 for (
int j = 0; j < dim; ++ j)
660 result. add (tCube (coord, dim));
670 spcCoord coordBound = 1 << subdivDepth;
671 int dim = X [0]. dim ();
675 while (enhanceTimes --)
677 int curBound = X. size ();
678 for (; cur < curBound; ++ cur)
681 chomp::homology::tNeighbors<spcCoord> n (c, dim);
683 while ((nc = n. get ()) != 0)
685 bool outOfSpace =
false;
686 for (
int i = 0; i < dim; ++ i)
689 (nc [i] >= coordBound))
715 double cubeVolume (1.0);
718 return nCubes * cubeVolume;
723template <
class CoordType>
724inline void cropRanges (CoordType *left, CoordType *right,
int dim,
725 int intwidth,
bool throwIfCropped,
bool &cropped)
727 for (
int i = 0; i < dim; ++ i)
731 left [i] = (right [i] > 0) ?
734 throw "Image sticks outside: left.";
737 if (right [i] > intwidth)
739 right [i] = (left [i] < intwidth) ?
740 intwidth : (left [i] + 1);
742 throw "Image sticks outside: right.";
754template <
class CoordType>
756 int dim,
int &maxImgDiam,
int &maxImgVol,
757 int maxImgDiamAllowed,
int maxImgVolAllowed)
760 for (
int i = 0; i < dim; ++ i)
762 int size = right [i] - left [i];
763 if (maxImgDiam < size)
765 if (size > maxImgDiamAllowed)
766 throw "Excessive diameter of box image.";
768 if (volume > maxImgVolAllowed)
769 throw "Excessive volume of box image.";
771 if (maxImgVol < volume)
778template <
class ObjectType,
class ArrayType>
780 const ArrayType &theArray, int_t arraySize)
782 for (int_t n = 0; n < arraySize; ++ n)
784 if (theArray [n] == theObject)
800template <
class HashSetType>
804 int_t size = X. size ();
807 const int_t step1 = 25013 * step;
808 for (int_t i = step1 / 7; i < size; i += step1)
810 const int_t step2 = 503 * step;
811 for (int_t i = step2 / 13; i < size; i += step2)
813 for (int_t i = step >> 1; i < size; i += step)
817 const int_t step3 = 751;
818 for (int_t i = step3 >> 1; i < size; i += step3)
820 const int_t step4 = 73;
821 for (int_t i = step4 >> 1; i < size; i += step4)
823 const int_t step5 = 7;
824 for (int_t i = step5 >> 1; i < size; i += step5)
828 for (int_t i = 0; i < size; ++ i)
832 if (Y. size () != size)
833 throw "Wrong size of the new set in 'moveSomeToFront'.";
843template <
class CoordType,
class IntervalType>
848 for (
int i = 0; i < dim; ++ i)
851 if (c [i] >= intervalBox [i]. rightBound ())
854 if (c [i] + 1 <= intervalBox [i]. leftBound ())
857 if (c [i] < intervalBox [i]. leftBound ())
860 if (c [i] + 1 > intervalBox [i]. rightBound ())
862 vol *= intervalBox [i]. rightBound () -
863 intervalBox [i]. leftBound ();
868 vol *= c [i] + 1 - intervalBox [i]. leftBound ();
875 if (c [i] + 1 > intervalBox [i]. rightBound ())
877 vol *= intervalBox [i]. rightBound () - c [i];
899template <
class elemType,
class setType>
914 return theSet. check (n) ? 1 : 0;
929template <
class SetType,
class GraphType>
931 const SetType &full,
const SetType &subset)
933 int_t subsetCount = subset. size ();
934 for (int_t i = 0; i < subsetCount; ++ i)
937 int_t vertex = full. getnumber (subset [i]);
938 int_t nEdges = g. countEdges (vertex);
939 for (int_t j = 0; j < nEdges; ++ j)
941 int_t target = g. getEdge (vertex, j);
942 int_t number = subset. getnumber
945 restr. addEdge (number);
960 std::ostringstream out;
961 out. setf (std::ios::fixed);
962 out. precision (precision);
976 chomp::homology::scon << std::setw (9) << n << c <<
977 "\b\b\b\b\b\b\b\b\b\b";
A class whose objects store, update and show coordinate ranges.
spcCoord coordLeftMin[spaceDim]
The minimal coordinates.
bool defined() const
Returns true if at least one cube was taken for the ranges.
void operator()(const spcCubes &theSet)
Updates the coordinate ranges for the given set of cubes.
CoordMinMax()
The default constructor.
spcCoord coordRightMax[spaceDim]
The strict upper bound for the maximal coordinates.
Initializes the given variable with the value provided and restores the previous value at the end of ...
local_value(T &_variable, const T &_value)
The only allowed constructor.
~local_value()
The destructor which restores the original value of the variable.
T previous_value
The original value of the variable.
T & variable
A reference of the variable.
An imitation of an array whose entries are 1 iff the index belongs to the given set.
const setType & theSet
A reference to the wrapped set.
set2arrayWrapper(const setType &_theSet)
The constructor of a set wrapper.
int operator[](const elemType &n) const
The operator [] which returns 1 iff the element belongs to the set.
Choice of configuration settings.
const SpaceOffsetType spaceOffset
An imitation of an array which returns the offset of the rectangular area in the phase space which co...
const int paramSelect[paramDim]
The numbers of parameters to subdivide.
const double paramRight[paramCount]
The right bounds on the parameters: b, h.
const int paramDim
The dimension of the parameter space to iterate.
const int spaceDim
The dimension of the phase space.
const double paramLeft[paramCount]
The left bounds on the parameters: b > 1, h < (sqrt(b) - 1)^2.
const int paramCount
The number of all the parameters, both varying and fixed.
const SpaceWidthType spaceWidth
An imitation of an array which returns the width of the rectangular area in the phase space which con...
const int finalDepth
The final depth of subdivisions in the phase space.
const short int paramSubdiv[paramDim]
The numbers of subintervals in each direction of the parameter space.
Customizable data types for the Conley-Morse graphs computation program.
capd::DInterval IntervalType
The type of an interval (from the CAPD library 2.9/3.0 beta).
short int parCoord
The type of coordinates of cubes in the set of parameters.
chomp::homology::tRectangle< parCoord > parRect
The type of a rectangle used to iterate sets of cubes of parameters.
chomp::homology::hashedset< spcCube > spcCubes
The type of a set of cubes in the phase space.
int spcCoord
The type of coordinates of cubes in the phase space.
chomp::homology::tCubeBase< spcCoord > spcCube
The type of a cube in the phase space.
void embedCubes(const typeCubes &X, typeCubes &result)
Embeds a set of cubes to another set of cubes with respect to twice coarser grid.
bool internalBoundaryPoint(const intType1 *coord, const intType2 *left, const intType2 *right, const intType3 *limit, int dim)
Verifies whether the given box is located at the boundary of the small region which is either not at ...
std::string double2string(const double &x, int precision)
Creates a text representation of a floating-point number at the prescribed precision.
void subdivideCube(const typeCube &q, typeCubes &result)
Subdivides a cube to a set of cubes with respect to twice finer grid.
int countDigits(intType x)
Returns the number of digits of the given non-negative integer number.
void subdivideCubes(const typeCubes &X, typeCubes &result)
Subdivides one set of cubes to another set of cubes with respect to twice finer grid.
std::ostream & operator<<(std::ostream &out, const CoordMinMax &range)
Outputs the ranges of coordinates to an output stream.
void showProgress(int n, char c)
Shows a progress indicator to the screen as an integer number followed by an additional character (e....
OutStream & showRealCoords(OutStream &out, const CoordMinMax &range)
Shows the real coordinates of the coordinate range.
bool objectInArray(const ObjectType &theObject, const ArrayType &theArray, int_t arraySize)
Checks if the given object is in the array of the given size.
double cubesVolume(int_t nCubes, int subdiv)
Returns the volume of the given number of cubes in the phase space at the provided subdivision level.
bool fileExists(const char *filename)
Returns 'true' iff the given file exists and it is allowed to read it.
std::string fileNameWithoutExt(const std::string &fileName)
Returns the file name without its extension if any.
double intersectionVolume(const CoordType *c, const IntervalType *intervalBox, int dim)
Computes the volume of the intersection of the unitary cube with the provided coordinates with the gi...
parRect * subRectangles(parCoord *zeroIter, parCoord *maxIter, std::vector< parCoord > *subCoords, int minCount)
Determines heuristically an optimal subdivision of a large parameter region into the given minimal nu...
void enhanceCubes(spcCubes &X, int enhanceTimes, int subdivDepth)
Enhances the given set of cubes provided number of times.
std::string coord2str(const intType1 *coords, const intType2 *maxCoords, int dim)
Generates an underscore-separated list of non-negative coordinates padded with zeros to the same widt...
void computeRestrictedGraph(GraphType &restr, const GraphType &g, const SetType &full, const SetType &subset)
Computes the graph corresponding to the restriction of the combinatorial map to the provided subset.
void cropRanges(CoordType *left, CoordType *right, int dim, int intwidth, bool throwIfCropped, bool &cropped)
Crops the ranges, marks this fact if it occurred, and throws an exception if requested to.
bool checkIsolation(const spcCubes &theSet, int depth)
Verifies if the given set of cubes is contained in the interior of the phase space box.
long checkImageSize(const CoordType *left, const CoordType *right, int dim, int &maxImgDiam, int &maxImgVol, int maxImgDiamAllowed, int maxImgVolAllowed)
Checks if the size of the image does not exceed the globally defined maximal diameter and maximal vol...
void computeParam(const intType *curCoord, double *leftMapParam, double *rightMapParam)
Computes the real coordinates of the parameter cube which corresponds to the given box.
void moveSomeToFront(HashSetType &X, int_t step)
Replaces the hashed set of objects with another one that contains the same objects,...