-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_and_known_constants.h
More file actions
140 lines (123 loc) · 5.22 KB
/
functions_and_known_constants.h
File metadata and controls
140 lines (123 loc) · 5.22 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
#ifndef _functions_and_known_constants_h_
#define _functions_and_known_constants_h_
// Uncomment the following line to enable complex mode
//#define COMPLEX_MODE 1
//Includes
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
#include <complex>
#include <limits>
/*
you can add a new constant or function following these steps
1-increment constants_count or functions_count respectively (both in COMPLEX_MODE and normal mode)
2-add enum value accordingly say you wanna add sigmoid then add the name SIGMOID to functions_codes (both in COMPLEX_MODE and normal mode)
3-add a string representing that function or constant to the according array (both in COMPLEX_MODE and normal mode)
4-don't add a function before log
5-head to the functions_and_known_constants.tpp then add evaluation of it
6-then head to calculus_tree.cpp
and add the differeniation of that function in diff_function
string calculus_tree<DataType>::diff_function(const int fn,node*ptr,const string&var ){
*/
#ifdef COMPLEX_MODE
const int functions_count =21 ;
const int constants_count =5 ;
const int keywords_count =26;
enum functions_codes{
//don't add functions' codes here
LOG,
SQRT,ABS,SIN,COS,TAN,
SEC,CSC,COTAN,ASIN,ACOS,
ATAN,EXP,LN,SINH,COSH,
TANH,ASINH,ACOSH,ATANH,IMG,
//add new functions' codes here
};
enum constants_codes{
PI,E,I,
INF_ERR,NAN_ERR
//add new constants codes here
}
/*
functions must be followed by parenthese
for log you just attach base to it
for ex:log2
by default it's log10
pi,i,e are known constants no need to write the value explicitly
*/
const std::string known_functions[functions_count]={
"log",
"sqrt","abs","sin","cos","tan",
"sec","csc","cotan","asin","acos",
"atan","exp","ln","sinh","cosh",
"tanh","asinh","acosh","atanh","img",
//add new functions here
};
const std::string known_constants[constants_count]={
"pi","e","i",
"inf","nan"
//add new constants here
};
#else
//normal mode
const int functions_count =20 ;
const int constants_count =4 ;
const int keywords_count =24;
enum functions_codes{
LOG,
SQRT,ABS,SIN,COS,TAN,
SEC,CSC,COTAN,ASIN,ACOS,
ATAN,EXP,LN,SINH,COSH,
TANH,ASINH,ACOSH,ATANH,
};
enum constants_codes{
PI,E,
INF_ERR,NAN_ERR
};
/*
functions must be followed by parenthese
for log you just attach base to it
for ex:log2
by default it's log10
pi,i,e are known constants no need to write the value explicitly
*/
const std::string known_functions[functions_count]={
"log",
"sqrt","abs","sin","cos","tan",
"sec","csc","cotan","asin","acos",
"atan","exp","ln","sinh","cosh",
"tanh","asinh","acosh","atanh",
//add new functions here
};
const std::string known_constants[constants_count]={
"pi","e",
"inf","nan"
//add new constants here
};
#endif
/*
extracts operand at pos then returns it's enum value if exists
else it returns -1
*/
int is_known_function(const std::string &expression,unsigned int pos ) ;
/*
returns true if expression[pos] is an operator
*/
bool is_op(const std::string &expression,unsigned int pos ) ;
//checks if var is a number
//with decimal points and signs
bool is_num(const std::string &var);
//returns enum value if operand starting at pos
//else -1
int is_keyword(const std::string &expression,unsigned int pos);
//returns enum value if an operand starting at pos if it's a known constant
//else -1
int is_known_constant(const std::string &var,unsigned int pos ) ;
template<typename DataType>
DataType evaluate_function(const int fn,const DataType var, const DataType base);
template<typename DataType>
DataType evaluate_constant(const std::string&symbol);
template<typename DataType>
DataType evaluate_operator(char op,const DataType&left_operand,const DataType&right_operand);
#include "functions_and_known_constants.tpp"
#endif