-
Notifications
You must be signed in to change notification settings - Fork 0
/
scmath.cpp
239 lines (219 loc) · 9.26 KB
/
scmath.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
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
#include "scmath.h"
void scmath::Simplificate(BasicNode *&now)
{
if(now->getType() != Fun)
return;
FunNode* tempNow = (FunNode*)now;
//处理加减乘除的拆括号问题
if(tempNow->getEntity()->NAME == "*" || tempNow->getEntity()->NAME == "/")
{
if(tempNow->sonNode[0]->getType() == Fun){
FunNode* tempson = (FunNode*)(tempNow->sonNode[0]);
if(tempson->getEntity()->NAME == "+" || tempson->getEntity()->NAME == "-")
{
//处理 (1+2)*3 (1+2)/3 乘除法左操作树为加减法这种情况
FunNode* newnow = new FunNode(tempson->getEntity());
for(int i = 0; i <= 1; i++){
FunNode* newson = new FunNode(tempNow->getEntity());
newson->addNode(copyHelp::copyNode(tempson->sonNode[i]));
newson->addNode(copyHelp::copyNode(tempNow->sonNode[1]));
newnow->addNode(newson);
}
delete now;
now = newnow;
tempNow = (FunNode*)now;
}
}
}
if(tempNow->getEntity()->NAME == "*")
{
if(tempNow->sonNode[1]->getType() == Fun){
FunNode* tempson = (FunNode*)(tempNow->sonNode[1]);
if(tempson->getEntity()->NAME == "+" || tempson->getEntity()->NAME == "-")
{
//处理 1*(2+3) 乘法右操作树为加减法这种情况
FunNode* newnow = new FunNode(tempson->getEntity());
for(int i = 0; i <= 1; i++)
{
FunNode* newson = new FunNode(tempNow->getEntity());
newson->addNode(copyHelp::copyNode(tempNow->sonNode[0]));
newson->addNode(copyHelp::copyNode(tempson->sonNode[i]));
newnow->addNode(newson);
}
delete now;
now = newnow;
tempNow = (FunNode*)now;
}
}
}
//化简各个子树并求值,注意:没有检查除数为0
int countNodeSonNum = 0, n = tempNow->getEntity()->getParnum();
for(int i = 0; i < n; i++)
{
Simplificate(now->sonNode[i]);
if(now->sonNode[i]->getType() == Num)
countNodeSonNum++;
}
if(countNodeSonNum == n){
BasicNode* newnow = now->eval();
delete now;
now = newnow;
return;
}
//处理某个值为0/1的问题
//为了防止将(0+0)+1化简为0+1就停止,这里用从叶向上的方法
if(tempNow->getEntity()->NAME == "+")//加法某个树为0
{
for(int i = 0; i <= 1; i++){
if(tempNow->sonNode[i]->getType() == Num && ((NumNode*)(tempNow->sonNode[i]))->getNum() == 0){
BasicNode* newnow = copyHelp::copyNode(tempNow->sonNode[i ^ 1]);
delete now;
now = newnow;
return;
}
}
}
if(tempNow->getEntity()->NAME == "*")//乘法某个树为1
{
for(int i = 0; i <= 1; i++){
if(tempNow->sonNode[i]->getType() == Num && ((NumNode*)(tempNow->sonNode[i]))->getNum() == 1){
BasicNode* newnow = copyHelp::copyNode(tempNow->sonNode[i ^ 1]);
delete now;
now = newnow;
return;
}
}
}
if(tempNow->getEntity()->NAME == "*")//乘法某个树为0
{
for(int i = 0; i <= 1; i++){
if(tempNow->sonNode[i]->getType() == Num && ((NumNode*)(tempNow->sonNode[i]))->getNum() == 0){
BasicNode* newnow = new NumNode(0);
delete now;
now = newnow;
return;
}
}
}
if(tempNow->getEntity()->NAME == "-")//减法右树为0
{
if(tempNow->sonNode[1]->getType() == Num && ((NumNode*)(tempNow->sonNode[1]))->getNum() == 0){
BasicNode* newnow = copyHelp::copyNode(tempNow->sonNode[0]);
delete now;
now = newnow;
return;
}
}
if(tempNow->getEntity()->NAME == "-")//减法左树为0
{
if(tempNow->sonNode[1]->getType() == Num && ((NumNode*)(tempNow->sonNode[0]))->getNum() == 0){
BasicNode* newnow = new FunNode(record::globalScope.functionList["*"]);
BasicNode* newnowson = copyHelp::copyNode(tempNow->sonNode[1]);
newnow->addNode(new NumNode(-1));
newnow->addNode(newnowson);
delete now;
now = newnow;
return;
}
}
if(tempNow->getEntity()->NAME == "/")//除法左树为0
{
if(tempNow->sonNode[0]->getType() == Num && ((NumNode*)(tempNow->sonNode[0]))->getNum() == 0){
BasicNode* newnow = new NumNode(0);
delete now;
now = newnow;
return;
}
}
//warn:除法右树为0应该报错
}
BasicNode* scmath::Derivation(BasicNode* now, const string &value){
if(now->getType() == Num)//数字的导数为0
return new NumNode(0);
if(now->getType() == Var && ((VarNode*)now)->NAME != value)//其它变量的导数为0
return new NumNode(0);
if(now->getType() == Var && ((VarNode*)now)->NAME == value)//自己的导数为1
return new NumNode(1);
if(now->getType() == Fun){
FunNode* temp = (FunNode*)now;
string op = temp->getEntity()->NAME;
if(op == "+" || op == "-")//加减法:f ± g == f' ± g'
{
FunNode* retn = new FunNode(temp->getEntity());
for(int i = 0; i <= 1; i++)
retn->addNode(Derivation(temp->sonNode[i], value));
return retn;
}
if(op == "*")//乘法:f * g == f' * g + g' * f
{
FunNode* retn = new FunNode(record::globalScope.functionList["+"]);
for(int i = 0; i <= 1; i++)
{
FunNode* retnson = new FunNode(temp->getEntity());
retnson->addNode(Derivation(temp->sonNode[i], value));
retnson->addNode(copyHelp::copyNode(temp->sonNode[i ^ 1]));
//当i==0(左树)的时候,i^1==1(右树) 当i==1(右树)的时候,i^1==0(左树)
retn->addNode(retnson);
}
return retn;
}
if(op == "/")//除法:f / g == (f' * g - g' * f) / g ^ 2
{
BasicNode *f = copyHelp::copyNode(temp->sonNode[0]), *g = copyHelp::copyNode(temp->sonNode[1]);
BasicNode *df = Derivation(f, value), *dg = Derivation(g, value);
BasicNode* num2 = new NumNode(2);
BasicNode* retn = D(D(D(df) * D(g)) - D(D(dg) * D(f))) / D(D(copyHelp::copyNode(g)) ^ D(num2));//^的优先级比较低,要加括号
//copyHelp::copyNode(g)是防止两个g指向相同部分
//此时f等的所有权转移到了retn里面
return retn;
}
if(op == "^")//幂:(f^g)' == (f ^ g) * (g' * ln f + g * f' / f)
{
BasicNode *f = copyHelp::copyNode(temp->sonNode[0]), *g = copyHelp::copyNode(temp->sonNode[1]);
BasicNode *df = Derivation(f, value), *dg = Derivation(g, value);
BasicNode* lnf = new FunNode(record::globalScope.functionList["ln"]);
lnf->addNode(copyHelp::copyNode(f));
BasicNode* retn = D(D(f) ^ D(g)) * D(D(D(dg) * D(lnf)) + D(D(D(copyHelp::copyNode(g)) * D(df)) / D(copyHelp::copyNode(f))));
return retn;
}
if(op == "sin")//正弦函数:(sin(f))' == f' * cos(f)
{
BasicNode *f = copyHelp::copyNode(temp->sonNode[0]);
BasicNode *df = Derivation(f, value);
BasicNode* cosf = new FunNode(record::globalScope.functionList["cos"]);
cosf->addNode(copyHelp::copyNode(f));
BasicNode* retn = D(df) * D(cosf);
return retn;
}
if(op == "cos")//余弦函数:(cos(f))' == -1 * f' * sin(f)
{
BasicNode *f = copyHelp::copyNode(temp->sonNode[0]);
BasicNode *df = Derivation(f, value);
BasicNode *numNeg1 = new NumNode(-1);
BasicNode* sinf = new FunNode(record::globalScope.functionList["sin"]);
sinf->addNode(copyHelp::copyNode(f));
BasicNode* retn = D(numNeg1) * D(D(df) * D(sinf));
return retn;
}
if(op == "log")//对数函数:(log(f, g))' = g' / (g * ln(f)) - f' * ln(g) / (f * ln(f) ^ 2)
{
BasicNode *f = copyHelp::copyNode(temp->sonNode[0]), *g = copyHelp::copyNode(temp->sonNode[1]);
BasicNode *df = Derivation(f, value), *dg = Derivation(g, value);
BasicNode* lnf = new FunNode(record::globalScope.functionList["ln"]);
lnf->addNode(copyHelp::copyNode(f));
BasicNode* lng = new FunNode(record::globalScope.functionList["ln"]);
lng->addNode(copyHelp::copyNode(g));
BasicNode* num2 = new NumNode(2);
BasicNode* retn = D(D(dg) / D(D(g) * D(lnf))) - D(D(df) * D(D(lng) / D(D(f) * D(D(copyHelp::copyNode(lnf)) ^ D(num2)))));
return retn;
}
if(op == "ln")//以e为底对数函数(ln(f))' == f' / f
{
BasicNode* f = copyHelp::copyNode(temp->sonNode[0]);
BasicNode* df = Derivation(f, value);
BasicNode* retn = D(df) / D(f);
return retn;
}
}
//warn:其它函数还没有写
}