forked from FlagerLee/F.A.T-Computational-Graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
146 lines (132 loc) · 3.56 KB
/
Node.cpp
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
#include <string>
#include <map>
#include <cmath>
#include <iostream>
#include <cstdio>
#include "Node.h"
using namespace std ;
map < string , Node* > Var_map ;
//Node类
void Node::add_next ( Node* N ) { Node::next.push_back ( N ) ; }
void Node::del ()
{
int len = next.size () ;
for ( int i = 0 ; i < len ; i ++ ) delete next [ i ] ;
vector < Node* > ().swap ( next ) ;
}
//#end#
//Calculator类
template <typename _Tp_>
double Calculator::get_value ( _Tp_ T )
{
if ( T.name == "var" || T.name == "placeholder" || T.name == "constant" || T.name == "var_constant" ) return T.value ;
else std::cout << "Error: " << T.name << "does not have value\n" ;
}
template <typename _Tp_>
void Calculator::eval ( double v , _Tp_ T )
{
if ( T.name == "var" || T.name == "placeholder" || T.name == "var_constant" ) T.value = v ;
else std::cout << "Error: You cannot evaluate a " << T.name << "\n" ;
}
double Unary_Operator::cal ( string s , double v , bool& is_legal )
{
if ( s == "SIN" ) return sin ( v ) ;
else if ( s == "EXP" ) return exp ( v ) ;
else if ( s == "TANH" ) return ( exp ( v ) - exp ( -v ) ) / ( exp ( v ) + exp ( -v ) ) ;
else if ( s == "SIGMOID" ) return 1.0 / ( 1.0 + exp ( -v ) ) ;
else if ( s == "LOG" )
{
if ( v > 0 ) return log ( v ) ;
else { is_legal = false ; cout << "Error: LOG operator's input must be positive\n" ; return 0.0 ; }
}
else if ( s == "PRINT" )
{
cout << "Print Operator: " << s << "=" << v << "\n" ;
return v ;
}
return 0.0 ;
}
double Binary_Operator::cal ( string s , double v1 , double v2 , bool& is_legal )
{
switch ( s [ 0 ] )
{
case '+' : return v1 + v2 ;
case '-' : return v1 - v2 ;
case '*' : return v1 * v2 ;
case '/' :
{
if ( v2 == 0 )
{
is_legal = false ;
cout << "ERROR: Division by zero\n" ;
return 0.0 ;
}
else return v1 / v2 ;
}
case '<' :
{
if ( s.length() == 1 )
{
if ( v1 < v2 ) return 1.0 ;
else return 0.0 ;
}
else if ( s [ 1 ] == '=' )
{
if ( v1 <= v2 ) return 1.0 ;
else return 0.0 ;
}
}
case '>' :
{
if ( s.length() == 1 )
{
if ( v1 > v2 ) return 1.0 ;
else return 0.0 ;
}
else if ( s [ 1 ] == '-' )
{
if ( v1 >= v2 ) return 1.0 ;
else return 0.0 ;
}
}
case '=' :
{
if ( s.length() == 1 )
{
is_legal = false ;
cout << "Error: Cannot evaluate in an expression\n" ;
return 0.0 ;
}
else
{
if ( v1 == v2 ) return 1.0 ;
else return 0.0 ;
}
}
default :
{
is_legal = false ;
cout << "Error: No matching operators for '" << s << "'\n" ;
return 0.0 ;
}
}
return 0.0 ;
}
double Ternary_Operator::cal ( string s , double v1 , double v2 , double v3 , bool& is_legal )
{
if ( s == "COND" )
{
return v1 ? v2 : v3 ;
}
else
{
is_legal = false ;
cout << " Error: No matching operators for '" << s << "'\n" ;
return 0.0 ;
}
return 0.0 ;
}
//#end#
void build_tree ( string exp ) //根据输入的结点建树
{
}