Skip to content

Sinh & cosh numpy_intrinsic functions implemented #1002

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 5 commits into from
Aug 30, 2022
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ RUN(NAME elemental_01 LABELS cpython llvm)
RUN(NAME elemental_02 LABELS cpython llvm)
RUN(NAME elemental_03 LABELS cpython llvm)
RUN(NAME elemental_04 LABELS cpython llvm)
RUN(NAME elemental_05 LABELS cpython llvm)
RUN(NAME elemental_06 LABELS cpython llvm)
RUN(NAME test_random LABELS cpython llvm)
RUN(NAME test_os LABELS cpython llvm)
Expand Down
131 changes: 131 additions & 0 deletions integration_tests/elemental_05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from ltypes import i32, f64, f32
from numpy import empty, sinh, cosh, reshape, int32, float64, sin

def verify1d(array: f32[:], result: f32[:], size: i32):
i: i32
eps: f32 = 1e-6

for i in range(size):
assert abs(sinh(sinh(array[i])) - result[i]) <= eps

def verifynd(array: f64[:, :, :, :], result: f64[:, :, :, :], size1: i32, size2: i32, size3: i32, size4: i32):
i: i32; size: i32;
eps: f64 = 1e-12
shape: i32[1] = empty((1,), dtype=int32)
size = size1 * size2 * size3 * size4
shape[0] = size
array1d: f64[12800] = reshape(array, shape)
result1d: f64[12800] = reshape(result, shape)

for i in range(size):
assert abs((sinh(array1d[i]) + 2)/2 - result1d[i]) <= eps


def elemental_sinh():
i: i32; j: i32; k: i32; l: i32; size: i32;

array1d: f32[10] = empty(10)
sinh1d: f32[10] = empty(10)

for i in range(10):
array1d[i] = i/10.0

sinh1d = sinh(sinh(array1d))
verify1d(array1d, sinh1d, 10)

arraynd: f64[40, 10, 16, 2] = empty((40, 10, 16, 2))
sinhnd: f64[40, 10, 16, 2] = empty((40, 10, 16, 2))
size = 40 * 10 * 16 * 2

for i in range(40):
for j in range(10):
for k in range(16):
for l in range(2):
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/size

sinhnd = (sinh(arraynd) + 2)/2

verifynd(arraynd, sinhnd, 40, 10, 16, 2)

def verify2d(array: f64[:, :], result: f64[:, :], size1: i32, size2: i32):
i: i32; j: i32;
eps: f64 = 1e-12

for i in range(size1):
for j in range(size2):
assert abs(cosh(5 + array[i, j])**2 - result[i, j]) <= eps

def elemental_cosh():
i: i32; j: i32

array2d: f64[20, 10] = empty((20, 10))
cosh2d: f64[20, 10] = empty((20, 10))

for i in range(20):
for j in range(10):
array2d[i, j] = (i + 2*j)/200.0

cosh2d = cosh(5 + (array2d))**2
verify2d(array2d, cosh2d, 20, 10)

def elemental_cosh_():
i: i32
j: i32

array2d: f64[20, 10] = empty((20, 10))
cosh2d: f64[20, 10] = empty((20, 10))

for i in range(20):
for j in range(10):
array2d[i, j] = (i + 2*j)/200.0

cosh2d = cosh(5 + (array2d))**2
verify2d(array2d, cosh2d, 20, 10)

def elemental_trig_identity():
i: i32; j: i32; k: i32; l: i32
eps: f64 = 1e-12

arraynd: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)

identity1: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
identity2: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
identity3: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
identity4: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)

observed1d_1: f64[400] = empty(400, dtype=float64)
observed1d_2: f64[400] = empty(400, dtype=float64)
observed1d_3: f64[400] = empty(400, dtype=float64)
observed1d_4: f64[400] = empty(400, dtype=float64)

for i in range(10):
for j in range(5):
for k in range(2):
for l in range(4):
arraynd[i, j, k, l] = sin(float(i + j + k + l))

identity1 = 1.0 - cosh(arraynd)**2 + sinh(arraynd)**2
identity2 = cosh(-1 * arraynd) - cosh(arraynd)
identity3 = sinh(-1 * arraynd) + sinh(arraynd)
identity4 = (cosh(arraynd/4 + arraynd/2) -
cosh(arraynd/4) * cosh(arraynd/2) -
sinh(arraynd/4) * sinh(arraynd/2))

newshape: i32[1] = empty(1, dtype=int)
newshape[0] = 400

observed1d_1 = reshape(identity1, newshape)
observed1d_2 = reshape(identity2, newshape)
observed1d_3 = reshape(identity3, newshape)
observed1d_4 = reshape(identity4, newshape)

for i in range(400):
assert abs(observed1d_1[i] - 0.0) <= eps
assert abs(observed1d_2[i] - 0.0) <= eps
assert abs(observed1d_3[i] - 0.0) <= eps
assert abs(observed1d_4[i] - 0.0) <= eps


elemental_sinh()
elemental_cosh()
elemental_trig_identity()
24 changes: 16 additions & 8 deletions src/libasr/pass/array_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,47 +705,55 @@ class ArrayOpVisitor : public PassUtils::PassVisitor<ArrayOpVisitor>
if( doloop == nullptr ) {
ASR::expr_t* ref = PassUtils::create_array_ref(arr_expr, idx_vars_value, al);
ASR::expr_t* res = PassUtils::create_array_ref(result_var, idx_vars, al);
ASR::expr_t *lexpr = nullptr, *rexpr = nullptr;
if( rank_left > 0 ) {
lexpr = ref;
rexpr = other_expr;
} else {
rexpr = ref;
lexpr = other_expr;
}
ASR::expr_t* op_el_wise = nullptr;
switch( x.class_type ) {
case ASR::exprType::IntegerBinOp:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_IntegerBinOp_t(
al, x.base.base.loc,
ref, (ASR::binopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::binopType)x.m_op, rexpr, x.m_type, nullptr));
break;
case ASR::exprType::RealBinOp:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_RealBinOp_t(
al, x.base.base.loc,
ref, (ASR::binopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::binopType)x.m_op, rexpr, x.m_type, nullptr));
break;
case ASR::exprType::ComplexBinOp:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_ComplexBinOp_t(
al, x.base.base.loc,
ref, (ASR::binopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::binopType)x.m_op, rexpr, x.m_type, nullptr));
break;
case ASR::exprType::LogicalBinOp:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_LogicalBinOp_t(
al, x.base.base.loc,
ref, (ASR::logicalbinopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::logicalbinopType)x.m_op, rexpr, x.m_type, nullptr));
break;
case ASR::exprType::IntegerCompare:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_IntegerCompare_t(
al, x.base.base.loc,
ref, (ASR::cmpopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::cmpopType)x.m_op, rexpr, x.m_type, nullptr));
break;
case ASR::exprType::RealCompare:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_RealCompare_t(
al, x.base.base.loc,
ref, (ASR::cmpopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::cmpopType)x.m_op, rexpr, x.m_type, nullptr));
break;
case ASR::exprType::ComplexCompare:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_ComplexCompare_t(
al, x.base.base.loc,
ref, (ASR::cmpopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::cmpopType)x.m_op, rexpr, x.m_type, nullptr));
break;
case ASR::exprType::LogicalCompare:
op_el_wise = LFortran::ASRUtils::EXPR(ASR::make_LogicalCompare_t(
al, x.base.base.loc,
ref, (ASR::cmpopType)x.m_op, other_expr, x.m_type, nullptr));
lexpr, (ASR::cmpopType)x.m_op, rexpr, x.m_type, nullptr));
break;
default:
throw LCompilersException("The desired operation is not supported yet for arrays.");
Expand Down
42 changes: 42 additions & 0 deletions src/runtime/lpython_intrinsic_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,48 @@ def _lfortran_stan(x: f32) -> f32:
def tan(x: f32) -> f32:
return _lfortran_stan(x)


########## sinh ##########

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

@overload
@vectorize
def sinh(x: f64) -> f64:
return _lfortran_dsinh(x)

@ccall
def _lfortran_ssinh(x: f32) -> f32:
pass

@overload
@vectorize
def sinh(x: f32) -> f32:
return _lfortran_ssinh(x)

########## cosh ##########

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

@overload
@vectorize
def cosh(x: f64) -> f64:
return _lfortran_dcosh(x)

@ccall
def _lfortran_scosh(x: f32) -> f32:
pass

@overload
@vectorize
def cosh(x: f32) -> f32:
return _lfortran_scosh(x)


########## log ##########

@ccall
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "85bcc57879d6a94bf5e51e106e7ffbe00137da998cdd03ca9771397e",
"stdout_hash": "5ecf6039e91c9a83a28aeb6a43fdc4d72d9561a3d8f0542ccdf019a3",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_02_decl-e8f6874.stdout",
"stdout_hash": "d255150e3a3cab32c1c10b7d87e6bf41a5c08b1a02a3262aa03ca643",
"stdout_hash": "4a38b14377eb0d1bb08af1a3ac8298d87507a01528f564d6c2c77ed0",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-elemental_01-b58df26.stdout",
"stdout_hash": "8196b92c8852e196721b484ed4cbf08597f08e14d03ac317778f7b09",
"stdout_hash": "cf4ee24dc8526427340655eeda0cf3810220555b1f021271612787c6",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_03-e600a49.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_numpy_03-e600a49.stdout",
"stdout_hash": "5b56db1f6eae87e55daa8941552f6b701c87a2b5da2847b4efa88ee7",
"stdout_hash": "a02228cec6fd33552a8d2c9789cd3ef12793d9d86dcae2cca85772a2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_03-e600a49.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_04-ecbb614.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_numpy_04-ecbb614.stdout",
"stdout_hash": "dd38ca7e26c58fbd295e5114536da8096c4b3d706e70149d033bf015",
"stdout_hash": "fcf50d51c7ac3174a6204e49aaaf2f0de1c075237dc013ca0292ce67",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_04-ecbb614.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-vec_01-66ac423.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-vec_01-66ac423.stdout",
"stdout_hash": "5c6c944844a7321b1d638d30ef7bd55ff73157280cdf4be5eb1a8323",
"stdout_hash": "27e2d720253a51fcc8522c2c43f572c4fc1d55adf4b351eb407867fe",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-vec_01-66ac423.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/pass_loop_vectorise-vec_01-be9985e.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "pass_loop_vectorise-vec_01-be9985e.stdout",
"stdout_hash": "b791badfb88621d1820f169e435b1d34cb0a8a9c359139c151e42d66",
"stdout_hash": "5e63d66337232a414c2dcc035de82b554cd346fe7d579429deddb397",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/pass_loop_vectorise-vec_01-be9985e.stdout

Large diffs are not rendered by default.