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 <memory>
34 #include <cmath>
35 
36 #undef LOGLEVEL
37 #define LOGLEVEL 0
38 
39 namespace schnek {
40 namespace expression {
41 
42  template<class vtype>
43  struct OperatorId
44  {
45  static vtype eval(vtype val);
46  };
47 
48  template<class vtype>
49  struct OperatorNeg
50  {
51  static vtype eval(vtype val);
52  };
53 
54  template<class vtype>
55  struct OperatorInv
56  {
57  static vtype eval(vtype val);
58  };
59 
60  template<class vtype>
62 
63  template<class vtype>
64  struct OperatorAdd
65  {
66  enum { isPositive = true };
70 
71  static vtype eval(vtype val1, vtype val2);
72  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
73  };
74 
75  template<class vtype>
76  struct OperatorSubtract
77  {
78  enum { isPositive = false };
82 
83  static vtype eval(vtype val1, vtype val2);
84  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
85  };
86 
87  template<class vtype>
89 
90  template<class vtype>
92  {
93  enum { isPositive = true };
97 
98  static vtype eval(vtype val1, vtype val2);
99  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
100  };
101 
102  template<class vtype>
103  struct OperatorDivide
104  {
105  enum { isPositive = false };
109 
110  static vtype eval(vtype val1, vtype val2);
111  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
112  };
113 
114  template<class vtype>
116  {
117  enum { isPositive = true };
121 
122  static vtype eval(vtype val1, vtype val2);
123  static typename Expression<vtype>::pExpression negate(typename Expression<vtype>::pExpression val);
124  };
125 
126  template<class vtype>
127  vtype OperatorId<vtype>::eval(vtype val)
128  {
129  SCHNEK_TRACE_LOG(5, "OperatorId<vtype>::eval(" << val << ")")
130  return val;
131  }
132 
133  template<class vtype>
134  vtype OperatorNeg<vtype>::eval(vtype val)
135  {
136  SCHNEK_TRACE_LOG(5, "OperatorNeg<vtype>::eval(" << val << ")")
137  return -val;
138  }
139 
140  template<class vtype>
141  vtype OperatorInv<vtype>::eval(vtype val)
142  {
143  SCHNEK_TRACE_LOG(5, "OperatorInv<vtype>::eval(" << val << ")")
144  return 1/val;
145  }
146 
147  template<class vtype>
148  vtype OperatorAdd<vtype>::eval(vtype val1, vtype val2)
149  {
150  SCHNEK_TRACE_LOG(5, "OperatorAdd<vtype>::eval(" << val1 << ", " << val2 << ")")
151  return val1 + val2;
152  }
153 
154  template<class vtype>
155  vtype OperatorSubtract<vtype>::eval(vtype val1, vtype val2)
156  {
157  SCHNEK_TRACE_LOG(5, "OperatorSubtract<vtype>::eval(" << val1 << ", " << val2 << ")")
158  return val1 - val2;
159  }
160 
161  template<class vtype>
162  vtype OperatorMultiply<vtype>::eval(vtype val1, vtype val2)
163  {
164  SCHNEK_TRACE_LOG(5, "OperatorMultiply<vtype>::eval(" << val1 << ", " << val2 << ")")
165  return val1 * val2;
166  }
167 
168  template<class vtype>
169  vtype OperatorDivide<vtype>::eval(vtype val1, vtype val2)
170  {
171  SCHNEK_TRACE_LOG(5, "OperatorDivide<vtype>::eval(" << val1 << ", " << val2 << ")")
172  return val1 / val2;
173  }
174 
175  template<class vtype>
176  vtype OperatorExponent<vtype>::eval(vtype val1, vtype val2)
177  {
178  SCHNEK_TRACE_LOG(5, "OperatorExponent<vtype>::eval(" << val1 << ", " << val2 << ")")
179  return pow(val1,val2);
180  }
181 
182 
183  template<class vtype>
185  {
186  return std::make_shared<UnaryOp<OperatorNeg<vtype>, vtype> >(val);
187  }
188 
189  template<class vtype>
191  {
192  return std::make_shared<UnaryOp<OperatorNeg<vtype>, vtype> >(val);
193  }
194 
195  template<class vtype>
197  {
198  return std::make_shared<UnaryOp<OperatorInv<vtype>, vtype> >(val);
199  }
200 
201  template<class vtype>
203  {
204  return std::make_shared<UnaryOp<OperatorInv<vtype>, vtype> >(val);
205  }
206 
207  template<class vtype>
209  {
210  return val;
211  }
212 
213 
214  template<>
215  inline std::string OperatorNeg<std::string>::eval(std::string) { return ""; }
216 
217  template<>
218  inline std::string OperatorInv<std::string>::eval(std::string) { return ""; }
219 
220  template<>
221  inline std::string OperatorSubtract<std::string>::eval(std::string, std::string) { return ""; }
222 
223  template<>
224  inline std::string OperatorMultiply<std::string>::eval(std::string, std::string) { return ""; }
225 
226  template<>
227  inline std::string OperatorDivide<std::string>::eval(std::string, std::string) { return ""; }
228 
229  template<>
230  inline std::string OperatorExponent<std::string>::eval(std::string, std::string) { return ""; }
231 
232 } // namespace expression
233 } // namespace schnek
234 
235 #undef LOGLEVEL
236 #define LOGLEVEL 0
237 
238 #endif // SCHNEK_OPERATORS_HPP_
std::shared_ptr< Expression > pExpression
A pointer to an Expression.
Definition: expression.hpp:113
Definition: operators.hpp:115
Definition: operators.hpp:49
Definition: algo.hpp:30
Definition: operators.hpp:64
Definition: operators.hpp:55
#define SCHNEK_TRACE_LOG(i, x)
Definition: logger.hpp:54
Definition: operators.hpp:88
Definition: operators.hpp:43
Definition: operators.hpp:61
Definition: operators.hpp:91