Skip to content

Commit 3317fec

Browse files
authored
Merge pull request #1663 from virendrakabra14/scratch
Add cmath polar functions
2 parents e8f7d21 + 29737ad commit 3317fec

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

integration_tests/test_cmath.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from cmath import (exp, log, sqrt, acos, asin, atan, cos, sin, tan,
2-
acosh, asinh, atanh, cosh, sinh, tanh)
3-
from lpython import c64, c32
2+
acosh, asinh, atanh, cosh, sinh, tanh,
3+
phase, polar, rect)
4+
from lpython import c64, c32, f64
45

56
def test_power_logarithmic():
67
x: c64
@@ -59,6 +60,17 @@ def test_hyperbolic():
5960
b = tanh(a)
6061

6162

63+
def test_polar():
64+
x: c64
65+
eps: f64
66+
eps = 1e-12
67+
x = complex(1, -2)
68+
assert f64(abs(f64(phase(x)) - (-1.1071487177940904))) < eps
69+
assert f64(abs(f64(polar(x)[0]) - (2.23606797749979))) < eps
70+
assert abs(abs(rect(2.23606797749979, -1.1071487177940904))-abs(x)) < eps
71+
72+
6273
test_power_logarithmic()
6374
test_trigonometric()
6475
test_hyperbolic()
76+
test_polar()

src/libasr/runtime/lfortran_intrinsics.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,17 @@ LFORTRAN_API double_complex_t _lfortran_zatanh(double_complex_t x)
653653
return catanh(x);
654654
}
655655

656+
// phase --------------------------------------------------------------------
657+
658+
LFORTRAN_API float _lfortran_cphase(float_complex_t x)
659+
{
660+
return atan2f(cimagf(x), crealf(x));
661+
}
662+
663+
LFORTRAN_API double _lfortran_zphase(double_complex_t x)
664+
{
665+
return atan2(cimag(x), creal(x));
666+
}
656667

657668
// strcat --------------------------------------------------------------------
658669

src/libasr/runtime/lfortran_intrinsics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ LFORTRAN_API float _lfortran_satanh(float x);
137137
LFORTRAN_API double _lfortran_datanh(double x);
138138
LFORTRAN_API float_complex_t _lfortran_catanh(float_complex_t x);
139139
LFORTRAN_API double_complex_t _lfortran_zatanh(double_complex_t x);
140+
LFORTRAN_API float _lfortran_cphase(float_complex_t x);
141+
LFORTRAN_API double _lfortran_zphase(double_complex_t x);
140142
LFORTRAN_API bool _lpython_str_compare_eq(char** s1, char** s2);
141143
LFORTRAN_API bool _lpython_str_compare_noteq(char** s1, char** s2);
142144
LFORTRAN_API bool _lpython_str_compare_gt(char** s1, char** s2);

src/runtime/cmath.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lpython import c64, ccall, f64, overload, c32
1+
from lpython import c64, ccall, f64, overload, c32, f32
22

33
pi: f64 = 3.141592653589793238462643383279502884197
44
e: f64 = 2.718281828459045235360287471352662497757
@@ -250,3 +250,40 @@ def tanh(x: c64) -> c64:
250250
@overload
251251
def tanh(x: c32) -> c32:
252252
return _lfortran_ctanh(x)
253+
254+
255+
@ccall
256+
def _lfortran_zphase(x: c64) -> f64:
257+
pass
258+
259+
@ccall
260+
def _lfortran_cphase(x: c32) -> f32:
261+
pass
262+
263+
@overload
264+
def phase(x: c64) -> f64:
265+
return _lfortran_zphase(x)
266+
267+
@overload
268+
def phase(x: c32) -> f32:
269+
return _lfortran_cphase(x)
270+
271+
272+
@overload
273+
def polar(x: c32) -> tuple[f32, f32]:
274+
return (abs(x), phase(x))
275+
276+
@overload
277+
def polar(x: c64) -> tuple[f64, f64]:
278+
return (abs(x), phase(x))
279+
280+
@ccall
281+
def _lfortran_dcos(x: f64) -> f64:
282+
pass
283+
284+
@ccall
285+
def _lfortran_dsin(x: f64) -> f64:
286+
pass
287+
288+
def rect(r: f64, phi: f64) -> c64:
289+
return c64(complex(r*_lfortran_dcos(phi), r*_lfortran_dsin(phi)))

0 commit comments

Comments
 (0)