Skip to content

Commit 3707a8a

Browse files
authored
Merge pull request #2278 from Shaikh-Ubaid/support_more_dtypes
Numpy: Support more dtypes
2 parents 8df052e + bc01cbf commit 3707a8a

File tree

9 files changed

+152
-8
lines changed

9 files changed

+152
-8
lines changed

integration_tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ RUN(NAME variable_decl_03 LABELS cpython llvm c)
413413
RUN(NAME array_expr_01 LABELS cpython llvm c)
414414
RUN(NAME array_expr_02 LABELS cpython llvm c NOFAST)
415415
RUN(NAME array_expr_03 LABELS cpython llvm c)
416+
RUN(NAME array_expr_04 LABELS cpython llvm c)
417+
RUN(NAME array_expr_05 LABELS cpython llvm c)
418+
RUN(NAME array_expr_06 LABELS cpython llvm c)
419+
RUN(NAME array_expr_07 LABELS cpython llvm c)
420+
RUN(NAME array_expr_08 LABELS cpython llvm c)
416421
RUN(NAME array_size_01 LABELS cpython llvm c)
417422
RUN(NAME array_size_02 LABELS cpython llvm c)
418423
RUN(NAME array_01 LABELS cpython llvm wasm c)

integration_tests/array_expr_04.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from lpython import i8, i16, i32, i64
2+
from numpy import int8, int16, int32, int64, array
3+
4+
def g():
5+
a8: i8[4] = array([127, -127, 3, 111], dtype=int8)
6+
a16: i16[4] = array([127, -127, 3, 111], dtype=int16)
7+
a32: i32[4] = array([127, -127, 3, 111], dtype=int32)
8+
a64: i64[4] = array([127, -127, 3, 111], dtype=int64)
9+
10+
print(a8)
11+
print(a16)
12+
print(a32)
13+
print(a64)
14+
15+
assert (a8[0] == i8(127))
16+
assert (a8[1] == i8(-127))
17+
assert (a8[2] == i8(3))
18+
assert (a8[3] == i8(111))
19+
20+
assert (a16[0] == i16(127))
21+
assert (a16[1] == i16(-127))
22+
assert (a16[2] == i16(3))
23+
assert (a16[3] == i16(111))
24+
25+
assert (a32[0] == i32(127))
26+
assert (a32[1] == i32(-127))
27+
assert (a32[2] == i32(3))
28+
assert (a32[3] == i32(111))
29+
30+
assert (a64[0] == i64(127))
31+
assert (a64[1] == i64(-127))
32+
assert (a64[2] == i64(3))
33+
assert (a64[3] == i64(111))
34+
35+
g()

integration_tests/array_expr_05.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from lpython import u8, u16, u32, u64
2+
from numpy import uint8, uint16, uint32, uint64, array
3+
4+
def g():
5+
a8: u8[3] = array([127, 3, 111], dtype=uint8)
6+
a16: u16[3] = array([127, 3, 111], dtype=uint16)
7+
a32: u32[3] = array([127, 3, 111], dtype=uint32)
8+
a64: u64[3] = array([127, 3, 111], dtype=uint64)
9+
10+
print(a8)
11+
print(a16)
12+
print(a32)
13+
print(a64)
14+
15+
assert (a8[0] == u8(127))
16+
assert (a8[1] == u8(3))
17+
assert (a8[2] == u8(111))
18+
19+
assert (a16[0] == u16(127))
20+
assert (a16[1] == u16(3))
21+
assert (a16[2] == u16(111))
22+
23+
assert (a32[0] == u32(127))
24+
assert (a32[1] == u32(3))
25+
assert (a32[2] == u32(111))
26+
27+
assert (a64[0] == u64(127))
28+
assert (a64[1] == u64(3))
29+
assert (a64[2] == u64(111))
30+
31+
g()

integration_tests/array_expr_06.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from lpython import f32, f64
2+
from numpy import float32, float64, array
3+
4+
def g():
5+
a32: f32[4] = array([127, -127, 3, 111], dtype=float32)
6+
a64: f64[4] = array([127, -127, 3, 111], dtype=float64)
7+
8+
print(a32)
9+
print(a64)
10+
11+
assert (abs(a32[0] - f32(127)) <= f32(1e-5))
12+
assert (abs(a32[1] - f32(-127)) <= f32(1e-5))
13+
assert (abs(a32[2] - f32(3)) <= f32(1e-5))
14+
assert (abs(a32[3] - f32(111)) <= f32(1e-5))
15+
16+
assert (abs(a64[0] - f64(127)) <= 1e-5)
17+
assert (abs(a64[1] - f64(-127)) <= 1e-5)
18+
assert (abs(a64[2] - f64(3)) <= 1e-5)
19+
assert (abs(a64[3] - f64(111)) <= 1e-5)
20+
21+
g()

integration_tests/array_expr_07.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from lpython import c32, c64, f32
2+
from numpy import complex64, complex128, array
3+
4+
def g():
5+
a32: c32[4] = array([127, -127, 3, 111], dtype=complex64)
6+
a64: c64[4] = array([127, -127, 3, 111], dtype=complex128)
7+
8+
print(a32)
9+
print(a64)
10+
11+
assert (abs(a32[0] - c32(127)) <= f32(1e-5))
12+
assert (abs(a32[1] - c32(-127)) <= f32(1e-5))
13+
assert (abs(a32[2] - c32(3)) <= f32(1e-5))
14+
assert (abs(a32[3] - c32(111)) <= f32(1e-5))
15+
16+
assert (abs(a64[0] - c64(127)) <= 1e-5)
17+
assert (abs(a64[1] - c64(-127)) <= 1e-5)
18+
assert (abs(a64[2] - c64(3)) <= 1e-5)
19+
assert (abs(a64[3] - c64(111)) <= 1e-5)
20+
21+
g()

integration_tests/array_expr_08.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from lpython import i1
2+
from numpy import bool_, array
3+
4+
def g():
5+
a1: i1[4] = array([0, -127, 0, 111], dtype=bool_)
6+
7+
print(a1)
8+
9+
assert not a1[0]
10+
assert a1[1]
11+
assert not a1[2]
12+
assert a1[3]
13+
14+
g()

src/libasr/asr_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ static inline std::string type_to_str(const ASR::ttype_t *t)
408408
case ASR::ttypeType::Integer: {
409409
return "integer";
410410
}
411+
case ASR::ttypeType::UnsignedInteger: {
412+
return "unsigned integer";
413+
}
411414
case ASR::ttypeType::Real: {
412415
return "real";
413416
}

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
510510
std::map<std::string, std::string> imported_functions;
511511

512512
std::map<std::string, std::string> numpy2lpythontypes = {
513+
{"bool", "bool"},
514+
{"bool_", "bool"},
513515
{"int8", "i8"},
516+
{"int16", "i16"},
517+
{"int32", "i32"},
518+
{"int64", "i64"},
519+
{"uint8", "u8"},
520+
{"uint16", "u16"},
521+
{"uint32", "u32"},
522+
{"uint64", "u64"},
523+
{"float32", "f32"},
524+
{"float64", "f64"},
525+
{"float_", "f64"},
526+
{"complex64", "c32"},
527+
{"complex128", "c64"},
528+
{"complex_", "c64"},
514529
};
515530

516531
CommonVisitor(Allocator &al, LocationManager &lm, SymbolTable *symbol_table,
@@ -920,20 +935,20 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
920935
if (var_sym->m_type->type == ASR::ttypeType::TypeParameter) {
921936
ASR::TypeParameter_t *type_param = ASR::down_cast<ASR::TypeParameter_t>(var_sym->m_type);
922937
type = ASRUtils::TYPE(ASR::make_TypeParameter_t(al, loc, type_param->m_param));
923-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
938+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
924939
}
925940
} else {
926941
ASR::symbol_t *der_sym = ASRUtils::symbol_get_past_external(s);
927942
if( der_sym ) {
928943
if ( ASR::is_a<ASR::StructType_t>(*der_sym) ) {
929944
type = ASRUtils::TYPE(ASR::make_Struct_t(al, loc, s));
930-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
945+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
931946
} else if( ASR::is_a<ASR::EnumType_t>(*der_sym) ) {
932947
type = ASRUtils::TYPE(ASR::make_Enum_t(al, loc, s));
933-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
948+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
934949
} else if( ASR::is_a<ASR::UnionType_t>(*der_sym) ) {
935950
type = ASRUtils::TYPE(ASR::make_Union_t(al, loc, s));
936-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
951+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument);
937952
}
938953
}
939954
}
@@ -7589,10 +7604,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
75897604
tmp = ASR::make_UnsignedIntegerBitNot_t(al, x.base.base.loc, operand, operand_type, value);
75907605
return;
75917606
} else if( call_name == "array" ) {
7607+
parse_args(x, args);
75927608
ASR::ttype_t* type = nullptr;
7593-
if( x.n_keywords == 0 ) {
7594-
parse_args(x, args);
7595-
} else {
7609+
if( x.n_keywords > 0) {
75967610
args.reserve(al, 1);
75977611
visit_expr_list(x.m_args, x.n_args, args);
75987612
if( x.n_keywords > 1 ) {

src/lpython/semantics/python_comptime_eval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct ProceduresDatabase {
2828
"complex64", "complex128",
2929
"int8", "exp", "exp2",
3030
"uint8", "uint16", "uint32", "uint64",
31-
"size"}},
31+
"size", "bool_"}},
3232
{"math", {"sin", "cos", "tan",
3333
"asin", "acos", "atan",
3434
"exp", "exp2", "expm1"}},

0 commit comments

Comments
 (0)