FLOPC++
MP_constant.cpp
Go to the documentation of this file.
1 // ******************** FlopCpp **********************************************
2 // File: MP_constant.cpp
3 // $Id$
4 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
5 // Copyright (C) 2003 Tim Helge Hultberg
6 // All Rights Reserved.
7 //****************************************************************************
8 
9 #include <float.h>
10 #include <cmath>
11 #include <sstream>
12 #include "MP_constant.hpp"
13 #include "MP_data.hpp"
14 #include "MP_domain.hpp"
15 #include "MP_index.hpp"
16 
17 namespace flopc {
18 
19  class Constant_index : public Constant_base {
20  friend class Constant;
21  private:
22  Constant_index(const MP_index_exp& i) : I(i) {}
23  double evaluate() const {
24  return I->evaluate();
25  }
26  const MP_index_exp I;
27  };
28 
29  class Constant_double : public Constant_base {
30  friend class Constant;
31  private:
32  Constant_double(double d) : D(d) {}
33  double evaluate() const {
34  return D;
35  }
36  double D;
37  };
38 
39  class Constant_abs : public Constant_base {
40  friend Constant abs(const Constant& c);
41  private:
42  Constant_abs(const Constant& c) : C(c) {}
43  double evaluate() const {
44  return fabs(C->evaluate());
45  }
47  };
48  Constant abs(const Constant& c) {
49  return new Constant_abs(c);
50  }
51 
52  class Constant_pos : public Constant_base {
53  friend Constant pos(const Constant& c);
54  private:
55  Constant_pos(const Constant& c) : C(c) {}
56  double evaluate() const {
57  double temp = C->evaluate();
58  if (temp>0) {
59  return temp;
60  } else {
61  return 0.0;
62  }
63  }
65  };
66  Constant pos(const Constant& c) {
67  return new Constant_pos(c);
68  }
69 
70  class Constant_ceil : public Constant_base {
71  friend Constant ceil(const Constant& c);
72  private:
73  Constant_ceil(const Constant& c) : C(c) {}
74  double evaluate() const {
75  return std::ceil(C->evaluate());
76  }
78  };
79  Constant ceil(const Constant& c) {
80  return new Constant_ceil(c);
81  }
82 
83  class Constant_floor : public Constant_base {
84  friend Constant floor(const Constant& c);
85  private:
86  Constant_floor(const Constant& c) : C(c) {}
87  double evaluate() const {
88  return std::floor(C->evaluate());
89  }
91  };
92  Constant floor(const Constant& c) {
93  return new Constant_floor(c);
94  }
95 
96  class Constant_exp : public Constant_base {
97  protected:
98  Constant_exp(const Constant& i, const Constant& j) : left(i),right(j) {}
100  };
101 
102  class Constant_min_2 : public Constant_exp {
103  friend Constant minimum(const Constant& a, const Constant& b);
104  private:
105  Constant_min_2(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
106  double evaluate() const {
107  return std::min(left->evaluate(),right->evaluate());
108  }
109  };
110 
111  Constant minimum(const Constant& a, const Constant& b) {
112  return new Constant_min_2(a,b);
113  }
114 
115  class Constant_max_2 : public Constant_exp {
116  friend Constant maximum(const Constant& a, const Constant& b);
117  private:
118  Constant_max_2(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
119  double evaluate() const {
120  return std::max(left->evaluate(),right->evaluate());
121  }
122  };
123 
124  Constant maximum(const Constant& a, const Constant& b) {
125  return new Constant_max_2(a,b);
126  }
127 
128  class Constant_plus : public Constant_exp {
129  friend Constant operator+(const Constant& a, const Constant& b);
130  friend Constant operator+(MP_index& a, MP_index& b);
131  private:
132  Constant_plus(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
133  double evaluate() const {
134  return left->evaluate()+right->evaluate();
135  }
136  };
137 
138  Constant operator+(const Constant& a, const Constant& b) {
139  return new Constant_plus(a,b);
140  }
142  return new Constant_plus(Constant(a),Constant(b));
143  }
144 
145  class Constant_minus : public Constant_exp {
146  friend Constant operator-(const Constant& a, const Constant& b);
147  friend Constant operator-(MP_index& a, MP_index& b);
148  private:
149  Constant_minus(const Constant& i, const Constant& j): Constant_exp(i,j) {}
150  double evaluate() const {
151  return left->evaluate()-right->evaluate();
152  }
153  };
154 
155  Constant operator-(const Constant& a, const Constant& b) {
156  return new Constant_minus(a,b);
157  }
158 
160  return new Constant_minus(Constant(a),Constant(b));
161  }
162 
163 
165  friend Constant operator-(const Constant& a);
166  private:
168  double evaluate() const {
169  return -left->evaluate();
170  }
172  };
174  return new Constant_unary_minus(a);
175  }
176 
177  class Constant_mult : public Constant_exp {
178  friend Constant operator*(const Constant& a, const Constant& b);
179  private:
180  Constant_mult(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
181  double evaluate() const {
182  return left->evaluate()*right->evaluate();
183  }
184  };
185 
186  Constant operator*(const Constant& a, const Constant& b) {
187  return new Constant_mult(a,b);
188  }
189 
190  class Constant_div : public Constant_exp {
191  friend Constant operator/(const Constant& a, const Constant& b);
192  private:
193  Constant_div(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
194  double evaluate() const {
195  return left->evaluate()/right->evaluate();
196  }
197  };
198 
199  Constant operator/(const Constant& a, const Constant& b) {
200  return new Constant_div(a,b);
201  }
202 
203  class Constant_if : public Constant_exp {
204  friend Constant mpif(const MP_boolean& c, const Constant& a, const Constant& b);
205  private:
206  Constant_if(const MP_boolean b, const Constant& i, const Constant& j) : Constant_exp(i,j), B(b) {}
207  double evaluate() const {
208  if (B->evaluate()==true) {
209  return left->evaluate();
210  } else {
211  return right->evaluate();
212  }
213  }
214 
216  };
217 
218  Constant mpif(const MP_boolean& c, const Constant& a, const Constant& b) {
219  return new Constant_if(c,a,b);
220  }
221 
222 
223  class Constant_max : public Constant_base {
224  friend Constant maximum(const MP_domain& i, const Constant& e);
225  private:
226  Constant_max(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
227  double evaluate() const {
228  MaxFunctor MF(exp);
229  d.forall(MF);
230  return MF.the_max;
231  }
232  class MaxFunctor : public Functor {
233  public:
234  MaxFunctor(Constant exp) : C(exp), the_max(DBL_MIN) {}
235  void operator()() const {
236  double temp = C->evaluate();
237  if (temp > the_max) {
238  the_max = temp;
239  }
240  }
242  mutable double the_max;
243  };
244 
247  };
248 
249  class Constant_min : public Constant_base, public Functor {
250  friend Constant minimum(const MP_domain& i, const Constant& e);
251  private:
252  Constant_min(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
253  void operator()() const {
254  double temp = exp->evaluate();
255  if (temp < the_min) {
256  the_min = temp;
257  }
258  }
259  double evaluate() const {
260  the_min = DBL_MAX;
261  d.forall(this);
262  return the_min;
263  }
264 
267  mutable double the_min;
268  };
269 
270  class Constant_sum : public Constant_base, public Functor {
271  friend Constant sum(const MP_domain& i, const Constant& e);
272  private:
273  Constant_sum(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
274  void operator()() const {
275  the_sum += exp->evaluate();
276  }
277  double evaluate() const {
278  the_sum = 0;
279  d.forall(this);
280  return the_sum;
281  }
282 
285  mutable double the_sum;
286  };
287 
288  class Constant_product : public Constant_base, public Functor {
289  friend Constant product(const MP_domain& i, const Constant& e);
290  private:
291  Constant_product(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
292  void operator()() const {
293  the_product *= exp->evaluate();
294  }
295  double evaluate() const {
296  the_product = 1;
297  d.forall(this);
298  return the_product;
299  }
300 
303  mutable double the_product;
304  };
305 
306  Constant maximum(const MP_domain& i, const Constant& e) {
307  return new Constant_max(i,e);
308  }
309  Constant minimum(const MP_domain& i, const Constant& e) {
310  return new Constant_min(i,e);
311  }
312  Constant sum(const MP_domain& i, const Constant& e) {
313  return new Constant_sum(i,e);
314  }
315  Constant product(const MP_domain& i, const Constant& e) {
316  return new Constant_product(i,e);
317  }
318 
320  Handle<Constant_base*>(const_cast<DataRef*>(&d)) {}
321 
324 
325  Constant::Constant(double d) :
327 
330 
331 } // End of namespace flopc
332 
Constant_pos(const Constant &c)
Definition: MP_constant.cpp:55
double evaluate() const
Constant_exp(const Constant &i, const Constant &j)
Definition: MP_constant.cpp:98
Constant maximum(const Constant &a, const Constant &b)
Returns the larger of two constants.This is used in the formation of an expression.
Constant_min_2(const Constant &i, const Constant &j)
friend Constant minimum(const Constant &a, const Constant &b)
Returns the smaller of two constants.This is used in the formation of an expression.
Constant_sum(const MP_domain &i, const Constant &e)
Constant_max_2(const Constant &i, const Constant &j)
friend Constant maximum(const MP_domain &i, const Constant &e)
Returns the maximum over the domain of the constant.
Constant abs(const Constant &c)
for computing the absolute value of a constant value.This is used in the normal formation of an expre...
Definition: MP_constant.cpp:48
friend Constant operator+(const Constant &a, const Constant &b)
Returns the sum of two constants.This is used in the formation of an expression.
Constant operator/(const Constant &a, const Constant &b)
Returns the quotient of two constants.This is used in the formation of an expression.
Constant_max(const MP_domain &i, const Constant &e)
double evaluate() const
Constant_abs(const Constant &c)
Definition: MP_constant.cpp:42
Constant_div(const Constant &i, const Constant &j)
friend Constant ceil(const Constant &c)
The ceiling integral value of the input constant.This is used in the formation of an expression...
Definition: MP_constant.cpp:79
const MP_index_exp I
Definition: MP_constant.cpp:26
friend Constant floor(const Constant &c)
The floor integral value of the input constant.This is used in the formation of an expression...
Definition: MP_constant.cpp:92
Constant product(const MP_domain &i, const Constant &e)
Returns the sum of two constants.
Constant_floor(const Constant &c)
Definition: MP_constant.cpp:86
void operator()() const
double evaluate() const
Definition: MP_constant.cpp:23
Constant(const MP_index_exp &i)
Representation of an index.This is one of the main public interface classes. It is used to iterate th...
Definition: MP_index.hpp:53
void operator()() const
Utility for doing reference counted pointers.
Constant ceil(const Constant &c)
The ceiling integral value of the input constant.This is used in the formation of an expression...
Definition: MP_constant.cpp:79
Constant floor(const Constant &c)
The floor integral value of the input constant.This is used in the formation of an expression...
Definition: MP_constant.cpp:92
Constant operator+(const Constant &a, const Constant &b)
Returns the sum of two constants.This is used in the formation of an expression.
double evaluate() const
Constant mpif(const MP_boolean &c, const Constant &a, const Constant &b)
void operator()() const
Constant_mult(const Constant &i, const Constant &j)
double evaluate() const
friend Constant product(const MP_domain &i, const Constant &e)
Returns the sum of two constants.
double evaluate() const
Definition: MP_constant.cpp:56
friend Constant operator/(const Constant &a, const Constant &b)
Returns the quotient of two constants.This is used in the formation of an expression.
Representation of an expression involving an index.This is one of the main public interface classes...
Definition: MP_index.hpp:141
Base class for all "constant" types of data.
Definition: MP_constant.hpp:20
friend Constant minimum(const MP_domain &i, const Constant &e)
Returns the sum of two constants.
double evaluate() const
friend Constant maximum(const Constant &a, const Constant &b)
Returns the larger of two constants.This is used in the formation of an expression.
double evaluate() const
Constant_index(const MP_index_exp &i)
Definition: MP_constant.cpp:22
friend Constant abs(const Constant &c)
for computing the absolute value of a constant value.This is used in the normal formation of an expre...
Definition: MP_constant.cpp:48
double evaluate() const
Definition: MP_constant.cpp:33
Constant_minus(const Constant &i, const Constant &j)
Reference to a set of data.
Definition: MP_data.hpp:27
double evaluate() const
Constant_min(const MP_domain &i, const Constant &e)
double evaluate() const
Constant_plus(const Constant &i, const Constant &j)
friend Constant operator*(const Constant &a, const Constant &b)
Returns the product of two constants.This is used in the formation of an expression.
Constant operator-(const Constant &a, const Constant &b)
Returns the difference of two constants.This is used in the formation of an expression.
All flopc++ code is contained within the flopc namespace.
Definition: flopc.cpp:11
double evaluate() const
Constant_unary_minus(const Constant &i)
Constant minimum(const Constant &a, const Constant &b)
Returns the smaller of two constants.This is used in the formation of an expression.
Reference counted class for all "boolean" types of data.This contains counters to ConstantBase pointe...
Definition: MP_boolean.hpp:40
Range over which some other constuct is defined.This is one of the main public interface classes...
Definition: MP_domain.hpp:61
Function object. Often used.
friend Constant sum(const MP_domain &i, const Constant &e)
Returns the sum of two constants.
Constant pos(const Constant &c)
for returning non-negative value of the constant.This is used in the formation of an expression...
Definition: MP_constant.cpp:66
Reference counted class for all "constant" types of data.
Definition: MP_constant.hpp:49
friend Constant mpif(const MP_boolean &c, const Constant &a, const Constant &b)
void forall(const Functor *op) const
Special conditional operation on the domain.
Definition: MP_domain.cpp:88
double evaluate() const
Definition: MP_constant.cpp:74
double evaluate() const
Definition: MP_constant.cpp:87
Constant operator*(const Constant &a, const Constant &b)
Returns the product of two constants.This is used in the formation of an expression.
Constant_if(const MP_boolean b, const Constant &i, const Constant &j)
friend Constant pos(const Constant &c)
for returning non-negative value of the constant.This is used in the formation of an expression...
Definition: MP_constant.cpp:66
double evaluate() const
Definition: MP_constant.cpp:43
Constant sum(const MP_domain &i, const Constant &e)
Returns the sum of two constants.
friend Constant operator-(const Constant &a, const Constant &b)
Returns the difference of two constants.This is used in the formation of an expression.
double evaluate() const
double evaluate() const
Constant_ceil(const Constant &c)
Definition: MP_constant.cpp:73
friend Constant operator-(const Constant &a)
Constant_product(const MP_domain &i, const Constant &e)