Repository navigation
Expand file tree
/
Copy pathreal_math.hpp
More file actions
executable file
·876 lines (801 loc) · 39.3 KB
/
Copy pathreal_math.hpp
File metadata and controls
executable file
·876 lines (801 loc) · 39.3 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
#ifndef BOOST_REAL_MATH_HPP
#define BOOST_REAL_MATH_HPP
#include <tuple>
#include "real/exact_number.hpp"
#include "real/real_exception.hpp"
#include "real/irrational_helpers.hpp"
namespace boost{
namespace real{
/**
* EXPONENT FUNCTION USING TAYLOR EXPANSION
* @brief: calculates exponent of a exact_number using taylor expansion
* @param: num: the exact number. whose exponent is to be found
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
exact_number<T> exponent_taylor(exact_number<T> num, size_t max_error_exponent, bool upper){
exact_number<T> result("1");
exact_number<T> term_number("1");
exact_number<T> factorial("1");
exact_number<T> cur_term("0");
exact_number<T> max_error(std::vector<T> {1}, -max_error_exponent, true);
exact_number<T> x_pow("1");
do{
result += cur_term;
factorial *= term_number;
term_number = term_number + literals::one_exact<T>;
x_pow *= num;
cur_term = x_pow;
cur_term.divide_vector(factorial, max_error_exponent, upper);
result.up_to(max_error_exponent, upper);
factorial.up_to(max_error_exponent, upper);
x_pow.up_to(max_error_exponent, upper);
}while(cur_term.abs() > max_error);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* EXPONENT FUNCTION USING BINARY EXPONENTIATION AND TAYLOR EXPANSION
* @brief: calculates exponent of a exact_number using binary exponentiation
* When num is sufficiently small, taylor expansion is used
* @param: num: the exact number. whose exponent is to be found
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
exact_number<T> exponent(exact_number<T> num, size_t max_error_exponent, bool upper) {
// if -1 <= num <= 1, i.e. num is sufficiently small, we use taylor series
if (num >= literals::minus_one_exact<T> && num <= literals::one_exact<T>) {
return exponent_taylor(num, max_error_exponent, upper);
}
exact_number<T> half_num = num;
half_num.divide_vector(literals::two_exact<T>, max_error_exponent, upper);
// (e ^ num) is calculated as (e ^ num/2) * (e ^ num/2)
exact_number<T> result;
result = exponent(half_num, max_error_exponent, upper);
result *= result;
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* LOGARITHM(BASE e) FUNCTION USING TAYLOR EXPANSION
* @brief: calculates log(base e) of a exact_number using taylor expansion
* @param: x: the exact number. whose logarithm (ln(x)) is to be found
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
exact_number<T> logarithm_taylor(exact_number<T> x, size_t max_error_exponent, bool upper){
static const exact_number<T> two("2");
exact_number<T> result("0");
exact_number<T> term_number("1");
unsigned int term_number_int = 1;
exact_number<T> cur_term("0");
exact_number<T> x_pow ("1");
exact_number<T> max_error(std::vector<T> {1}, -max_error_exponent, true);
if(x > literals::zero_exact<T> && x < two){
do{
if(term_number_int %2 == 1)
result -= cur_term;
else
result += cur_term;
x_pow = x_pow * (x - literals::one_exact<T>);
cur_term = x_pow;
if (term_number_int % 2 == 1)
cur_term.divide_vector(term_number, max_error_exponent, upper);
else
cur_term.divide_vector(term_number, max_error_exponent, !upper); // as we are subtracting next, !upper is passed
++term_number_int;
term_number = term_number + literals::one_exact<T>;
result.up_to(max_error_exponent, upper);
x_pow.up_to(max_error_exponent, upper);
}while(cur_term.abs() > max_error);
return result;
}
do{
result += cur_term;
x_pow = x_pow * (x - literals::one_exact<T>);
x_pow.divide_vector(x, max_error_exponent, upper);
cur_term = x_pow ;
cur_term.divide_vector(term_number, max_error_exponent, upper);
++term_number_int;
term_number = term_number + literals::one_exact<T>;
result.up_to(max_error_exponent, upper);
x_pow.up_to(max_error_exponent, upper);
}while(cur_term.abs() > max_error);
result = result.up_to(max_error_exponent, upper);
return result;
}
// storing log(2) value, which is computed once for recursive procedure
template<typename T>
exact_number<T> log_two;
template<typename T>
exact_number<T> logarithm_recurse(exact_number<T> x, size_t max_error_exponent, bool upper) {
// if x <= 4, i.e. x is sufficiently small, we use taylor series
if (x <= literals::four_exact<T>) {
return logarithm_taylor(x, max_error_exponent, upper);
}
exact_number<T> half_x = x;
half_x.divide_vector(literals::two_exact<T>, max_error_exponent, upper);
// log(x) is calculated as log(x / 2) + log(2)
exact_number<T> result = logarithm_recurse(half_x, max_error_exponent, upper);
result += log_two<T>;
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* LOGARITHM(BASE e) FUNCTION USING RECURSION AND TAYLOR EXPANSION
* @brief: calculates log(base e) of a exact_number using recursion
* When x is sufficiently small, taylor expansion is used
* @param: x: the exact number. whose logarithm (ln(x)) is to be found
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
exact_number<T> logarithm(exact_number<T> x, size_t max_error_exponent, bool upper) {
// log is only defined for numbers greater than 0
if(x == literals::zero_exact<T> || x.positive == false){
throw logarithm_not_defined_for_non_positive_number();
}
// finding log(2) only once for recursive procedure
log_two<T> = logarithm_taylor(literals::two_exact<T>, max_error_exponent, upper);
exact_number<T> result;
if (x >= literals::one_exact<T>) {
result = logarithm_recurse(x, max_error_exponent, upper);
}
else {
// converting x to > 1
exact_number<T> rev_x = literals::one_exact<T>;
exact_number<T> minus_one = literals::minus_one_exact<T>;
// log(x) = -log(1 / x)
rev_x.divide_vector(x, max_error_exponent, upper);
result = minus_one * logarithm_recurse(rev_x, max_error_exponent, !upper);
}
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* SQUARE ROOT FUNCTION USING NEWTON'S METHOD
* @brief: calculates square root of a exact_number using Newton's Method
* @param: x: the exact number whose square root is to be found
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
exact_number<T> square_root(exact_number<T> x, size_t max_error_exponent, bool upper) {
// sqrt is defined for non negative numbers
if (x.positive == false) {
throw sqrt_not_defined_for_negative_number();
}
if (x == literals::zero_exact<T>) {
return literals::zero_exact<T>;
}
// initial guess using scalar estimate
exact_number<T> result;
if(x.exponent%2==0)
{
if(x.digits[0]>=1)
result=exact_number<T> (std::vector<T> {6}, (x.exponent)/2, true);
else
result=exact_number<T> (std::vector<T> {2}, (x.exponent)/2, true);
}
else
result=exact_number<T>(std::vector<T> {2}, (x.exponent-1)/2, true);
exact_number<T> error;
exact_number<T> max_error(std::vector<T> {1}, -max_error_exponent, true);
static exact_number<T> two("2");
// initial error
error = x - (result * result);
error = error.abs();
error.divide_vector(result, max_error_exponent, upper);
while (error > max_error) {
// result = (result + x / result) / 2, Newton's method
exact_number<T> reverse = x;
reverse.divide_vector(result, max_error_exponent, upper);
result = result + reverse;
result.divide_vector(two, max_error_exponent, upper);
exact_number<T> temp;
temp = error * error;
temp.divide_vector((two * (error + literals::one_exact<T>)), max_error_exponent, upper);
error = temp;
}
result = result.up_to(max_error_exponent, upper);
return result;
}
template<typename T>
struct max_precise_pi
{
// most precise pi calculated
exact_number<T> curr_pi;
// maximum error exponent of pi calculated
size_t max_error_exponent_pi = 0;
// returns pi with the given max_error_exponent
exact_number<T> get(size_t max_error_exponent, bool upper)
{
if (max_error_exponent > max_error_exponent_pi) {
// if we need pi at more precision than current precision, we recalculate pi
curr_pi = boost::real::irrational::pi<T>(max_error_exponent);
max_error_exponent_pi = max_error_exponent;
return curr_pi;
}
else {
// otherwise we use the up_to method on the previous most precise pi
return curr_pi.up_to(max_error_exponent, upper);
}
}
};
template<typename T>
max_precise_pi<T> pi;
/**
* SINE FUNCTION USING TAYLOR EXPANSION
* @brief: calculates sin(x) of a exact_number using taylor expansion
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
exact_number<T> sine_taylor(exact_number<T> x, size_t max_error_exponent, bool upper){
exact_number<T> result("0");
exact_number<T> term_number("0");
unsigned int term_number_int = 0;
exact_number<T> cur_term(x);
exact_number<T> x_pow(x);
exact_number<T> factorial("1");
exact_number<T> tmp;
exact_number<T> x_square = x*x;
exact_number<T> max_error(std::vector<T> {1}, -max_error_exponent, true);
static exact_number<T> two("2");
do{
if(term_number_int % 2 == 0){ // if this term is even
result += cur_term;
}
else
result -= cur_term; // if this term is odd
++term_number_int;
term_number = term_number + literals::one_exact<T>;
x_pow *= x_square; // increasing power by two powers of original x
factorial = factorial * ( two * term_number) * ( (two * term_number) + literals::one_exact<T>); // increasing the values of factorial by two
cur_term = x_pow;
if (term_number_int % 2 == 0) {
cur_term.divide_vector(factorial, max_error_exponent, upper);
}
else {
// as we are subtracting next, !upper is passed
cur_term.divide_vector(factorial, max_error_exponent, upper);
}
result = result.up_to(max_error_exponent, upper);
factorial = factorial.up_to(max_error_exponent, upper);
x_pow = x_pow.up_to(max_error_exponent, upper);
}while(cur_term.abs() > max_error);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* SINE FUNCTION USING ARGUMENT REDUCTION AND TAYLOR EXPANSION
* @brief: calculates sin(x) of a exact_number using argument reduction
* After reduction, taylor expansion is used
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
exact_number<T> sine(exact_number<T> x, size_t max_error_exponent, bool upper) {
// modifying x to lie between 0 and 2PI, using periodic nature of sine
exact_number<T> k = x;
exact_number<T> red_x = x;
static exact_number<T> two("2");
// floor(x / 2PI) times 2PI is not needed
k.divide_vector(two, max_error_exponent, upper);
k.divide_vector(pi<T>.get(max_error_exponent, upper), max_error_exponent, upper);
k.floor();
k = k * two * pi<T>.get(max_error_exponent, !upper);
red_x -= k;
return sine_taylor(red_x, max_error_exponent, upper);
}
/**
* COSINE FUNCTION USING TAYLOR EXPANSION
* @brief: calculates cos(x) of a exact_number using taylor expansion
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
exact_number<T> cosine_taylor(exact_number<T> x, size_t max_error_exponent, bool upper){
exact_number<T> result("1");
exact_number<T> cur_term("0");
exact_number<T> square_x = x*x;
exact_number<T> cur_power("1");
exact_number<T> factorial("1");
static exact_number<T> two("2");
exact_number<T> term_number("0");
exact_number<T> max_error(std::vector<T> {1}, -max_error_exponent, true);
int term_number_int = 0;
do{
if(term_number_int % 2 == 0)
result += cur_term;
else
result -= cur_term;
for(exact_number<T> i = (two * term_number) + literals::one_exact<T> ; i <= two * (term_number + literals::one_exact<T>); i = i + literals::one_exact<T>){
factorial *= i;
}
cur_power *= square_x;
cur_term = cur_power;
++ term_number_int;
if (term_number_int % 2 == 0) {
cur_term.divide_vector(factorial, max_error_exponent, upper);
}
else {
// as we are subtracting next, !upper is passed
cur_term.divide_vector(factorial, max_error_exponent, !upper);
}
term_number = term_number + literals::one_exact<T>;
result.up_to(max_error_exponent, upper);
factorial.up_to(max_error_exponent, upper);
cur_power.up_to(max_error_exponent, upper);
}while(cur_term.abs() > max_error);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* COSINE FUNCTION USING ARGUMENT REDUCTION AND TAYLOR EXPANSION
* @brief: calculates cos(x) of a exact_number using argument reduction
* After reduction, taylor expansion is used
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
exact_number<T> cosine(exact_number<T> x, size_t max_error_exponent, bool upper) {
// modifying x to lie between 0 and 2PI, using periodic nature of cosine
exact_number<T> k = x;
exact_number<T> red_x = x;
static exact_number<T> two("2");
// floor(x / 2PI) times 2PI is not needed
k.divide_vector(two, max_error_exponent, upper);
k.divide_vector(pi<T>.get(max_error_exponent, upper), max_error_exponent, upper);
k.floor();
k = k * two * pi<T>.get(max_error_exponent, !upper);
red_x -= k;
return cosine_taylor(red_x, max_error_exponent, upper);
}
/**
* SINE AND COSINE FUNCTION USING TAYLOR EXPANSION
* @brief: calculates cos(x) and sin(x) of a exact_number using taylor expansion
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @return: a tuple containing sin(x) and cos(x)
* @author: Vikram Singh Chundawat
**/
template<typename T>
std::tuple<exact_number<T>, exact_number<T> > sin_cos_taylor(exact_number<T> x, size_t max_error_exponent, bool upper){
exact_number<T> sin_result("0");
exact_number<T> cos_result("0");
exact_number<T> cur_sin_term = x;
exact_number<T> cur_cos_term("1");
exact_number<T> cur_power = x;
exact_number<T> factorial("1");
static exact_number<T> two("2");
exact_number<T> factorial_number("1");
unsigned int term_number_int = 0;
exact_number<T> max_error(std::vector<T> {1}, -max_error_exponent, true);
do{
if(term_number_int % 2 == 0){
sin_result += cur_sin_term;
cos_result += cur_cos_term;
}
else{
sin_result -= cur_sin_term;
cos_result -= cur_cos_term;
}
++term_number_int;
factorial_number = factorial_number + literals::one_exact<T>;
factorial *= factorial_number;
cur_power *= x;
cur_cos_term = cur_power;
if (term_number_int % 2 == 0) {
cur_cos_term.divide_vector(factorial, max_error_exponent, upper);
}
else {
// as we are subtracting next, !upper is passed
cur_cos_term.divide_vector(factorial, max_error_exponent, !upper);
}
factorial_number = factorial_number + literals::one_exact<T>;
factorial *= factorial_number;
cur_power *= x;
cur_sin_term = cur_power;
if (term_number_int % 2 == 0) {
cur_sin_term.divide_vector(factorial, max_error_exponent, upper);
}
else {
// as we are subtracting next, !upper is passed
cur_sin_term.divide_vector(factorial, max_error_exponent, !upper);
}
}while( (cur_cos_term.abs() > max_error) || (cur_sin_term.abs() > max_error) );
return std::make_tuple(sin_result, cos_result);
}
/**
* SINE AND COSINE FUNCTION USING ARGUMENT REDUCTION AND TAYLOR EXPANSION
* @brief: calculates cos(x) and sin(x) of a exact_number using argument reduction
* After reduction, taylor expansion is used
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @return: a tuple containing sin(x) and cos(x)
* @author: Divyam Singal
**/
template<typename T>
std::tuple<exact_number<T>, exact_number<T> > sin_cos(exact_number<T> x, size_t max_error_exponent, bool upper){
// modifying x to lie between 0 and 2PI, using periodic nature of sin & cosine
exact_number<T> k = x;
exact_number<T> red_x = x;
static exact_number<T> two("2");
// floor(x / 2PI) times 2PI is not needed
k.divide_vector(two, max_error_exponent, upper);
k.divide_vector(pi<T>.get(max_error_exponent, upper), max_error_exponent, upper);
k.floor();
k = k * two * pi<T>.get(max_error_exponent, !upper);
red_x -= k;
return sin_cos_taylor(red_x, max_error_exponent, upper);
}
/**
* TANGENT FUNCTION USING TAYLOR EXPANSION
* @brief: calculates tan(x) of a exact_number using taylor expansion
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
inline exact_number<T> tangent(exact_number<T> x, size_t max_error_exponent, bool upper){
auto [result, cos] = sin_cos(x, max_error_exponent, upper);
result.divide_vector(cos, max_error_exponent, upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* COTANGENT FUNCTION USING TAYLOR EXPANSION
* @brief: calculates cot(x) of a exact_number using taylor expansion
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
inline exact_number<T> cotangent(exact_number<T> x, size_t max_error_exponent, bool upper){
auto [sin, result] = sin_cos(x, max_error_exponent, upper);
result.divide_vector(sin, max_error_exponent, upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* SECANT FUNCTION USING TAYLOR EXPANSION
* @brief: calculates sec(x) of a exact_number using taylor expansion
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
inline exact_number<T> secant(exact_number<T> x, size_t max_error_exponent, bool upper){
exact_number<T> result("1");
exact_number<T> cos = cosine(x, max_error_exponent, upper);
result.divide_vector(cos, max_error_exponent, upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* COSECANT FUNCTION USING TAYLOR EXPANSION
* @brief: calculates cosec(x) of a exact_number using taylor expansion
* @param: x: the exact_number, representing angle in radian
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Vikram Singh Chundawat
**/
template<typename T>
inline exact_number<T> cosecant(exact_number<T> x, size_t max_error_exponent, bool upper){
exact_number<T> result("1");
exact_number<T> sin = sine(x, max_error_exponent, upper);
result.divide_vector(sin, max_error_exponent, upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* INVERSE TANGENT FUNCTION USING TAYLOR EXPANSION
* @brief: calculates tan_inverse(x) of a exact_number using taylor expansion
* @param: x: the exact_number, x ∈ R
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> tan_inverse_taylor(exact_number<T> x, size_t max_error_exponent, bool upper) {
exact_number<T> result("0");
exact_number<T> denominator("1");
unsigned int term_number_int = 0;
exact_number<T> cur_term(x);
exact_number<T> x_pow(x);
exact_number<T> x_square = x*x;
exact_number<T> max_error(std::vector<T> {1}, -max_error_exponent, true);
static exact_number<T> two("2");
do{
if(term_number_int % 2 == 0){ // if this term is even
result += cur_term;
}
else
result -= cur_term; // if this term is odd
++term_number_int;
x_pow *= x_square; // increasing power by two powers of original x
denominator += two;
cur_term = x_pow;
if (term_number_int % 2 == 0) {
cur_term.divide_vector(denominator, max_error_exponent, upper);
}
else {
// as we are subtracting next, !upper is passed
cur_term.divide_vector(denominator, max_error_exponent, !upper);
}
result = result.up_to(max_error_exponent, upper);
x_pow = x_pow.up_to(max_error_exponent, upper);
}while(cur_term.abs() > max_error);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* INVERSE TANGENT FUNCTION USING RECURSION AND TAYLOR EXPANSION
* @brief: calculates tan_inverse(x) of a exact_number using recursion
* When x is sufficiently small, taylor expansion is used
* @param: x: the exact_number, x ∈ R
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> tan_inverse(exact_number<T> x, size_t max_error_exponent, bool upper) {
static exact_number<T> two("2");
exact_number<T> bar("1");
bar.divide_vector(two, max_error_exponent, upper);
exact_number<T> neg_bar = bar;
neg_bar.positive = false;
if (x > neg_bar && x < bar) {
// If -0.5 < x < 0.5, i.e. x is sufficiently small, we use taylor series
return tan_inverse_taylor(x, max_error_exponent, upper);
}
// atan(x) = 2 * atan(x / (1 + sqrt(1 + x^2)))
exact_number<T> red_x = x;
red_x.divide_vector(square_root((x * x) + literals::one_exact<T>, max_error_exponent, upper) + literals::one_exact<T>, max_error_exponent, upper);
exact_number<T> result = tan_inverse(red_x, max_error_exponent, upper);
result = result + result;
result.up_to(max_error_exponent, upper);
return result;
}
/**
* INVERSE COTANGENT FUNCTION USING INVERSE TANGENT FUNCTION
* @brief: calculates cot_inverse(x) of a exact_number using tan_inverse
* @param: x: the exact_number, x ∈ R
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> cot_inverse(exact_number<T> x, size_t max_error_exponent, bool upper) {
if (x >= literals::one_exact<T>) {
// (1 / x) <= 1
// acot(x) = atan(1 / x), i.e. more the x, faster is calculating atan(1 / x)
exact_number<T> reciprocal("1");
reciprocal.divide_vector(x, max_error_exponent, upper);
return tan_inverse(reciprocal, max_error_exponent, upper);
}
else if (x <= literals::minus_one_exact<T>) {
// (1 / x) <= -1
// acot(x) = pi + atan(1 / x), i.e. more negative the x, faster is calculating atan(1 / x)
exact_number<T> reciprocal("1");
reciprocal.divide_vector(x, max_error_exponent, upper);
return tan_inverse(reciprocal, max_error_exponent, upper) + pi<T>.get(max_error_exponent, upper);
}
else {
// -1 < x < 1
// here it is better to calculate atan(x) rather than atan(1 / x)
static exact_number<T> two("2");
// acot(x) = pi/2 - atan(x)
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result.divide_vector(two, max_error_exponent, upper);
result = result - tan_inverse(x, max_error_exponent, !upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
}
/**
* INVERSE SINE FUNCTION USING INVERSE TANGENT FUNCTION
* @brief: calculates sin_inverse(x) of a exact_number using tan_inverse
* @param: x: the exact_number, x ∈ [-1, 1]
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> sin_inverse(exact_number<T> x, size_t max_error_exponent, bool upper) {
// asin(x) is defined for x between -1 and 1 inclusive
if (x < literals::minus_one_exact<T> || x > literals::one_exact<T>) {
throw max_precision_for_inverse_trigonometric_function_error();
}
// special cases when x = -1 or 1, to avoid division by 0
static exact_number<T> two("2");
if (x == literals::one_exact<T>) {
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result.divide_vector(two, max_error_exponent, upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
else if (x == literals::minus_one_exact<T>) {
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result.positive = true;
result.divide_vector(two, max_error_exponent, upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
// asin(x) = atan(x / sqrt(1 - x * x))
static exact_number<T> one("1");
exact_number<T> x_tan = x;
x_tan.divide_vector(square_root(one - x * x, max_error_exponent, !upper), max_error_exponent, upper);
return tan_inverse(x_tan, max_error_exponent, upper);
}
/**
* INVERSE COSINE FUNCTION USING INVERSE SINE FUNCTION
* @brief: calculates cos_inverse(x) of a exact_number using sin_inverse
* @param: x: the exact_number, x ∈ [-1, 1]
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> cos_inverse(exact_number<T> x, size_t max_error_exponent, bool upper) {
// acos(x) is defined for x between -1 and 1 inclusive
if (x < literals::minus_one_exact<T> || x > literals::one_exact<T>) {
throw max_precision_for_inverse_trigonometric_function_error();
}
static exact_number<T> two("2");
// acos(x) = pi/2 - asin(x)
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result.divide_vector(two, max_error_exponent, upper);
result = result - sin_inverse(x, max_error_exponent, !upper);
result = result.up_to(max_error_exponent, upper);
return result;
}
/**
* INVERSE SECANT FUNCTION USING INVERSE COSINE FUNCTION
* @brief: calculates sec_inverse(x) of a exact_number using cos_inverse
* @param: x: the exact_number, x ∈ (-∞, -1] U [1, ∞)
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> sec_inverse(exact_number<T> x, size_t max_error_exponent, bool upper) {
// asec(x) is defined for x <= -1 or x >= 1
if (x > literals::minus_one_exact<T> && x < literals::one_exact<T>) {
throw max_precision_for_inverse_trigonometric_function_error();
}
// asec(x) = acos(1 / x)
exact_number<T> reciprocal("1");
reciprocal.divide_vector(x, max_error_exponent, upper);
return cos_inverse(reciprocal, max_error_exponent, upper);
}
/**
* INVERSE COSECANT FUNCTION USING INVERSE SINE FUNCTION
* @brief: calculates cosec_inverse(x) of a exact_number using sin_inverse
* @param: x: the exact_number, x ∈ (-∞, -1] U [1, ∞)
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> cosec_inverse(exact_number<T> x, size_t max_error_exponent, bool upper) {
// acosec(x) is defined for x <= -1 or x >= 1
if (x > literals::minus_one_exact<T> && x < literals::one_exact<T>) {
throw max_precision_for_inverse_trigonometric_function_error();
}
// asec(x) = asin(1 / x)
exact_number<T> reciprocal("1");
reciprocal.divide_vector(x, max_error_exponent, upper);
return sin_inverse(reciprocal, max_error_exponent, upper);
}
/**
* TWO ARGUMENT INVERSE TANGENT FUNCTION USING INVERSE TANGENT FUNCTION
* @brief: calculates tan2_inverse(y, x) of a exact_number using tan_inverse
* @param: y: the y-coordinate as exact_number
* @param: x: the x-coordinate as exact_number
* @param: max_error_exponent: Absolute Error in the result should be < 1*base^(-max_error_exponent)
* @param: upper: if true: error lies in [0, +epsilon]
* else: error lies in [-epsilon, 0], here epsilon = 1*base^(-max_error_exponent)
* @author: Divyam Singal
**/
template<typename T>
inline exact_number<T> tan2_inverse(exact_number<T> y, exact_number<T> x, size_t max_error_exponent, bool upper) {
static exact_number<T> two("2");
exact_number<T> x_tan;
if (x > literals::zero_exact<T>) {
// x > 0
// atan2(y, x) = atan(y / x)
x_tan = y;
x_tan.divide_vector(x, max_error_exponent, upper);
return tan_inverse(x_tan, max_error_exponent, upper);
}
else if (x < literals::zero_exact<T> && y >= literals::zero_exact<T>) {
// x < 0, y >= 0
// atan2(y, x) = atan(y / x) + PI
x_tan = y;
x_tan.divide_vector(x, max_error_exponent, upper);
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result = result + tan_inverse(x_tan, max_error_exponent, upper);
result.up_to(max_error_exponent, upper);
return result;
}
else if (x < literals::zero_exact<T> && y < literals::zero_exact<T>) {
// x < 0, y < 0
// atan2(y, x) = atan(y / x) - PI
x_tan = y;
x_tan.divide_vector(x, max_error_exponent, upper);
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result.positive = false;
result = result + tan_inverse(x_tan, max_error_exponent, upper);
result.up_to(max_error_exponent, upper);
return result;
}
else if (x == literals::zero_exact<T> && y > literals::zero_exact<T>) {
// x = 0, y > 0
// atan2(y, x) = PI / 2
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result.divide_vector(two, max_error_exponent, upper);
result.up_to(max_error_exponent, upper);
return result;
}
else if (x == literals::zero_exact<T> && y < literals::zero_exact<T>) {
// x = 0, y < 0
// atan2(y, x) = -PI / 2
exact_number<T> result = pi<T>.get(max_error_exponent, upper);
result.positive = false;
result.divide_vector(two, max_error_exponent, upper);
result.up_to(max_error_exponent, upper);
return result;
}
else {
// undefined for x = 0 and y = 0
throw max_precision_for_inverse_trigonometric_function_error();
}
}
}
}
#endif//BOOST_REAL_MATH_HPP