Skip to content

Commit

Permalink
Add cmath polar functions
Browse files Browse the repository at this point in the history
  • Loading branch information
virendrakabra14 committed Apr 3, 2023
1 parent 071dd40 commit c74f00c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
16 changes: 14 additions & 2 deletions integration_tests/test_cmath.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
21 changes: 21 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 36 additions & 1 deletion src/runtime/cmath.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

0 comments on commit c74f00c

Please sign in to comment.