The Conley-Morse Graphs Software
mapparam.h
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2///
3/// @file mapparam.h
4///
5/// A simple aggregate data structure for map parameters.
6///
7/// @author Pawel Pilarczyk
8///
9/////////////////////////////////////////////////////////////////////////////
10
11// Copyright (C) 1997-2014 by Pawel Pilarczyk.
12//
13// This file is part of my research software package. This is free software:
14// you can redistribute it and/or modify it under the terms of the GNU
15// General Public License as published by the Free Software Foundation,
16// either version 3 of the License, or (at your option) any later version.
17//
18// This software is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with this software; see the file "license.txt". If not,
25// please, see <https://www.gnu.org/licenses/>.
26
27// Started on March 25, 2013. Last revision: November 12, 2013.
28
29
30#ifndef _CMGRAPHS_MAPPARAM_H_
31#define _CMGRAPHS_MAPPARAM_H_
32
33
34// include some standard C++ header files
35#include <string>
36#include <sstream>
37#include <vector>
38#include <iostream>
39#include <iomanip>
40#include <algorithm>
41#include <memory>
42#include <utility>
43
44// include all the necessary header files from the CHomP library
45#include "chomp/system/config.h"
46#include "chomp/system/textfile.h"
47#include "chomp/struct/autoarray.h"
48#include "chomp/multiwork/mwdata.h"
49
50
51// --------------------------------------------------
52// ------------ map parameters aggregate ------------
53// --------------------------------------------------
54
55/// A data structure for storing map parameters.
57{
58public:
59 /// The default constructor.
60 MapParam ();
61
62 /// The destructor.
63 ~MapParam ();
64
65 /// Defines a data structure based on the given parameters.
66 void define (const double *leftParam, const double *rightParam,
67 int nParam);
68
69 /// Returns the number of parameters.
70 size_t size () const;
71
72 /// Returns the n-th left bound for the parameters.
73 const double &left (size_t n) const;
74
75 /// Returns the n-th right bound for the parameters.
76 const double &right (size_t n) const;
77
78 /// Writes a parameter aggregate data object to an output stream.
79 template <class OutputStream>
80 OutputStream &write (OutputStream &out) const;
81
82 /// Reads a parameter aggregate data object from an input stream.
83 template <class InputStream>
84 InputStream &read (InputStream &in);
85
86private:
87 /// An aggregate data container for the parameters.
88 std::vector<std::pair<double,double> > param;
89
90}; /* class MapParam */
91
92// --------------------------------------------------
93
95{
96 return;
97} /* MapParam::MapParam */
98
100{
101 return;
102} /* MapParam::~MapParam */
103
104inline void MapParam::define (const double *leftParam,
105 const double *rightParam, int nParam)
106{
107 param. resize (nParam);
108 for (int i = 0; i < nParam; ++ i)
109 {
110 param [i]. first = leftParam [i];
111 param [i]. second = rightParam [i];
112 }
113 return;
114} /* MapParam::define */
115
116inline size_t MapParam::size () const
117{
118 return param. size ();
119} /* MapParam::size */
120
121inline const double &MapParam::left (size_t n) const
122{
123 return param [n]. first;
124} /* MapParam::left */
125
126inline const double &MapParam::right (size_t n) const
127{
128 return param [n]. second;
129} /* MapParam::right */
130
131template <class OutputStream>
132inline OutputStream &MapParam::write (OutputStream &out) const
133{
134 int precision = out. precision ();
135 int hiprec = 12;
136 out << std::setprecision (hiprec);
137 for (size_t n = 0; n < param. size (); ++ n)
138 {
139 out << (n ? "x[" : "[") << param [n]. first << "," <<
140 param [n]. second << "]";
141 }
142 out << std::setprecision (precision);
143 return out;
144} /* MapParam::write */
145
146template <class InputStream>
147inline InputStream &MapParam::read (InputStream &in)
148{
149 using chomp::homology::ignorecomments;
150
151 param. clear ();
152 while (1)
153 {
154 ignorecomments (in);
155 if (in. get () != '[')
156 throw "Can't read parameters: Missing '['.";
157 ignorecomments (in);
158 std::pair<double,double> x;
159 in >> x. first;
160 ignorecomments (in);
161 if (in. get () != ',')
162 throw "Can't read paramters: Missing comma.";
163 ignorecomments (in);
164 in >> x. second;
165 ignorecomments (in);
166 if (in. get () != ']')
167 throw "Can't read parameters: Missing ']'.";
168 ignorecomments (in);
169 param. push_back (x);
170 if (in. peek () != 'x')
171 break;
172 in. get ();
173 }
174 return in;
175} /* MapParam::read */
176
177// --------------------------------------------------
178
179/// Checks if the two collections of parameters are exactly the same.
180/// Returns true if yes, false if no.
181inline bool operator == (const MapParam &a, const MapParam &b)
182{
183 size_t size (a. size ());
184 if (size != b. size ())
185 return false;
186 for (size_t i = 0; i < size; ++ i)
187 {
188 if (a. left (i) != b. left (i))
189 return false;
190 if (a. right (i) != b. right (i))
191 return false;
192 }
193 return true;
194} /* operator == */
195
196// --------------------------------------------------
197
198template <class InputStream>
199inline InputStream &operator >> (InputStream &in, MapParam &param)
200{
201 return param. read (in);
202} /* operator >> */
203
204template <class OutputStream>
205inline OutputStream &operator << (OutputStream &out, const MapParam &param)
206{
207 return param. write (out);
208} /* operator << */
209
210// --------------------------------------------------
211
212/// Serializes an object with map parameters.
213inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
214 const MapParam &mapParam)
215{
216 int nParam (mapParam. size ());
217 data << nParam;
218 for (int i = 0; i < nParam; ++ i)
219 {
220 data << mapParam. left (i);
221 data << mapParam. right (i);
222 }
223 return data;
224} /* operator << */
225
226/// Deserializes an object with map parameters.
227inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
228 MapParam &mapParam)
229{
230 int nParam (-1);
231 data >> nParam;
232 if (nParam < 0)
233 throw "A negative number of map parameters in mwData.";
234 if (!nParam)
235 {
236 mapParam. define (0, 0, 0);
237 }
238 else
239 {
240 chomp::homology::auto_array<double> paramPtr
241 (new double [nParam << 1]);
242 double *param (paramPtr. get ());;
243 for (int i = 0; i < nParam; ++ i)
244 {
245 data >> param [i];
246 data >> param [nParam + i];
247 }
248 mapParam. define (param, param + nParam, nParam);
249 }
250 return data;
251} /* operator >> */
252
253// --------------------------------------------------
254
255/// Checks if two aggregate parameter objects are so close to each other
256/// that after a small blow-up they intersect.
257inline bool intersect (const MapParam &par1, const MapParam &par2)
258{
259 if (par1. size () != par2. size ())
260 return false;
261 double margin = 0.00001;
262 for (size_t n = 0; n < par1. size (); ++ n)
263 {
264 double left1 = par1. left (n);
265 left1 *= (left1 > 0) ? (1.0 - margin) : (1.0 + margin);
266 double right1 = par1. right (n);
267 right1 *= (right1 > 0) ? (1.0 + margin) : (1.0 - margin);
268 double left2 = par2. left (n);
269 left2 *= (left2 > 0) ? (1.0 - margin) : (1.0 + margin);
270 double right2 = par2. right (n);
271 right2 *= (right2 > 0) ? (1.0 + margin) : (1.0 - margin);
272 if ((right1 < left2) || (right2 < left1))
273 return false;
274 }
275 return true;
276} /* intersect */
277
278/// Checks if two aggregate parameter objects that intersect
279/// actually share interiors in the parameters of non-zero length.
280/// Note: It must be first verified that they intersect,
281/// because the parameters of zero width are not checked.
282inline bool shareInteriors (const MapParam &par1, const MapParam &par2)
283{
284 if (par1. size () != par2. size ())
285 return false;
286 double marginFraction = 0.1;
287 for (size_t n = 0; n < par1. size (); ++ n)
288 {
289 double left1 = par1. left (n);
290 double right1 = par1. right (n);
291 if (left1 == right1)
292 continue;
293 double left2 = par2. left (n);
294 double right2 = par2. right (n);
295 if (left2 == right2)
296 continue;
297 double margin1 = marginFraction * (right1 - left1);
298 left1 += margin1;
299 right1 -= margin1;
300 double margin2 = marginFraction * (right2 - left2);
301 left2 += margin2;
302 right2 -= margin2;
303 if ((right1 < left2) || (right2 < left1))
304 return false;
305 }
306 return true;
307} /* shareInteriors */
308
309
310#endif // _CMGRAPHS_MAPPARAM_H_
311
A data structure for storing map parameters.
Definition: mapparam.h:57
void define(const double *leftParam, const double *rightParam, int nParam)
Defines a data structure based on the given parameters.
Definition: mapparam.h:104
const double & left(size_t n) const
Returns the n-th left bound for the parameters.
Definition: mapparam.h:121
InputStream & read(InputStream &in)
Reads a parameter aggregate data object from an input stream.
Definition: mapparam.h:147
std::vector< std::pair< double, double > > param
An aggregate data container for the parameters.
Definition: mapparam.h:88
const double & right(size_t n) const
Returns the n-th right bound for the parameters.
Definition: mapparam.h:126
OutputStream & write(OutputStream &out) const
Writes a parameter aggregate data object to an output stream.
Definition: mapparam.h:132
size_t size() const
Returns the number of parameters.
Definition: mapparam.h:116
MapParam()
The default constructor.
Definition: mapparam.h:94
~MapParam()
The destructor.
Definition: mapparam.h:99
bool intersect(const MapParam &par1, const MapParam &par2)
Checks if two aggregate parameter objects are so close to each other that after a small blow-up they ...
Definition: mapparam.h:257
bool operator==(const MapParam &a, const MapParam &b)
Checks if the two collections of parameters are exactly the same.
Definition: mapparam.h:181
OutputStream & operator<<(OutputStream &out, const MapParam &param)
Definition: mapparam.h:205
bool shareInteriors(const MapParam &par1, const MapParam &par2)
Checks if two aggregate parameter objects that intersect actually share interiors in the parameters o...
Definition: mapparam.h:282
InputStream & operator>>(InputStream &in, MapParam &param)
Definition: mapparam.h:199