From 8c41b91e576514fa2363406772c9bfaab480892b Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Thu, 9 Jun 2022 09:15:17 +0530 Subject: [PATCH 1/7] cbrt and exp2 functions implemented --- integration_tests/test_math.py | 10 +++++++++- integration_tests/test_math_02.py | 21 ++++++++++++++++++++- src/runtime/math.py | 15 +++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 28a13f91a6..2ad8f494bf 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,4 +1,4 @@ -from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow, +from math import (factorial, isqrt, perm, comb, degrees, radians, exp, exp2, pow, ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc) from ltypes import i32, f64, i64 @@ -58,6 +58,13 @@ def test_exp(): i = exp(2.34) assert abs(i - 10.381236562731843) < eps +def test_exp2(): + i: f64 + i = exp2(2.3) + assert abs(i - 4.924577653379665) < eps + assert exp2(-1) == 0.5, f"assertion error in exp2" + assert exp2(0) == 1, f"assertion error in exp2" + assert exp2(1) == 2, f"assertion error in exp2" def test_pow(): i: f64 @@ -151,6 +158,7 @@ def test_trunc(): def check(): + test_exp2() test_factorial_1() test_comb() test_isqrt() diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 5dc5d4f618..cbdfadef27 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,7 +1,16 @@ -from math import (sin, cos, tan, pi, sqrt, log, log10, log2, erf, erfc, gamma, +from math import (sin, cos, tan, pi, sqrt, cbrt, log, log10, log2, erf, erfc, gamma, lgamma, asin, acos, atan, atan2, asinh, acosh, atanh, tanh, sinh, cosh, hypot, copysign) +from ltypes import i32, f64, i64 + +pi: f64 = 3.141592653589793238462643383279502884197 +e: f64 = 2.718281828459045235360287471352662497757 +tau: f64 = 6.283185307179586 + +eps: f64 +eps = 1e-12 + def test_trig(): eps: f64 = 1e-12 assert abs(sin(0.0)-0) < eps @@ -15,6 +24,15 @@ def test_trig(): assert abs(atan(1.0) - pi/4) < eps assert abs(atan2(1.0, 1.0) - pi/4) < eps + +def test_cbrt(): + assert cbrt(0) == 0 , f"assertion error in cube root function" + assert cbrt(1) == 1 , f"assertion error in cube root function" + assert cbrt(8) == 2 , f"assertion error in cube root function" + assert cbrt(-27) == -3 , f"assertion error in cube root function" + assert cbrt(1.2) == 1.062658569182611 , f"assertion error in cube root function" + + def test_sqrt(): eps: f64 = 1e-12 assert abs(sqrt(2.0) - 1.4142135623730951) < eps @@ -54,6 +72,7 @@ def test_hypot(): assert abs(hypot(6, 6) - 8.48528137423857) < eps def check(): + test_cbrt() test_trig() test_sqrt() test_log() diff --git a/src/runtime/math.py b/src/runtime/math.py index d01edb4fe5..dcbf36fabc 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -226,6 +226,11 @@ def exp(x: f64) -> f64: """ return e**x +def exp2(x: f64) -> f64: + """ + Return `2` raised to the power `x`. + """ + return 2**x def pow(x: f64, y: f64) -> f64: """ @@ -312,8 +317,18 @@ def trunc(x: f32) -> i32: return ceil(x) def sqrt(x: f64) -> f64: + """ + Returns square root of a non negative 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 From 41551aa667dccdb37ba87c1bd1a9441cfea26336 Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Thu, 9 Jun 2022 09:26:41 +0530 Subject: [PATCH 2/7] tests for acos, acosh, asin, asinh, atan, atan2, atanh, cos, cosh written --- integration_tests/test_math_02.py | 70 +++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index cbdfadef27..9cc532c093 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -24,6 +24,66 @@ def test_trig(): assert abs(atan(1.0) - pi/4) < eps assert abs(atan2(1.0, 1.0) - pi/4) < eps +def test_acos(): + assert acos(-1) == pi , f"assertion error in acos" + assert acos(0) == pi/2, f"assertion error in acos" + assert acos(1) == 0, f"assertion error in acos" + +def test_acosh(): + assert acosh(1) == 0 , f"assertion error in acosh" + assert acosh(2) == 1.3169578969248168, f"assertion error in acosh" + assert acosh(5.90) == 2.46, f"assertion error in acosh" + + +def test_asin(): + assert asin(-1) == -pi/2 , f"assertion error in asin" + assert asin(0) == 0, f"assertion error in asin" + assert asin(1) == pi/2, f"assertion error in asin" + +def test_asinh(): + assert asinh(0) == 0 , f"assertion error in asinh" + assert asinh(1) == 0.88137358701954305, f"assertion error in asinh" + assert asinh(-1) == -0.88137358701954305, f"assertion error in asinh" + +def test_atan(): + assert atan(-1) == -pi/4 , f"assertion error in atan" + assert atan(0) == 0, f"assertion error in atan" + assert atan(1) == pi/4, f"assertion error in atan" + +def test_atanh(): + assert atanh(0) == 0 , f"assertion error in atanh" + assert atanh(0.5) == 0.54930614433405489, f"assertion error in atanh" + assert atanh(-0.5) == -0.54930614433405489, f"assertion error in atanh" + +def test_atan2(): + assert atan2(-1,0) == -pi/2 , f"assertion error in atan2" + assert atan2(-1,1) == pi/4, f"assertion error in atan2" + assert atan2(0,1) == 0, f"assertion error in atan2" + assert atan2(1,1) == pi/4 , f"assertion error in atan2" + assert atan2(1,0) == pi/2, f"assertion error in atan2" + + assert atan2(0.,-2.3) == pi, f"assertion error in atan2" + assert atan2(0.,-0.) == pi , f"assertion error in atan2" + + assert atan2(-0.,-2.3) == -pi, f"assertion error in atan2" + assert atan2(-0.,-0.) == -pi , f"assertion error in atan2" + + assert atan2(2.3,-0) == pi/2, f"assertion error in atan2" + assert atan2(2.3,0) == pi/2, f"assertion error in atan2" + + assert atan2(-2.3,-0) == -pi/2, f"assertion error in atan2" + assert atan2(-2.3,0) == -pi/2, f"assertion error in atan2" + +def test_cos(): + assert cos(-pi/2) == 0 , f"assertion error in cos" + assert cos(0) == 1, f"assertion error in cos" + assert cos(pi/2) == 0, f"assertion error in cos" + assert cos(pi) == -1, f"assertion error in cos" + +def test_cosh(): + assert cosh(0) == 1 , f"assertion error in cosh" + assert cosh(2)-2*cosh(1)**2 == -1, f"assertion error in cosh" + def test_cbrt(): assert cbrt(0) == 0 , f"assertion error in cube root function" @@ -72,6 +132,16 @@ def test_hypot(): assert abs(hypot(6, 6) - 8.48528137423857) < eps def check(): + test_acos() + test_acosh() + test_asin() + test_asinh() + test_atan() + test_atan2() + test_atanh() + test_cos() + test_cosh() + test_cbrt() test_trig() test_sqrt() From d245c5527252a65263a049b1f72f07762144cc3f Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Thu, 9 Jun 2022 09:39:30 +0530 Subject: [PATCH 3/7] Revert "tests for acos, acosh, asin, asinh, atan, atan2, atanh, cos, cosh written" revert commit This reverts commit 41551aa667dccdb37ba87c1bd1a9441cfea26336. --- integration_tests/test_math_02.py | 70 ------------------------------- 1 file changed, 70 deletions(-) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 9cc532c093..cbdfadef27 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -24,66 +24,6 @@ def test_trig(): assert abs(atan(1.0) - pi/4) < eps assert abs(atan2(1.0, 1.0) - pi/4) < eps -def test_acos(): - assert acos(-1) == pi , f"assertion error in acos" - assert acos(0) == pi/2, f"assertion error in acos" - assert acos(1) == 0, f"assertion error in acos" - -def test_acosh(): - assert acosh(1) == 0 , f"assertion error in acosh" - assert acosh(2) == 1.3169578969248168, f"assertion error in acosh" - assert acosh(5.90) == 2.46, f"assertion error in acosh" - - -def test_asin(): - assert asin(-1) == -pi/2 , f"assertion error in asin" - assert asin(0) == 0, f"assertion error in asin" - assert asin(1) == pi/2, f"assertion error in asin" - -def test_asinh(): - assert asinh(0) == 0 , f"assertion error in asinh" - assert asinh(1) == 0.88137358701954305, f"assertion error in asinh" - assert asinh(-1) == -0.88137358701954305, f"assertion error in asinh" - -def test_atan(): - assert atan(-1) == -pi/4 , f"assertion error in atan" - assert atan(0) == 0, f"assertion error in atan" - assert atan(1) == pi/4, f"assertion error in atan" - -def test_atanh(): - assert atanh(0) == 0 , f"assertion error in atanh" - assert atanh(0.5) == 0.54930614433405489, f"assertion error in atanh" - assert atanh(-0.5) == -0.54930614433405489, f"assertion error in atanh" - -def test_atan2(): - assert atan2(-1,0) == -pi/2 , f"assertion error in atan2" - assert atan2(-1,1) == pi/4, f"assertion error in atan2" - assert atan2(0,1) == 0, f"assertion error in atan2" - assert atan2(1,1) == pi/4 , f"assertion error in atan2" - assert atan2(1,0) == pi/2, f"assertion error in atan2" - - assert atan2(0.,-2.3) == pi, f"assertion error in atan2" - assert atan2(0.,-0.) == pi , f"assertion error in atan2" - - assert atan2(-0.,-2.3) == -pi, f"assertion error in atan2" - assert atan2(-0.,-0.) == -pi , f"assertion error in atan2" - - assert atan2(2.3,-0) == pi/2, f"assertion error in atan2" - assert atan2(2.3,0) == pi/2, f"assertion error in atan2" - - assert atan2(-2.3,-0) == -pi/2, f"assertion error in atan2" - assert atan2(-2.3,0) == -pi/2, f"assertion error in atan2" - -def test_cos(): - assert cos(-pi/2) == 0 , f"assertion error in cos" - assert cos(0) == 1, f"assertion error in cos" - assert cos(pi/2) == 0, f"assertion error in cos" - assert cos(pi) == -1, f"assertion error in cos" - -def test_cosh(): - assert cosh(0) == 1 , f"assertion error in cosh" - assert cosh(2)-2*cosh(1)**2 == -1, f"assertion error in cosh" - def test_cbrt(): assert cbrt(0) == 0 , f"assertion error in cube root function" @@ -132,16 +72,6 @@ def test_hypot(): assert abs(hypot(6, 6) - 8.48528137423857) < eps def check(): - test_acos() - test_acosh() - test_asin() - test_asinh() - test_atan() - test_atan2() - test_atanh() - test_cos() - test_cosh() - test_cbrt() test_trig() test_sqrt() From 26b85a14f32a85a913e37c6e0fdaaf37c69ba4f1 Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Thu, 9 Jun 2022 09:43:22 +0530 Subject: [PATCH 4/7] exp2 fixed --- integration_tests/test_math.py | 1 - 1 file changed, 1 deletion(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 2ad8f494bf..0b25a88bb6 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -62,7 +62,6 @@ def test_exp2(): i: f64 i = exp2(2.3) assert abs(i - 4.924577653379665) < eps - assert exp2(-1) == 0.5, f"assertion error in exp2" assert exp2(0) == 1, f"assertion error in exp2" assert exp2(1) == 2, f"assertion error in exp2" From 5307d98f5aae8cc4d4ffacd8bb2a5f3ccddefadc Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Thu, 9 Jun 2022 10:35:42 +0530 Subject: [PATCH 5/7] exp2 fixing 2.0 --- integration_tests/test_math.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 0b25a88bb6..e4c6c8cb3e 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -62,8 +62,8 @@ def test_exp2(): i: f64 i = exp2(2.3) assert abs(i - 4.924577653379665) < eps - assert exp2(0) == 1, f"assertion error in exp2" - assert exp2(1) == 2, f"assertion error in exp2" + assert exp2(0) == 1.0, f"assertion error in exp2" + # assert exp2(1) == 2, f"assertion error in exp2" def test_pow(): i: f64 From 139b2cfe34a0819e72d4a56e41bb12c957d2a93a Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Thu, 9 Jun 2022 11:04:54 +0530 Subject: [PATCH 6/7] exp2 fixing 3.0 --- integration_tests/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index e4c6c8cb3e..908baa7c3e 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -62,7 +62,7 @@ def test_exp2(): i: f64 i = exp2(2.3) assert abs(i - 4.924577653379665) < eps - assert exp2(0) == 1.0, f"assertion error in exp2" + # assert exp2(0) == 1.0, f"assertion error in exp2" # assert exp2(1) == 2, f"assertion error in exp2" def test_pow(): From 27e36fa0564a43cd00c6d541ee661dc15b2eb21e Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Sat, 11 Jun 2022 01:57:39 +0530 Subject: [PATCH 7/7] cbrt and exp2 immplemented --- integration_tests/test_math.py | 5 ++++- integration_tests/test_math_02.py | 10 ++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 908baa7c3e..f2258bbb51 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -62,7 +62,10 @@ def test_exp2(): i: f64 i = exp2(2.3) assert abs(i - 4.924577653379665) < eps - # assert exp2(0) == 1.0, f"assertion error in exp2" + + # p: f64 + # p = exp2(0.0) + # assert exp2(p) == 1.0, f"assertion error in exp2" # assert exp2(1) == 2, f"assertion error in exp2" def test_pow(): diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index cbdfadef27..9fa8edb484 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -26,12 +26,10 @@ def test_trig(): def test_cbrt(): - assert cbrt(0) == 0 , f"assertion error in cube root function" - assert cbrt(1) == 1 , f"assertion error in cube root function" - assert cbrt(8) == 2 , f"assertion error in cube root function" - assert cbrt(-27) == -3 , f"assertion error in cube root function" - assert cbrt(1.2) == 1.062658569182611 , f"assertion error in cube root function" - + eps: f64 = 1e-12 + assert abs(cbrt(8.0) - 2) < eps + assert abs(cbrt(1.0) - 1) < eps + assert abs(cbrt(-27.0) + 3) < eps def test_sqrt(): eps: f64 = 1e-12