-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.js
390 lines (362 loc) · 9.4 KB
/
number.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
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
"use strict"
/*
* Author: Adrian Stoll
* Date: 2 July 2016
*/
var number = {};
number.Real = function(r) {
if(typeof r !== 'number' || isNaN(r)) {
return NaN;
} else if(Number.isInteger(r)) {
return number.Rational(r,1);
}
return {
isReal: function() { return true; },
isDecimal: function() { return true; },
toDecimal: function() { return r },
toString: function() {
return r.toString();
},
abs: function() {
return number.Real(Math.abs(r));
},
inv: function() {
const i = 1/r;
if(isNaN(i)) {
return NaN;
}
return number.Real(i);
},
neg: function() {
return number.Real(-r);
},
isZero: function(epsilon) {
if(epsilon) {
return Math.abs(r) < epsilon;
} else {
return r === 0;
}
}
};
}
number.gcd = function(a,b) {
//https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
while(b !== 0) {
const t = b;
b = a % b;
a = t;
}
return a;
}
number.lcm = function(a,b) {
//take advantage of the equality a*b = gcd(a,b)*lcm(a,b)
return a*b/number.gcd(a,b);
}
number.Rational = function(n,d) {
if(!Number.isInteger(n) || !Number.isInteger(d) || d === 0) {
return NaN;
}
const gcd = number.gcd(n,d);
n /= gcd;
d /= gcd;
if(d < 0) {
d = -d;
n = -n;
}
return {
isReal: function() { return true; },
isDecimal: function() { return false; },
toDecimal: function() { return n/d; },
toString: function() {
if(d === 1) {
return n.toString();
} else if(n === 0) {
return '0';
} else {
return n.toString() + '/' + d.toString();
}
},
abs: function() {
return number.Rational(Math.abs(n),d);
},
inv: function() {
//the Rational constructor will handle case of inv(0)
return number.Rational(d,n);
},
neg: function() {
return number.Rational(-n, d);
},
isZero: function(epsilon) {
if(epsilon) {
return Math.abs(n/d) < epsilon;
} else {
return n === 0;
}
},
numerator: function() { return n; },
denominator: function() { return d; },
};
}
number.Complex = function(r, i) {
//Check that r and i are 'number.Real/Rational'
//if r or i are native JavaScript numbers, wrap them up
if(Number.isFinite(r)) {
r = number.Real(r);
} else if(!(typeof r === 'object' && r.isReal && r.isReal())) {
return NaN;
}
if(Number.isFinite(i)) {
i = number.Real(i);
} else if(!(typeof i === 'object' && i.isReal && i.isReal())) {
return NaN;
}
return {
toString: function() {
if(i.isZero()) {
return r.toString();
}
if(r.isZero()) {
if(i.abs().toDecimal() == 1) {
if(i.toDecimal() < 0) return '-i';
return 'i';
}
return i.toString() + 'i';
}
const sep = i.toDecimal() < 0 ? ' - ' : ' + ';
const ai = i.abs();
const ip = ai.toDecimal(1) === 1 ? 'i' : ai.toString() + 'i';
return r.toString() + sep + ip;
},
abs: function() {
//if possible, try not to convert to floating point
if(r.isZero()) {
return i.abs();
} else if(i.isZero()) {
return r.abs();
} else {
return number.Real(Math.sqrt(Math.pow(r.abs().toDecimal(),2) + Math.pow(i.abs().toDecimal(),2)));
}
},
//FIXME TODO:
//Some code uses isReal to see if Real specific functionality (that
//Complex does not have), can be used.
isReal: function() {
//TODO: allow epsilon parameter?
return i.isZero();
},
Re: function() {
return r;
},
Im: function() {
return i;
},
arg: function() {
return Math.atan(i.toDecimal()/r.toDecimal());
},
conj: function() {
return number.Complex(r, number.subReal(number.Real(0), i));
},
isZero: function(epsilon) {
if(epsilon) {
return this.abs() < epsilon;
} else {
return r.isZero() && i.isZero();
}
},
inv: function() {
//1/(a + bi) = (a - bi)/(a^2 + b^2)
return number.div(1,number.Complex(r,i));
},
neg: function() {
return number.Complex(r.neg(), i.neg());
},
};
}
number.toComplex = function(a) {
//if native JavaScript number
if(Number.isFinite(a)) {
return number.Complex(a,0);
}
if(typeof a === 'object' && a.isReal) {
//if number.Real/Rational
if(!a.Im) {
return number.Complex(a,0);
//else already complex
} else {
return a;
}
}
//TODO: handle this case
console.error("Not a number");
}
number.addReal = function(a,b) {
//Decimal + Rational -> Decimal
if(a.isDecimal() || b.isDecimal())
return number.Real(a.toDecimal() + b.toDecimal());
//Rational + Rational -> Rational
const n = a.numerator()*b.denominator() + b.numerator()*a.denominator();
const d = a.denominator() * b.denominator();
return number.Rational(n,d);
}
number.subReal = function(a,b) {
//Decimal - Rational -> Decimal
if(a.isDecimal() || b.isDecimal())
return number.Real(a.toDecimal() - b.toDecimal());
//Rational - Rational -> Rational
const n = a.numerator()*b.denominator() - b.numerator()*a.denominator();
const d = a.denominator() * b.denominator();
return number.Rational(n,d);
}
number.multReal = function(a,b) {
//Decimal - Rational -> Decimal
if(a.isDecimal() || b.isDecimal())
return number.Real(a.toDecimal() * b.toDecimal());
//Rational - Rational -> Rational
const n = a.numerator()*b.numerator();
const d = a.denominator() * b.denominator();
return number.Rational(n,d);
}
number.divReal = function(a,b) {
if(b.isZero()) return NaN;
return number.multReal(a,b.inv());
}
number.lt = function(a,b) {
if(a.isDecimal() || b.isDecimal()) return a.toDecimal() < b.toDecimal();
return a.numerator()*b.denominator() < b.numerator()*a.denominator();
}
number.leq = function(a,b) {
if(a.isDecimal() || b.isDecimal()) return a.toDecimal() <= b.toDecimal();
return a.numerator()*b.denominator() <= b.numerator()*a.denominator();
}
number.eq = function(a,b, epsilon) {
if(epsilon) return Math.abs(a.toDecimal() - b.toDecimal()) < epsilon;
if(a.isDecimal() || b.isDecimal()) return a.toDecimal() === b.toDecimal();
return a.numerator()*b.denominator() === b.numerator()*a.denominator();
}
number.geq = function(a,b) {
return !number.lt(a,b);
}
number.gt = function(a,b) {
return !number.leq(a,b);
}
number.equal = function(a,b, epsilon) {
a = number.toComplex(a);
b = number.toComplex(b);
return number.eq(a.Re(),b.Re(), epsilon) && number.eq(a.Im(),b.Im(), epsilon);
}
//convert to complex
//apply operation
//if real, convert back to real
number.add = function(a,b) {
a = number.toComplex(a);
b = number.toComplex(b);
const r = number.addReal(a.Re(), b.Re());
const i = number.addReal(a.Im(), b.Im());
if(i.isZero()) return r;
return number.Complex(r,i);
}
number.sub = function(a,b) {
//a,b -> a - b
a = number.toComplex(a);
b = number.toComplex(b);
const r = number.subReal(a.Re(), b.Re());
const i = number.subReal(a.Im(), b.Im());
if(i.isZero()) return r;
return number.Complex(r,i);
}
number.mult = function(a,b) {
const N = number;
a = N.toComplex(a);
b = N.toComplex(b);
const A = a.Re();
const B = a.Im();
const C = b.Re();
const D = b.Im();
const r = N.subReal(N.multReal(A,C), N.multReal(B,D));
const i = N.addReal(N.multReal(A,D), N.multReal(B,C));
if(i.isZero()) return r;
return N.Complex(r,i);
}
//TODO: test this more
number.div = function(a,b) {
//a,b -> a/b or NaN if b = 0
const N = number;
a = N.toComplex(a);
b = N.toComplex(b);
if(b.isZero()) return NaN;
const A = a.Re();
const B = a.Im();
const C = b.Re();
const D = b.Im();
const t = N.addReal(N.multReal(C,C),N.multReal(D,D));
const r = N.divReal(N.addReal(N.multReal(A,C),N.multReal(B,D)),t);
const i = N.divReal(N.subReal(N.multReal(B,C),N.multReal(A,D)),t);
if(i.isZero()) return r;
return N.Complex(r,i);
}
//TODO: implement exp
number.exp = function(a,n) {
//if decimal -> use Math.pow
//if rational -> Math.pow(n)/Math.pow(b) (if Math.pow(n) non int
// then use Math.pow(n/d)
//also look at de Moivre's identity for complex #s
}
number.parseReal = function(str) {
function strictParseInt(s) {
if(/^(\-|\+)?([0-9]+)$/.test(s)) return Number(s);
return NaN;
}
function strictParseFloat(s) {
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat#A_stricter_parse_function
if(/^(\-|\+)?([0-9]+(\.[0-9]+)?)$/.test(s)) return Number(s);
return NaN
}
//get rid of whitespace
str = str.replace(/\s+/g,'')
const p = str.split('/');
//Rational [-][0-9]+/[0-9]+
if(p.length === 2) {
return number.Rational(strictParseInt(p[0]), strictParseInt(p[1]));
} else {
return number.Real(strictParseFloat(str));
}
}
number.parseComplex = function(str) {
function parseImgPart(s) {
//validate input: +|-[num]i or +|-i[num]
if(!/^(\-|\+)?([0-9]+(\.[0-9]+)?)?i$/.test(s) &&
!/^(\-|\+)?i([0-9]+(\.[0-9]+)?)?$/.test(s) &&
!/^(\-|\+)?[0-9]+\/[0-9]+i$/.test(s) &&
!/^(\-|\+)?i[0-9]+\/[0-9]+$/.test(s)) {
return NaN;
}
s = s.replace('i','');
//case: i,-i,+i
if(s === '' || s === '-' || s === '+') {
s = s + '1';
}
return number.parseReal(s);
}
//get rid of whitespace
str = str.replace(/\s+/g,'')
//if no imaginary part, then Real number
if(str.indexOf('i') == -1) {
return number.parseReal(str);
}
//make sure terms have a '+' between them
str = str.replace(/\-/g,'+-');
let p = str.split('+');
p = p.filter(function(x) { return x !== ''; });
//only the complex piece
if(p.length === 1) {
return number.Complex(0,parseImgPart(p[0]));
} else if(p.length === 2) {
//imaginary part comes first
if(p[0].indexOf('i') >= 0)
return number.Complex(number.parseReal(p[1]),parseImgPart(p[0]));
//imaginary part comes second
return number.Complex(number.parseReal(p[0]),parseImgPart(p[1]));
} else {
return NaN;
}
}