Repository navigation
Expand file tree
/
Copy pathMathFunction.h
More file actions
328 lines (265 loc) · 8.25 KB
/
Copy pathMathFunction.h
File metadata and controls
328 lines (265 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#pragma once
#include <string>
#include <map>
#include <unordered_map>
#include <utility>
#include <memory>
#include <iostream>
#include <typeinfo>
#include <map>
#include <utility>
#include <cmath>
#include <tuple>
#include <algorithm>
#include "boost/lexical_cast.hpp"
//all valid operatons, ADD = +/-
enum Operator{
ADD,
SUB,
DIV,
MULT,
MOD,
POW,
NUM,
VAR
};
//node for the internal tree for the expression
template <typename T>
struct node{
typedef T type;
std::unique_ptr<node<T> > left;
std::unique_ptr<node<T> > right;
Operator op;
T value;
node(){
left = nullptr;
right = nullptr;
op = Operator::NUM;
value = T{};
}
};
//whole class
template <typename T>
class MathFunction{
private:
static std::multimap<int, std::string> _operatorOrder;
static std::unordered_map<std::string, int> _operatorPriority;
static std::unordered_map<std::string,Operator> _strToOp;
static std::map<Operator,std::string> _opToStr;
std::string _term;
std::unique_ptr<node<T> > _expression_root;
void _buildExpressionTree(node<T> *n,std::string);
void _compressTree(node<T>*);
bool _varInSubTree(node<T>*) const;
T _evaluateExpressionTree(node<T> *n,T) const;
std::tuple<std::string, Operator, std::string> _splitTerm(const std::string &) const;
std::pair<Operator,size_t> _smallestOperator(const std::string&) const;
public:
typedef T type;
void test();
MathFunction(std::string);
T evaluate(T) const;
T operator()(T v) const { return _evaluateExpressionTree(_expression_root.get(),v);}
};
//init of the static variable
template<typename T>
std::multimap<int, std::string> MathFunction<T>::_operatorOrder{{1,"+"},{1,"-"},{2,"*"},{2,"/"},{"%",2},{3,"^"}};
template<typename T>
std::unordered_map<std::string, int> MathFunction<T>::_operatorPriority{{"+",1},{"-",1},{"*",2},{"/",2},{"%",2},{"^",3},{"x",4}};
template<typename T>
std::unordered_map<std::string,Operator> MathFunction<T>::_strToOp{
{"+",Operator::ADD},
{"-",Operator::SUB},
{"*",Operator::MULT},
{"/",Operator::DIV},
{"%",Operator::MOD},
{"^",Operator::POW},
{"x",Operator::VAR}};
template<typename T>
std::map<Operator,std::string> MathFunction<T>::_opToStr{
{Operator::ADD,"+"},
{Operator::SUB,"-"},
{Operator::MULT,"*"},
{Operator::MOD,"%"},
{Operator::DIV,"/"},
{Operator::POW,"^"},
{Operator::VAR,"x"}};
template <typename T>
std::tuple<std::string, Operator, std::string> MathFunction<T>::_splitTerm(const std::string &term) const{
//search from tail to head for the operator with the smallest priority, which is not inside a braket
int pos = term.size()-1;
int priority = 100;
Operator result = Operator::NUM;
bool operator_found = false;
int splitpos = -1;
int bracket_level = 0;
while(pos >= 0){
if(term[pos]==')'){
++bracket_level;
}else if(term[pos]=='('){
--bracket_level;
}
// inside a braket?
if(bracket_level>0){
--pos;
continue;
}
std::string t = term.substr(pos,1);
auto op = _strToOp.find(t);
if(op != _strToOp.end() ){
auto found_op = op->second;
if( _operatorPriority[t] < priority ){
priority = _operatorPriority[t];
result = found_op;
operator_found = true;
splitpos = pos;
}
}
--pos;
}
//end of string and nothing found? => remove outer brakets
if(not operator_found){
if(term[0]=='(' and term[term.size()-1]==')'){
return _splitTerm(term.substr(1,term.size()-2));
}else{
if ("x"!= term ){
return std::make_tuple(term,Operator::NUM,term);
}else{
return std::make_tuple(term,Operator::VAR,term);
}
}
}else{
return std::make_tuple(term.substr(0,splitpos),result,term.substr(splitpos+1,term.size()-splitpos));
}
}
template <typename T>
std::pair<Operator,size_t> MathFunction<T>::_smallestOperator(const std::string &term) const{
size_t pos = std::string::npos;
int priority = 0;
Operator result = Operator::NUM;
//run through string, only consider operators with bracket depth == 0
//always bigger than 0 => remove a wrapping pair of brackets
for(auto op: _operatorOrder){
//break: found operator with lower priority
if(priority!=0 and op.first > priority)
break;
size_t new_pos = term.rfind(op.second);
if(new_pos!=std::string::npos){
if(pos==std::string::npos){
pos = new_pos;
result = _strToOp[op.second];
priority = op.first;
}else{
if(new_pos>pos){
pos = new_pos;
result = _strToOp[op.second];
}
}
}
}
//nothing => num
if(result==Operator::NUM and term.find(_opToStr[Operator::VAR])!=std::string::npos)
result = Operator::VAR;
return std::make_pair(result,pos);
}
template <typename T>
bool MathFunction<T>::_varInSubTree(node<T> *n) const{
if (n->op==Operator::NUM)
return false;
if (n->op==Operator::VAR)
return true;
return _varInSubTree(n->left.get()) or _varInSubTree(n->right.get());
}
template <typename T>
void MathFunction<T>::_compressTree(node<T> *n){
if(n==nullptr or (n->left==nullptr and n->right==nullptr))
return;
if( not _varInSubTree(n->left.get()) and not _varInSubTree(n->right.get()) ){
n->value = _evaluateExpressionTree(n,T{}/*does not matter*/);
//std::cout<<"compessing node with op "<<_opToStr[n->op]<<" to "<<n->value<<std::endl;
n->left = nullptr;
n->right = nullptr;
n->op = Operator::NUM;
return;
}
_compressTree(n->left.get());
_compressTree(n->right.get());
}
template <typename T>
void MathFunction<T>::_buildExpressionTree(node<T> *n,std::string term){
///*
//std::cout<<"examing: "<<term<<std::endl;
auto splitted = _splitTerm(term);
//std::cout<<"found "<<((_opToStr[std::get<1>(splitted)]=="")?"number":_opToStr[std::get<1>(splitted)])<<std::endl;
//std::cout<<"left: "<< std::get<0>(splitted)<<std::endl;
//std::cout<<"right: "<< std::get<2>(splitted)<<"\n\n"<<std::endl;
n->op = std::get<1>(splitted);
auto &op = n->op;
if(op==Operator::NUM){
n->value = boost::lexical_cast<T>(std::get<0>(splitted));
}else if(op==Operator::VAR){
return;
}else{
n->left = std::move(std::unique_ptr<node<T> >(new node<T>()));
_buildExpressionTree(n->left.get(), std::get<0>(splitted) );
n->right = std::move(std::unique_ptr<node<T> >(new node<T>()));
_buildExpressionTree(n->right.get(), std::get<2>(splitted) );
}//*/
}
template <typename T>
T MathFunction<T>::_evaluateExpressionTree(node<T> *n,T t) const{
switch(n->op){
case Operator::NUM :
return n->value;
case Operator::VAR :
return t;
case Operator::ADD :
return _evaluateExpressionTree(n->left.get(),t)
+ _evaluateExpressionTree(n->right.get(),t);
case Operator::SUB :
return _evaluateExpressionTree(n->left.get(),t)
- _evaluateExpressionTree(n->right.get(),t);
case Operator::DIV :
return _evaluateExpressionTree(n->left.get(),t)
/ _evaluateExpressionTree(n->right.get(),t);
case Operator::MULT :
return _evaluateExpressionTree(n->left.get(),t)
* _evaluateExpressionTree(n->right.get(),t);
case Operator::MOD :
return fmod(_evaluateExpressionTree(n->left.get(),t),
_evaluateExpressionTree(n->right.get(),t));
case Operator::POW :
return pow(_evaluateExpressionTree(n->left.get(),t),
_evaluateExpressionTree(n->right.get(),t));
default: return n->value;
}
}
template <typename T>
MathFunction<T>::MathFunction(std::string s):_term(s){
_expression_root = std::move(std::unique_ptr<node<T> >(new node<T>()));
std::string copy = _term;
copy.erase(std::remove_if(copy.begin(), copy.end(), isspace), copy.end());
_buildExpressionTree(_expression_root.get(), copy);
_compressTree(_expression_root.get());
}
template <typename T>
T MathFunction<T>::evaluate(T t) const{
//std::cout<< "Evaluating "<<_term<<" with type "<<typeid(type).name()
// <<"\nwith given parameter "<<t<<std::endl;
T res = _evaluateExpressionTree(_expression_root.get(),t);
//std::cout <<_term<<" with x="<<t<<" = "<<res<< std::endl;
return res;
}
template <typename T>
void MathFunction<T>::test(){
std::string s = "(((+44)))";
std::tuple<std::string, Operator, std::string> result = _splitTerm(s);
std::cout<<"splitting: "<<s<<std::endl;
std::cout<< "result: '"
<< std::get<0>(result)
<< "' "
<< _opToStr[std::get<1>(result)]
<< " '"
<< std::get<2>(result)
<< "'" <<std::endl;
}