Skip to content

Commit e5b51aa

Browse files
Add cmath polar functions
1 parent 071dd40 commit e5b51aa

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,27 @@ 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+
}
667+
668+
// rect --------------------------------------------------------------------
669+
670+
LFORTRAN_API double_complex_t _lfortran_rect(double r, double phi)
671+
{
672+
double re = r*cos(phi);
673+
double im = r*sin(phi);
674+
double_complex_t c = CMPLX(re, im);
675+
return c;
676+
}
656677

657678
// strcat --------------------------------------------------------------------
658679

src/libasr/runtime/lfortran_intrinsics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ 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);
142+
LFORTRAN_API double_complex_t _lfortran_rect(double r, double phi);
140143
LFORTRAN_API bool _lpython_str_compare_eq(char** s1, char** s2);
141144
LFORTRAN_API bool _lpython_str_compare_noteq(char** s1, char** s2);
142145
LFORTRAN_API bool _lpython_str_compare_gt(char** s1, char** s2);

src/runtime/cmath.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from lpython import c64, ccall, f64, overload, c32
1+
from lpython import c64, ccall, f64, overload, c32, f32
2+
from lpython_builtin import abs
23

34
pi: f64 = 3.141592653589793238462643383279502884197
45
e: f64 = 2.718281828459045235360287471352662497757
@@ -250,3 +251,37 @@ def tanh(x: c64) -> c64:
250251
@overload
251252
def tanh(x: c32) -> c32:
252253
return _lfortran_ctanh(x)
254+
255+
256+
@ccall
257+
def _lfortran_zphase(x: c64) -> f64:
258+
pass
259+
260+
@ccall
261+
def _lfortran_cphase(x: c32) -> f32:
262+
pass
263+
264+
@overload
265+
def phase(x: c64) -> f64:
266+
return _lfortran_zphase(x)
267+
268+
@overload
269+
def phase(x: c32) -> f32:
270+
return _lfortran_cphase(x)
271+
272+
273+
@overload
274+
def polar(x: c32) -> tuple[f32, f32]:
275+
return (abs(x), phase(x))
276+
277+
@overload
278+
def polar(x: c64) -> tuple[f64, f64]:
279+
return (abs(x), phase(x))
280+
281+
282+
@ccall
283+
def _lfortran_rect(r: f64, phi: f64) -> c64:
284+
pass
285+
286+
def rect(r: f64, phi: f64) -> c64:
287+
return _lfortran_rect(r, phi)

0 commit comments

Comments
 (0)