Skip to content

Add cmath polar functions #1663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
11 changes: 11 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,17 @@ 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));
}

// strcat --------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ 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 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
39 changes: 38 additions & 1 deletion src/runtime/cmath.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import c64, ccall, f64, overload, c32
from lpython import c64, ccall, f64, overload, c32, f32

pi: f64 = 3.141592653589793238462643383279502884197
e: f64 = 2.718281828459045235360287471352662497757
Expand Down Expand Up @@ -250,3 +250,40 @@ 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_dcos(x: f64) -> f64:
pass

@ccall
def _lfortran_dsin(x: f64) -> f64:
pass

def rect(r: f64, phi: f64) -> c64:
return c64(complex(r*_lfortran_dcos(phi), r*_lfortran_dsin(phi)))