From d6034a15bc3e62e5abb9f3cd7e55daabf36632df Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 15 Aug 2023 16:25:43 +0530 Subject: [PATCH 1/2] ASR: Support other numpy2lpythontypes --- src/lpython/semantics/python_ast_to_asr.cpp | 15 +++++++++++++++ src/lpython/semantics/python_comptime_eval.h | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 003b7cdf74c..4d10ff965cc 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -510,7 +510,22 @@ class CommonVisitor : public AST::BaseVisitor { std::map imported_functions; std::map numpy2lpythontypes = { + {"bool", "bool"}, + {"bool_", "bool"}, {"int8", "i8"}, + {"int16", "i16"}, + {"int32", "i32"}, + {"int64", "i64"}, + {"uint8", "u8"}, + {"uint16", "u16"}, + {"uint32", "u32"}, + {"uint64", "u64"}, + {"float32", "f32"}, + {"float64", "f64"}, + {"float_", "f64"}, + {"complex64", "c32"}, + {"complex128", "c64"}, + {"complex_", "c64"}, }; CommonVisitor(Allocator &al, LocationManager &lm, SymbolTable *symbol_table, diff --git a/src/lpython/semantics/python_comptime_eval.h b/src/lpython/semantics/python_comptime_eval.h index 1780e500592..ac609af1939 100644 --- a/src/lpython/semantics/python_comptime_eval.h +++ b/src/lpython/semantics/python_comptime_eval.h @@ -28,7 +28,7 @@ struct ProceduresDatabase { "complex64", "complex128", "int8", "exp", "exp2", "uint8", "uint16", "uint32", "uint64", - "size"}}, + "size", "bool_"}}, {"math", {"sin", "cos", "tan", "asin", "acos", "atan", "exp", "exp2", "expm1"}}, From bc01cbf71633dd64e6dc882fe54a5d96051c4ba3 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 15 Aug 2023 16:14:34 +0530 Subject: [PATCH 2/2] TEST: Add for numpy dtypes --- integration_tests/CMakeLists.txt | 5 +++++ integration_tests/array_expr_04.py | 35 ++++++++++++++++++++++++++++++ integration_tests/array_expr_05.py | 31 ++++++++++++++++++++++++++ integration_tests/array_expr_06.py | 21 ++++++++++++++++++ integration_tests/array_expr_07.py | 21 ++++++++++++++++++ integration_tests/array_expr_08.py | 14 ++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 integration_tests/array_expr_04.py create mode 100644 integration_tests/array_expr_05.py create mode 100644 integration_tests/array_expr_06.py create mode 100644 integration_tests/array_expr_07.py create mode 100644 integration_tests/array_expr_08.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index a673e0f515a..88aaf529614 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -413,6 +413,11 @@ RUN(NAME variable_decl_03 LABELS cpython llvm c) RUN(NAME array_expr_01 LABELS cpython llvm c) RUN(NAME array_expr_02 LABELS cpython llvm c NOFAST) RUN(NAME array_expr_03 LABELS cpython llvm c) +RUN(NAME array_expr_04 LABELS cpython llvm c) +RUN(NAME array_expr_05 LABELS cpython llvm c) +RUN(NAME array_expr_06 LABELS cpython llvm c) +RUN(NAME array_expr_07 LABELS cpython llvm c) +RUN(NAME array_expr_08 LABELS cpython llvm c) RUN(NAME array_size_01 LABELS cpython llvm c) RUN(NAME array_size_02 LABELS cpython llvm c) RUN(NAME array_01 LABELS cpython llvm wasm c) diff --git a/integration_tests/array_expr_04.py b/integration_tests/array_expr_04.py new file mode 100644 index 00000000000..c14e4cc988b --- /dev/null +++ b/integration_tests/array_expr_04.py @@ -0,0 +1,35 @@ +from lpython import i8, i16, i32, i64 +from numpy import int8, int16, int32, int64, array + +def g(): + a8: i8[4] = array([127, -127, 3, 111], dtype=int8) + a16: i16[4] = array([127, -127, 3, 111], dtype=int16) + a32: i32[4] = array([127, -127, 3, 111], dtype=int32) + a64: i64[4] = array([127, -127, 3, 111], dtype=int64) + + print(a8) + print(a16) + print(a32) + print(a64) + + assert (a8[0] == i8(127)) + assert (a8[1] == i8(-127)) + assert (a8[2] == i8(3)) + assert (a8[3] == i8(111)) + + assert (a16[0] == i16(127)) + assert (a16[1] == i16(-127)) + assert (a16[2] == i16(3)) + assert (a16[3] == i16(111)) + + assert (a32[0] == i32(127)) + assert (a32[1] == i32(-127)) + assert (a32[2] == i32(3)) + assert (a32[3] == i32(111)) + + assert (a64[0] == i64(127)) + assert (a64[1] == i64(-127)) + assert (a64[2] == i64(3)) + assert (a64[3] == i64(111)) + +g() diff --git a/integration_tests/array_expr_05.py b/integration_tests/array_expr_05.py new file mode 100644 index 00000000000..8736470c71a --- /dev/null +++ b/integration_tests/array_expr_05.py @@ -0,0 +1,31 @@ +from lpython import u8, u16, u32, u64 +from numpy import uint8, uint16, uint32, uint64, array + +def g(): + a8: u8[3] = array([127, 3, 111], dtype=uint8) + a16: u16[3] = array([127, 3, 111], dtype=uint16) + a32: u32[3] = array([127, 3, 111], dtype=uint32) + a64: u64[3] = array([127, 3, 111], dtype=uint64) + + print(a8) + print(a16) + print(a32) + print(a64) + + assert (a8[0] == u8(127)) + assert (a8[1] == u8(3)) + assert (a8[2] == u8(111)) + + assert (a16[0] == u16(127)) + assert (a16[1] == u16(3)) + assert (a16[2] == u16(111)) + + assert (a32[0] == u32(127)) + assert (a32[1] == u32(3)) + assert (a32[2] == u32(111)) + + assert (a64[0] == u64(127)) + assert (a64[1] == u64(3)) + assert (a64[2] == u64(111)) + +g() diff --git a/integration_tests/array_expr_06.py b/integration_tests/array_expr_06.py new file mode 100644 index 00000000000..b6dc397d875 --- /dev/null +++ b/integration_tests/array_expr_06.py @@ -0,0 +1,21 @@ +from lpython import f32, f64 +from numpy import float32, float64, array + +def g(): + a32: f32[4] = array([127, -127, 3, 111], dtype=float32) + a64: f64[4] = array([127, -127, 3, 111], dtype=float64) + + print(a32) + print(a64) + + assert (abs(a32[0] - f32(127)) <= f32(1e-5)) + assert (abs(a32[1] - f32(-127)) <= f32(1e-5)) + assert (abs(a32[2] - f32(3)) <= f32(1e-5)) + assert (abs(a32[3] - f32(111)) <= f32(1e-5)) + + assert (abs(a64[0] - f64(127)) <= 1e-5) + assert (abs(a64[1] - f64(-127)) <= 1e-5) + assert (abs(a64[2] - f64(3)) <= 1e-5) + assert (abs(a64[3] - f64(111)) <= 1e-5) + +g() diff --git a/integration_tests/array_expr_07.py b/integration_tests/array_expr_07.py new file mode 100644 index 00000000000..598a7fcb640 --- /dev/null +++ b/integration_tests/array_expr_07.py @@ -0,0 +1,21 @@ +from lpython import c32, c64, f32 +from numpy import complex64, complex128, array + +def g(): + a32: c32[4] = array([127, -127, 3, 111], dtype=complex64) + a64: c64[4] = array([127, -127, 3, 111], dtype=complex128) + + print(a32) + print(a64) + + assert (abs(a32[0] - c32(127)) <= f32(1e-5)) + assert (abs(a32[1] - c32(-127)) <= f32(1e-5)) + assert (abs(a32[2] - c32(3)) <= f32(1e-5)) + assert (abs(a32[3] - c32(111)) <= f32(1e-5)) + + assert (abs(a64[0] - c64(127)) <= 1e-5) + assert (abs(a64[1] - c64(-127)) <= 1e-5) + assert (abs(a64[2] - c64(3)) <= 1e-5) + assert (abs(a64[3] - c64(111)) <= 1e-5) + +g() diff --git a/integration_tests/array_expr_08.py b/integration_tests/array_expr_08.py new file mode 100644 index 00000000000..ba0cf0dcb79 --- /dev/null +++ b/integration_tests/array_expr_08.py @@ -0,0 +1,14 @@ +from lpython import i1 +from numpy import bool_, array + +def g(): + a1: i1[4] = array([0, -127, 0, 111], dtype=bool_) + + print(a1) + + assert not a1[0] + assert a1[1] + assert not a1[2] + assert a1[3] + +g()