-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathbindpy_03.py
136 lines (108 loc) · 3.37 KB
/
bindpy_03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from lpython import i32, i64, f64, pythoncall, Const, TypeVar
from numpy import empty, int32, int64, float64
n = TypeVar("n")
m = TypeVar("m")
p = TypeVar("p")
q = TypeVar("q")
r = TypeVar("r")
@pythoncall(module = "bindpy_03_module")
def get_cpython_version() -> str:
pass
@pythoncall(module = "bindpy_03_module")
def get_int_array_sum(n: i32, a: i32[:], b: i32[:]) -> i32[n]:
pass
@pythoncall(module = "bindpy_03_module")
def get_int_array_product(n: i32, a: i32[:], b: i32[:]) -> i32[n]:
pass
@pythoncall(module = "bindpy_03_module")
def get_float_array_sum(n: i32, m: i32, a: f64[:], b: f64[:]) -> f64[n, m]:
pass
@pythoncall(module = "bindpy_03_module")
def get_float_array_product(n: i32, m: i32, a: f64[:], b: f64[:]) -> f64[n, m]:
pass
@pythoncall(module = "bindpy_03_module")
def get_array_dot_product(m: i32, a: i64[:], b: f64[:]) -> f64[m]:
pass
@pythoncall(module = "bindpy_03_module")
def get_multidim_array_i64(p: i32, q: i32, r: i32) -> i64[p, q, r]:
pass
# Integers:
def test_array_ints():
n: Const[i32] = 5
a: i32[n] = empty([n], dtype=int32)
b: i32[n] = empty([n], dtype=int32)
i: i32
for i in range(n):
a[i] = i + 10
for i in range(n):
b[i] = i + 20
c: i32[n] = get_int_array_sum(n, a, b)
print(c)
for i in range(n):
assert c[i] == (i + i + 30)
c = get_int_array_product(n, a, b)
print(c)
for i in range(n):
assert c[i] == ((i + 10) * (i + 20))
# Floats
def test_array_floats():
n: Const[i32] = 3
m: Const[i32] = 5
a: f64[n, m] = empty([n, m], dtype=float64)
b: f64[n, m] = empty([n, m], dtype=float64)
i: i32
j: i32
for i in range(n):
for j in range(m):
a[i, j] = f64((i + 10) * (j + 10))
for i in range(n):
for j in range(m):
b[i, j] = f64((i + 20) * (j + 20))
c: f64[n, m] = get_float_array_sum(n, m, a, b)
print(c)
for i in range(n):
for j in range(m):
assert abs(c[i, j] - (f64((i + 10) * (j + 10)) + f64((i + 20) * (j + 20)))) <= 1e-4
c = get_float_array_product(n, m, a, b)
print(c)
for i in range(n):
for j in range(m):
assert abs(c[i, j] - (f64((i + 10) * (j + 10)) * f64((i + 20) * (j + 20)))) <= 1e-4
def test_array_broadcast():
n: Const[i32] = 3
m: Const[i32] = 5
a: i64[n] = empty([n], dtype=int64)
b: f64[n, m] = empty([n, m], dtype=float64)
i: i32
j: i32
for i in range(n):
a[i] = i64(i + 10)
for i in range(n):
for j in range(m):
b[i, j] = f64((i + 1) * (j + 1))
c: f64[m] = get_array_dot_product(m, a, b)
print(c)
assert abs(c[0] - (68.0)) <= 1e-4
assert abs(c[1] - (136.0)) <= 1e-4
assert abs(c[2] - (204.0)) <= 1e-4
assert abs(c[3] - (272.0)) <= 1e-4
assert abs(c[4] - (340.0)) <= 1e-4
def test_multidim_array_return_i64():
p: Const[i32] = 3
q: Const[i32] = 4
r: Const[i32] = 5
a: i64[p, q, r] = empty([p, q, r], dtype=int64)
a = get_multidim_array_i64(p, q, r)
print(a)
i: i32; j: i32; k: i32
for i in range(p):
for j in range(q):
for k in range(r):
assert a[i, j, k] == i64(i * 2 + j * 3 + k * 4)
def main0():
print("CPython version: ", get_cpython_version())
test_array_ints()
test_array_floats()
test_array_broadcast()
test_multidim_array_return_i64()
main0()