-
Notifications
You must be signed in to change notification settings - Fork 0
/
funinterface.cpp
289 lines (244 loc) · 7.95 KB
/
funinterface.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
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
#include "funinterface.h"
#include <cmath>
#include "matrix.hpp"
//typeCheck
bool BuiltinFunc::hasOneSonNode(vector<BasicNode*> &sonNode)
{
return sonNode.at(0)->getType() == Num;
}
bool BuiltinFunc::hasTwoSonNodes(vector<BasicNode*> &sonNode)
{
return sonNode.at(0)->getType() == Num && sonNode.at(1)->getType() == Num;
}
bool BuiltinFunc::assignmentCheck(vector<BasicNode *> &sonNode)
{
if(sonNode.at(0)->getType() == Var)
{
BasicNode* result=sonNode.at(1)->eval();
if(copyHelp::isLiteral(result))
{
return true;
}
}
else
return false;
}
bool BuiltinFunc::onestr(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == String;
}
bool BuiltinFunc::oneMat(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Matrix;
}
bool BuiltinFunc::twoVec(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Vector && sonNode.at(1)->getType() == Vector;
}
bool BuiltinFunc::twoMat(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Matrix && sonNode.at(1)->getType() == Matrix;
}
bool BuiltinFunc::vecNum(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Vector && sonNode.at(1)->getType() == Num;
}
bool BuiltinFunc::matNum(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Matrix && sonNode.at(1)->getType() == Num;
}
bool BuiltinFunc::matNum2(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Matrix && sonNode.at(1)->getType() == Num
&& sonNode.at(2)->getType() == Num;
}
bool BuiltinFunc::matNum3(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Matrix && sonNode.at(1)->getType() == Num
&& sonNode.at(2)->getType() == Num && sonNode.at(3)->getType() == Num;
}
bool BuiltinFunc::matVec(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Matrix && sonNode.at(1)->getType() == Vector;
}
bool BuiltinFunc::pmatVecNum(vector<BasicNode *> &sonNode)
{
return sonNode.at(0)->getType() == Var && ((VarNode*)sonNode.at(0))->getValType() == Matrix &&
sonNode.at(1)->getType() == Vector && sonNode.at(2)->getType() == Num;
}
double BuiltinFunc::getNum(BasicNode *node)
{
return dynamic_cast<NumNode*>(node)->getNum();
}
//FunBody
BasicNode* BuiltinFunc::add(vector<BasicNode *> &sonNode)
{
return new NumNode(BuiltinFunc::getNum(sonNode[0]) + BuiltinFunc::getNum(sonNode[1]));
}
BasicNode* BuiltinFunc::sub(vector<BasicNode *> &sonNode)
{
return new NumNode(BuiltinFunc::getNum(sonNode[0]) - BuiltinFunc::getNum(sonNode[1]));
}
BasicNode* BuiltinFunc::mul(vector<BasicNode *> &sonNode)
{
return new NumNode(BuiltinFunc::getNum(sonNode[0]) * BuiltinFunc::getNum(sonNode[1]));
}
BasicNode* BuiltinFunc::div(vector<BasicNode *> &sonNode)
{
return new NumNode(BuiltinFunc::getNum(sonNode[0]) / BuiltinFunc::getNum(sonNode[1]));
}
BasicNode* BuiltinFunc::pow(vector<BasicNode *> &sonNode)
{
return new NumNode(std::pow(BuiltinFunc::getNum(sonNode[0]), BuiltinFunc::getNum(sonNode[1])));
}
BasicNode* BuiltinFunc::sin(vector<BasicNode *> &sonNode)
{
return new NumNode(std::sin(BuiltinFunc::getNum(sonNode[0])));
}
BasicNode* BuiltinFunc::cos(vector<BasicNode *> &sonNode)
{
return new NumNode(std::cos(BuiltinFunc::getNum(sonNode[0])));
}
BasicNode* BuiltinFunc::log(vector<BasicNode *> &sonNode)
{
return new NumNode(std::log(BuiltinFunc::getNum(sonNode[1])) / std::log(BuiltinFunc::getNum(sonNode[0])));
}
BasicNode* BuiltinFunc::ln(vector<BasicNode *> &sonNode)
{
return new NumNode(std::log(BuiltinFunc::getNum(sonNode[0])));
}
BasicNode* BuiltinFunc::puts(vector<BasicNode *> &sonNode)
{
cout << ((StringNode*)sonNode[0])->getStr();
return new nullNode();
}
BasicNode* BuiltinFunc::vecDot(vector<BasicNode *> &sonNode)
{
vectorNode* m1=(vectorNode*)sonNode[0];
vectorNode* m2=(vectorNode*)sonNode[1];
return new NumNode(m1->dot(*m2));
}
BasicNode* BuiltinFunc::matDot(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
matrixNode* m2=(matrixNode*)sonNode[1];
return new matrixNode(m1->dot(*m2));
}
BasicNode* BuiltinFunc::vecAdd(vector<BasicNode *> &sonNode)
{
vectorNode* m1=(vectorNode*)sonNode[0];
vectorNode* m2=(vectorNode*)sonNode[1];
return new vectorNode(m1->add(*m2));
}
BasicNode* BuiltinFunc::matAdd(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
matrixNode* m2=(matrixNode*)sonNode[1];
return new matrixNode(m1->add(*m2));
}
BasicNode* BuiltinFunc::vecSub(vector<BasicNode *> &sonNode)
{
vectorNode* m1=(vectorNode*)sonNode[0];
vectorNode* m2=(vectorNode*)sonNode[1];
vectorNode rm2=m2->mul(-1);
return new vectorNode(m1->add(rm2));
}
BasicNode* BuiltinFunc::matSub(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
matrixNode* m2=(matrixNode*)sonNode[1];
matrixNode rm2=m2->mul(-1);
return new matrixNode(m1->add(rm2));
}
BasicNode* BuiltinFunc::vecMul(vector<BasicNode *> &sonNode)
{
vectorNode* v1=(vectorNode*)sonNode[0];
NumNode* d2=(NumNode*)sonNode[1];
return new vectorNode(v1->mul(d2->getNum()));
}
BasicNode* BuiltinFunc::matMul(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
NumNode* d2=(NumNode*)sonNode[1];
return new matrixNode(m1->mul(d2->getNum()));
}
BasicNode* BuiltinFunc::getCVector(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
NumNode* d2=(NumNode*)sonNode[1];
return new vectorNode(m1->getCVector(d2->getNum()));
}
BasicNode* BuiltinFunc::getRVector(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
NumNode* d2=(NumNode*)sonNode[1];
return new vectorNode(m1->getRVector(d2->getNum()));
}
BasicNode* BuiltinFunc::setCVector(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)(((VarNode*)sonNode[0])->getVal());
vectorNode* v2=(vectorNode*)sonNode[1];
NumNode* n3=(NumNode*)sonNode[2];
m1->setCVector(*v2,n3->getNum());
return new nullNode();
}
BasicNode* BuiltinFunc::setRVector(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)(((VarNode*)sonNode[0])->getVal());
vectorNode* v2=(vectorNode*)sonNode[1];
NumNode* n3=(NumNode*)sonNode[2];
m1->setRVector(*v2,n3->getNum());
return new nullNode();
}
BasicNode* BuiltinFunc::det(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
return new NumNode(m1->det());
}
BasicNode* BuiltinFunc::inv(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
return new matrixNode(m1->inv());
}
BasicNode* BuiltinFunc::adjoint(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
return new matrixNode(m1->adjoint());
}
BasicNode* BuiltinFunc::linerSolve(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
vectorNode* m2=(vectorNode*)sonNode[1];
return new vectorNode(matrixNode::solve(*m1,*m2));
}
BasicNode* BuiltinFunc::assignment(vector<BasicNode *> &sonNode)
{
VarNode* v=(VarNode*)sonNode[0];
v->setVal(copyHelp::copyVal(sonNode[1])); //在参数类型检查时已经进行eval
return v->eval(); //支持yoda表达法
}
BasicNode* BuiltinFunc::rsub(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
NumNode* n1=(NumNode*)sonNode[1];
NumNode* n2=(NumNode*)sonNode[2];
NumNode* n3=(NumNode*)sonNode[3];
m1->rsub(n1->getNum(),n2->getNum(),n3->getNum());
return new matrixNode(*m1);
}
BasicNode* BuiltinFunc::rmul(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
NumNode* n1=(NumNode*)sonNode[1];
NumNode* n2=(NumNode*)sonNode[2];
m1->rmul(n1->getNum(),n2->getNum());
return new matrixNode(*m1);
}
BasicNode* BuiltinFunc::rswap(vector<BasicNode *> &sonNode)
{
matrixNode* m1=(matrixNode*)sonNode[0];
NumNode* n1=(NumNode*)sonNode[1];
NumNode* n2=(NumNode*)sonNode[2];
m1->rswap(n1->getNum(),n2->getNum());
return new matrixNode(*m1);
}