Schnek
operators.hpp
1 /*
2  * operators.hpp
3  *
4  * Created on: 27 Jan 2011
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_OPERATORS_HPP_
28 #define SCHNEK_OPERATORS_HPP_
29 
30 #include "expression.hpp"
31 #include "../util/logger.hpp"
32 
33 #include <boost/make_shared.hpp>
34 
35 #include <cmath>
36 
37 #undef LOGLEVEL
38 #define LOGLEVEL 0
39 
40 namespace schnek {
41 namespace expression {
42 
43  template<class vtype>
44  struct OperatorId
45  {
46  static vtype eval(vtype val);
47  };
48 
49  template<class vtype>
50  struct OperatorNeg
51  {
52  static vtype eval(vtype val);
53  };
54 
55  template<class vtype>
56  struct OperatorInv
57  {
58  static vtype eval(vtype val);
59  };
60 
61  template<class vtype>
63 
64  template<class vtype>
65  struct OperatorAdd
66  {
67  enum { isPositive = true };
71 
72  static vtype eval(vtype val1, vtype val2);
73  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
74  };
75 
76  template<class vtype>
77  struct OperatorSubtract
78  {
79  enum { isPositive = false };
83 
84  static vtype eval(vtype val1, vtype val2);
85  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
86  };
87 
88  template<class vtype>
90 
91  template<class vtype>
93  {
94  enum { isPositive = true };
98 
99  static vtype eval(vtype val1, vtype val2);
100  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
101  };
102 
103  template<class vtype>
104  struct OperatorDivide
105  {
106  enum { isPositive = false };
110 
111  static vtype eval(vtype val1, vtype val2);
112  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
113  };
114 
115  template<class vtype>
117  {
118  enum { isPositive = true };
122 
123  static vtype eval(vtype val1, vtype val2);
124  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
125  };
126 
127  template<class vtype>
128  vtype OperatorId<vtype>::eval(vtype val)
129  {
130  SCHNEK_TRACE_LOG(5, "OperatorId<vtype>::eval(" << val << ")")
131  return val;
132  }
133 
134  template<class vtype>
135  vtype OperatorNeg<vtype>::eval(vtype val)
136  {
137  SCHNEK_TRACE_LOG(5, "OperatorNeg<vtype>::eval(" << val << ")")
138  return -val;
139  }
140 
141  template<class vtype>
142  vtype OperatorInv<vtype>::eval(vtype val)
143  {
144  SCHNEK_TRACE_LOG(5, "OperatorInv<vtype>::eval(" << val << ")")
145  return 1/val;
146  }
147 
148  template<class vtype>
149  vtype OperatorAdd<vtype>::eval(vtype val1, vtype val2)
150  {
151  SCHNEK_TRACE_LOG(5, "OperatorAdd<vtype>::eval(" << val1 << ", " << val2 << ")")
152  return val1 + val2;
153  }
154 
155  template<class vtype>
156  vtype OperatorSubtract<vtype>::eval(vtype val1, vtype val2)
157  {
158  SCHNEK_TRACE_LOG(5, "OperatorSubtract<vtype>::eval(" << val1 << ", " << val2 << ")")
159  return val1 - val2;
160  }
161 
162  template<class vtype>
163  vtype OperatorMultiply<vtype>::eval(vtype val1, vtype val2)
164  {
165  SCHNEK_TRACE_LOG(5, "OperatorMultiply<vtype>::eval(" << val1 << ", " << val2 << ")")
166  return val1 * val2;
167  }
168 
169  template<class vtype>
170  vtype OperatorDivide<vtype>::eval(vtype val1, vtype val2)
171  {
172  SCHNEK_TRACE_LOG(5, "OperatorDivide<vtype>::eval(" << val1 << ", " << val2 << ")")
173  return val1 / val2;
174  }
175 
176  template<class vtype>
177  vtype OperatorExponent<vtype>::eval(vtype val1, vtype val2)
178  {
179  SCHNEK_TRACE_LOG(5, "OperatorExponent<vtype>::eval(" << val1 << ", " << val2 << ")")
180  return pow(val1,val2);
181  }
182 
183 
184  template<class vtype>
186  {
187  return boost::make_shared<UnaryOp<OperatorNeg<vtype>, vtype> >(val);
188  }
189 
190  template<class vtype>
192  {
193  return boost::make_shared<UnaryOp<OperatorNeg<vtype>, vtype> >(val);
194  }
195 
196  template<class vtype>
198  {
199  return boost::make_shared<UnaryOp<OperatorInv<vtype>, vtype> >(val);
200  }
201 
202  template<class vtype>
204  {
205  return boost::make_shared<UnaryOp<OperatorInv<vtype>, vtype> >(val);
206  }
207 
208  template<class vtype>
210  {
211  return val;
212  }
213 
214 
215  template<>
216  inline std::string OperatorNeg<std::string>::eval(std::string val) { return ""; }
217 
218  template<>
219  inline std::string OperatorInv<std::string>::eval(std::string val) { return ""; }
220 
221  template<>
222  inline std::string OperatorSubtract<std::string>::eval(std::string val1, std::string val2) { return ""; }
223 
224  template<>
225  inline std::string OperatorMultiply<std::string>::eval(std::string val1, std::string val2) { return ""; }
226 
227  template<>
228  inline std::string OperatorDivide<std::string>::eval(std::string val1, std::string val2) { return ""; }
229 
230  template<>
231  inline std::string OperatorExponent<std::string>::eval(std::string val1, std::string val2) { return ""; }
232 
233 } // namespace expression
234 } // namespace schnek
235 
236 #undef LOGLEVEL
237 #define LOGLEVEL 0
238 
239 #endif // SCHNEK_OPERATORS_HPP_
boost::shared_ptr< Expression > pExpression
A pointer to an Expression.
Definition: expression.hpp:108
Definition: operators.hpp:116
Definition: operators.hpp:50
Definition: algo.hpp:30
Definition: operators.hpp:65
Definition: operators.hpp:56
#define SCHNEK_TRACE_LOG(i, x)
Definition: logger.hpp:54
Definition: operators.hpp:89
Definition: operators.hpp:44
Definition: operators.hpp:62
Definition: operators.hpp:92