forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.py
841 lines (697 loc) · 15.7 KB
/
math.py
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
from lpython import i8, i16, i32, f32, i64, f64, ccall, overload
pi: f64 = 3.141592653589793238462643383279502884197
e: f64 = 2.718281828459045235360287471352662497757
tau: f64 = 6.283185307179586
@overload
def modf(x: f64) -> tuple[f64, f64]:
"""
Return fractional and integral parts of `x` as a pair.
Both results carry the sign of x and are floats.
"""
return (x - f64(int(x)), float(int(x)))
def factorial(n: i32) -> i64:
"""Computes the factorial of `n`."""
MAX_LOOKUP_VALUE: i32 = 20
FACTORIAL_LOOKUP_TABLE: list[i64] = [
i64(1),
i64(1),
i64(2),
i64(6),
i64(24),
i64(120),
i64(720),
i64(5040),
i64(40320),
i64(362880),
i64(3628800),
i64(39916800),
i64(479001600),
i64(6227020800),
i64(87178291200),
i64(1307674368000),
i64(20922789888000),
i64(355687428096000),
i64(6402373705728000),
i64(121645100408832000),
i64(2432902008176640000),
]
if n < 0:
# Exceptions are not implemented currently
# raise ValueError("factorial() not defined for negative values")
assert 1 == 0, "factorial() not defined for negative values."
elif n < MAX_LOOKUP_VALUE:
return FACTORIAL_LOOKUP_TABLE[n]
else:
f: list[i32] = [0] * 4300
f[0] = 0
f[1] = 0
f[2] = 0
f[3] = 0
f[4] = 4
f[5] = 6
f[6] = 6
f[7] = 7
f[8] = 1
f[9] = 8
f[10] = 0
f[11] = 0
f[12] = 2
f[13] = 0
f[14] = 9
f[15] = 2
f[16] = 3
f[17] = 4
f[18] = 2
f_size: i32 = 19
i: i32 = 21
while i <= n:
index: i32 = 0
carry: i32 = 0
while index < f_size:
product: i32 = f[index] * i + carry
f[index] = product % 10
carry = product // 10
index += 1
while carry > 0:
f[f_size] = carry % 10
carry = carry // 10
f_size += 1
i += 1
result: str = ""
idx: i32
for idx in range(f_size - 1, -1, -1):
result += str(f[idx])
return i64(0)
@overload
def factorial(n: i64) -> i64:
"""Computes the factorial of `n`."""
MAX_LOOKUP_VALUE: i64 = i64(20)
FACTORIAL_LOOKUP_TABLE: list[i64] = [
i64(1),
i64(1),
i64(2),
i64(6),
i64(24),
i64(120),
i64(720),
i64(5040),
i64(40320),
i64(362880),
i64(3628800),
i64(39916800),
i64(479001600),
i64(6227020800),
i64(87178291200),
i64(1307674368000),
i64(20922789888000),
i64(355687428096000),
i64(6402373705728000),
i64(121645100408832000),
i64(2432902008176640000),
]
if n < i64(0):
# Exceptions are not implemented currently
# raise ValueError("factorial() not defined for negative values")
assert 1 == 0, "factorial() not defined for negative values."
elif n < MAX_LOOKUP_VALUE:
return FACTORIAL_LOOKUP_TABLE[n]
else:
f: list[i32] = [0] * 4300
f[0] = 0
f[1] = 0
f[2] = 0
f[3] = 0
f[4] = 4
f[5] = 6
f[6] = 6
f[7] = 7
f[8] = 1
f[9] = 8
f[10] = 0
f[11] = 0
f[12] = 2
f[13] = 0
f[14] = 9
f[15] = 2
f[16] = 3
f[17] = 4
f[18] = 2
f_size: i32 = 19
i: i32 = 21
while i64(i) <= n:
index: i32 = 0
carry: i32 = 0
while index < f_size:
product: i32 = f[index] * i + carry
f[index] = product % 10
carry = product // 10
index += 1
while carry > 0:
f[f_size] = carry % 10
carry = carry // 10
f_size += 1
i += 1
result: str = ""
idx: i32
for idx in range(f_size - 1, -1, -1):
result += str(f[idx])
return i64(0)
@overload
def floor(x: i32) -> i32:
return x
@overload
def floor(x: i64) -> i64:
return x
@overload
def floor(x: f64) -> i64:
r: i64
r = int(x)
if x >= f64(0) or x == f64(r):
return r
return r - i64(1)
@overload
def floor(x: f32) -> i32:
r: i32
r = i32(x)
if x >= f32(0) or x == f32(r):
return r
return r - 1
@overload
def ceil(x: i32) -> i32:
return x
@overload
def ceil(x: i64) -> i64:
return x
@overload
def ceil(x: f64) -> i64:
r: i64
r = int(x)
if x <= f64(0) or f64(r) == x:
return r
return r + i64(1)
@overload
def ceil(x: f32) -> i32:
r: i32
r = i32(x)
if x <= f32(0) or f32(r) == x:
return r
return r + 1
# fsum
# supported data types: i32, i64, f32, f64
@overload
def fsum(arr: list[i32]) -> f64:
"""
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
i: i32
for i in range(len(arr)):
sum += float(arr[i])
return sum
@overload
def fsum(arr: list[i64]) -> f64:
"""
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
i: i32
for i in range(len(arr)):
sum += float(arr[i])
return sum
@overload
def fsum(arr: list[f32]) -> f64:
"""
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
i: i32
for i in range(len(arr)):
sum += float(arr[i])
return sum
@overload
def fsum(arr: list[f64]) -> f64:
"""
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
i: i32
for i in range(len(arr)):
sum += arr[i]
return sum
# prod
# supported data types: i32, i64, f32, f64
@overload
def prod(arr: list[i32]) -> f64:
"""
Return the product of the elements of `arr`.
"""
result: f64
result = 1.0
i: i32
for i in range(len(arr)):
result *= float(arr[i])
return result
@overload
def prod(arr: list[i64]) -> f64:
"""
Return the product of the elements of `arr`.
"""
result: f64
result = 1.0
i: i32
for i in range(len(arr)):
result *= float(arr[i])
return result
@overload
def prod(arr: list[f32]) -> f64:
"""
Return the product of the elements of `arr`.
"""
result: f64
result = 1.0
i: i32
for i in range(len(arr)):
result *= float(arr[i])
return result
@overload
def prod(arr: list[f64]) -> f64:
"""
Return the product of the elements of `arr`.
"""
result: f64
result = 1.0
i: i32
for i in range(len(arr)):
result *= arr[i]
return result
def dist(x: list[f64], y: list[f64]) -> f64:
"""
Return euclidean distance between `x` and `y` points.
"""
if len(x) != len(y):
raise ValueError("Length of lists should be same")
res: f64
res = 0.0
i: i32
for i in range(len(x)):
res += (x[i] - y[i]) * (x[i] - y[i])
return res**0.5
def comb(n: i32, k: i32) -> i32:
"""
Computes the result of `nCk`, i.e, the number of ways to choose `k`
items from `n` items without repetition and without order.
"""
if n < k or n < 0:
return 0
return i32(factorial(n)//(factorial(k)*factorial(n-k)))
def perm(n: i32, k: i32) -> i32:
"""
Computes the result of `nPk`, i.e, the number of ways to choose `k` items
from `n` items without repetition and with order.
"""
if n < k or n < 0:
return 0
return i32(factorial(n)//factorial(n-k))
def isqrt(n: i32) -> i32:
"""
Computes the integer square root of the nonnegative integer `n`.
"""
if n < 0:
raise ValueError('`n` should be nonnegative')
low: i32
mid: i32
high: i32
low = 0
high = n+1
while low + 1 < high:
mid = i32((low + high)//2)
if mid*mid <= n:
low = mid
else:
high = mid
return low
# degrees
# supported data types: i8, i16, i32, i64, f32, f64
@overload
def degrees(x: i8) -> f64:
"""
Convert angle `x` from radians to degrees.
"""
return f64(x) * 180.0 / pi
@overload
def degrees(x: i16) -> f64:
"""
Convert angle `x` from radians to degrees.
"""
return f64(x) * 180.0 / pi
@overload
def degrees(x: i32) -> f64:
"""
Convert angle `x` from radians to degrees.
"""
return f64(x) * 180.0 / pi
@overload
def degrees(x: i64) -> f64:
"""
Convert angle `x` from radians to degrees.
"""
return f64(x) * 180.0 / pi
@overload
def degrees(x: f32) -> f64:
"""
Convert angle `x` from radians to degrees.
"""
return f64(x) * 180.0 / pi
@overload
def degrees(x: f64) -> f64:
"""
Convert angle `x` from radians to degrees.
"""
return x * 180.0 / pi
# radians
# supported data types: i8, i16, i32, i64, f32, f64
@overload
def radians(x: i8) -> f64:
"""
Convert angle `x` from degrees to radians.
"""
return f64(x) * pi / 180.0
@overload
def radians(x: i16) -> f64:
"""
Convert angle `x` from degrees to radians.
"""
return f64(x) * pi / 180.0
@overload
def radians(x: i32) -> f64:
"""
Convert angle `x` from degrees to radians.
"""
return f64(x) * pi / 180.0
@overload
def radians(x: i64) -> f64:
"""
Convert angle `x` from degrees to radians.
"""
return f64(x) * pi / 180.0
@overload
def radians(x: f32) -> f64:
"""
Convert angle `x` from degrees to radians.
"""
return f64(x) * pi / 180.0
@overload
def radians(x: f64) -> f64:
"""
Convert angle `x` from degrees to radians.
"""
return x * pi / 180.0
# fabs
# supported data types: i32, i64, f32, f64
@overload
def fabs(x: f32) -> f32:
"""
Return the absolute value of `x`.
"""
if x < f32(0.0):
return -x
return x
@overload
def fabs(x: f64) -> f64:
"""
Return the absolute value of `x`.
"""
if x < 0.0:
return -x
return x
@overload
def fabs(x: i64) -> f64:
"""
Return the absolute value of `x`.
"""
if f64(x) < 0.0:
return -float(x)
return float(x)
@overload
def fabs(x: i32) -> f64:
"""
Return the absolute value of `x`.
"""
if f64(x) < 0.0:
return -float(x)
return float(x)
@overload
def fabs(x: i16) -> f64:
"""
Return the absolute value of `x`.
"""
if f64(x) < 0.0:
return -float(x)
return float(x)
@overload
def fabs(x: i8) -> f64:
"""
Return the absolute value of `x`.
"""
if f64(x) < 0.0:
return -float(x)
return float(x)
# pow
# supported data types: i32, i64, f32, f64
@overload
def pow(x: f64, y: f64) -> f64:
"""
Return `x` raised to the power `y`.
"""
if y < 0.0:
raise ValueError('y should be nonnegative')
result: f64
result = x**y
return result
@overload
def pow(x: i64, y: i64) -> i64:
"""
Return `x` raised to the power `y`.
"""
if y < i64(0):
raise ValueError('y should be nonnegative')
return i64(x**y)
@overload
def pow(x: f32, y: f32) -> f64:
"""
Return `x` raised to the power `y`.
"""
if y < f32(0):
raise ValueError('y should be nonnegative')
return f64(x**y)
@overload
def pow(x: i32, y: i32) -> i32:
"""
Return `x` raised to the power `y`.
"""
if y < 0:
raise ValueError('y should be nonnegative')
result: i32
result = x**y
return result
@overload
def ldexp(x: f64, i: i32) -> f64:
result: f64
result = x * f64(2**i)
return result
def mod(a: i32, b: i32) -> i32:
"""
Returns a%b
"""
return a - i32(a//b)*b
def gcd(a: i32, b: i32) -> i32:
"""
Returns greatest common divisor of `a` and `b`
"""
temp: i32
a_: i32
b_: i32
a_ = a
b_ = b
if a_ < 0:
a_ = -a_
if b_ < 0:
b_ = -b_
while b_ != 0:
a_ = mod(a_, b_)
temp = a_
a_ = b_
b_ = temp
return a_
def lcm(a: i32, b: i32) -> i32:
"""
Returns least common multiple of `a` and `b`
"""
a_: i32
b_: i32
a_ = a
b_ = b
if a_ < 0:
a_ = -a_
if b_ < 0:
b_ = -b_
if a_*b_ == 0:
return 0
return i32((a_*b_)//gcd(a_, b_))
def copysign(x: f64, y: f64) -> f64:
"""
Return `x` with the sign of `y`.
"""
if y > 0.0 or (y == 0.0 and atan2(y, -1.0) > 0.0):
return fabs(x)
else:
return -fabs(x)
def hypot(x: i32, y: i32) -> f64:
"""
Returns the hypotenuse of the right triangle with sides `x` and `y`.
"""
return sqrt(f64(1.0)*f64(x**2 + y**2))
@overload
def trunc(x: f64) -> i64:
"""
Return x with the fractional part removed, leaving the integer part.
"""
if x > f64(0):
return floor(x)
else:
return ceil(x)
@overload
def trunc(x: f32) -> i32:
"""
Return x with the fractional part removed, leaving the integer part.
"""
if x > f32(0):
return floor(x)
else:
return ceil(x)
def sqrt(x: f64) -> f64:
"""
Returns square root of a number x
"""
return x**(1/2)
def cbrt(x: f64) -> f64:
"""
Returns cube root of a number x
"""
return x**(1/3)
@ccall
def _lfortran_dsin(x: f64) -> f64:
pass
def sin(x: f64) -> f64:
return _lfortran_dsin(x)
@ccall
def _lfortran_dcos(x: f64) -> f64:
pass
def cos(x: f64) -> f64:
return _lfortran_dcos(x)
@ccall
def _lfortran_dtan(x: f64) -> f64:
pass
def tan(x: f64) -> f64:
return _lfortran_dtan(x)
@ccall
def _lfortran_dlog(x: f64) -> f64:
pass
def log(x: f64) -> f64:
return _lfortran_dlog(x)
@ccall
def _lfortran_dlog10(x: f64) -> f64:
pass
def log10(x: f64) -> f64:
return _lfortran_dlog10(x)
def log2(x: f64) -> f64:
return _lfortran_dlog(x)/_lfortran_dlog(2.0)
@ccall
def _lfortran_derf(x: f64) -> f64:
pass
def erf(x: f64) -> f64:
return _lfortran_derf(x)
@ccall
def _lfortran_derfc(x: f64) -> f64:
pass
def erfc(x: f64) -> f64:
return _lfortran_derfc(x)
@ccall
def _lfortran_dgamma(x: f64) -> f64:
pass
def gamma(x: f64) -> f64:
return _lfortran_dgamma(x)
@ccall
def _lfortran_dlog_gamma(x: f64) -> f64:
pass
def lgamma(x: f64) -> f64:
return _lfortran_dlog_gamma(x)
@ccall
def _lfortran_dasin(x: f64) -> f64:
pass
def asin(x: f64) -> f64:
return _lfortran_dasin(x)
@ccall
def _lfortran_dacos(x: f64) -> f64:
pass
def acos(x: f64) -> f64:
return _lfortran_dacos(x)
@ccall
def _lfortran_datan(x: f64) -> f64:
pass
def atan(x: f64) -> f64:
return _lfortran_datan(x)
@ccall
def _lfortran_datan2(y: f64, x: f64) -> f64:
pass
def atan2(y: f64, x: f64) -> f64:
return _lfortran_datan2(y, x)
@ccall
def _lfortran_dsinh(x: f64) -> f64:
pass
def sinh(x: f64) -> f64:
return _lfortran_dsinh(x)
@ccall
def _lfortran_dcosh(x: f64) -> f64:
pass
def cosh(x: f64) -> f64:
return _lfortran_dcosh(x)
@ccall
def _lfortran_dtanh(x: f64) -> f64:
pass
def tanh(x: f64) -> f64:
return _lfortran_dtanh(x)
@ccall
def _lfortran_dasinh(x: f64) -> f64:
pass
def asinh(x: f64) -> f64:
return _lfortran_dasinh(x)
@ccall
def _lfortran_dacosh(x: f64) -> f64:
pass
def acosh(x: f64) -> f64:
return _lfortran_dacosh(x)
@ccall
def _lfortran_datanh(x: f64) -> f64:
pass
def atanh(x: f64) -> f64:
return _lfortran_datanh(x)
def log1p(x: f64) -> f64:
return log(1.0 + x)
def fmod(x: f64, y: f64) -> f64:
if y == 0.0:
raise ValueError('math domain error')
return _lfortran_dfmod(x, y)
@ccall
def _lfortran_dfmod(x: f64, y: f64) -> f64:
pass
def remainder(x: f64, y: f64) -> f64:
q: i64
q = int(x/y)
if x - y*f64(q) > y*f64(q + i64(1)) - x:
return x - y*f64(q + i64(1))
return x - y*f64(q)