-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractions.js
347 lines (290 loc) · 8.72 KB
/
fractions.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
// Support for fractions
// Programmer: Mayer Goldberg, 2021
// ...with additions and corrections by Amit Rubin, 2022
let pc = require('./pc.js');
BigInt.factorial = (n) => (n === 0n) ? 1n : n * BigInt.factorial(n-1n);
BigInt.gcd = (a, b) => {
while (b !== 0n) [a, b] = [b, a % b];
return a;
}
Math.gcd = (a, b) => {
while (b !== 0) [a, b] = [b, a % b];
return a;
}
BigInt.abs = function(bigint_num){
if(bigint_num < 0n) return - bigint_num;
else return bigint_num;
}
BigInt.toFloatString = function(bigint_num, precision) {
let a = bigint_num;
if(a < 0n) a = - bigint_num;
let s = a.toString();
if(precision === undefined)precision = 5;
if(s.length <precision) return s;
else{
;
return `${bigint_num/(10n**BigInt(s.length-precision))}e${s.length-precision}`;
}
}
let nt_nat =
pc.Grammar
.start()
.push(pc.range('0', '9'))
.pack((ch) => ch.charCodeAt(0) - '0'.charCodeAt(0))
.plus()
.pack((arr) => pc.fold_left((a, b) => 10n * a + BigInt(b), 0n, arr))
.done();
let nt_int =
pc.Grammar
.start()
.push(pc.character('+'))
.pack((_) => true)
.push(pc.character('-'))
.pack((_) => false)
.disj()
.maybe()
.pack((arr) => (arr[0]) ? arr[1] : true)
.push(nt_nat)
.caten()
.pack((arr) => (arr[0]) ? arr[1] : -arr[1])
.done();
let nt_number =
pc.Grammar
.start()
.push(nt_int)
.push(pc.character('/'))
.push(nt_nat)
.caten()
.pack((arr) => arr[1])
.maybe()
.pack((arr) => (arr[0]) ? arr[1] : 1n)
.caten()
.pack((arr) => Fraction.make_fraction_from_bignums(arr[0], arr[1]))
.done();
let nt_spaced_number =
pc.Grammar
.start()
.const((ch) => ch <= ' ')
.star()
.dup()
.push(nt_number)
.swap()
.caten()
.pack((arr) => arr[0])
.caten()
.pack((arr) => arr[1])
.push(pc.nt_end_of_input)
.caten()
.pack((arr) => arr[0])
.done();
class Fraction {
// DO NOT call the constructor directly! Use the factory-methods!
constructor(num, den) {
if (den < 0n) {
this.num = - num;
this.den = - den;
}
else {
this.num = num;
this.den = den;
}
}
static make_fraction_from_bignums(num, den) {
let num_ = BigInt(num);
let den_ = BigInt(den);
let d = BigInt.gcd(num_, den_);
return new Fraction(num_ / d, den_ / d);
}
static make_fraction_from_string(str) {
return nt_spaced_number.match(str, 0).value;
}
clone(){
return new Fraction(this.num,this.den);
}
toString() {
return `new Fraction(${this.num}, ${this.den})`;
}
to_text_string() {
if (this.den === 0n) throw `ZeroDenominator`;
if (this.num === 0n) return `0`;
if (this.den === 1n) return `${this.num}`;
if (this.den === -1n) return `${-this.num}`;
return `${this.num}/${this.den}`;
}
to_text_string2() {
if (this.den === 0n) throw `ZeroDenominator`;
if (this.num === 0n) return `0`;
if (this.den === 1n) return `${BigInt.toFloatString(this.num)}`;
if (this.den === -1n) return `${BigInt.toFloatString(-this.num)}`;
return `${BigInt.toFloatString(this.num)}/${BigInt.toFloatString(this.den)}`;
}
to_decimal_string(n) {
if(this.num * this.den < 0n) return "-"+this.negate().to_decimal_string(n);
n = BigInt(n);
let result = `${this.trunc_to_BigInt()}.`;
let frac = this.mantissa();
for (let i = 0n, a = frac.num * 10n, b = frac.den; i < n; ++i) {
let d = a / b;
result += `${d}`;
a = (a - d * b) * 10n;
}
return result;
}
abs() {
return (this.is_positive()) ? this : this.negate();
}
is_positive() {
return (this.num > 0n);
}
negate() {
return new Fraction(-this.num, this.den);
}
inverse() {
return new Fraction(this.den, this.num);
}
add(frac) {
return new Fraction(this.num * frac.den + this.den * frac.num, this.den * frac.den);
}
sub(frac) {
return new Fraction(this.num * frac.den - this.den * frac.num, this.den * frac.den);
}
mul(frac) {
return new Fraction(this.num * frac.num, this.den * frac.den);
}
div(frac) {
return new Fraction(this.num * frac.den, this.den * frac.num);
}
power(n) {
let nn = BigInt(n);
if(nn >= 0) return new Fraction(this.num ** nn, this.den ** nn);
else return new Fraction(this.den ** -nn, this.num ** -nn);
}
trunc() {
return new Fraction(this.num / this.den, 1n);
}
trunc_to_BigInt() {
return this.trunc().num;
}
mantissa() {
return this.sub(this.trunc());
}
compare(frac) {
return this.num * frac.den - this.den * frac.num;
}
is_gt(frac) {
return this.compare(frac) > 0n;
}
is_lt(frac) {
return this.compare(frac) < 0n;
}
is_ge(frac) {
return this.compare(frac) >= 0n;
}
is_le(frac) {
return this.compare(frac) <= 0n;
}
is_eq(frac) {
return this.compare(frac) === 0n;
}
is_ne(frac) {
return this.compare(frac) !== 0n;
}
is_zero() {
return this.num === 0n;
}
is_non_zero() {
return this.num !== 0n;
}
to_continued_fraction_list() {
let result = new Array();
let frac = this;
if (frac.is_zero()) {
throw `Division by zero`;
} else {
frac = frac.inverse();
while (frac.is_non_zero()) {
frac = frac.inverse();
result.push(frac.trunc_to_BigInt());
frac = frac.mantissa();
}
}
return result;
}
is_integer() {
return (this.num / this.den) * this.den === this.num;
}
simplify() {
let d = BigInt.gcd(this.num, this.den);
if(d < 0n)d = -d;
this.num = this.num/d;
this.den = this.den/d;
}
dist(frac){
return this.sub(frac).abs();
}
static distance(num1,den1,num2,den2){
return Fraction.make_fraction_from_bignums(num1,den1).dist(Fraction.make_fraction_from_bignums(num2,den2));
}
static average(frac_lst){ //frac_lst non-empty array
let sum_of_fractions = frac_lst.reduce((prev,curr) => prev.add(curr));
let to_divide_by = new Fraction(BigInt(frac_lst.length), 1n);
return sum_of_fractions.div(to_divide_by);
}
static min(frac_lst){
return frac_lst.reduce((prev,curr) => prev.is_lt(curr) ? prev : curr);
}
static max(frac_lst){
return frac_lst.reduce((prev,curr) => prev.is_gt(curr) ? prev : curr);
}
static continued_fraction_list_to_partial_fractions_list(cf_list) {
let a = 0n, b = 1n, c = 1n, d = 0n;
let result = new Array();
for (let k in cf_list) {
[a, b] = [b, BigInt(cf_list[k]) * b + a];
[c, d] = [d, BigInt(cf_list[k]) * d + c];
result.push(new Fraction(b, d));
}
return result;
}
static continued_fraction_list_to_fraction(cf_list) {
let a = 0n, b = 1n, c = 1n, d = 0n;
for (let k in cf_list) {
[a, b] = [b, BigInt(cf_list[k]) * b + a];
[c, d] = [d, BigInt(cf_list[k]) * d + c];
}
let out = new Fraction(b, d);
out.simplify();
return out;
}
static rationals_continued_fraction_list_to_fraction(rational_cf_list) {
let a = zero(), b = one(), c = one(), d = zero();
for (let k in rational_cf_list) {
[a, b] = [b, (rational_cf_list[k]).mul(b).add(a)];
[c, d] = [d, (rational_cf_list[k]).mul(d).add(c)];
}
let out = b.div(d);
out.simplify();
return out;
}
static float_to_continued_fraction_list(x, n) {
let result = new Array();
for (let i = 0; i < n; ++i) {
let ip = Math.trunc(x);
result.push(BigInt(ip));
x = 1 / (x - ip);
}
return result;
}
}
module.exports = {
Fraction
}
// let f1 = Fraction.make_fraction_from_string(' -355/113 ');
// let f2 = Fraction.make_fraction_from_string('42');
// console.log(`${f1.to_text_string()}`);
// console.log(`${f2.to_text_string()}`);
// let a69 = Fraction.make_fraction_from_bignums(100n,3n);
// let b69 = a69.to_continued_fraction_list();
// console.log(b69);
// let c69 = Fraction.continued_fraction_list_to_fraction([0,1,1,1,2,2,4,2,1,3]); //0.631
// console.log(c69);