Skip to content
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

Numpy: Support more dtypes #2278

Merged
merged 5 commits into from
Aug 15, 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
5 changes: 5 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions integration_tests/array_expr_04.py
Original file line number Diff line number Diff line change
@@ -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()
31 changes: 31 additions & 0 deletions integration_tests/array_expr_05.py
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 21 additions & 0 deletions integration_tests/array_expr_06.py
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 21 additions & 0 deletions integration_tests/array_expr_07.py
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 14 additions & 0 deletions integration_tests/array_expr_08.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ static inline std::string type_to_str(const ASR::ttype_t *t)
case ASR::ttypeType::Integer: {
return "integer";
}
case ASR::ttypeType::UnsignedInteger: {
return "unsigned integer";
}
case ASR::ttypeType::Real: {
return "real";
}
Expand Down
28 changes: 21 additions & 7 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
std::map<std::string, std::string> imported_functions;

std::map<std::string, std::string> 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,
Expand Down Expand Up @@ -920,20 +935,20 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
if (var_sym->m_type->type == ASR::ttypeType::TypeParameter) {
ASR::TypeParameter_t *type_param = ASR::down_cast<ASR::TypeParameter_t>(var_sym->m_type);
type = ASRUtils::TYPE(ASR::make_TypeParameter_t(al, loc, type_param->m_param));
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
}
} else {
ASR::symbol_t *der_sym = ASRUtils::symbol_get_past_external(s);
if( der_sym ) {
if ( ASR::is_a<ASR::StructType_t>(*der_sym) ) {
type = ASRUtils::TYPE(ASR::make_Struct_t(al, loc, s));
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
} else if( ASR::is_a<ASR::EnumType_t>(*der_sym) ) {
type = ASRUtils::TYPE(ASR::make_Enum_t(al, loc, s));
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
} else if( ASR::is_a<ASR::UnionType_t>(*der_sym) ) {
type = ASRUtils::TYPE(ASR::make_Union_t(al, loc, s));
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
}
}
}
Expand Down Expand Up @@ -7589,10 +7604,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = ASR::make_UnsignedIntegerBitNot_t(al, x.base.base.loc, operand, operand_type, value);
return;
} else if( call_name == "array" ) {
parse_args(x, args);
ASR::ttype_t* type = nullptr;
if( x.n_keywords == 0 ) {
parse_args(x, args);
} else {
if( x.n_keywords > 0) {
args.reserve(al, 1);
visit_expr_list(x.m_args, x.n_args, args);
if( x.n_keywords > 1 ) {
Expand Down
2 changes: 1 addition & 1 deletion src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand Down