-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.cpp
More file actions
1256 lines (1135 loc) · 33 KB
/
Copy pathsqrt.cpp
File metadata and controls
1256 lines (1135 loc) · 33 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
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
#include <limits>
#include <stdexcept>
#include <format>
#include <utility>
#include <functional>
#include <random>
#include <iostream>
#include <vector>
using namespace std;
#include <bits/stdc++.h>
bool is_good_enuf(double guess, double x) {
if (guess * guess - x < 0.001 && guess * guess - x > -0.01) {
return true;
}
else {
return false;
}
};
double improve(double guess, double x) {
return (guess + (x / guess))/2;
};
double sqrt_iter(double guess, double x) {
if (is_good_enuf(guess, x)) {
return guess;
}
else {
return sqrt_iter(improve(guess, x), x);
}
};
double sqrt_myfunc(double x) {
return sqrt_iter(1, x);
};
int A(int x, int y) {
if (y == 0) {
return 0;
}
else if (x == 0) {
return 2 * y;
}
else if (y == 1) {
return 2;
}
else {
return A(x-1, A(x, y-1));
}
}
int fib_iter(int a, int b, int count) {
if (count <= 3) {
return a;
}
else {
int total = a + b;
b = a;
a = total;
return fib_iter(a, b, count - 1);
}
}
int fib(int x) {
return fib_iter(1, 1, x);
};
int f1_recurse(int n) {
if (n < 3) {
return n;
}
else {
return f1_recurse(n-1) + 2 * f1_recurse(n-2) + 3 * f1_recurse(n-3);
}
}
int f1_iter(int a, int b, int c, int count) {
if (count < 4) {
return 3 * a + 2 * b + c;
}
else {
int new_c = 3 * a + 2 * b + c;
return f1_iter(b, c, new_c, count-1);
}
}
int f1_iterFull(int n) {
if (n < 3) {
return n;
}
else {
return f1_iter(0, 1, 2, n);
}
}
vector<int> Pascal(int n) {
if (n == 1) {
return {1};
}
else if (n == 2) {
return {1, 1};
}
else {
vector<int> prev_row = Pascal(n-1);
vector<int> new_row = {1};
for (int i = prev_row.size()-1; i >= 1; --i) {
new_row.push_back(prev_row[i]+prev_row[i-1]);
}
new_row.push_back(1);
return new_row;
}
}
int exp_recurse(int b, int n) {
if (n == 1) {
return b;
}
else {
return b * exp_recurse(b, n-1);
}
}
int exp_iter(int b, int count, int product) {
if (count == 0) {
return product;
}
else {
return exp_iter(b, count-1, product * b);
}
}
int exp(int b, int n) {
return exp_iter(b, n, 1);
}
int fast_exp_recurse(int b, int n) {
if (n == 1) {
return b;
}
else if (n%2 == 0) {
return fast_exp_recurse(b, n/2) * fast_exp_recurse(b, n/2);
}
else {
return b * fast_exp_recurse(b, n-1);
}
}
double fast_exp_iter(double b, int count, double product) {
if (count == 0) {
return product;
}
else if (count % 2 == 0) {
return fast_exp_iter(b*b, count/2, product);
}
else {
return fast_exp_iter(b, count - 1, product * b);
}
}
double fast_exp(double b, int n) {
return fast_exp_iter(b, n, 1);
}
int double_myfunc(int a) {
return a*2;
}
int halve(int a) {
return a/2;
}
int multiply_recurse(int a, int b) {
if (b == 0) {
return 0;
}
else {
if (b == 1) {
return a;
}
else {
return a + multiply_recurse(a, b-1);
}
}
}
int fast_multiply_recurse(int a, int b) {
if (b == 0) {
return 0;
}
else {
if (b == 1) {
return a;
}
else {
if (b % 2 == 0) {
return fast_multiply_recurse(double_myfunc(a), halve(b));
}
else {
return a + fast_multiply_recurse(a, b-1);
}
}
}
}
int fast_multiply_iter(int a, int count, int total) {
if (count == 0) {
return total;
}
else {
if (count % 2 == 0) {
return fast_multiply_iter(double_myfunc(a), halve(count), total);
}
else {
return fast_multiply_iter(a, count-1, total + a);
}
}
}
int fast_multiply(int a, int b) {
return fast_multiply_iter(a, b, 0);
}
int euclid_gcd(int a, int b) {
if (b == 0) {
return a;
}
else {
return (euclid_gcd(b, a%b));
}
}
bool check_divide(int a, int b) {
if (a % b == 0) {
return true;
}
else {
return false;
}
}
int check_divisibility(int n, int count, int no_of_divisors) {
if (count * count > n) {
return no_of_divisors;
}
else {
if (check_divide(n, count)) {
no_of_divisors += 1;
return check_divisibility(n, count + 1, no_of_divisors);
}
else {
return check_divisibility(n, count+1, no_of_divisors);
}
}
}
bool is_prime(double n) {
if (n <= 1) {
return false;
}
if (check_divisibility(n, 2, 0) == 0) {
return true;
}
else {
return false;
}
}
bool fermat_lil(int a, int n) {
int modpow = static_cast<int>(fast_exp(a, n)) % n;
int mod = a % n;
if (modpow == mod) {
return true;
}
else {
return false;
}
}
random_device rd;
mt19937 gen(rd());
bool fermat_prime_test(int n, int repeat) {
if (repeat <= 0) {
return true;
}
else {
uniform_int_distribution<int> distribution(1, n-1);
int a = distribution(gen);
if ( ! fermat_lil(a, n) ) {
return false;
}
else {
return fermat_prime_test(n, repeat - 1);
}
}
}
int summation (function<int(int)> term, int x, function<int(int)> next, int y) {
if (x > y) {
return 0;
}
else {
return term(x) + summation(term, next(x), next, y);
}
}
int cube(int x) {
return x * x * x;
}
int inc(int x) {
return x + 1;
}
double def_integral(function<double(double)> func, double x, double dx, double y, function<double(double, double)> change, double tot) {
if (x > y) {
return tot;
}
else {
return def_integral(func, change(x, dx), dx, y, change, tot + func(x+dx/2) * dx);
}
}
double change(double x, double dx) {
return x + dx;
}
double random_math_func(double x) {
return x * x * x + 2 * x;
}
double simpsons_est(function<double(double)> func, double initial, double final, double dx, double est) {
if (initial + 2 * dx - 0.0000001 > final) {
return est;
}
else {
double term_sum_main = func(initial) + 4 * func(change(initial, dx)) + func(change(initial, 2 * dx));
double term_sum_acc = term_sum_main * dx / 3;
return simpsons_est(func, change(initial, 2 * dx), final, dx, est + term_sum_acc);
}
}
double product_iter(function<double(double)> term, double initial, function<double(double)> next, double final, double result) {
if (initial > final) {
return result;
}
else {
return product_iter(term, next(initial), next, final, result * term(initial));
}
}
double product_recurse(function<double(double)> term, double initial, function<double(double)> next, double final) {
if (initial >= final) {
return term(initial);
}
else {
return term(initial) * product_recurse(term, next(initial), next, final);
}
}
double equiv(int n) {
return (double)n;
}
double fact_abstraction(int n) {
return product_iter(equiv, 1, inc, n, 1);
}
int inc_double(int x) {
return x + 2;
}
double multiply_consec_eo_no(double n) {
return product_iter(equiv, n, inc_double, n+2, 1);
}
double pi_approx(int n) {
double top;
double bottom;
if (n % 2 == 0) {
top = product_iter(multiply_consec_eo_no, 2, inc_double, (double)n, 1);
bottom = 3 * (n+1) * product_iter(multiply_consec_eo_no, 3, inc_double, (double)n, 1);
} else {
top = product_iter(multiply_consec_eo_no, 2, inc_double, (double)n, 1) * (n+1);
bottom = 3 * product_iter(multiply_consec_eo_no, 3, inc_double, (double)n, 1);
}
return top / bottom * 4;
}
double accumulate(function<double(double, double)> combiner, double null_value, function<double(double)> term, double a, function<double(double)> next, double b) {
if (a > b) {
return null_value;
}
else {
return accumulate(combiner, combiner(null_value, term(a)), term, next(a), next, b);
}
}
double accumulate_recursive(function<double(double, double)> combiner, double null_value, function<double(double)> term, double a, function<double(double)> next, double b) {
if (a > b) {
return null_value;
}
else {
return combiner(term(a), accumulate_recursive(combiner, null_value, term, next(a), next, b));
}
}
double filtered_accumulate(function<bool(double)> filter, function<double(double, double)> combiner, double null_value, function<double(double)> term, double a, function<double(double)> next, double b) {
if (a > b) {
return null_value;
}
else {
if (filter(a)) {
return filtered_accumulate(filter, combiner, combiner(null_value, term(a)), term, next(a), next, b);
}
else {
return filtered_accumulate(filter, combiner, null_value, term, next(a), next, b);
}
}
};
double test_combiner(double a, double b) {
return a + b;
}
double filtered_cubes(double x, double y) {
return filtered_accumulate(
[](double x) {return static_cast<int>(x) % 2 == 0;},
[](double x, double y) {return x + y;},
0,
[](double x) {return x * x * x;},
x,
[](double x) {return x + 1;},
y
);
}
double search_for_root(function<double(double)> func, double neg_point, double pos_point) {
auto good_enuf = [](double x, double y) {
return abs(x - y) < 0.0001;
};
auto average = [](double x, double y) {return (x + y)/2;};
if (good_enuf(neg_point, pos_point)) {
return average(neg_point, pos_point);
}
else {
if (func(neg_point) > 0 and func(pos_point) < 0) {
return search_for_root(func, pos_point, neg_point);
}
else if (func(neg_point) < 0 and func(pos_point) > 0) {
double avg = average(neg_point,pos_point);
if (func(avg) > 0) {
return search_for_root(func, neg_point, avg);
}
else if (func(avg) < 0) {
return search_for_root(func, avg, pos_point);
}
else {
return avg;
}
}
else {
return 92387091283128903;
}
}
}
double fixed_point(function<double(double)> func, double guess) {
if (abs(guess - func(guess)) < 0.001) {
return func(guess);
}
else if (abs(guess - func(guess)) > 1000 ) {
return func(guess);
}
else if ( isnan(guess) || guess == INFINITY) {
return guess;
}
else {
return fixed_point(func, func(guess));
}
}
double fixed_damping(function<double(double)> func, double guess) {
auto avg = [](double x, double y) -> double {
return (x + y) / 2;
};
if (abs(guess - func(guess)) < 0.001) {
return func(guess);
}
else if (abs(guess - func(guess)) > 1000 ) {
return func(guess);
}
else if ( isnan(guess) || guess == INFINITY) {
return guess;
}
else {
return fixed_point(func, func(0.5 * avg(guess, func(guess))));
}
}
double log_approx(double base, double x) {
auto log_mac_term = [](double n, double x) -> double {
int power = static_cast<int>(n);
if (power % 2 == 0) {
return -1 * fast_exp(x, power) / power;
} else {
return fast_exp(x, power) / power;
}
};
auto log_mac_use = [&log_mac_term, x](double n) -> double {
if (x > 1) {
return log_mac_term(n, (1 / x) - 1) * -1;
} else{
return log_mac_term(n, x-1);
}
};
auto log_mac_base = [&log_mac_term, base](double n) -> double {
return -1 * log_mac_term(n, (1 / base) - 1);
};
if (base <= 1 || x < -1) {
return 21312323423212;
}
else {
return accumulate(
[](double a, double b){return a + b;},
0,
log_mac_use,
1,
[](double a){return a + 1;},
15
) / accumulate(
[](double a, double b){return a + b;},
0,
log_mac_base,
1,
[](double a){return a + 1;},
15
);
}
}
double xx_solution(double equal) {
return fixed_point([equal](double x){return log2(equal)/log2(x);}, 2);
}
double cont_frac_recursive(double i, function<double(double)> term_n, function<double(double)>term_d, double count) {
if (i >= count) {
return term_n(i)/term_d(i);
}
else {
return term_n(i) / (term_d(i) + cont_frac_recursive(i+1, term_n, term_d, count));
}
}
double cont_frac(function<double(double)> term_n, function<double(double)> term_d, double count, double result) {
if (count <= 0) {
return result;
}
else {
return cont_frac(term_n, term_d, count-1, term_n(count)/(term_d(count) + result));
}
}
double tan_cf(double x, double k, double result) {
auto square = [](double x){
return x * x;
};
if (k <= 0) {
return result;
}
else if (k == 1) {
return tan_cf(x, k-1, x/(k-result));
}
else {
return tan_cf(x, k-1, square(x)/((2 * k - 1)-result));
}
}
double deriv(function<double(double)> func, double x, double dx) {
return (func(x + dx) - func(x))/dx;
}
double newton_method(function<double(double)> func, double guess) {
if (abs(func(guess)) < 0.001) {
return guess;
}
else {
return newton_method(func, guess - func(guess)/deriv(func, guess, 0.0001));
}
}
double cubic_solve(double a, double b, double c) {
return newton_method([a, b, c](double x){return fast_exp(x, 3) + a * fast_exp(x, 2) + b * x + c;}, 1);
}
double composite2(function<double(double)> f, function<double(double)> g, double x) {
return f(g(x));
}
double repeatedapp(function<double(double)> func, double x, int count) {
if (count == 1) {
return func(x);
}
else {
return repeatedapp(func, func(x), count-1);
}
}
double nfoldsmoothing(function<double(double)> func, double dx, double x, int count) {
auto smooth = [func, dx](double x) -> double {
double tot = func(x-dx) + func(x) + func(x+dx);
return tot / 3;
};
return repeatedapp(smooth, x, count);
}
double damping_func_gen(double power, double x, double y) {
if (power <= 2) {
return 0.5 * (x + y);
}
else {
return damping_func_gen(power - 1, x, 0.5 * (x + y));
}
}
double solving_xnequals(double power, double equals) {
auto damping_func = [power, equals](double x) -> double {
return damping_func_gen(power, x, equals/fast_exp(x, power-1));
};
return fixed_point(damping_func, 1);
}
function<double(function<double(double)>, double)> iterative_improve(function<bool(function<double(double)>, double, double)> good_enough, function<double(double)> improvement) {
function<double(function<double(double)>, double)> returning_func;
returning_func = [&, good_enough, improvement](function<double(double)> solving, double x) -> double {
if (good_enough(solving, x, 0.0001)) {
return x;
}
else {
return returning_func(solving, improvement(x));
}
};
return returning_func;
}
double generic_equation_solver(function<double(double)> LHS, double RHS) {
auto good_enough = [RHS](function<double(double)> solver, double x, double tolerance) -> bool {
if (abs(solver(x) - RHS) < tolerance) {
return true;
}
else {return false;}
};
auto newton_improve_func = [LHS, RHS](double x) -> double {
return x - (LHS(x) - RHS)/deriv([LHS, RHS](double x){return LHS(x)-RHS;}, x, 0.0001);
};
return iterative_improve(good_enough, newton_improve_func)(LHS, 1);
}
class Rational {
private:
int numer;
int denom;
void simplify() {
if (denom == 0) {
cout << "Division by Zero Error";
numer = 0;
denom = 0;
}
else {
int g = euclid_gcd(numer, denom);
numer = numer / g;
denom = denom / g;
}
if (denom < 0) {
denom = -1 * denom;
numer = -1 * numer;
}
}
public:
Rational(int n, int d) : numer(n), denom(d) {
simplify();
}
int get_numer() const {
return numer;
}
int get_denom() const {
return denom;
}
void print_rational() const {
if (denom == 1) {
cout << numer << endl;
}
else {
cout << to_string(numer) + " / " + to_string(denom) << endl;
}
}
Rational& operator+=(const Rational& other) {
numer = numer * other.denom + other.numer * denom;
denom = denom * other.denom;
simplify();
return *this;
}
Rational& operator*=(const Rational& other) {
numer = numer * other.numer;
denom = denom * other.denom;
simplify();
return *this;
}
Rational& operator-=(const Rational& other) {
Rational neg(-1 * other.numer, other.denom);
return *this += neg;
}
Rational& operator/=(const Rational& other) {
Rational flip(other.denom, other.numer);
return *this*=flip;
}
};
Rational operator+(Rational r1, const Rational& r2) {
r1 += r2;
return r1;
}
Rational operator-(Rational r1, const Rational& r2) {
r1 -= r2;
return r1;
}
Rational operator*(Rational r1, const Rational& r2) {
r1 *= r2;
return r1;
}
Rational operator/(Rational r1, const Rational& r2) {
r1 /= r2;
return r1;
}
bool operator==(const Rational& r1, const Rational& r2) {
return (r1.get_numer() == r2.get_numer() && r1.get_denom() == r2.get_denom());
}
bool operator!=(const Rational& r1, const Rational& r2) {
return !(r1 == r2);
}
template<typename T>
Rational to_rationalise(T n) {
function<Rational(T, int)> process;
process = [&](T n, int denom) {
if (abs(static_cast<int>(n * denom) - n * denom) < 0.000001) {
return Rational(static_cast<int>(n*denom), denom);
}
else {
return process(n, denom * 10);
}
};
if constexpr (is_integral_v<T>) {
return Rational(n, 1);
}
else {
return process(n, 1);
}
}
class Point {
private:
double x_coord;
double y_coord;
public:
Point(double x, double y) : x_coord(x), y_coord(y) {}
string print_point() const {
return ("(" + to_string(x_coord) + ", " + to_string(y_coord) + ")");
}
double get_x() const {
return x_coord;
}
double get_y() const {
return y_coord;
}
};
class LineSegment {
private:
Point start_pt;
Point end_pt;
public:
LineSegment (Point p1, Point p2) : start_pt(p1), end_pt(p2) {}
string print_seg() const {
return "Start pt: " + start_pt.print_point() + ", End pt: " + end_pt.print_point();
}
Point midpoint() const {
return Point((start_pt.get_x() + end_pt.get_x())/2, (start_pt.get_y() + end_pt.get_y())/2);
}
double length() const {
auto square = [](double x) -> double {
return x * x;
};
double length = sqrt_myfunc(square(start_pt.get_x() - end_pt.get_x()) + square(start_pt.get_y() - end_pt.get_y()));
return length;
}
};
class Rectangle1 {
private:
Point bLeft_corner;
double length;
double width;
double rotation_angle;
Point tLeft_corner;
Point tRight_corner;
Point bRight_corner;
Rectangle1& normalize_angle() {
if (rotation_angle > M_PI) {
int k = ceil((rotation_angle - M_PI)/(2 * M_PI));
rotation_angle -= 2 * k * M_PI;
return *this;
}
else if (rotation_angle <= -1 * M_PI) {
int k = floor((rotation_angle + M_PI)/(2 * M_PI));
rotation_angle -= 2 * k * M_PI;
return *this;
}
else {
return *this;
}
}
Rectangle1& calc_update_vertices() {
auto pivot = [](Point pivot_pt, double l, double angle) -> Point {
return Point(pivot_pt.get_x() + (l * cos(angle)), pivot_pt.get_y() + (l * sin(angle)));
};
tLeft_corner = pivot(bLeft_corner, width, rotation_angle + M_PI/2);
bRight_corner = pivot(bLeft_corner, length, rotation_angle);
tRight_corner = pivot(tLeft_corner, length, rotation_angle);
return *this;
}
public:
Rectangle1(const Point& BL, double l, double w, double rot_angle, Point p_dummy) : bLeft_corner(BL), length(l), width(w), rotation_angle(rot_angle), tLeft_corner(p_dummy), tRight_corner(p_dummy), bRight_corner(p_dummy) {
normalize_angle();
calc_update_vertices();
}
string print_vertices() const {
return ("P1: " + bLeft_corner.print_point() + ", P2: " + tLeft_corner.print_point() + ", P3: " + bRight_corner.print_point() + ", P4: " + tRight_corner.print_point());
}
double area() const {
return length * width;
}
double perimeter() const {
return 2 * (length + width);
}
};
function<double(function<double(double, double)>)> my_pair(double a, double b) {
return [a, b](function<double(double, double)> m) -> double {
return m(a, b);
};
}
double head(function<double(function<double(double, double)>)> a) {
auto z = [](double a, double b) -> double {
return a;
};
return a(z);
}
double tail(function<double(function<double(double, double)>)> a) {
auto z = [](double a, double b) -> double {
return b;
};
return a(z);
}
double my_pair2(int a, int b) {
return fast_exp(2, a) * fast_exp(3, b);
}
double head2(double x) {
function<int(double, double)> counting2s;
counting2s = [&](double a, double b) -> int {
if (static_cast<int>(a) % 2 != 0) {
return static_cast<int>(b);
}
else {
return counting2s(a/2, b+1);
}
};
return counting2s(x, 0);
}
function<function<int(int)>(function<int(int)>)> zero() {
return [](function<int(int)> f) -> function<int(int)> {
return [](int x) -> int {
return x;
};
};
}
function<function<int(int)>(function<int(int)>)> one() {
return [](function<int(int)> f) -> function<int(int)> {
return [f](int x) -> int {
return f(x);
};
};
}
function<function<int(int)>(function<int(int)>)> succ(function<function<int(int)>(function<int(int)>)> n) {
return [n](function<int(int)> f) -> function<int(int)> {
return [f, n](int x) -> int {
return f((n(f))(x));
};
};
}
function<function<int(int)>(function<int(int)>)> two() {
return [](function<int(int)> f) -> function<int(int)> {
return [f](int x) -> int {
return f(f(x));
};
};
}
class Interval {
private:
double upper;
double lower;
public:
Interval(string method, double a, double b) {
if (method == "low_high") {
if (a > b) {
upper = a;
lower = b;
} else {
upper = b;
lower = a;
}
} else if (method == "center_percent") {
lower = a - (b / 100 * a);
upper = a + (b / 100 * a);
} else {
throw invalid_argument("Error: String unrecognised by class Interval");
}
}
Interval(double a, double b) : Interval("low_high", a, b) {}
double get_upper() const {
return upper;
}
double get_lower() const {
return lower;
}
string print_interval() const {
return "(" + to_string(lower) + ", " + to_string(upper) + ")";
}
Interval& operator += (const Interval& inter2) {
upper += inter2.upper;
lower += inter2.lower;
return *this;
}
Interval& operator *= (const Interval& inter2) {
double UU = upper * inter2.upper;
double UL = upper * inter2.lower;
double LU = lower * inter2.upper;
double LL = lower * inter2.lower;
vector<double> options = {UU, UL, LU, LL};
double lowest = numeric_limits<double>::max();
double highest = numeric_limits<double>::lowest();
function<void(int)> z;
z = [&](int n) -> void {
if (n >= options.size()) {
return;
} else {
if (options[n] > highest) {
highest = options[n];
}
if (options[n] < lowest) {
lowest = options[n];
}
}
z(n+1);
};
z(0);
upper = highest;
lower = lowest;
return *this;
}