-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectExpression.js
More file actions
545 lines (505 loc) · 16.4 KB
/
objectExpression.js
File metadata and controls
545 lines (505 loc) · 16.4 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
"use strict";
function isZero(a) {
return ((a instanceof Const) && (a.getValue() === 0));
}
function isOne(a) {
return ((a instanceof Const) && (a.getValue() === 1));
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function Operation(type) {
this.operation = generateOperation(type);
this.differ = generateDiff(type);
this.simple = generateSimplify(type);
this.type = type;
this.arr = [];
for (var i = 1; i < arguments.length; i++) {
this.arr.push(arguments[i])
}
this.evaluate = function() {
var array = [];
for (var i = 0; i < this.arr.length; i++) {
array.push(this.arr[i].evaluate.apply(this.arr[i], arguments));
}
return this.operation.apply(null, array);
};
this.diff = function (base) {
var array = [base];
for (var i = 0; i < this.arr.length; i++) {
array.push(this.arr[i]);
}
return this.differ.apply(null, array);
};
this.simplify = function () {
var array = [];
for (var i = 0; i < this.arr.length; i++) {
array.push(this.arr[i].simplify());
}
return this.simple.apply(null, array);
};
this.prefix = function () {
var stack = this.type;
for (var i = 0; i < this.arr.length; i++) {
stack += " " + this.arr[i].prefix();
}
return "(" + stack + ")"
};
this.postfix = function () {
var stack = "";
for (var i = 0; i < this.arr.length; i++) {
stack += this.arr[i].prefix() + " ";
}
return "(" + stack + this.type + ")"
};
this.toString = function () {
return this.arr.join(" ") + " " + this.type;
};
function generateOperation(type) {
switch (type) {
case '+' : return function (a, b) {
return a + b;
};
case '/' : return function (a, b) {
return a / b;
};
case '*' : return function (a, b) {
return a * b;
};
case '-' : return function (a, b) {
return a - b;
};
case 'log' : return function (a, b) {
return Math.log(Math.abs(b)) / Math.log(Math.abs(a));
};
case 'pow' : return function (a, b) {
return Math.pow(a, b);
};
case 'sqrt' : return function (a) {
return Math.pow(a, 1/2);
};
case 'square' : return function (a) {
return Math.pow(a, 2);
};
case 'negate' : return function (a) {
return -a;
};
case 'exp' : return function (a) {
return Math.pow(Math.E, a);
};
case 'atan' : return function (a) {
return Math.atan(a);
};
}
}
function generateDiff(type) {
switch (type) {
case '-' :
return function (base, a, b) {
return new Subtract(a.diff(base), b.diff(base));
};
case '+' :
return function (base, a, b) {
return new Add(a.diff(base), b.diff(base));
};
case '*' :
return function (base, a, b) {
var da = a.diff(base);
var db = b.diff(base);
return new Add(new Multiply(da, b), new Multiply(a, db));
};
case '/' :
return function (base, a, b) {
var da = a.diff(base);
var db = b.diff(base);
return new Divide(new Subtract(new Multiply(da, b), new Multiply(a, db)), new Multiply(b, b));
};
case 'pow' :
return function (base, a, b) {
return new Multiply(
new Power(
a,
new Subtract(
b,
MY_CNST[1]
)
),
new Add(
new Multiply(
b,
a.diff(base)
),
new Multiply(
a,
new Multiply(
new Log(MY_CNST['e'], a),
b.diff(base)
)
)
)
)
};
case 'log' :
return function (base, a, b) {
return new Subtract(
new Divide(
b.diff(base),
new Multiply(
b,
new Log(MY_CNST[e], a)
)
),
new Divide(
new Multiply(
a.diff(base),
new Log(MY_CNST[e], b)
),
new Multiply(
a,
new Square(
new Log(MY_CNST[e], a)
)
)
)
);
};
case 'sqrt' :
return function (base, a) {
var da = a.diff(base);
return new Divide(new Multiply(a, da), new Multiply(MY_CNST[2], new Sqrt(new Multiply(new Square(a), a))));
};
case 'square' :
return function (base, a) {
return new Multiply(new Multiply(MY_CNST[2], a), a.diff(base));
};
case 'negate' :
return function (base, a) {
return new Negate(a.diff(base));
}
}
return null;
}
function generateSimplify(type) {
switch (type) {
case '+' : return function (a, b) {
if (isZero(a) && isZero(b)) return MY_CNST[0];
if (isZero(a)) return b;
if (isZero(b)) return a;
if (a instanceof Const && b instanceof Const) return new Const(a.getValue() + b.getValue());
return new Add(a, b);
};
case '-' : return function (a, b) {
if (isZero(a) && isZero(b)) return MY_CNST[0];
if (isZero(a)) return (new Negate(b)).simplify();
if (isZero(b)) return a;
if (a instanceof Const && b instanceof Const) return new Const(a.getValue() - b.getValue());
return new Subtract(a, b);
};
case '*' : return function (a, b) {
if (isZero(a) || isZero(b)) return MY_CNST[0];
if (isOne(a)) return b;
if (isOne(b)) return a;
if (a instanceof Const && b instanceof Const) return new Const(a.getValue() * b.getValue());
return new Multiply(a, b);
};
case '/' : return function (a, b) {
if (isZero(a)) return MY_CNST[0];
if (isOne(b)) return a;
if (a instanceof Const && b instanceof Const) return new Const(a.getValue() / b.getValue());
return new Divide(a, b);
};
case 'log' : return function (a, b) {
if (isOne(b)) return MY_CNST[0];
if (a instanceof Const && b instanceof Const) {
return new Const(Math.log(Math.abs(b.getValue())) / Math.log(Math.abs(a.getValue())));
}
return new Log(a, b);
};
case 'pow' : return function (a, b) {
if (isZero(a)) return MY_CNST[0];
if (isOne(a) || isZero(b)) return MY_CNST[1];
if (a instanceof Const && b instanceof Const) {
return new Const(Math.pow(a.getValue(), b.getValue()));
}
return new Power(a, b);
};
case 'sqrt' : return function (a) {
if (isZero(a)) return MY_CNST[0];
if (isOne(a)) return MY_CNST[1];
if (a instanceof Const) return new Const(Math.pow(a.getValue(), 1/2));
return new Sqrt(a);
};
case 'square' : return function (a) {
if (isZero(a)) return MY_CNST[0];
if (isOne(a)) return MY_CNST[1];
if (a instanceof Const) return new Const(Math.pow(a.getValue(), 2));
return new Square(a);
};
case 'negate' : return function (a) {
if (a instanceof Const) return new Const(-a.getValue());
return new Negate(a);
};
}
}
}
function Add(a, b) {
return new Operation('+', a, b)
}
function Divide(a, b) {
return new Operation('/', a, b)
}
function Subtract(a, b) {
return new Operation('-', a, b)
}
function Multiply(a, b) {
return new Operation('*', a, b)
}
function Negate(a) {
return new Operation('negate', a)
}
function Sqrt(a) {
return new Operation('sqrt', a)
}
function Square(a) {
return new Operation('square', a)
}
function Power(a, b) {
return new Operation('pow', a, b)
}
function Log(a, b) {
return new Operation('log', a, b)
}
function Exp(a) {
return new Operation('exp', a)
}
function ArcTan(a) {
return new Operation('atan', a)
}
var MY_VRBL = Object.freeze({
'x' : 0,
'y' : 1,
'z' : 2
});
var MY_EXPR = Object.freeze({
"-": [Subtract, 2],
"+": [Add, 2],
"/": [Divide, 2],
"*": [Multiply, 2],
"negate": [Negate, 1],
"square": [Square, 1],
"sqrt": [Sqrt, 1],
"pow": [Power, 2],
"log": [Log, 2],
"exp": [Exp, 1],
"atan": [ArcTan, 1]
});
var MY_CNST = Object.freeze({
'pi' : new Const(Math.PI),
'e' : new Const(Math.E),
'0' : new Const(0),
'1' : new Const(1),
'2' : new Const(2)
});
function Variable(str) {
this.evaluate = function () {
return arguments[MY_VRBL[str]];
};
this.diff = function (base) {
return MY_CNST[base === str ? 1 : 0];
};
this.simplify = function () {
return new Variable(str);
};
this.prefix = function () {
return str;
};
this.postfix = function () {
return str;
};
this.toString = function () {
return str;
};
}
function Const(number) {
this.evaluate = function () {
return number;
};
this.diff = function () {
return MY_CNST[0];
};
this.simplify = function () {
return new Const(number)
};
this.toString = function () {
if (Math.E === number) return 'E';
if (Math.PI === number) return 'Pi';
return number.toString();
};
this.prefix = function () {
return number.toString();
};
this.postfix = function () {
return number.toString();
};
this.getValue = function () {
return +number;
}
}
function parse(expression){
var stack = [];
var tokens = expression.trim().split(' ');
for (var i = 0; i < tokens.length; i++) {
tokens[i] = tokens[i].trim();
if (tokens[i].length <= 0) continue;
if (MY_EXPR[tokens[i]] !== undefined) {
var array = [];
for (var j = 0; j < MY_EXPR[tokens[i]][1]; j++) {
array.push(stack.pop())
}
var arr = [];
var n = array.length;
for (j = 0; j < n; j++) {
arr.push(array.pop());
}
stack.push(MY_EXPR[tokens[i]][0].apply(null, arr));
continue;
}
if (MY_VRBL[tokens[i]] !== undefined) {
stack.push(new Variable(tokens[i]));
continue;
}
if (isNumeric(tokens[i])) {
stack.push(new Const(+tokens[i]));
continue;
}
throw new Error("Parser error: unknown operation : " + tokens[i])
}
return stack.pop();
}
function mainParser(expression, start, next, openBrace, closeBraces, endOfExpr, reverse, addStr) {
expression = expression.trim();
var ind = start;
var balance = 0;
var token;
if (expression === '()') throw new ParseError(expression, "Incorrect expression", 0).message;
var ans = parseSubstring();
if (balance !== 0) throw new ParseError(expression, "Incorrect expression", reverse(ind, 1)).message;
if (endOfExpr(expression, ind)) throw new ParseError(expression, "Invalid expression", ind).message;
return ans;
function next_token() {
while (endOfExpr(expression, ind) && expression[ind] === ' ') {
ind = next(ind);
}
if (expression[ind] === '(' || expression[ind] === ')') {
token = expression[ind];
return;
}
var tmp = '';
var c;
while (endOfExpr(expression, ind) && (c = expression[ind]) !== ' '
&& c !== '('
&& c !== ')') {
if (c === undefined) break;
tmp = addStr(tmp, c);
ind = next(ind);
}
if (tmp) {
token = tmp;
} else {
throw new ParseError(expression, "Haven't operand at pos : " + ind, ind).message;
}
}
function parseSubstring() {
next_token();
switch (token) {
case '(' :
ind = next(ind);
balance = reverse(balance, -1);
return openBrace(parseSubstring);
case ')' :
balance = reverse(balance, 1);
if (balance < 0) {
throw new ParseError(expression, "The bracket does not have a pair", ind).message;
}
ind = next(ind);
return closeBraces(parseSubstring);
default :
if (isNumeric(token)) {
return new Const(+token);
}
if (MY_CNST[token] !== undefined) {
return new Const(MY_CNST[token]);
}
if (MY_VRBL[token] !== undefined) {
return new Variable(token);
}
if (MY_EXPR[token] !== undefined) {
var operation = token;
var tmp = [];
while ((token = parseSubstring()) !== closeBraces()) {
tmp.push(token);
}
if (MY_EXPR[operation][1] === tmp.length) {
return MY_EXPR[operation][0].apply(null, tmp)
} else {
throw new ParseError(expression, "Incorrect number of arguments", ind).message;
}
}
throw new ParseError(expression, "Incorrect token : '" + token + "'", reverse(ind, token.length)).message;
}
}
}
function parsePostfix(expression) {
return mainParser(expression, expression.length - 1,
function (a) {
return a - 1;
},
function () {
return '('
},
function (a) {
return a();
},
function (expression, ind) {
return ind >= 0;
},
function (a, b) {
return a + b;
},
function (a, b) {
return b + a;
})
}
function parsePrefix(expression) {
return mainParser(expression, 0,
function (a) {
return a + 1;
},
function (a) {
return a();
},
function () {
return ')';
},
function (expression, ind) {
return ind < expression.length;
},
function (a, b) {
return a - b;
},
function (a, b) {
return a + b;
})
}
var ParseError = function (expression, message, pos) {
this.name = 'ParserError';
this.message = (message + "\n" + expression + "\n" + getPos(pos)).toString();
function getPos(pos) {
var str = '';
for (var i = 0; i < pos; i++) {
str += ' ';
}
str += '^';
return str;
}
};