All Classes Functions Friends
interval.h
1 #ifndef _INTERVAL_H_
2 #define _INTERVAL_H_
3 
4 /**
5  * Data Structure for interval [a, b]
6  * and interval lists for real roots of multi-variates,
7  * i.e. []*..*[], .., []*..*[]
8  * -------- --------
9  * each root of variates
10  **/
11 
12 #include "../globals.h"
13 
14 class Interval {
15  public:
16  lfixq left;
17  lfixq right;
18 };
19 
20 /**
21  * Interval addition
22  * @res = @a + @b
23  **/
24 inline void intervalAddition(Interval* res, Interval* a, Interval* b) {
25  res->left = a->left + b->left;
26  res->right = a->right + b->right;
27 }
28 
29 /**
30  * Interval multiplication
31  * @res = @a * @b
32  **/
33 void intervalMultiplication(Interval* res, Interval* a, Interval* b);
34 
35 class Intervals {
36  private:
37  int var; // Number of variates
38  std::string* names; // Variable names
39  std::vector<Interval> roots; // Roots, using one dimension
40  // to present two dimensions
41  // []*..*[], .., []*..*[]
42  // Each root is in the ascending
43  // order of the weight of variates
44 
45  public:
46  /**
47  * Construct intervals
48  *
49  * @param
50  **/
51  Intervals() : var(1) {
52  names = new std::string[2];
53  names[0] = "1";
54  names[1] = "_1";
55  }
56  /**
57  * Construct intervals with number of variates
58  *
59  * @param v: Number of variates
60  **/
61  Intervals(int v) : var(v) {
62  names = new std::string[var+1];
63  names[0] = "1";
64  for (int i = 1; i <= var; ++i) {
65  std::ostringstream convert;
66  convert << var - i + 1;
67  names[i] = "_";
68  names[i] += convert.str();
69  }
70  }
71  /**
72  * Copy construct
73  *
74  * @param b: Intervals
75  **/
76  Intervals (const Intervals& b) : var(b.var), roots(b.roots) {
77  names = new std::string[var+1];
78  std::copy(b.names, b.names+var+1, names);
79  }
80  /**
81  * Destroy intervals
82  *
83  * @param
84  **/
86  delete [] names;
87  roots.clear();
88  }
89  /**
90  * Overload operator =
91  *
92  * @param b: Intervals
93  **/
95  if (this != &b) {
96  delete [] names;
97  var = b.var;
98  roots = b.roots;
99  names = new std::string[var+1];
100  std::copy(b.names, b.names+var+1, names);
101  }
102  return *this;
103  }
104  /**
105  * Get number of variables
106  *
107  * @param
108  **/
109  inline int numberOfVariables() {
110  return var;
111  }
112 
113  /**
114  * Get the number of roots
115  *
116  * @param
117  **/
118  inline int numberOfIntervals() {
119  return roots.size() / var;
120  }
121 
122  /**
123  * Push an interval to the end of the list
124  *
125  * @param pI: An interval
126  **/
127  inline void pushInterval(Interval pI) {
128  roots.push_back(pI);
129  }
130  /**
131  * Push an interval to the end of the list
132  *
133  * @param l: Interval's left
134  * @param r: Interval's right
135  **/
136  inline void pushInterval(lfixq l, lfixq r) {
137  Interval elem;
138  elem.left = l;
139  elem.right = r;
140  roots.push_back(elem);
141  }
142  inline void pushInterval(double l, double r) {
143  Interval elem;
144  elem.left = l;
145  elem.right = r;
146  roots.push_back(elem);
147  }
148  /**
149  * Pop the last interval
150  *
151  * @param
152  **/
153  inline void popInterval() {
154  if (roots.size())
155  roots.pop_back();
156  }
157  /**
158  * Clear all the intervals
159  *
160  * @param
161  **/
162  inline void clear() {
163  roots.clear();
164  }
165 
166  /**
167  * Get a root of a variate
168  *
169  * @param k: Offset of roots
170  * @param l: Offset of variables in an ascending order
171  **/
172  inline Interval* interval(int k, int l=0) {
173  return &roots[k*var+l];
174  }
175  /**
176  * Get a root of variates
177  *
178  * @param k: Offset of roots
179  **/
180  inline Intervals root(int k) {
181  Intervals pIs(var);
182  for (int i = 0; i < var; ++i)
183  pIs.roots.push_back(roots[k*var+i]);
184  return pIs;
185  }
186  /**
187  * Set variable names
188  *
189  * @param xs: Variable names
190  **/
191  inline void setVariableNames (std::vector<std::string> xs) {
192  int ns = xs.size();
193  if (ns >= var) {
194  names[0] = "9";
195  for (int i = var, j = 0; i > 0; --i, ++j)
196  names[i] = xs[j];
197  }
198  else
199  std::cout << "BPAS: warning, not enough variables to set in Intervals." << std::endl;
200  }
201  /**
202  * Copy the k-th root
203  *
204  * @param pIs: A list of roots
205  * @param k: Offset of roots
206  **/
207  void copyFrom(Intervals& pIs, int k);
208  /**
209  * Scale to 2^k
210  *
211  * @param k: The power of 2
212  **/
213  void scale(int k);
214  /**
215  * Scale to that over 2
216  *
217  * @param
218  **/
219  void transformLeft();
220  /**
221  * Scale to that plus 1 and then over 2
222  *
223  * @param
224  **/
225  void transformRight();
226  /**
227  * Negate each interval and re-sort
228  *
229  * @param
230  **/
231  void negate();
232  /**
233  * Merge another intervals into the end of the current one
234  *
235  * @param pIs: A list of intervals
236  **/
237  void concatenate(Intervals& pIs);
238 
239  /**
240  * Overload stream operator <<
241  *
242  * @param out: Stream object
243  * @param a: Intervals
244  **/
245  friend std::ostream& operator<< (std::ostream &out, Intervals& a);
246  bool isExactEqual(Intervals& pIs);
247 };
248 
249 
250 /**
251  * Swap two intervals
252  **/
253 inline void swapInterval(Interval* a, Interval* b) {
254  Interval tmp;
255  tmp.left = a->left;
256  tmp.right = a->right;
257  a->left = b->left;
258  a->right = b->right;
259  b->left = tmp.left;
260  b->right = tmp.right;
261 }
262 
263 #endif
264 /* This file is part of the BPAS library http://www.bpaslib.org
265 
266  BPAS is free software: you can redistribute it and/or modify
267  it under the terms of the GNU General Public License as published by
268  the Free Software Foundation, either version 3 of the License, or
269  (at your option) any later version.
270 
271  BPAS is distributed in the hope that it will be useful,
272  but WITHOUT ANY WARRANTY; without even the implied warranty of
273  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
274  GNU General Public License for more details.
275 
276  You should have received a copy of the GNU General Public License
277  along with BPAS. If not, see <http://www.gnu.org/licenses/>.
278 
279  Copyright: Changbo Chen <changbo.chen@hotmail.com>
280  Farnam Mansouri <mansouri.farnam@gmail.com>
281  Marc Moreno Maza <moreno@csd.uwo.ca>
282  Ning Xie <nxie6@csd.uwo.ca>
283  Yuzhen Xie <yuzhenxie@yahoo.ca>
284 
285 */
286 
287 
Intervals root(int k)
Definition: interval.h:180
int numberOfVariables()
Definition: interval.h:109
void transformRight()
int numberOfIntervals()
Definition: interval.h:118
void transformLeft()
void scale(int k)
Intervals(const Intervals &b)
Definition: interval.h:76
Intervals(int v)
Definition: interval.h:61
Definition: interval.h:14
void setVariableNames(std::vector< std::string > xs)
Definition: interval.h:191
Intervals()
Definition: interval.h:51
void clear()
Definition: interval.h:162
void negate()
Intervals & operator=(Intervals b)
Definition: interval.h:94
void concatenate(Intervals &pIs)
void pushInterval(lfixq l, lfixq r)
Definition: interval.h:136
void pushInterval(Interval pI)
Definition: interval.h:127
void popInterval()
Definition: interval.h:153
friend std::ostream & operator<<(std::ostream &out, Intervals &a)
Interval * interval(int k, int l=0)
Definition: interval.h:172
void copyFrom(Intervals &pIs, int k)
Definition: interval.h:35
~Intervals()
Definition: interval.h:85