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 #ifdef _MSC_VER
13 #include <algorithm>
14 #endif
15 #include "MP_constant.hpp"
16 #include "MP_data.hpp"
17 #include "MP_domain.hpp"
18 #include "MP_index.hpp"
19 
20 using namespace std;
21 
22 namespace flopc {
23 
24  class Constant_index : public Constant_base {
25  friend class Constant;
26  private:
27  Constant_index(const MP_index_exp& i) : I(i) {}
28  double evaluate() const {
29  return I->evaluate();
30  }
31  const MP_index_exp I;
32  };
33 
34  class Constant_double : public Constant_base {
35  friend class Constant;
36  private:
37  Constant_double(double d) : D(d) {}
38  double evaluate() const {
39  return D;
40  }
41  double D;
42  };
43 
44  class Constant_abs : public Constant_base {
45  friend Constant abs(const Constant& c);
46  private:
47  Constant_abs(const Constant& c) : C(c) {}
48  double evaluate() const {
49  return fabs(C->evaluate());
50  }
52  };
53  Constant abs(const Constant& c) {
54  return new Constant_abs(c);
55  }
56 
57  class Constant_pos : public Constant_base {
58  friend Constant pos(const Constant& c);
59  private:
60  Constant_pos(const Constant& c) : C(c) {}
61  double evaluate() const {
62  double temp = C->evaluate();
63  if (temp>0) {
64  return temp;
65  } else {
66  return 0.0;
67  }
68  }
70  };
71  Constant pos(const Constant& c) {
72  return new Constant_pos(c);
73  }
74 
75  class Constant_ceil : public Constant_base {
76  friend Constant ceil(const Constant& c);
77  private:
78  Constant_ceil(const Constant& c) : C(c) {}
79  double evaluate() const {
80  return std::ceil(C->evaluate());
81  }
83  };
84  Constant ceil(const Constant& c) {
85  return new Constant_ceil(c);
86  }
87 
88  class Constant_floor : public Constant_base {
89  friend Constant floor(const Constant& c);
90  private:
91  Constant_floor(const Constant& c) : C(c) {}
92  double evaluate() const {
93  return std::floor(C->evaluate());
94  }
96  };
97  Constant floor(const Constant& c) {
98  return new Constant_floor(c);
99  }
100 
101  class Constant_exp : public Constant_base {
102  protected:
103  Constant_exp(const Constant& i, const Constant& j) : left(i),right(j) {}
105  };
106 
107  class Constant_min_2 : public Constant_exp {
108  friend Constant minimum(const Constant& a, const Constant& b);
109  private:
110  Constant_min_2(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
111  double evaluate() const {
112  return std::min(left->evaluate(),right->evaluate());
113  }
114  };
115 
116  Constant minimum(const Constant& a, const Constant& b) {
117  return new Constant_min_2(a,b);
118  }
119 
120  class Constant_max_2 : public Constant_exp {
121  friend Constant maximum(const Constant& a, const Constant& b);
122  private:
123  Constant_max_2(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
124  double evaluate() const {
125  return std::max(left->evaluate(),right->evaluate());
126  }
127  };
128 
129  Constant maximum(const Constant& a, const Constant& b) {
130  return new Constant_max_2(a,b);
131  }
132 
133  class Constant_plus : public Constant_exp {
134  friend Constant operator+(const Constant& a, const Constant& b);
135  friend Constant operator+(MP_index& a, MP_index& b);
136  private:
137  Constant_plus(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
138  double evaluate() const {
139  return left->evaluate()+right->evaluate();
140  }
141  };
142 
143  Constant operator+(const Constant& a, const Constant& b) {
144  return new Constant_plus(a,b);
145  }
147  return new Constant_plus(Constant(a),Constant(b));
148  }
149 
150  class Constant_minus : public Constant_exp {
151  friend Constant operator-(const Constant& a, const Constant& b);
152  friend Constant operator-(MP_index& a, MP_index& b);
153  private:
154  Constant_minus(const Constant& i, const Constant& j): Constant_exp(i,j) {}
155  double evaluate() const {
156  return left->evaluate()-right->evaluate();
157  }
158  };
159 
160  Constant operator-(const Constant& a, const Constant& b) {
161  return new Constant_minus(a,b);
162  }
163 
165  return new Constant_minus(Constant(a),Constant(b));
166  }
167 
168  class Constant_mult : public Constant_exp {
169  friend Constant operator*(const Constant& a, const Constant& b);
170  private:
171  Constant_mult(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
172  double evaluate() const {
173  return left->evaluate()*right->evaluate();
174  }
175  };
176 
177  Constant operator*(const Constant& a, const Constant& b) {
178  return new Constant_mult(a,b);
179  }
180 
181  class Constant_div : public Constant_exp {
182  friend Constant operator/(const Constant& a, const Constant& b);
183  private:
184  Constant_div(const Constant& i, const Constant& j) : Constant_exp(i,j) {}
185  double evaluate() const {
186  return left->evaluate()/right->evaluate();
187  }
188  };
189 
190  Constant operator/(const Constant& a, const Constant& b) {
191  return new Constant_div(a,b);
192  }
193 
194  class Constant_max : public Constant_base, public Functor {
195  friend Constant maximum(const MP_domain& i, const Constant& e);
196  private:
197  Constant_max(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
198  void operator()() const {
199  double temp = exp->evaluate();
200  if (temp > the_max) {
201  the_max = temp;
202  }
203  }
204  double evaluate() const {
205  the_max = DBL_MIN;
206  d.Forall(this);
207  return the_max;
208  }
209 
212  mutable double the_max;
213  };
214 
215  class Constant_min : public Constant_base, public Functor {
216  friend Constant minimum(const MP_domain& i, const Constant& e);
217  private:
218  Constant_min(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
219  void operator()() const {
220  double temp = exp->evaluate();
221  if (temp < the_min) {
222  the_min = temp;
223  }
224  }
225  double evaluate() const {
226  the_min = DBL_MAX;
227  d.Forall(this);
228  return the_min;
229  }
230 
233  mutable double the_min;
234  };
235 
236  class Constant_sum : public Constant_base, public Functor {
237  friend Constant sum(const MP_domain& i, const Constant& e);
238  private:
239  Constant_sum(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
240  void operator()() const {
241  the_sum += exp->evaluate();
242  }
243  double evaluate() const {
244  the_sum = 0;
245  d.Forall(this);
246  return the_sum;
247  }
248 
251  mutable double the_sum;
252  };
253 
254  class Constant_product : public Constant_base, public Functor {
255  friend Constant product(const MP_domain& i, const Constant& e);
256  private:
257  Constant_product(const MP_domain& i, const Constant& e) : d(i), exp(e) {}
258  void operator()() const {
259  the_product *= exp->evaluate();
260  }
261  double evaluate() const {
262  the_product = 1;
263  d.Forall(this);
264  return the_product;
265  }
266 
269  mutable double the_product;
270  };
271 
272  Constant maximum(const MP_domain& i, const Constant& e) {
273  return new Constant_max(i,e);
274  }
275  Constant minimum(const MP_domain& i, const Constant& e) {
276  return new Constant_min(i,e);
277  }
278  Constant sum(const MP_domain& i, const Constant& e) {
279  return new Constant_sum(i,e);
280  }
281  Constant product(const MP_domain& i, const Constant& e) {
282  return new Constant_product(i,e);
283  }
284 
285  Constant::Constant(const DataRef& d) :
286  Handle<Constant_base*>(const_cast<DataRef*>(&d)) {}
287 
290 
291  Constant::Constant(double d) :
293 
296 
297 } // End of namespace flopc
298 
299 using namespace flopc;
300 
Constant_pos(const Constant &c)
Definition: MP_constant.cpp:60
double evaluate() const
Constant_exp(const Constant &i, const Constant &j)
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)
Constant_sum(const MP_domain &i, const Constant &e)
Constant_max_2(const Constant &i, const Constant &j)
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:53
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)
Constant_abs(const Constant &c)
Definition: MP_constant.cpp:47
Constant_div(const Constant &i, const Constant &j)
const MP_index_exp I
Definition: MP_constant.cpp:31
Constant product(const MP_domain &i, const Constant &e)
Returns the sum of two constants.
Constant_floor(const Constant &c)
Definition: MP_constant.cpp:91
void operator()() const
double evaluate() const
Definition: MP_constant.cpp:28
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:84
void operator()() const
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:97
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
void operator()() const
Constant_mult(const Constant &i, const Constant &j)
double evaluate() const
double evaluate() const
Definition: MP_constant.cpp:61
Representation of an expression involving an index.This is one of the main public interface classes...
Definition: MP_index.hpp:145
Base class for all &quot;constant&quot; types of data.
Definition: MP_constant.hpp:20
double evaluate() const
double evaluate() const
Constant_index(const MP_index_exp &i)
Definition: MP_constant.cpp:27
double evaluate() const
Definition: MP_constant.cpp:38
Constant_minus(const Constant &i, const Constant &j)
Reference to a set of data.
Definition: MP_data.hpp:29
double evaluate() const
Constant_min(const MP_domain &i, const Constant &e)
double evaluate() const
Constant_plus(const Constant &i, const Constant &j)
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
Constant(Constant_base *r)
Definition: MP_constant.hpp:50
Constant minimum(const Constant &a, const Constant &b)
Returns the smaller of two constants.This is used in the formation of an expression.
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.
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:71
Reference counted class for all &quot;constant&quot; types of data.
Definition: MP_constant.hpp:48
double evaluate() const
Definition: MP_constant.cpp:79
double evaluate() const
Definition: MP_constant.cpp:92
Constant operator*(const Constant &a, const Constant &b)
Returns the product of two constants.This is used in the formation of an expression.
double evaluate() const
Definition: MP_constant.cpp:48
Constant sum(const MP_domain &i, const Constant &e)
Returns the sum of two constants.
double evaluate() const
double evaluate() const
Constant_ceil(const Constant &c)
Definition: MP_constant.cpp:78
Constant_product(const MP_domain &i, const Constant &e)