Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53cdade

Browse files
authored
Merge branch 'main' into tanh
2 parents f088de2 + d4e2f74 commit 53cdade

40 files changed

+544
-43
lines changed

integration_tests/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ RUN(NAME expr_01 LABELS cpython llvm c)
134134
RUN(NAME expr_02 LABELS cpython llvm c)
135135
RUN(NAME expr_03 LABELS cpython llvm c)
136136
RUN(NAME expr_04 LABELS cpython llvm c)
137-
RUN(NAME expr_05 LABELS cpython llvm)
137+
RUN(NAME expr_05 LABELS cpython llvm c)
138138
RUN(NAME expr_06 LABELS cpython llvm c)
139139
RUN(NAME expr_07 LABELS cpython llvm)
140140
RUN(NAME expr_08 LABELS llvm c)
@@ -160,6 +160,7 @@ RUN(NAME test_tuple_02 LABELS cpython llvm)
160160
RUN(NAME test_dict_01 LABELS cpython llvm)
161161
RUN(NAME test_dict_02 LABELS cpython llvm)
162162
RUN(NAME test_dict_03 LABELS cpython llvm)
163+
RUN(NAME test_dict_04 LABELS cpython llvm)
163164
RUN(NAME modules_01 LABELS cpython llvm)
164165
RUN(NAME modules_02 LABELS cpython llvm)
165166
RUN(NAME test_math LABELS cpython llvm)
@@ -171,7 +172,8 @@ RUN(NAME elemental_01 LABELS cpython llvm)
171172
RUN(NAME elemental_02 LABELS cpython llvm)
172173
RUN(NAME elemental_03 LABELS cpython llvm)
173174
RUN(NAME elemental_04 LABELS cpython llvm)
174-
175+
RUN(NAME elemental_05 LABELS cpython llvm)
176+
RUN(NAME elemental_06 LABELS cpython llvm)
175177
RUN(NAME elemental_07 LABELS cpython llvm)
176178
RUN(NAME elemental_08 LABELS cpython llvm)
177179
RUN(NAME test_random LABELS cpython llvm)

integration_tests/elemental_05.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
from ltypes import i32, f64, f32
2+
from numpy import empty, sinh, cosh, reshape, int32, float64, sin
3+
4+
def verify1d(array: f32[:], result: f32[:], size: i32):
5+
i: i32
6+
eps: f32 = 1e-6
7+
8+
for i in range(size):
9+
assert abs(sinh(sinh(array[i])) - result[i]) <= eps
10+
11+
def verifynd(array: f64[:, :, :, :], result: f64[:, :, :, :], size1: i32, size2: i32, size3: i32, size4: i32):
12+
i: i32; size: i32;
13+
eps: f64 = 1e-12
14+
shape: i32[1] = empty((1,), dtype=int32)
15+
size = size1 * size2 * size3 * size4
16+
shape[0] = size
17+
array1d: f64[12800] = reshape(array, shape)
18+
result1d: f64[12800] = reshape(result, shape)
19+
20+
for i in range(size):
21+
assert abs((sinh(array1d[i]) + 2)/2 - result1d[i]) <= eps
22+
23+
24+
def elemental_sinh():
25+
i: i32; j: i32; k: i32; l: i32; size: i32;
26+
27+
array1d: f32[10] = empty(10)
28+
sinh1d: f32[10] = empty(10)
29+
30+
for i in range(10):
31+
array1d[i] = i/10.0
32+
33+
sinh1d = sinh(sinh(array1d))
34+
verify1d(array1d, sinh1d, 10)
35+
36+
arraynd: f64[40, 10, 16, 2] = empty((40, 10, 16, 2))
37+
sinhnd: f64[40, 10, 16, 2] = empty((40, 10, 16, 2))
38+
size = 40 * 10 * 16 * 2
39+
40+
for i in range(40):
41+
for j in range(10):
42+
for k in range(16):
43+
for l in range(2):
44+
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/size
45+
46+
sinhnd = (sinh(arraynd) + 2)/2
47+
48+
verifynd(arraynd, sinhnd, 40, 10, 16, 2)
49+
50+
def verify2d(array: f64[:, :], result: f64[:, :], size1: i32, size2: i32):
51+
i: i32; j: i32;
52+
eps: f64 = 1e-12
53+
54+
for i in range(size1):
55+
for j in range(size2):
56+
assert abs(cosh(5 + array[i, j])**2 - result[i, j]) <= eps
57+
58+
def elemental_cosh():
59+
i: i32; j: i32
60+
61+
array2d: f64[20, 10] = empty((20, 10))
62+
cosh2d: f64[20, 10] = empty((20, 10))
63+
64+
for i in range(20):
65+
for j in range(10):
66+
array2d[i, j] = (i + 2*j)/200.0
67+
68+
cosh2d = cosh(5 + (array2d))**2
69+
verify2d(array2d, cosh2d, 20, 10)
70+
71+
def elemental_cosh_():
72+
i: i32
73+
j: i32
74+
75+
array2d: f64[20, 10] = empty((20, 10))
76+
cosh2d: f64[20, 10] = empty((20, 10))
77+
78+
for i in range(20):
79+
for j in range(10):
80+
array2d[i, j] = (i + 2*j)/200.0
81+
82+
cosh2d = cosh(5 + (array2d))**2
83+
verify2d(array2d, cosh2d, 20, 10)
84+
85+
def elemental_trig_identity():
86+
i: i32; j: i32; k: i32; l: i32
87+
eps: f64 = 1e-12
88+
89+
arraynd: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
90+
91+
identity1: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
92+
identity2: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
93+
identity3: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
94+
identity4: f64[10, 5, 2, 4] = empty((10, 5, 2, 4), dtype=float64)
95+
96+
observed1d_1: f64[400] = empty(400, dtype=float64)
97+
observed1d_2: f64[400] = empty(400, dtype=float64)
98+
observed1d_3: f64[400] = empty(400, dtype=float64)
99+
observed1d_4: f64[400] = empty(400, dtype=float64)
100+
101+
for i in range(10):
102+
for j in range(5):
103+
for k in range(2):
104+
for l in range(4):
105+
arraynd[i, j, k, l] = sin(float(i + j + k + l))
106+
107+
identity1 = 1.0 - cosh(arraynd)**2 + sinh(arraynd)**2
108+
identity2 = cosh(-1 * arraynd) - cosh(arraynd)
109+
identity3 = sinh(-1 * arraynd) + sinh(arraynd)
110+
identity4 = (cosh(arraynd/4 + arraynd/2) -
111+
cosh(arraynd/4) * cosh(arraynd/2) -
112+
sinh(arraynd/4) * sinh(arraynd/2))
113+
114+
newshape: i32[1] = empty(1, dtype=int)
115+
newshape[0] = 400
116+
117+
observed1d_1 = reshape(identity1, newshape)
118+
observed1d_2 = reshape(identity2, newshape)
119+
observed1d_3 = reshape(identity3, newshape)
120+
observed1d_4 = reshape(identity4, newshape)
121+
122+
for i in range(400):
123+
assert abs(observed1d_1[i] - 0.0) <= eps
124+
assert abs(observed1d_2[i] - 0.0) <= eps
125+
assert abs(observed1d_3[i] - 0.0) <= eps
126+
assert abs(observed1d_4[i] - 0.0) <= eps
127+
128+
129+
elemental_sinh()
130+
elemental_cosh()
131+
elemental_trig_identity()

integration_tests/elemental_06.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
from ltypes import i32, f32, f64
2+
from numpy import empty, arcsin, arccos, sin, cos, sqrt
3+
from math import pi
4+
5+
def verify1d_same(array: f32[:], result: f32[:], size: i32):
6+
i: i32
7+
eps: f32
8+
eps = 1e-6
9+
for i in range(size):
10+
assert abs(array[i] - result[i]) <= eps
11+
12+
def verify_arcsin_1d(array: f32[:], result: f32[:], size: i32):
13+
i: i32
14+
eps: f32
15+
eps = 1e-6
16+
for i in range(size):
17+
assert abs(arcsin(array[i])**2 - result[i]) <= eps
18+
19+
def verify_arcsin_2d(array: f64[:, :], result: f64[:, :], size1:i32, size2:i32):
20+
i: i32
21+
j: i32
22+
eps: f64
23+
eps = 1e-12
24+
for i in range(size1):
25+
for j in range(size2):
26+
assert abs(arcsin(array[i, j])**2 - result[i, j]) <= eps
27+
28+
def verify_arccos_1d(array: f32[:], result: f32[:], size: i32):
29+
i: i32
30+
eps: f32
31+
eps = 1e-6
32+
for i in range(size):
33+
assert abs(arccos(array[i])**2 - result[i]) <= eps
34+
35+
def verify_arccos_2d(array: f64[:, :], result: f64[:, :], size1:i32, size2:i32):
36+
i: i32
37+
j: i32
38+
eps: f64
39+
eps = 1e-12
40+
for i in range(size1):
41+
for j in range(size2):
42+
assert abs(arccos(array[i, j])**2 - result[i, j]) <= eps
43+
44+
def elemental_arcsin():
45+
i: i32
46+
j: i32
47+
array1d: f32[201] = empty(201)
48+
arcsin1d: f32[201] = empty(201)
49+
for i in range(201):
50+
array1d[i] = float((i - 100)/100)
51+
arcsin1d = arcsin(array1d) ** 2
52+
verify_arcsin_1d(array1d, arcsin1d, 201)
53+
54+
array2d: f64[64, 64] = empty((64, 64))
55+
arcsin2d: f64[64, 64] = empty((64, 64))
56+
for i in range(64):
57+
for j in range(64): # 2048 = 64 * 32
58+
array2d[i,j]= float((i * 64 + j - 2048 )/2048)
59+
60+
arcsin2d = arcsin(array2d) ** 2
61+
verify_arcsin_2d(array2d, arcsin2d, 64, 64)
62+
63+
def elemental_arccos():
64+
i: i32
65+
j: i32
66+
array1d: f32[201] = empty(201)
67+
arccos1d: f32[201] = empty(201)
68+
for i in range(201):
69+
array1d[i] = float((i - 100)/100)
70+
arccos1d = arccos(array1d) ** 2
71+
verify_arccos_1d(array1d, arccos1d, 201)
72+
73+
array2d: f64[64, 64] = empty((64, 64))
74+
arccos2d: f64[64, 64] = empty((64, 64))
75+
for i in range(64):
76+
for j in range(64): # 2048 = 64 * 32
77+
array2d[i,j]= float((i * 64 + j - 2048 )/2048)
78+
79+
arccos2d = arccos(array2d) ** 2
80+
verify_arccos_2d(array2d, arccos2d, 64, 64)
81+
82+
def elemental_trig_identity():
83+
i: i32
84+
eps: f32
85+
eps = 1e-6
86+
array1d: f32[201] = empty(201)
87+
observed1d: f32[201] = empty(201)
88+
for i in range(201):
89+
array1d[i] = float((i - 100)/100)
90+
91+
observed1d = arcsin(array1d) + arccos(array1d)
92+
for i in range(201):
93+
assert abs(observed1d[i] - pi / 2) <= eps
94+
95+
def elemental_reverse():
96+
i: i32
97+
array1d: f32[201] = empty(201)
98+
observed1d: f32[201] = empty(201)
99+
for i in range(201):
100+
array1d[i] = float((i - 100)/100)
101+
observed1d = sin(arcsin(array1d))
102+
verify1d_same(observed1d, array1d, 201)
103+
104+
observed1d = cos(arccos(array1d))
105+
verify1d_same(observed1d, array1d, 201)
106+
107+
def elemental_trig_identity_extra():
108+
i: i32
109+
array1d: f32[201] = empty(201)
110+
array_x: f32[201] = empty(201)
111+
array_y: f32[201] = empty(201)
112+
for i in range(201):
113+
array1d[i] = float((i - 100)/100)
114+
array_x = sin(arccos(array1d))
115+
array_y = cos(arcsin(array1d))
116+
for i in range(201):
117+
array1d[i] = 1 - array1d[i] ** 2
118+
array1d = sqrt(array1d)
119+
verify1d_same(array_x, array_y, 201)
120+
verify1d_same(array_x, array1d, 201)
121+
122+
123+
elemental_arcsin()
124+
elemental_arccos()
125+
elemental_trig_identity()
126+
elemental_reverse()
127+
elemental_trig_identity_extra()

integration_tests/expr_01.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
def add(x: i32, y: i32) -> i32:
55
return x + y
66

7+
@inline
8+
def and_op(x: i32, y: i32) -> i32:
9+
return x & y
10+
711
def main0():
8-
x: i32; y: i32
12+
x: i32
13+
y: i32
14+
z: i32
915
x = (2+3)*5
1016
y = add(x, 2)*2
1117
assert x == 25
1218
assert y == 54
1319

20+
z = and_op(x, y)
21+
assert z == 16
22+
23+
1424
main0()
1525

1626
# Not implemented yet in LPython:

integration_tests/test_dict_04.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from ltypes import i32, i64, f64
2+
from math import pi, sin, cos
3+
4+
def test_dict():
5+
terms2poly: dict[tuple[i32, i32], i64] = {}
6+
rtheta2coords: dict[tuple[i64, i64], tuple[f64, f64]] = {}
7+
i: i32
8+
n: i64
9+
size: i32 = 7000
10+
size1: i32
11+
theta: f64
12+
r: f64
13+
coords: tuple[f64, f64]
14+
eps: f64 = 1e-12
15+
16+
n = 0
17+
for i in range(1000, 1000 + size, 7):
18+
terms2poly[(i, i*i)] = int(i + i*i)
19+
20+
theta = float(n) * pi
21+
r = float(i)
22+
rtheta2coords[(int(i), n)] = (r * sin(theta), r * cos(theta))
23+
24+
n += int(1)
25+
26+
size1 = size/7
27+
n = 0
28+
for i in range(1000, 1000 + size//2, 7):
29+
assert terms2poly.pop((i, i*i)) == int(i + i*i)
30+
31+
theta = float(n) * pi
32+
r = float(i)
33+
coords = rtheta2coords.pop((int(i), n))
34+
assert abs(coords[0] - r * sin(theta)) <= eps
35+
assert abs(coords[1] - r * cos(theta)) <= eps
36+
37+
size1 = size1 - 1
38+
assert len(terms2poly) == size1
39+
n += int(1)
40+
41+
n = 0
42+
for i in range(1000, 1000 + size//2, 7):
43+
terms2poly[(i, i*i)] = int(1 + 2*i + i*i)
44+
45+
theta = float(n) * pi
46+
r = float(i)
47+
rtheta2coords[(int(i), n)] = (r * cos(theta), r * sin(theta))
48+
49+
n += int(1)
50+
51+
n = 0
52+
for i in range(1000, 1000 + size//2, 7):
53+
assert terms2poly[(i, i*i)] == (i + 1)*(i + 1)
54+
55+
theta = float(n) * pi
56+
r = float(i)
57+
assert abs(rtheta2coords[(int(i), n)][0] - r * cos(theta)) <= eps
58+
assert abs(rtheta2coords[(int(i), n)][1] - r * sin(theta)) <= eps
59+
60+
n += int(1)
61+
62+
n = 0
63+
for i in range(1000, 1000 + size, 7):
64+
terms2poly[(i, i*i)] = int(1 + 2*i + i*i)
65+
66+
theta = float(n) * pi
67+
r = float(i)
68+
rtheta2coords[(int(i), n)] = (r * cos(theta), r * sin(theta))
69+
n += int(1)
70+
71+
n = 0
72+
for i in range(1000, 1000 + size, 7):
73+
assert terms2poly[(i, i*i)] == (i + 1)*(i + 1)
74+
75+
theta = float(n) * pi
76+
r = float(i)
77+
assert abs(r**2 - rtheta2coords[(int(i), n)][0]**2 - r**2 * sin(theta)**2) <= eps
78+
n += int(1)
79+
80+
test_dict()

0 commit comments

Comments
 (0)