-
Notifications
You must be signed in to change notification settings - Fork 0
/
continued_fractions.js
2260 lines (2116 loc) · 92.2 KB
/
continued_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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let fr = require('./fractions.js'); // note - fractions.js adds methods to class BigInt which are utilized here.
let make_fraction = fr.Fraction.make_fraction_from_bignums;
//-----------------------<remove leading zeroes>-----------------------
Array.remove_zero_pairs = function (arr) {
if(arr.length === 0) return arr;
else if(arr[0].constructor.name === 'BigInt') return bignum_remove_zero_pairs(arr);
else if(arr[0].constructor.name === 'Fraction') return fraction_remove_zero_pairs(arr);
}
let remove_leading_zero_pairs = function (bignum_arr) {
if ((bignum_arr.length > 2) && bignum_arr[0] === 0n && bignum_arr[1] === 0n) {
return remove_leading_zero_pairs(bignum_arr.slice(2));
}
else return bignum_arr;
}
function bignum_remove_zero_pairs(bignum_arr){
let x = remove_leading_zero_pairs(bignum_arr);
if(x.length > 2){
return [x[0]].concat(bignum_remove_zero_pairs(x.slice(1)));
}
else{
return x;
}
}
let fraction_remove_leading_zero_pairs = function (fractions_arr) {
if ((fractions_arr.length > 2) && fractions_arr[0].is_zero() && fractions_arr[1].is_zero()) {
return fraction_remove_leading_zero_pairs(fractions_arr.slice(2));
}
else return fractions_arr;
}
function fraction_remove_zero_pairs(fractions_arr){
let x = fraction_remove_leading_zero_pairs(fractions_arr);
if(x.length > 2){
return [x[0]].concat(fraction_remove_zero_pairs(x.slice(1)));
}
else{
return x;
}
}
//-----------------------</remove leading zeroes>-----------------------
function mat_mul(mat1,mat2){
let x = [mat1[0].mul(mat2[0]).add(mat1[1].mul(mat2[2])),
mat1[0].mul(mat2[1]).add(mat1[1].mul(mat2[3])),
mat1[2].mul(mat2[0]).add(mat1[3].mul(mat2[2])),
mat1[2].mul(mat2[1]).add(mat1[3].mul(mat2[3]))];
x.map((x)=>x.simplify());
return x;
}
function fraction_simplifier(frac, p){
p = BigInt(p);
p = 10n**p;
let pp = p*p;
let f = frac.clone();
let bool_flip = false;
if(!f.is_positive()){
f = f.negate();
bool_flip = true;
}
while(f.num > pp && f.den > pp){
f.num = f.num/p;
f.den = f.den/p;
}
while(f.num > p && f.den > p){
f.num = f.num/10n;
f.den = f.den/10n;
}
if(bool_flip){
f = f.negate();
}
return f;
}
//the following two methods are used in CF to initialize to_decimal_string(decimal_precision)
function to_decimal_string_for_fully_known_that_isnt_finite(a_cf,decimal_precision){
let [a,b] = [make_fraction(1n,1n), make_fraction(0n,1n)];
let [c,d] = [make_fraction(0n,1n), make_fraction(1n,1n)];
let within_precision = false;
for (var i = 0; ! within_precision; i++) {
let new_a = a.mul(a_cf.element(i)).add(b);
let new_c = c.mul(a_cf.element(i)).add(d);
b = a;
a = new_a;
d = c;
c = new_c;
if(c.is_non_zero() && d.is_non_zero()){
within_precision = a.div(c).dist(b.div(d)).is_lt(make_fraction(1n,10n**BigInt(decimal_precision + 1)));
}
}
let ret_frac;
if(a.mul(c).is_lt(make_fraction(0,1))) ret_frac = a.div(c).sub(make_fraction(5,10n**BigInt(decimal_precision + 1)));
else ret_frac = a.div(c).add(make_fraction(5,10n**BigInt(decimal_precision + 1)));
return ret_frac.to_decimal_string(decimal_precision);
}
class CF {
static toString(){
return `COMMONLY USED FACTORY METHODS:
static make_cf_from_Fraction(frac), frac should be a Fraction object (from fraction.js).
static make_cf_from_fraction(numerator, denominator),
examples: cf.CF.make_cf_from_fraction(13,2) -> FiniteCF { frac: Fraction { num: 13n, den: 2n } }.
cf.CF.make_cf_from_fraction(-420n,130n) -> FiniteCF { frac: Fraction { num: -42n, den: 13n } }.
i.e. syntactic sugar for initializing a Fraction frac with num = numerator, den = denominator, and then calling make_cf_from_Fraction(frac).
static make_sqrt_of_fraction(num, den, b_, c_), returns square roots of rational numbers.
den_, b_, c_, are optional parameters.
Called with only the argument num: sqrt(num).
For example, sqrt(3) = make_sqrt_of_fraction(3) -> PeriodicCF { lst_initial: [ 1n ], lst_periodic: [ 1n, 2n ] }
Called with only the arguments num & den: sqrt(num/den).
For example, sqrt(1.5) = make_sqrt_of_fraction(3,2) -> PeriodicCF { lst_initial: [ 1n ], lst_periodic: [ 4n, 2n ] }
Called with all 4 argumentss: (sqrt(num/den)+b_) / (c_).
For example, the golden ratio = (sqrt(5) + 1)/2 = make_sqrt_of_fraction(5,1,1,2) -> PeriodicCF { lst_initial: [], lst_periodic: [ 1n ] }
static make_first_composite_cf(cf,a,b,c,d),
i.e. (a*cf + b)/(c*cf + d), for a continued fraction cf.
Used for arithmetic operations.
static make_second_composite_cf(cfX,cfY,a,b,c,d,e,f,g,h),
i.e. (a*cfX*cfY + b*cfX + c*cfY + d)/(e*cfX*cfY + f*cfX + g*cfY + h), for two continued fraction objects cfX, cfY.
Used for arithmetic operations.
static make_cf_from_method(function_from_nonnegative_indices_to_nonnegative_bigints),
for example the constant e: make_cf_from_method((n) => (n===0) ? 2n : (n%3 !== 2) ? 1n : BigInt((2*n+2)/3)) -> FormulaCF { formula: [Function] }
Useful for adding new continued fraction identities. Continued fraction identities can be found in the class Constants.
OTHER FACTORY METHODS:
static make_cf_from_integer_continued_fraction_array(num_arr),
for example: 10/7 = 1 + 1/(2 + 1/3): make_cf_from_integer_continued_fraction_array([1,2,3]) -> FiniteCF { frac: Fraction { num: 10n, den: 7n } }.
If you wish to make a continued fraction from an infinitely repeating array, use make_cf_from_repeating_pattern instead.
static make_cf_from_integer(n), syntactic sugar for make_cf_from_fraction(n,1).
static make_cf_from_javascript_number(javascript_number, decimal_precision),
examples: make_cf_from_javascript_number(0.0123456, 2) -> FiniteCF { frac: Fraction { num: 1n, den: 100n } }
make_cf_from_javascript_number(-100.0123456, 2) -> FiniteCF { frac: Fraction { num: -10001n, den: 100n } }
This method exists for convinience, the unequivocal factory method make_cf_from_fraction is preferable.
static make_cf_from_repeating_pattern(initial_elements_list, repeating_elements_list),
for example sqrt of(3): make_cf_from_repeating_pattern([1], [1,2]) -> PeriodicCF { lst_initial: [ 1n ], lst_periodic: [ 1n, 2n ] }
The factory method make_sqrt_of_fraction makes this pointless in most cases - unless there's a specific value for which make_sqrt_of_fraction fails.
COMMONLY USED INSTANCE METHODS:
.clone(), note that cloning means that computation done in one instance will not transfer to the other.
.to_decimal_string(decimal_precision), for an integer decimal_precision.
Calculates and returns as a string the first decimal_precision decimal points of the CF instance.
e.g. for e to the power of six we would have:
e_6.to_decimal_string(4) -> '403.4288'
e_6.to_decimal_string(6) -> '403.428793'
.negate(), the cf multiplied by minus one (returns a continued fraction representing that).
.inverse(), one divided by the cf (returns a continued fraction representing that).
.add(cftoadd), cftoadd is a CF instance.
Returns a continued fraction representing this+cftoadd.
.sub(cftosub), cftosub is a CF instance, returns a continued fraction representing this-cftosub.
.mul(cftomult), cftomult is a CF instance.
Returns a continued fraction representing this*cftomult.
.div(cftodiv), cftodiv is a CF instance.
Returns a continued fraction representing this/cftodiv.
.sqrt(), returns the square root of the cf (returns a continued fraction representing that).
.to_text_string(), returns a string description of the CF. Careful: it can get very big very quickly.
.to_text_string2(), returns a smaller string description of the CF. Note: unlike to_text_string, the description is imprecise.
OTHER INSTANCE METHODS:
.to_float(decimal_precision), for an integer decimal_precision.
Returns a javascript Number object approximation of the cf.
.element(i),
Use in CF classes except FiniteCF. Returns the i'th continued fraction series element as a Fraction.
.find_n_elements(n)
Finds the first n continued fraction series elements if they are not already known.
.add_Fraction(toadd), i.e. syntactic sugar for CF.make_cf_from_Fraction(toadd).add(this).
.add_fraction(num, den), , i.e. syntactic sugar for this.add_Fraction(make_fraction(num,den)).
.length() - Use in CF classes except FiniteCF. Returns the length of the known continued fraction series.
.convergents(n, debug_if_this_is_slow_optional_argument), returns a series of fraction approximations ("convergents").
Useful for demonstrations but computationaly expensive.`;
}
// FACTORY METHODS:
static make_cf_from_integer_continued_fraction_array(num_arr){
// Example: 10/7 = 1 + 1/(2 + 1/3):: make_cf_from_integer_continued_fraction_array([1,2,3]) -> FiniteCF { frac: Fraction { num: 10n, den: 7n } }
// If you wish to make a continued fraction from an infinitely repeating array, use make_cf_from_repeating_pattern instead.
let converted_arr = num_arr.map(x => BigInt(x)); //just in case
let x = fr.Fraction.continued_fraction_list_to_fraction(converted_arr);
return new FiniteCF(x);
}
//
static make_cf_from_Fraction(frac){
return new FiniteCF(frac);
}
static make_cf_from_fraction(numerator, denominator){
return new FiniteCF(make_fraction(numerator,denominator));
}
static make_cf_from_integer(n){
return new FiniteCF(make_fraction(BigInt(n),1));
}
static make_cf_from_javascript_number(javascript_number, decimal_precision){
//example: make_cf_from_javascript_number(-1238.545642,5) -> FiniteCF { frac: Fraction { num: -30963641n, den: 25000n } }
let num = BigInt(javascript_number.toFixed(decimal_precision)* (10**decimal_precision));
let den = BigInt(10 ** decimal_precision);
return new FiniteCF(make_fraction(num, den));
}
static make_cf_from_repeating_pattern(initial_elements_list, repeating_elements_list){
//example: sqrt of 3:: make_cf_from_repeating_pattern([1], [1,2]) -> PeriodicCF { lst_initial: [ 1n ], lst_periodic: [ 1n, 2n ] }
initial_elements_list = initial_elements_list.map(x => BigInt(x));
repeating_elements_list = repeating_elements_list.map(x => BigInt(x));
return new PeriodicCF(initial_elements_list, repeating_elements_list);
}
static make_cf_from_method(function_from_nonnegative_indices_to_nonnegative_bigints){
//example: the constant e: make_cf_from_method((n) => (n===0) ? 2n : (n%3 !== 2) ? 1n : BigInt((2*n+2)/3)) -> FormulaCF { formula: [Function] }
return new FormulaCF(function_from_nonnegative_indices_to_nonnegative_bigints);
}
static make_first_composite_cf(cf, a, b, c, d){
return new FirstCompositeCF(cf,a,b,c,d);
}
static make_second_composite_cf(cfX,cfY,a,b,c,d,e,f,g,h){
return new SecondCompositeCF(cfX,cfY,a,b,c,d,e,f,g,h);
}
static is_normalized_cf(cf) {
return cf.constructor.name === "FiniteCF"
|| cf.constructor.name === "PeriodicCF"
|| cf.constructor.name === "FormulaCF"
|| cf.constructor.name === "FirstCompositeCF"
|| cf.constructor.name === "SecondCompositeCF"
|| cf.constructor.name === "SqrtCompositeCF"
|| cf.constructor.name === "GeneralizedCF";
}
static make_sqrt_of_fraction(num, den, b_, c_){
//den_, b_, c_, are optional parameters.
//With only num: sqrt(num).
//Example: make_sqrt_of_fraction(3) -> PeriodicCF { lst_initial: [ 1n ], lst_periodic: [ 1n, 2n ] }
//With only num & den: sqrt(num/den).
//Example: make_sqrt_of_fraction(3,2) -> PeriodicCF { lst_initial: [ 1n ], lst_periodic: [ 4n, 2n ] }
//With everyone: (sqrt(num/den)+b_) / (c_).
//Example the golden ratio: ncf.make_sqrt_of_fraction(5,1,1,2) -> PeriodicCF { lst_initial: [], lst_periodic: [ 1n ] }
//zero case:
if((num === 0 || num === 0n) && b_ === undefined && c_ === undefined) {
return CF.make_cf_from_fraction(0n,1n);
}
else if(num === 0 || num === 0n) {
return CF.make_cf_from_fraction(b_,c_);
}
if(den === undefined) {den = 1n;}
// Previously, this function failed for rationals that are perfect squares (such as 9/4, 3/2 ** 2 = 9/4).
// The following block of code uses Math.sqrt() to recognize such a case & solve it, but we require that
// Number.MIN_SAFE_INTEGER < num,den < Number.MAX_SAFE_INTEGER.
// Luckily, this restriction should suffice for all practical uses.
num = BigInt(num);
den = BigInt(den);
let num_as_number = Number(num);
let den_as_number = Number(den);
if ( num < BigInt(Number.MAX_SAFE_INTEGER)
&& num > BigInt(Number.MIN_SAFE_INTEGER)
&& den < BigInt(Number.MAX_SAFE_INTEGER)
&& den > BigInt(Number.MIN_SAFE_INTEGER)
&& Math.floor(Math.sqrt(num_as_number/Math.gcd(num_as_number,den_as_number))) * Math.floor(Math.sqrt(num_as_number/Math.gcd(num_as_number,den_as_number))) === num_as_number/Math.gcd(num_as_number,den_as_number)
&& Math.floor(Math.sqrt(den_as_number/Math.gcd(num_as_number,den_as_number))) * Math.floor(Math.sqrt(den_as_number/Math.gcd(num_as_number,den_as_number))) === den_as_number/Math.gcd(num_as_number,den_as_number)) {
let frac = make_fraction(Math.sqrt(num_as_number/Math.gcd(num_as_number,den_as_number)),Math.sqrt(den_as_number/Math.gcd(num_as_number,den_as_number)));
if(b_ !== undefined && c_ !== undefined){
frac = frac.add(make_fraction(b_,1n)).div(make_fraction(c_,1n));
}
return CF.make_cf_from_Fraction(frac);
}
num = BigInt(num);
den = BigInt(den);
if(num * den < 0n) throw "CF.make_sqrt_of_fraction() error: num/den must be positive";
let newtons_method = (bignum_a, bignum_b, frac_p) => {
let newtons_method_step = (x,a,p,b) => {
/* The math for a newtons method step is derived as follows:
We wish to discover the integer part of
bignum_a * sqrt(frac_p) + bignum_b
(for abbreviation, let's rewrite that as a*sqrt(p)+b ).
Thus
x = a sqrt(p) +b
x-b = a sqrt(p)
(x-b)^2 = a^2*p
x^2 - 2xb + b^2 - (a^2)*p = 0
So for the sake of newtons method step
f(x) = x^2 - 2xb + b^2 - (a^2)*p
f'(x) = 2(x-b)
->
x{n+1} = x{n} - f(x{n})/f'(x{n}) = ...
= [(x{n})^2 - b^2 + (a^2)*p] / [2(x - b)]
*/
x.simplify();
let two_ = make_fraction(2n,1n);
return x.mul(x).sub(b.mul(b)).add(a.mul(a).mul(p)).div(two_.mul(x.sub(b)));
}
let p = frac_p;
let a_ = make_fraction(bignum_a,1n);
let b_ = make_fraction(bignum_b,1n);
let x = a_.mul(a_).mul(p);
let prev_x = p.add(make_fraction(1n,1n)); // making sure it's initialized to be different from prev_x
let diff = make_fraction(1n,1n);
/*until the integer part of the current and previous x are equal (and even then - only if there's no danger of moving
to a different integer later, which we check using diff) perform a newton's method step.
*/
while(x.trunc_to_BigInt() !== prev_x.trunc_to_BigInt() || (x.add(diff).trunc_to_BigInt() !== x.sub(diff).trunc_to_BigInt()) ) {
diff = x.sub(prev_x);
prev_x = x;
x = newtons_method_step(x,a_,p,b_);
}
return x.trunc_to_BigInt();
}
let history_idx = 0;
let history = [];
let find_in_history = (a_,b_,c_) => {
let res = history.find((e) => {
let [idx__,n__,a__,b__,c__] = e;
return (a_=== a__ && b_=== b__ && c_ === c__);
});
if(res === undefined) return undefined;
else return res[0];
}
// a*sqrt(p) + b
// -------------
// c
let p = make_fraction(num,den); // remains unchanged
let [a,b,c] = [1n,0n,1n];
if(b_ !== undefined) {
b = BigInt(b_);
c = BigInt(c_);
}
while(find_in_history(a,b,c) === undefined){
let res = newtons_method(a,b,p);
let integer_part = res/c;
history.push([history_idx++, integer_part, a, b, c]);
let new_a = a*c*p.den;
let new_b = (c*c*integer_part - b*c)*p.den;
let new_c = a*a*p.num + p.den*(-b*b + 2n*b*c*integer_part - c*c*integer_part*integer_part);
let d = BigInt.gcd(BigInt.gcd(new_a,new_b),new_c);
[a,b,c] = [new_a/d, new_b/d, new_c/d];
}
let repeating_point = find_in_history(a,b,c);
let init_lst = [];
let repeating_lst = [];
for(let i = 0; i < repeating_point; i++){
init_lst.push(history[i][1]);
}
for(let i = repeating_point; i<history.length; i++){
repeating_lst.push(history[i][1]);
}
let out = new PeriodicCF(init_lst,repeating_lst);
// out.sqrt_of = [num, den, b_, c_]; // incorrect. I need to create a consistent use of the variables b_,c_ / b,c. for now I'm leaving it alone. Reminder: this is here in order to allow optimization.
return out;
}
clone() {
throw 'Abstract Class CF';
}
to_decimal_string(decimal_precision){
if(this.constructor.name === 'FiniteCF')
return this.frac.to_decimal_string(decimal_precision);
else if(this.is_fully_known_upon_construction())
return to_decimal_string_for_fully_known_that_isnt_finite(this, decimal_precision);
else
throw "Abstract Class CF: implement to_decimal_string for not-fully-known objects in their class";
}
to_float(decimal_precision){
return parseFloat(this.to_decimal_string(decimal_precision));
}
negate() {
// @OVERWRITE
// returns the result of multiplying by -1
throw 'Abstract Class CF';
}
inverse() {
// @OVERWRITE
// Returns a new CF which is the multiplicative inverse of the current CF.
throw 'Abstract Class CF';
}
simplify(){
//ONLY USE IN TESTING!
return undefined;//overwrite in FirstCompositeCF or SecondCompositeCF to perform gcd on the numerator and denominator and standardize the sign of the coefficients - useful for comparing cf's.
}
element(i) {
// @OVERWRITE
// if known, returns the CFs i'th element AS A Fraction. Otherwise throws an error.
throw 'Abstract Class CF';
}
find_n_elements(n){
// @OVERWRITE
return;
}
to_text_string(){
// @OVERWRITE
throw 'Abstract Class CF';
}
to_text_string2(){
// @OVERWRITE
throw 'Abstract Class CF';
}
add_Fraction(toadd) {
return CF.make_cf_from_Fraction(toadd).add(this);
}
add_fraction(num, den) {
return this.add_Fraction(make_fraction(num,den));
}
sqrt(){
//default case:
return new SqrtCompositeCF(this);
//for specific cases, overwrite this method.
}
//The following 4 charts are initialized by the method "initialize_arithmetics";
static add_chart=[];
static sub_chart=[];
static mul_chart=[];
static div_chart=[];
static class_name_to_index(name){
if(name.constructor.name !== "String"){
name = name.constructor.name;
}
switch(name){
case "FiniteCF":
return 0;
case "PeriodicCF":
return 1;
case "FormulaCF":
return 2;
case "FirstCompositeCF":
return 3;
case "SecondCompositeCF":
return 4;
case "SqrtCompositeCF":
return 5;
case "GeneralizedCF":
return 6;
default:
return -1;
}
}
add(cftoadd) {
return CF.add_chart[CF.class_name_to_index(this)][CF.class_name_to_index(cftoadd)](this,cftoadd);
}
sub(cftosub) {
if(this === cftosub) return new FiniteCF(make_fraction(0n,1n));
return CF.sub_chart[CF.class_name_to_index(this)][CF.class_name_to_index(cftosub)](this,cftosub);
}
mul(cftomult){
return CF.mul_chart[CF.class_name_to_index(this)][CF.class_name_to_index(cftomult)](this,cftomult);
}
div(cftodiv){
if(this === cftodiv) return new FiniteCF(make_fraction(1n,1n));
return CF.div_chart[CF.class_name_to_index(this)][CF.class_name_to_index(cftodiv)](this,cftodiv);
}
length() {
if(this.constructor.name === FiniteCF) throw "should never be called for FiniteCF";
else if(this.is_fully_known_upon_construction()) return Infinity;
else return this.found.length;
}
is_fully_known_upon_construction(){
// @OVERWRITE
throw 'Abstract Class CF';
/*
return value for different CF classes:
FiniteCF: true.
PeriodicCF: true.
FormulaCF: true.
Composite cases: false.
*/
}
//only useful for demonstrations:
convergents(n, debug_if_this_is_slow_optional_argument){
// note: in the class FiniteCF the parameter n is optional. For all others it is not.
let found_ = this.found;
let out = [];
if(this.constructor.name==="FiniteCF"){
if(n === undefined) n = Infinity;
if(this.frac.is_zero()) return [make_fraction(0,1)];
let remainder = this.frac.clone();
found_ = [];
while(remainder.den !== 0n){
let extract = make_fraction (remainder.num / remainder.den, 1n);
found_.push(extract);
remainder = remainder.sub(extract).inverse();
}
let mat = [make_fraction(1,1),make_fraction(0,1),make_fraction(0,1),make_fraction(1,1)]; //identity
for(let i=0; i<found_.length && i<n; i++) {
mat = mat_mul(mat, [found_[i], make_fraction(1,1),make_fraction(1,1),make_fraction(0,1)]);
out.push(mat[0].div(mat[2]));
}
return out;
}
else {
if(n===undefined || n<1) throw "for CFs other than FiniteCF the method convergents(n) requires a finite positive value n";
if(! this.is_fully_known_upon_construction()){
while(this.found.length < n){
this.find_next();
}
}
let mat = [make_fraction(1,1),make_fraction(0,1),make_fraction(0,1),make_fraction(1,1)]; //identity
for(let i=0; i<n; i++) {
mat = mat_mul(mat, [this.element(i), make_fraction(1,1),make_fraction(1,1),make_fraction(0,1)]);
if(debug_if_this_is_slow_optional_argument){
console.log(`state: \nmat=${mat}\n, this.element(${i}) = ${this.element(i)}`);
}
out.push(mat[0].div(mat[2]));
}
return out;
}
}
}
//-----------------------<initialize arithmetic charts>-----------------------
function initialize_arithmetics(){
initialize_add();
initialize_sub();
initialize_mul();
initialize_div();
}
initialize_arithmetics();
function initialize_add(){
const finite_idx = CF.class_name_to_index("FiniteCF");
const periodic_idx = CF.class_name_to_index("PeriodicCF");
const method_idx = CF.class_name_to_index("FormulaCF");
const first_comp_idx = CF.class_name_to_index("FirstCompositeCF");
const second_comp_idx = CF.class_name_to_index("SecondCompositeCF");
const sqrt_idx = CF.class_name_to_index("SqrtCompositeCF");
const generalized_idx = CF.class_name_to_index("GeneralizedCF");
//First, initialize the two dimensional array with the default case:
for(let i = 0; i<=6; i++){
CF.add_chart[i] = [];
for(let j = 0; j<=6; j++){
CF.add_chart[i][j] = (a,b) => {
if(a===b) return new FirstCompositeCF(a,2n,0n,0n,1n);
return new SecondCompositeCF(a,b,0n,1n,1n,0n,0n,0n,0n,1n);
}
}
}
//Next, deal with the special cases, all of which involve the class FiniteCF.
// if both are finite:
CF.add_chart[finite_idx][finite_idx] = (a,b) => new FiniteCF(a.frac.add(b.frac));
//if one of of the objects if a FiniteCF and the other is a PeriodicCF/FormulaCF/GeneralizedCF/SqrtCompositeCF
let f1 = (a,b) => {
if(a.is_zero()) return b;
return new FirstCompositeCF(b, a.frac.den, a.frac.num, 0n, a.frac.den);
}
CF.add_chart[finite_idx][periodic_idx] = f1;
CF.add_chart[periodic_idx][finite_idx] = (b,a) => f1(a,b);
CF.add_chart[finite_idx][method_idx] = f1;
CF.add_chart[method_idx][finite_idx] = (b,a) => f1(a,b);
CF.add_chart[finite_idx][generalized_idx] = f1;
CF.add_chart[generalized_idx][finite_idx] = (b,a) => f1(a,b);
CF.add_chart[finite_idx][sqrt_idx] = f1;
CF.add_chart[sqrt_idx][finite_idx] = (b,a) => f1(a,b);
//if one of them is a FiniteCF and the other is a FirstCompositeCF:
CF.add_chart[finite_idx][first_comp_idx] = (a,b) =>{
if(a.is_zero()) return b;//optimization
let [a_,b_,c_,d_] = b.initial_matrix_values;
return new FirstCompositeCF(b.parent_cf, a_*a.frac.den+c_*a.frac.num, d_*a.frac.num+b_*a.frac.den, c_*a.frac.den, d_*a.frac.den);
}
CF.add_chart[first_comp_idx][finite_idx] = (b,a) => CF.add_chart[finite_idx][first_comp_idx](a,b);
CF.add_chart[finite_idx][second_comp_idx] = (a,b) => {
if(a.is_zero()) return b;//optimization
let [a__,b__,c__,d__,e__,f__,g__,h__] = b.initial_matrix_values;
let arr_1 = [a__,b__,c__,d__].map((n)=>n*a.frac.den);
let arr_2 = [e__,f__,g__,h__].map((n)=>n*a.frac.num);
var new_numerator_coefficients = arr_1.map((x,idx) => x + arr_2[idx]);
[a__,b__,c__,d__] = new_numerator_coefficients;
[e__,f__,g__,h__] = [e__,f__,g__,h__].map((n)=>n*a.frac.den);
return new SecondCompositeCF(b.cfX,b.cfY,a__,b__,c__,d__,e__,f__,g__,h__);
}
CF.add_chart[second_comp_idx][finite_idx] = (b,a) => CF.add_chart[finite_idx][second_comp_idx](a,b);
}
function initialize_sub(){
const finite_idx = CF.class_name_to_index("FiniteCF");
const periodic_idx = CF.class_name_to_index("PeriodicCF");
const method_idx = CF.class_name_to_index("FormulaCF");
const first_comp_idx = CF.class_name_to_index("FirstCompositeCF");
const second_comp_idx = CF.class_name_to_index("SecondCompositeCF");
const sqrt_idx = CF.class_name_to_index("SqrtCompositeCF");
const generalized_idx = CF.class_name_to_index("GeneralizedCF");
//First, initialize the two dimensional array with the default case:
for(let i = 0; i<=6; i++){
CF.sub_chart[i] = [];
for(let j = 0; j<=6; j++){
CF.sub_chart[i][j] = (a,b) => new SecondCompositeCF(a,b,0n,1n,-1n,0n,0n,0n,0n,1n);
}
}
//Next, deal with the special cases, all of which involve the class FiniteCF.
// if both are finite:
CF.sub_chart[finite_idx][finite_idx] = (a,b) => new FiniteCF(a.frac.sub(b.frac));
//if one of of the objects if a FiniteCF and the other is a PeriodicCF/FormulaCF/GeneralizedCF/SqrtCompositeCF
let f1 = (a,b) => {
if(a.is_zero()) return b.negate();
return new FirstCompositeCF(b, - a.frac.den, a.frac.num, 0n, a.frac.den);
}
let f2 = (b,a) => {
if(a.is_zero()) return b.negate();
return new FirstCompositeCF(b, a.frac.den, - a.frac.num, 0n, a.frac.den);
}
CF.sub_chart[finite_idx][periodic_idx] = f1;
CF.sub_chart[periodic_idx][finite_idx] = f2
CF.sub_chart[finite_idx][method_idx] = f1;
CF.sub_chart[method_idx][finite_idx] = f2;
CF.sub_chart[finite_idx][generalized_idx] = f1;
CF.sub_chart[generalized_idx][finite_idx] = f2;
CF.sub_chart[finite_idx][sqrt_idx] = f1;
CF.sub_chart[sqrt_idx][finite_idx] = f2;
//if one of them is a FiniteCF and the other is a FirstCompositeCF:
CF.sub_chart[finite_idx][first_comp_idx] = (a,b) =>{
if(a.is_zero()) return b.negate();//optimization
let [a_,b_,c_,d_] = b.initial_matrix_values;
return new FirstCompositeCF(b.parent_cf, - a_*a.frac.den+c_*a.frac.num, d_*a.frac.num - b_*a.frac.den, c_*a.frac.den, d_*a.frac.den);
}
CF.sub_chart[first_comp_idx][finite_idx] = (b,a) =>{
if(a.is_zero()) return b;//optimization
let [a_,b_,c_,d_] = b.initial_matrix_values;
return new FirstCompositeCF(b.parent_cf, - a_*a.frac.den+c_*a.frac.num, d_*a.frac.num - b_*a.frac.den, - c_*a.frac.den, - d_*a.frac.den);
}
CF.sub_chart[finite_idx][second_comp_idx] = (a,b) => {
if(a.is_zero()) return b;//optimization
let [a__,b__,c__,d__,e__,f__,g__,h__] = b.initial_matrix_values;
let arr_1 = [a__,b__,c__,d__].map((n)=> - n*a.frac.den);
let arr_2 = [e__,f__,g__,h__].map((n)=>n*a.frac.num);
var new_numerator_coefficients = arr_1.map((x,idx) => x + arr_2[idx]);
[a__,b__,c__,d__] = new_numerator_coefficients;
[e__,f__,g__,h__] = [e__,f__,g__,h__].map((n)=>n*a.frac.den);
return new SecondCompositeCF(b.cfX,b.cfY,a__,b__,c__,d__,e__,f__,g__,h__);
}
CF.sub_chart[second_comp_idx][finite_idx] = (b,a) => {
if(a.is_zero()) return b;//optimization
let [a__,b__,c__,d__,e__,f__,g__,h__] = b.initial_matrix_values;
let arr_1 = [a__,b__,c__,d__].map((n)=> - n*a.frac.den);
let arr_2 = [e__,f__,g__,h__].map((n)=>n*a.frac.num);
var new_numerator_coefficients = arr_1.map((x,idx) => - x - arr_2[idx]);
[a__,b__,c__,d__] = new_numerator_coefficients;
[e__,f__,g__,h__] = [e__,f__,g__,h__].map((n)=>n*a.frac.den);
return new SecondCompositeCF(b.cfX,b.cfY,a__,b__,c__,d__,e__,f__,g__,h__);
}
}
function initialize_mul(){
const finite_idx = CF.class_name_to_index("FiniteCF");
const periodic_idx = CF.class_name_to_index("PeriodicCF");
const method_idx = CF.class_name_to_index("FormulaCF");
const first_comp_idx = CF.class_name_to_index("FirstCompositeCF");
const second_comp_idx = CF.class_name_to_index("SecondCompositeCF");
const sqrt_idx = CF.class_name_to_index("SqrtCompositeCF");
const generalized_idx = CF.class_name_to_index("GeneralizedCF");
//First, initialize the two dimensional array with the default case:
for(let i = 0; i<=6; i++) {
CF.mul_chart[i] = [];
for(let j = 0; j<=6; j++){
CF.mul_chart[i][j] = (a,b) => new SecondCompositeCF(a,b,1n,0n,0n,0n,0n,0n,0n,1n);
}
}
//Next, deal with the special cases, all of which involve the class FiniteCF.
// the default for FiniteCF is using FirstCompositeCF:
for(let i=0; i<=6; i++){
CF.mul_chart[finite_idx][i] = (a,b) => {
if(a.frac.is_zero()){ return new FiniteCF(make_fraction(0n,1n));}
if(a.frac.num === a.frac.den) {return b;}
return new FirstCompositeCF(b, a.frac.num, 0n, 0n, a.frac.den);
}
CF.mul_chart[i][finite_idx] = (b,a) => {
if(a.frac.is_zero()) {return new FiniteCF(make_fraction(0n,1n));}
if(a.frac.num === a.frac.den) {return b;}
return new FirstCompositeCF(b, a.frac.num, 0n, 0n, a.frac.den);
}
}
CF.mul_chart[finite_idx][finite_idx] = (a,b) => new FiniteCF(a.frac.mul(b.frac));
CF.mul_chart[finite_idx][first_comp_idx] = (a,b) => {
if(a.frac.is_zero()) return new FiniteCF(make_fraction(0n,1n));
if(a.frac.num === a.frac.den) return b;
let [a_,b_,c_,d_] = b.initial_matrix_values;
return new FirstCompositeCF(b.parent_cf, a.frac.num*a_, a.frac.num*b_, a.frac.den*c_, a.frac.den*d_);
}
CF.mul_chart[first_comp_idx][finite_idx] = (b,a) => {
if(a.frac.is_zero()) return new FiniteCF(make_fraction(0n,1n));
if(a.frac.num === a.frac.den) return b;
let [a_,b_,c_,d_] = b.initial_matrix_values;
return new FirstCompositeCF(b.parent_cf, a.frac.num*a_, a.frac.num*b_, a.frac.den*c_, a.frac.den*d_);
}
CF.mul_chart[finite_idx][second_comp_idx] = (a,b) => {
if(a.frac.is_zero()) return new FiniteCF(make_fraction(0n,1n));
if(a.frac.num === a.frac.den) return b;
let [a__,b__,c__,d__,e__,f__,g__,h__] = b.initial_matrix_values;
[a__,b__,c__,d__] = [a__,b__,c__,d__].map((n)=>n*a.frac.num);
[e__,f__,g__,h__] = [e__,f__,g__,h__].map((n)=>n*a.frac.den);
return new SecondCompositeCF(b.cfX,b.cfY,a__,b__,c__,d__,e__,f__,g__,h__);
}
CF.mul_chart[second_comp_idx][finite_idx] = (b,a) => {
if(a.frac.is_zero()) return new FiniteCF(make_fraction(0n,1n));
if(a.frac.num === a.frac.den) return b;
let [a__,b__,c__,d__,e__,f__,g__,h__] = b.initial_matrix_values;
[a__,b__,c__,d__] = [a__,b__,c__,d__].map((n)=>n*a.frac.num);
[e__,f__,g__,h__] = [e__,f__,g__,h__].map((n)=>n*a.frac.den);
return new SecondCompositeCF(b.cfX,b.cfY,a__,b__,c__,d__,e__,f__,g__,h__);
}
}
function initialize_div(){
const finite_idx = CF.class_name_to_index("FiniteCF");
const periodic_idx = CF.class_name_to_index("PeriodicCF");
const method_idx = CF.class_name_to_index("FormulaCF");
const first_comp_idx = CF.class_name_to_index("FirstCompositeCF");
const second_comp_idx = CF.class_name_to_index("SecondCompositeCF");
const sqrt_idx = CF.class_name_to_index("SqrtCompositeCF");
const generalized_idx = CF.class_name_to_index("GeneralizedCF");
//First, initialize the two dimensional array with the default case:
for(let i = 0; i<=6; i++) {
CF.div_chart[i] = [];
for(let j = 0; j<=6; j++){
CF.div_chart[i][j] = (a,b) => new SecondCompositeCF(a,b,0n,1n,0n,0n,0n,0n,1n,0n);
}
}
//Next, deal with the special cases, all of which involve the class FiniteCF.
// the default for FiniteCF is using FirstCompositeCF:
for(let i=0; i<=6; i++){
CF.div_chart[finite_idx][i] = (a,b) => {
if(a.frac.is_zero()) return new FiniteCF(make_fraction(0n,1n));
if(a.frac.num === a.frac.den) return b.inverse();
return new FirstCompositeCF(b, 0n, a.frac.num, a.frac.den, 0n);
}
CF.div_chart[i][finite_idx] = (b,a) => {
if(a.frac.is_zero()) throw "Division of CF by Zero";
if(a.frac.num === a.frac.den) return b;
return new FirstCompositeCF(b, a.frac.den, 0n, 0n, a.frac.num);
}
}
CF.div_chart[finite_idx][finite_idx] = (a,b) => new FiniteCF(a.frac.div(b.frac));
CF.div_chart[finite_idx][first_comp_idx] = (a,b) => {
if(a.frac.is_zero()) return new FiniteCF(make_fraction(0n,1n));
if(a.frac.num === a.frac.den) return b.inverse();
let [a_,b_,c_,d_] = b.initial_matrix_values;
return new FirstCompositeCF(b.parent_cf, c_*a.frac.num, d_*a.frac.num, a_*a.frac.den, b_*a.frac.den);
}
CF.div_chart[first_comp_idx][finite_idx] = (b,a) => {
if(a.frac.is_zero()) throw "Division of CF by Zero";
if(a.frac.num === a.frac.den) return b;
let [a_2,b_2,c_2,d_2] = b.initial_matrix_values;
return new FirstCompositeCF(b.parent_cf, a_2*a.frac.den, b_2*a.frac.den, c_2*a.frac.num, d_2*a.frac.num);
}
CF.div_chart[finite_idx][second_comp_idx] = (a,b) => {
if(a.frac.is_zero()) return new FiniteCF(make_fraction(0n,1n));
if(a.frac.num === a.frac.den) return b.inverse();
let [a__,b__,c__,d__,e__,f__,g__,h__] = b.initial_matrix_values;
let [_a__,_b__,_c__,_d__] = [e__,f__,g__,h__].map((n) => n*a.frac.num);
let [_e__,_f__,_g__,_h__] = [a__,b__,c__,d__].map((n) => n*a.frac.den);
return new SecondCompositeCF(b.cfX,b.cfY, _a__, _b__, _c__, _d__, _e__, _f__, _g__, _h__);
}
CF.div_chart[second_comp_idx][finite_idx] = (b,a) => {
if(a.frac.is_zero()) throw "Division of CF by Zero";
if(a.frac.num === a.frac.den) return b;
let [a2__,b2__,c2__,d2__,e2__,f2__,g2__,h2__] = b.initial_matrix_values;
let [_a2__,_b2__,_c2__,_d2__] = [a2__,b2__,c2__,d2__].map((n) => n*a.frac.den);
let [_e2__,_f2__,_g2__,_h2__] = [e2__,f2__,g2__,h2__].map((n) => n*a.frac.num);
return new SecondCompositeCF(b.cfX,b.cfY, _a2__, _b2__, _c2__, _d2__, _e2__, _f2__, _g2__, _h2__);
}
}
//-----------------------</initialize arithmetic charts>-----------------------
class FiniteCF extends CF {
constructor (frac){
super();
frac.simplify();
if(frac.den === 0n) throw 'ZeroDenominator';
this.frac = frac;
}
clone(){ // n - optional parameter
return new FiniteCF(this.frac.clone());
}
negate(){
return new FiniteCF(this.frac.negate());
}
inverse(){
return new FiniteCF(this.frac.inverse());//zero pairs are removed automatically by the constructor
}
element(i){
throw "should never be called for FiniteCF";
}
to_text_string(){
return `[Rational Number ${this.frac.to_text_string()}]`;
}
to_text_string2(){
return `[${this.frac.to_text_string()}]`;
}
is_fully_known_upon_construction(){
return true;
}
is_zero(){
return this.frac.is_zero();
}
is_non_zero(){
return this.frac.is_non_zero();
}
sqrt(){
return CF.make_sqrt_of_fraction(this.frac.num, this.frac.den);
}
}
class PeriodicCF extends CF {
constructor(lst_initial, lst_periodic) {//NOTE: BOTH PARAMETERS SHOULD BE BigInt Arrays.
if(lst_periodic.length === 0) return new FiniteCF(fr.Fraction.continued_fraction_list_to_fraction(lst_initial));
super();
//remove repeated zeroes from the cf and initialize the lists:
this.lst_initial = Array.remove_zero_pairs(lst_initial);
this.lst_periodic = Array.remove_zero_pairs(lst_periodic);
if(this.lst_periodic[0]===0n && this.lst_periodic[this.lst_periodic.length-1]===0n){
this.lst_initial = this.lst_initial.concat([0n]);
this.lst_periodic = this.lst_periodic.slice(1,this.lst_periodic.length-1);
}
else if(this.lst_initial[this.lst_initial.length-1]===0n && this.lst_periodic[0]===0n){
this.lst_initial = this.lst_initial.slice(0,this.lst_initial.length - 1);
this.lst_periodic = this.lst_periodic.slice(1).concat([0n]);
}
let first_nonzero = lst_periodic.find(element => element !== 0n);
if(first_nonzero === undefined){
throw "PeriodicCF expects the periodic list to have at least one non-zero element";
}
}
clone() {
return new PeriodicCF([...this.lst_initial], [...this.lst_periodic]);
}
negate() {
return new PeriodicCF([...this.lst_initial].map((x)=>-x), [...this.lst_periodic].map((x)=>-x));
}
inverse() {
return new PeriodicCF([0n,...this.lst_initial], [...this.lst_periodic]);//zero pairs are removed automatically by the constructor
}
element(i) {
if (i < this.lst_initial.length) return make_fraction(this.lst_initial[i],1n);
i = i - this.lst_initial.length;
return make_fraction(this.lst_periodic[i % (this.lst_periodic.length)], 1n) ;
}
to_text_string() {
return `[Periodic Continued Fraction - init=[${this.lst_initial}], repeat=[${this.lst_periodic}]]`;
}
to_text_string2(){
return `[${this.lst_initial}, repeat: ${this.lst_periodic}]`
// return `[${[...this.lst_initial, "repeat: ", ...this.lst_periodic]}]`;
}
is_fully_known_upon_construction(){
return true;
}
}
class FormulaCF extends CF {
constructor(formula) {
super();
this.formula = formula;
}
clone() {
return new FormulaCF(this.formula);
}
negate() {
return new FormulaCF((n)=> - this.formula(n));
}
inverse() {
if(this.formula(0) === 0n){
let new_func1 = (j)=> this.formula(j+1);
return new FormulaCF((new_func1));
}
else {
let new_func2 = (j)=>{
if(j===0) return 0n;
else return this.formula(j-1);
}
return new FormulaCF(new_func2);
}
}
element(i) {
return make_fraction(this.formula(i),1n);
}
to_text_string(){
return "[Formula CF. Formula: " + this.formula.toString() + "]";
}
to_text_string2(){
return "[Formula CF. Formula: " + this.formula.toString() + "]";
}
is_fully_known_upon_construction(){
return true;
}
}
// class IntervalWrapperCF extends CF {
// constructor(cf, _mat, _index) { //_mat and _index are optional parameters.
// super();
// this.cf = cf;
// if(_mat === undefined){[this.num1,this.den1,this.num2,this.den2] = [1n,0n,0n,1n]}
// else {this.mat = [..._mat]}
// if(_index === undefined){this.index = 0}
// else {this.index = _index}
// }
// clone() {
// return new IntervalWrapperCF(this.cf.clone(), [this.mat], this.index);
// }
// negate() {
// return new IntervalWrapperCF(this.cf.negate(), [this.mat].map((x)=>-x), this.index);
// }
// inverse() {
// if(this.formula(0) === 0n){
// let new_func1 = (j)=> this.formula(j+1);
// return new FormulaCF((new_func1));
// }
// else {
// let new_func2 = (j)=>{
// if(j===0) return 0n;
// else return this.formula(j-1);
// }
// return new FormulaCF(new_func2);
// }
// }
// element(i) {
// return make_fraction(this.formula(i),1n);
// }
// to_text_string(){
// return "[Formula CF. Formula: " + this.formula.toString() + "]";
// }
// to_text_string2(){
// return "[Formula CF. Formula: " + this.formula.toString() + "]";
// }
// is_fully_known_upon_construction(){
// return true;
// }
// }
class FirstCompositeCF extends CF {
// Given a normalized cf X, this CF will be
// (aX + b)
// --------
// (cX + d)
//
// ...using Gospers first algorithm (hence First composite).
//
// Rather than use Gospers algorithm verbatim,
// which finds the next number in the continued fraction by comparing the integer parts of a/c to b/d and demanding they be equal,
// we demand that the distance between the fractions a/c and b/d be smaller than a small fraction,
// and then we choose a/c as the next number in the continued fraction.
//
// Note: no detection of cycles.
//
// Note: For simplicity, FirstCompositeCF can only handle INFINTE source-cfs.
// Arithmetics with Finite CFs should be handled by constructing the appropriate FirstCompositeCF in add/mul/div/sub.
//
constructor(cf, a, b, c, d, found_, index_, initial_matrix_values_, top_){ ///found_, index_ = optional parameters. Useful for arithmetics with FiniteCF/cloning operations.
super();
if(! CF.is_normalized_cf(cf)) throw "FirstCompositeCF must be initialized with a normalized cf";
//initialize matrix |a b|
// |c d|
[this.a, this.b, this.c, this.d] = [BigInt(a), BigInt(b), BigInt(c), BigInt(d)];
if(this.c === 0n && this.d === 0n) throw "zero denominator (in FirstCompositeCF constructor)";
else if(this.a === 0n && this.b === 0n) return new FiniteCF(make_fraction(0,1)); //zero numerator
else if(this.a === 0n && this.c === 0n){
return new CF.make_cf_from_fraction(this.b, this.d);
}
else if(this.b === 0n && this.d === 0n){
return new CF.make_cf_from_fraction(this.a, this.c);
}
else if(this.b === 0n && this.c === 0n && this.a === this.d){
return cf.clone();
}
else{
let gcd_val = BigInt.abs(BigInt.gcd(BigInt.gcd(this.a,this.b), BigInt.gcd(this.c,this.d)));
this.a = this.a/gcd_val;
this.b = this.b/gcd_val;
this.c = this.c/gcd_val;
this.d = this.d/gcd_val;