Basic Polynomial Algebra Subprograms (BPAS)  v. 1.791
interval.h
1 #ifndef _INTERVAL_H_
2 #define _INTERVAL_H_
3 
4 #include "../DyadicRationalNumber/globals.h"
5 #include "../Symbol/Symbol.hpp"
6 #include <iostream>
7 
8 /**
9  * Data Structure for interval [a, b].
10  */
11 class Interval {
12  public:
13  lfixq left;
14  lfixq right;
15 };
16 
17 /**
18  * Interval addition
19  * @res = @a + @b
20  **/
21 inline void intervalAddition(Interval* res, Interval* a, Interval* b) {
22  res->left = a->left + b->left;
23  res->right = a->right + b->right;
24 }
25 
26 /**
27  * Interval multiplication
28  * @res = @a * @b
29  **/
30 void intervalMultiplication(Interval* res, Interval* a, Interval* b);
31 
32 /** Interval lists for real roots of multivariate polynomials.
33  * i.e. []*..*[], .., []*..*[]
34  **/
35 class Intervals {
36  private:
37  int var; // Number of variates
38  Symbol* 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 Symbol[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 Symbol[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 Symbol[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  **/
94  inline Intervals& operator= (Intervals b) {
95  if (this != &b) {
96  delete [] names;
97  var = b.var;
98  roots = b.roots;
99  names = new Symbol[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 (const std::vector<Symbol>& 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
Intervals root(int k)
Get a root of variates.
Definition: interval.h:180
int numberOfVariables()
Get number of variables.
Definition: interval.h:109
void setVariableNames(const std::vector< Symbol > &xs)
Set variable names.
Definition: interval.h:191
int numberOfIntervals()
Get the number of roots.
Definition: interval.h:118
Intervals(const Intervals &b)
Copy construct.
Definition: interval.h:76
Intervals(int v)
Construct intervals with number of variates.
Definition: interval.h:61
Data Structure for interval [a, b].
Definition: interval.h:11
Intervals()
Construct intervals.
Definition: interval.h:51
void clear()
Clear all the intervals.
Definition: interval.h:162
void pushInterval(lfixq l, lfixq r)
Push an interval to the end of the list.
Definition: interval.h:136
void pushInterval(Interval pI)
Push an interval to the end of the list.
Definition: interval.h:127
An encapsulation of a mathematical symbol.
Definition: Symbol.hpp:23
void popInterval()
Pop the last interval.
Definition: interval.h:153
Interval * interval(int k, int l=0)
Get a root of a variate.
Definition: interval.h:172
Interval lists for real roots of multivariate polynomials.
Definition: interval.h:35
~Intervals()
Destroy intervals.
Definition: interval.h:85