From 5e79b7be6104ebc69482df27487e610a69e07bbd Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 13:49:53 +0530 Subject: [PATCH 1/8] add log math function --- src/runtime/math.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/runtime/math.py b/src/runtime/math.py index 9cda71a852..101bafa40a 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -163,3 +163,20 @@ def _lfortran_dtan(x: f64) -> f64: 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) From 376ed8c3a958b74841a663b3205646c60f006917 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 13:50:07 +0530 Subject: [PATCH 2/8] add log tests --- integration_tests/test_math_02.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 9a0f3959a6..365122555a 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,4 +1,4 @@ -from math import sin, cos, tan, pi, sqrt +from math import sin, cos, tan, pi, sqrt, log, log10, log2 def test_trig(): # TODO: importing from `math` doesn't work here yet: @@ -16,5 +16,13 @@ def test_sqrt(): assert abs(sqrt(2.0) - 1.4142135623730951) < eps assert abs(sqrt(9.0) - 3) < eps +def test_log(): + eps: f64 = 1e-12 + assert abs(log(1.0) - 0) < eps + assert abs(log(2.718281828459) - 1) < eps + assert abs(log2(2.0) - 1) < eps + assert abs(log10(10.0) - 1) < eps + test_trig() test_sqrt() +test_log() From 95bdba50946e5a3f4473f2d902a0346d4d2d3382 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 14:01:33 +0530 Subject: [PATCH 3/8] add special math functions --- src/runtime/math.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/runtime/math.py b/src/runtime/math.py index 101bafa40a..185ae40b7e 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -180,3 +180,31 @@ def log10(x: f64) -> f64: 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) From 986e140e25256d001d46c0be48b69bd931c68558 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 14:01:44 +0530 Subject: [PATCH 4/8] add test for special math functions --- integration_tests/test_math_02.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 365122555a..e01da4332d 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,4 +1,5 @@ -from math import sin, cos, tan, pi, sqrt, log, log10, log2 +from math import (sin, cos, tan, pi, sqrt, log, log10, log2, erf, erfc, gamma, + lgamma) def test_trig(): # TODO: importing from `math` doesn't work here yet: @@ -23,6 +24,16 @@ def test_log(): assert abs(log2(2.0) - 1) < eps assert abs(log10(10.0) - 1) < eps +def test_special(): + eps: f64 = 1e-12 + assert abs(erf(2.0) - 0.9953222650189527) < eps + assert abs(erfc(2.0) - 0.0046777349810472645) < eps + assert abs(erf(2.0) + erfc(2.0) - 1) < eps + assert abs(gamma(5.0) - 24) < eps + assert abs(lgamma(5.0) - log(gamma(5.0))) < eps + + test_trig() test_sqrt() test_log() +test_special() From a7d5599e7f4f121ad60f16e2618c22c531c3c8f7 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 14:12:34 +0530 Subject: [PATCH 5/8] add remaining trigonometry functions --- src/runtime/math.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/runtime/math.py b/src/runtime/math.py index 185ae40b7e..41e18591e9 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -208,3 +208,31 @@ def _lfortran_dlog_gamma(x: f64) -> f64: 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) From 291d018d494c8efcbe2312cb6b760189c1f55dec Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 14:12:49 +0530 Subject: [PATCH 6/8] add tests for trigonometry --- integration_tests/test_math_02.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index e01da4332d..cad9b6e282 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,5 +1,5 @@ from math import (sin, cos, tan, pi, sqrt, log, log10, log2, erf, erfc, gamma, - lgamma) + lgamma, asin, acos, atan, atan2) def test_trig(): # TODO: importing from `math` doesn't work here yet: @@ -11,6 +11,10 @@ def test_trig(): assert abs(cos(pi/2)-0) < eps assert abs(tan(0.0)-0) < eps assert abs(tan(pi/4)-1) < eps + assert abs(asin(1.0) - pi/2) < eps + assert abs(acos(1.0) - 0) < eps + assert abs(atan(1.0) - pi/4) < eps + assert abs(atan2(1.0, 1.0) - pi/4) < eps def test_sqrt(): eps: f64 = 1e-12 From 3238033db202c2b04f129e380c0a6f5124d47fe1 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 14:29:11 +0530 Subject: [PATCH 7/8] add hyperbolic function --- src/runtime/math.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/runtime/math.py b/src/runtime/math.py index 41e18591e9..fcd594bf68 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -236,3 +236,45 @@ def _lfortran_datan2(y: f64, x: f64) -> f64: 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) From a3273d6c6fec3c494e9187a6587469c6ba7e3829 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 28 Feb 2022 14:29:28 +0530 Subject: [PATCH 8/8] add test for hyperbolic functions --- integration_tests/test_math_02.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index cad9b6e282..2b63a6ac0d 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,5 +1,6 @@ from math import (sin, cos, tan, pi, sqrt, log, log10, log2, erf, erfc, gamma, - lgamma, asin, acos, atan, atan2) + lgamma, asin, acos, atan, atan2, asinh, acosh, atanh, + tanh, sinh, cosh) def test_trig(): # TODO: importing from `math` doesn't work here yet: @@ -36,6 +37,14 @@ def test_special(): assert abs(gamma(5.0) - 24) < eps assert abs(lgamma(5.0) - log(gamma(5.0))) < eps +def test_hyperbolic(): + eps: f64 = 1e-12 + assert abs(sinh(1.0) - 1.1752011936438014) < eps + assert abs(cosh(1.0) - 1.5430806348152437) < eps + assert abs(tanh(1.0) - 0.7615941559557649) < eps + assert abs(asinh(1.0) - 0.881373587019543) < eps + assert abs(cosh(1.0) - 0) < eps + assert abs(tanh(0.0) - 0) < eps test_trig() test_sqrt()