-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
327 lines (276 loc) · 7.33 KB
/
math.js
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
function calc(expr) {
switch (expr.type) {
case "binOp":
switch (expr.op) {
case "+":
return add(calc(expr.expr0), calc(expr.expr1));
case "-":
return sub(calc(expr.expr0), calc(expr.expr1));
case "×":
return mul(calc(expr.expr0), calc(expr.expr1));
case "/":
return div(calc(expr.expr0), calc(expr.expr1));
case "^":
return pow(calc(expr.expr0), calc(expr.expr1));
case "=":
return eq(calc(expr.expr0), calc(expr.expr1));
case "≈":
return aprEq(calc(expr.expr0), calc(expr.expr1));
case "<":
return less(calc(expr.expr0), calc(expr.expr1));
case ">":
return greater(calc(expr.expr0), calc(expr.expr1));
case "≤":
return lessOrEq(calc(expr.expr0), calc(expr.expr1));
case "≥":
return greaterOrEq(calc(expr.expr0), calc(expr.expr1));
case "⋏":
return and(calc(expr.expr0), calc(expr.expr1));
case "⋎":
return or(calc(expr.expr0), calc(expr.expr1));
case "⊻":
return xor(calc(expr.expr0), calc(expr.expr1));
case ",":
return arr(calc(expr.expr0), calc(expr.expr1));
default:
failed = true;
err("Invalid operator")
return Infinity;
}
case "unOp":
var val = Number(calc(expr.expr));
switch (expr.op) {
case "sin":
return sin(val);
case "cos":
return cos(val);
case "tan":
return tan(val);
case "asin":
return asin(val);
case "acos":
return acos(val);
case "atan":
return atan(val);
case "ln":
return ln(val);
case "lg":
return lg(val);
case "abs":
return abs(val);
case "⌊":
return floor(val);
case "⌈":
return ceil(val);
case "round":
return round(val);
case "√":
return sqrt(val);
case "∛":
return pow(val, 1 / 3);
case "∜":
return pow(val, 1 / 4);
case "¬":
return not(val);
case "sgn":
return sgn(val);
case "!":
return factorial(val); //TODO: BigInt
case "‼":
return semifactorial(val);
case "years":
return years(val);
case "weeks":
return weeks(val);
case "days":
return days(val);
case "hours":
return hours(val);
case "minutes":
return minutes(val);
case "seconds":
return seconds(val);
default:
return calc(expr.expr);
}
case "func":
switch (expr.func) {
case "Σ":
return seq(expr.expr, add);
case "Π":
return seq(expr.expr, mul);
case "lgn":
return lgn(calc(expr.expr.expr0), calc(expr.expr.expr1));
case "P":
return permutations(calc(expr.expr.expr0), calc(expr.expr.expr1));
case "C":
return combinations(calc(expr.expr.expr0), calc(expr.expr.expr1));
}
case "var":
return k; //TODO: x and y
default:
return expr.num;
}
}
function add(term0, term1) {
if (typeof term0 !== typeof term1)
return Number(term0) + Number(term1);
return term0 + term1;
}
function sub(term0, term1) {
if (typeof term0 !== typeof term1)
return Number(term0) - Number(term1);
return term0 - term1;
}
function mul(fac0, fac1) {
if (fac0 == 0 || fac1 == 0)
return 0;
if (typeof fac0 !== typeof fac1)
return Number(fac0) * Number(fac1);
return fac0 * fac1;
}
function div(num, den) {
return Number(num) / Number(den);
}
function pow(base, exponent) {
if (typeof base !== typeof exponent)
return Number(base) ** Number(exponent);
return base ** exponent;
}
var abs = Math.abs;
var sin = Math.sin;
var cos = Math.cos;
var tan = Math.tan;
var asin = Math.asin;
var acos = Math.acos;
var atan = Math.atan;
var sqrt = Math.sqrt;
var ln = Math.log;
var lg = Math.log10;
var floor = Math.floor;
var ceil = Math.ceil;
var round = Math.round;
function lgn(base, arg) {
return Math.log(arg) / Math.log(base);
}
function factorial(a) {
var ans = 1;
for (; a > 1; a--) {
ans *= a;
}
return ans;
}
function semifactorial(a) {
var ans = 1;
for (; a > 1; a -= 2) {
ans *= a;
}
return ans;
}
function permutations(n, r) {
return div(factorial(n), factorial(sub(n, r)));
}
function combinations(n, r) {
return div(permutations(n, r), factorial(r));
}
function and(a, b) {
if (a >= 1 && b >= 1)
return 1;
return 0;
}
function or(a, b) {
if (a >= 1 || b >= 1)
return 1;
return 0;
}
function xor(a, b) {
if (or(a, b) && !and(a, b))
return 1;
return 0;
}
function not(a) {
if (a >= 1)
return 0;
return 1;
}
function eq(a, b) {
if (a == b || (a.value == b.value && a.value !== undefined))
return 1;
return 0;
}
function less(a, b) {
if (a < b)
return 1;
return 0;
}
function greater(a, b) {
if (a > b)
return 1;
return 0;
}
function lessOrEq(a, b) {
return 1 - greater(a, b);
}
function greaterOrEq(a, b) {
return 1 - less(a, b);
}
function compare(a, b) {
if (less(a, b))
return -1;
return greater(a, b);
}
function sgn(a) {
return compare(a, 0);
}
function aprEq(a, b) {
if (a == b || abs(sub(div(a, b), 1)) < 0.001) //Equality check for infinite numbers
return 1;
return 0;
}
function mod(a, b) {
return a % b;
}
function gcd(a, b) {
if (b == 0)
return a;
return gcd(b, mod(a, b));
}
function years(a) {
return a / 31557600000;
}
function weeks(a) {
return a / 604800000;
}
function days(a) {
return a / 86400000;
}
function hours(a) {
return a / 3600000;
}
function minutes(a) {
return a / 60000;
}
function seconds(a) {
return a / 1000;
}
function arr(a, b) {
return [a, b];
}
function seq(expr, op) {
var e0 = expr.expr0;
var kMax = calc(e0.expr1);
var ans = id(op);
for (k = calc(e0.expr0); k <= kMax; k = add(k, 1)) {
ans = op(ans, calc(expr.expr1));
}
k = "k";
return ans;
}
function id(op) {
switch (op) {
case add:
return 0;
default:
return 1;
}
}