From c74f00cfa2a54589a0f1c5427230df54f6d04168 Mon Sep 17 00:00:00 2001 From: Virendra Kabra Date: Mon, 3 Apr 2023 14:33:48 +0530 Subject: [PATCH 1/2] Add cmath polar functions --- integration_tests/test_cmath.py | 16 ++++++++-- src/libasr/runtime/lfortran_intrinsics.c | 21 ++++++++++++++ src/libasr/runtime/lfortran_intrinsics.h | 3 ++ src/runtime/cmath.py | 37 +++++++++++++++++++++++- 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/integration_tests/test_cmath.py b/integration_tests/test_cmath.py index c01b64bf3a..dd91b1f911 100644 --- a/integration_tests/test_cmath.py +++ b/integration_tests/test_cmath.py @@ -1,6 +1,7 @@ from cmath import (exp, log, sqrt, acos, asin, atan, cos, sin, tan, - acosh, asinh, atanh, cosh, sinh, tanh) -from lpython import c64, c32 + acosh, asinh, atanh, cosh, sinh, tanh, + phase, polar, rect) +from lpython import c64, c32, f64 def test_power_logarithmic(): x: c64 @@ -59,6 +60,17 @@ def test_hyperbolic(): b = tanh(a) +def test_polar(): + x: c64 + eps: f64 + eps = 1e-12 + x = complex(1, -2) + assert f64(abs(f64(phase(x)) - (-1.1071487177940904))) < eps + assert f64(abs(f64(polar(x)[0]) - (2.23606797749979))) < eps + assert abs(abs(rect(2.23606797749979, -1.1071487177940904))-abs(x)) < eps + + test_power_logarithmic() test_trigonometric() test_hyperbolic() +test_polar() \ No newline at end of file diff --git a/src/libasr/runtime/lfortran_intrinsics.c b/src/libasr/runtime/lfortran_intrinsics.c index d4b861694e..f236ce3bae 100644 --- a/src/libasr/runtime/lfortran_intrinsics.c +++ b/src/libasr/runtime/lfortran_intrinsics.c @@ -653,6 +653,27 @@ LFORTRAN_API double_complex_t _lfortran_zatanh(double_complex_t x) return catanh(x); } +// phase -------------------------------------------------------------------- + +LFORTRAN_API float _lfortran_cphase(float_complex_t x) +{ + return atan2f(cimagf(x), crealf(x)); +} + +LFORTRAN_API double _lfortran_zphase(double_complex_t x) +{ + return atan2(cimag(x), creal(x)); +} + +// rect -------------------------------------------------------------------- + +LFORTRAN_API double_complex_t _lfortran_rect(double r, double phi) +{ + double re = r*cos(phi); + double im = r*sin(phi); + double complex c = CMPLX(re, im); + return c; +} // strcat -------------------------------------------------------------------- diff --git a/src/libasr/runtime/lfortran_intrinsics.h b/src/libasr/runtime/lfortran_intrinsics.h index 6f9788e399..771f069999 100644 --- a/src/libasr/runtime/lfortran_intrinsics.h +++ b/src/libasr/runtime/lfortran_intrinsics.h @@ -137,6 +137,9 @@ LFORTRAN_API float _lfortran_satanh(float x); LFORTRAN_API double _lfortran_datanh(double x); LFORTRAN_API float_complex_t _lfortran_catanh(float_complex_t x); LFORTRAN_API double_complex_t _lfortran_zatanh(double_complex_t x); +LFORTRAN_API float _lfortran_cphase(float_complex_t x); +LFORTRAN_API double _lfortran_zphase(double_complex_t x); +LFORTRAN_API double_complex_t _lfortran_rect(double r, double phi); LFORTRAN_API bool _lpython_str_compare_eq(char** s1, char** s2); LFORTRAN_API bool _lpython_str_compare_noteq(char** s1, char** s2); LFORTRAN_API bool _lpython_str_compare_gt(char** s1, char** s2); diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py index 445b72fe80..87c6a1b346 100644 --- a/src/runtime/cmath.py +++ b/src/runtime/cmath.py @@ -1,4 +1,5 @@ -from lpython import c64, ccall, f64, overload, c32 +from lpython import c64, ccall, f64, overload, c32, f32 +from lpython_builtin import abs pi: f64 = 3.141592653589793238462643383279502884197 e: f64 = 2.718281828459045235360287471352662497757 @@ -250,3 +251,37 @@ def tanh(x: c64) -> c64: @overload def tanh(x: c32) -> c32: return _lfortran_ctanh(x) + + +@ccall +def _lfortran_zphase(x: c64) -> f64: + pass + +@ccall +def _lfortran_cphase(x: c32) -> f32: + pass + +@overload +def phase(x: c64) -> f64: + return _lfortran_zphase(x) + +@overload +def phase(x: c32) -> f32: + return _lfortran_cphase(x) + + +@overload +def polar(x: c32) -> tuple[f32, f32]: + return (abs(x), phase(x)) + +@overload +def polar(x: c64) -> tuple[f64, f64]: + return (abs(x), phase(x)) + + +@ccall +def _lfortran_rect(r: f64, phi: f64) -> c64: + pass + +def rect(r: f64, phi: f64) -> c64: + return _lfortran_rect(r, phi) From 29737ad92e9e196de18fa2bb494e9a0387d6eeef Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 3 Apr 2023 20:15:42 +0530 Subject: [PATCH 2/2] Remove rect from lfortran_intrinsics.c --- src/libasr/runtime/lfortran_intrinsics.c | 10 ---------- src/libasr/runtime/lfortran_intrinsics.h | 1 - src/runtime/cmath.py | 8 +++++--- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/libasr/runtime/lfortran_intrinsics.c b/src/libasr/runtime/lfortran_intrinsics.c index f236ce3bae..114716d06d 100644 --- a/src/libasr/runtime/lfortran_intrinsics.c +++ b/src/libasr/runtime/lfortran_intrinsics.c @@ -665,16 +665,6 @@ LFORTRAN_API double _lfortran_zphase(double_complex_t x) return atan2(cimag(x), creal(x)); } -// rect -------------------------------------------------------------------- - -LFORTRAN_API double_complex_t _lfortran_rect(double r, double phi) -{ - double re = r*cos(phi); - double im = r*sin(phi); - double complex c = CMPLX(re, im); - return c; -} - // strcat -------------------------------------------------------------------- LFORTRAN_API void _lfortran_strcat(char** s1, char** s2, char** dest) diff --git a/src/libasr/runtime/lfortran_intrinsics.h b/src/libasr/runtime/lfortran_intrinsics.h index 771f069999..ae01305df2 100644 --- a/src/libasr/runtime/lfortran_intrinsics.h +++ b/src/libasr/runtime/lfortran_intrinsics.h @@ -139,7 +139,6 @@ LFORTRAN_API float_complex_t _lfortran_catanh(float_complex_t x); LFORTRAN_API double_complex_t _lfortran_zatanh(double_complex_t x); LFORTRAN_API float _lfortran_cphase(float_complex_t x); LFORTRAN_API double _lfortran_zphase(double_complex_t x); -LFORTRAN_API double_complex_t _lfortran_rect(double r, double phi); LFORTRAN_API bool _lpython_str_compare_eq(char** s1, char** s2); LFORTRAN_API bool _lpython_str_compare_noteq(char** s1, char** s2); LFORTRAN_API bool _lpython_str_compare_gt(char** s1, char** s2); diff --git a/src/runtime/cmath.py b/src/runtime/cmath.py index 87c6a1b346..271f962542 100644 --- a/src/runtime/cmath.py +++ b/src/runtime/cmath.py @@ -1,5 +1,4 @@ from lpython import c64, ccall, f64, overload, c32, f32 -from lpython_builtin import abs pi: f64 = 3.141592653589793238462643383279502884197 e: f64 = 2.718281828459045235360287471352662497757 @@ -278,10 +277,13 @@ def polar(x: c32) -> tuple[f32, f32]: def polar(x: c64) -> tuple[f64, f64]: return (abs(x), phase(x)) +@ccall +def _lfortran_dcos(x: f64) -> f64: + pass @ccall -def _lfortran_rect(r: f64, phi: f64) -> c64: +def _lfortran_dsin(x: f64) -> f64: pass def rect(r: f64, phi: f64) -> c64: - return _lfortran_rect(r, phi) + return c64(complex(r*_lfortran_dcos(phi), r*_lfortran_dsin(phi)))