From 1c0813a00ca6d684c3553eeb9b9ead6de8b4833c Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 15:06:02 +0530 Subject: [PATCH 1/9] add cmath module --- src/runtime/cmath.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/runtime/cmath.py diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py new file mode 100644 index 0000000000..e1baafeddc --- /dev/null +++ b/src/runtime/cmath.py @@ -0,0 +1,29 @@ +from ltypes import c64, ccall, f64 + +pi: f64 = 3.141592653589793238462643383279502884197 +e: f64 = 2.718281828459045235360287471352662497757 +tau: f64 = 6.283185307179586 + + +@ccall +def _lfortran_zexp(x: c64) -> c64: + pass + +def exp(x: c64) -> c64: + return _lfortran_zexp(x) + + +@ccall +def _lfortran_zlog(x: c64) -> c64: + pass + +def log(x: c64) -> c64: + return _lfortran_zlog(x) + + +@ccall +def _lfortran_zsqrt(x: c64) -> c64: + pass + +def sqrt(x: c64) -> c64: + return _lfortran_zsqrt(x) From ec1931210ebe3fcd33795cad2e67515494d1d7cf Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 15:08:53 +0530 Subject: [PATCH 2/9] add tests for power and log functions --- integration_tests/run_tests.py | 1 + integration_tests/test_cmath.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 integration_tests/test_cmath.py diff --git a/integration_tests/run_tests.py b/integration_tests/run_tests.py index 15ddf581ca..5db5b37f3c 100755 --- a/integration_tests/run_tests.py +++ b/integration_tests/run_tests.py @@ -32,6 +32,7 @@ "test_math_02.py", "test_c_interop_01.py", "test_generics_01.py", + "test_cmath.py" ] # CPython tests only diff --git a/integration_tests/test_cmath.py b/integration_tests/test_cmath.py new file mode 100644 index 0000000000..813d99ab81 --- /dev/null +++ b/integration_tests/test_cmath.py @@ -0,0 +1,12 @@ +from cmath import exp, log, sqrt +from ltypes import c64 + +def test_power_logarithmic(): + x: c64 + y: c64 + x = complex(3, 3) + y = exp(x) + y = log(x) + y = sqrt(x) + +test_power_logarithmic() From 3a23adff73f9299cf34303f2a0bff9b082a342c1 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 15:45:37 +0530 Subject: [PATCH 3/9] add trigonometric functions --- src/runtime/cmath.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py index e1baafeddc..263b9b9779 100644 --- a/src/runtime/cmath.py +++ b/src/runtime/cmath.py @@ -27,3 +27,51 @@ def _lfortran_zsqrt(x: c64) -> c64: def sqrt(x: c64) -> c64: return _lfortran_zsqrt(x) + + +@ccall +def _lfortran_zacos(x: c64) -> c64: + pass + +def acos(x: c64) -> c64: + return _lfortran_zacos(x) + + +@ccall +def _lfortran_zasin(x: c64) -> c64: + pass + +def asin(x: c64) -> c64: + return _lfortran_zasin(x) + + +@ccall +def _lfortran_zatan(x: c64) -> c64: + pass + +def atan(x: c64) -> c64: + return _lfortran_zatan(x) + + +@ccall +def _lfortran_zcos(x: c64) -> c64: + pass + +def cos(x: c64) -> c64: + return _lfortran_zcos(x) + + +@ccall +def _lfortran_zsin(x: c64) -> c64: + pass + +def sin(x: c64) -> c64: + return _lfortran_zsin(x) + + +@ccall +def _lfortran_ztan(x: c64) -> c64: + pass + +def tan(x: c64) -> c64: + return _lfortran_ztan(x) From e7124ea353262b05147f37f4aaef0ce0b623b50a Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 15:45:47 +0530 Subject: [PATCH 4/9] add tests for trigonometric functions --- integration_tests/test_cmath.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_cmath.py b/integration_tests/test_cmath.py index 813d99ab81..5b99efafbc 100644 --- a/integration_tests/test_cmath.py +++ b/integration_tests/test_cmath.py @@ -1,4 +1,4 @@ -from cmath import exp, log, sqrt +from cmath import exp, log, sqrt, acos, asin, atan, cos, sin, tan from ltypes import c64 def test_power_logarithmic(): @@ -9,4 +9,18 @@ def test_power_logarithmic(): y = log(x) y = sqrt(x) + +def test_trigonometric(): + x: c64 + y: c64 + x = complex(3, 3) + y = acos(x) + y = asin(x) + y = atan(x) + y = cos(x) + y = sin(x) + y = tan(x) + + test_power_logarithmic() +test_trigonometric() From 6a2622cb4f0236dace1e3f4ce956d13dcf66d35b Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 15:53:33 +0530 Subject: [PATCH 5/9] add support for hyperbolic functions --- src/runtime/cmath.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py index 263b9b9779..41a31debf1 100644 --- a/src/runtime/cmath.py +++ b/src/runtime/cmath.py @@ -75,3 +75,51 @@ def _lfortran_ztan(x: c64) -> c64: def tan(x: c64) -> c64: return _lfortran_ztan(x) + + +@ccall +def _lfortran_zacosh(x: c64) -> c64: + pass + +def acosh(x: c64) -> c64: + return _lfortran_zacosh(x) + + +@ccall +def _lfortran_zasinh(x: c64) -> c64: + pass + +def asinh(x: c64) -> c64: + return _lfortran_zasinh(x) + + +@ccall +def _lfortran_zatanh(x: c64) -> c64: + pass + +def atanh(x: c64) -> c64: + return _lfortran_zatanh(x) + + +@ccall +def _lfortran_zcosh(x: c64) -> c64: + pass + +def cosh(x: c64) -> c64: + return _lfortran_zcosh(x) + + +@ccall +def _lfortran_zsinh(x: c64) -> c64: + pass + +def sinh(x: c64) -> c64: + return _lfortran_zsinh(x) + + +@ccall +def _lfortran_ztanh(x: c64) -> c64: + pass + +def tanh(x: c64) -> c64: + return _lfortran_ztanh(x) From b999cfe78a2c246bbf8f4b24fd915436e28d3864 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 15:53:44 +0530 Subject: [PATCH 6/9] add tests for hyperbolic functions --- integration_tests/test_cmath.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_cmath.py b/integration_tests/test_cmath.py index 5b99efafbc..59b6a9a8db 100644 --- a/integration_tests/test_cmath.py +++ b/integration_tests/test_cmath.py @@ -1,4 +1,5 @@ -from cmath import exp, log, sqrt, acos, asin, atan, cos, sin, tan +from cmath import (exp, log, sqrt, acos, asin, atan, cos, sin, tan, + acosh, asinh, atanh, cosh, sinh, tanh) from ltypes import c64 def test_power_logarithmic(): @@ -22,5 +23,18 @@ def test_trigonometric(): y = tan(x) +def test_hyperbolic(): + x: c64 + y: c64 + x = complex(3, 3) + y = acosh(x) + y = asinh(x) + y = atanh(x) + y = cosh(x) + y = sinh(x) + y = tanh(x) + + test_power_logarithmic() test_trigonometric() +test_hyperbolic() From 4848d5ce4d5ccac3f0f06450fcc5a11f64b65077 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Wed, 16 Mar 2022 17:01:21 +0530 Subject: [PATCH 7/9] overload with c32/c64 for power and log functions --- integration_tests/test_cmath.py | 6 ++++++ src/runtime/cmath.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_cmath.py b/integration_tests/test_cmath.py index 59b6a9a8db..e3d9957b8d 100644 --- a/integration_tests/test_cmath.py +++ b/integration_tests/test_cmath.py @@ -9,6 +9,12 @@ def test_power_logarithmic(): y = exp(x) y = log(x) y = sqrt(x) + a: c32 + b: c32 + a = complex(3, 3) + b = exp(a) + b = log(a) + b = sqrt(a) def test_trigonometric(): diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py index 41a31debf1..148aca5e47 100644 --- a/src/runtime/cmath.py +++ b/src/runtime/cmath.py @@ -1,4 +1,4 @@ -from ltypes import c64, ccall, f64 +from ltypes import c64, ccall, f64, overload, c32 pi: f64 = 3.141592653589793238462643383279502884197 e: f64 = 2.718281828459045235360287471352662497757 @@ -9,25 +9,52 @@ def _lfortran_zexp(x: c64) -> c64: pass +@ccall +def _lfortran_cexp(x: c32) -> c32: + pass + +@overload def exp(x: c64) -> c64: return _lfortran_zexp(x) +@overload +def exp(x: c32) -> c32: + return _lfortran_cexp(x) + @ccall def _lfortran_zlog(x: c64) -> c64: pass +@ccall +def _lfortran_clog(x: c32) -> c32: + pass + +@overload def log(x: c64) -> c64: return _lfortran_zlog(x) +@overload +def log(x: c32) -> c32: + return _lfortran_clog(x) + @ccall def _lfortran_zsqrt(x: c64) -> c64: pass +@ccall +def _lfortran_csqrt(x: c64) -> c64: + pass + +@overload def sqrt(x: c64) -> c64: return _lfortran_zsqrt(x) +@overload +def sqrt(x: c32) -> c32: + return _lfortran_csqrt(x) + @ccall def _lfortran_zacos(x: c64) -> c64: From 39cac2b9e4b4870762426eb33ab9961dd42eb233 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sat, 19 Mar 2022 19:55:07 +0530 Subject: [PATCH 8/9] overload with c32/c64 for trigonometric functions --- integration_tests/test_cmath.py | 9 ++++++ src/runtime/cmath.py | 51 ++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_cmath.py b/integration_tests/test_cmath.py index e3d9957b8d..a2f1a77655 100644 --- a/integration_tests/test_cmath.py +++ b/integration_tests/test_cmath.py @@ -27,6 +27,15 @@ def test_trigonometric(): y = cos(x) y = sin(x) y = tan(x) + a: c32 + b: c32 + a = complex(3, 3) + b = acos(a) + b = asin(a) + b = atan(a) + b = cos(a) + b = sin(a) + b = tan(a) def test_hyperbolic(): diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py index 148aca5e47..ee96ed4550 100644 --- a/src/runtime/cmath.py +++ b/src/runtime/cmath.py @@ -44,7 +44,7 @@ def _lfortran_zsqrt(x: c64) -> c64: pass @ccall -def _lfortran_csqrt(x: c64) -> c64: +def _lfortran_csqrt(x: c32) -> c32: pass @overload @@ -60,49 +60,98 @@ def sqrt(x: c32) -> c32: def _lfortran_zacos(x: c64) -> c64: pass +@ccall +def _lfortran_cacos(x: c32) -> c32: + pass + +@overload def acos(x: c64) -> c64: return _lfortran_zacos(x) +@overload +def acos(x: c32) -> c32: + return _lfortran_cacos(x) @ccall def _lfortran_zasin(x: c64) -> c64: pass +@ccall +def _lfortran_casin(x: c32) -> c32: + pass + +@overload def asin(x: c64) -> c64: return _lfortran_zasin(x) +@overload +def asin(x: c32) -> c32: + return _lfortran_casin(x) @ccall def _lfortran_zatan(x: c64) -> c64: pass +@ccall +def _lfortran_catan(x: c32) -> c32: + pass + +@overload def atan(x: c64) -> c64: return _lfortran_zatan(x) +@overload +def atan(x: c32) -> c32: + return _lfortran_catan(x) @ccall def _lfortran_zcos(x: c64) -> c64: pass +@ccall +def _lfortran_ccos(x: c32) -> c32: + pass + +@overload def cos(x: c64) -> c64: return _lfortran_zcos(x) +@overload +def cos(x: c32) -> c32: + return _lfortran_ccos(x) @ccall def _lfortran_zsin(x: c64) -> c64: pass +@ccall +def _lfortran_csin(x: c32) -> c32: + pass + +@overload def sin(x: c64) -> c64: return _lfortran_zsin(x) +@overload +def sin(x: c32) -> c32: + return _lfortran_csin(x) @ccall def _lfortran_ztan(x: c64) -> c64: pass +@ccall +def _lfortran_ctan(x: c32) -> c32: + pass + +@overload def tan(x: c64) -> c64: return _lfortran_ztan(x) +@overload +def tan(x: c32) -> c32: + return _lfortran_ctan(x) + @ccall def _lfortran_zacosh(x: c64) -> c64: From 742ee387d012375972032921f2348dfe049a754e Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sat, 19 Mar 2022 20:06:22 +0530 Subject: [PATCH 9/9] overload with c32/c64 for hyperbolic functions --- integration_tests/test_cmath.py | 9 ++++++ src/runtime/cmath.py | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/integration_tests/test_cmath.py b/integration_tests/test_cmath.py index a2f1a77655..bfc52328b5 100644 --- a/integration_tests/test_cmath.py +++ b/integration_tests/test_cmath.py @@ -48,6 +48,15 @@ def test_hyperbolic(): y = cosh(x) y = sinh(x) y = tanh(x) + a: c32 + b: c32 + a = complex(3, 3) + b = acosh(a) + b = asinh(a) + b = atanh(a) + b = cosh(a) + b = sinh(a) + b = tanh(a) test_power_logarithmic() diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py index ee96ed4550..f37c862045 100644 --- a/src/runtime/cmath.py +++ b/src/runtime/cmath.py @@ -157,45 +157,96 @@ def tan(x: c32) -> c32: def _lfortran_zacosh(x: c64) -> c64: pass +@ccall +def _lfortran_cacosh(x: c32) -> c32: + pass + +@overload def acosh(x: c64) -> c64: return _lfortran_zacosh(x) +@overload +def acosh(x: c32) -> c32: + return _lfortran_cacosh(x) @ccall def _lfortran_zasinh(x: c64) -> c64: pass +@ccall +def _lfortran_casinh(x: c32) -> c32: + pass + +@overload def asinh(x: c64) -> c64: return _lfortran_zasinh(x) +@overload +def asinh(x: c32) -> c32: + return _lfortran_casinh(x) @ccall def _lfortran_zatanh(x: c64) -> c64: pass +@ccall +def _lfortran_catanh(x: c32) -> c32: + pass + +@overload def atanh(x: c64) -> c64: return _lfortran_zatanh(x) +@overload +def atanh(x: c32) -> c32: + return _lfortran_catanh(x) + @ccall def _lfortran_zcosh(x: c64) -> c64: pass +@ccall +def _lfortran_ccosh(x: c32) -> c32: + pass + +@overload def cosh(x: c64) -> c64: return _lfortran_zcosh(x) +@overload +def cosh(x: c32) -> c32: + return _lfortran_ccosh(x) @ccall def _lfortran_zsinh(x: c64) -> c64: pass +@ccall +def _lfortran_csinh(x: c32) -> c32: + pass + +@overload def sinh(x: c64) -> c64: return _lfortran_zsinh(x) +@overload +def sinh(x: c32) -> c32: + return _lfortran_csinh(x) + @ccall def _lfortran_ztanh(x: c64) -> c64: pass +@ccall +def _lfortran_ctanh(x: c32) -> c32: + pass + +@overload def tanh(x: c64) -> c64: return _lfortran_ztanh(x) + +@overload +def tanh(x: c32) -> c32: + return _lfortran_ctanh(x)