Skip to content

Commit 9410226

Browse files
authored
Implement numpy.tanh and numpy.exp
2 parents ffd9b05 + 852b6ab commit 9410226

18 files changed

+156
-14
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ RUN(NAME elemental_03 LABELS cpython llvm)
174174
RUN(NAME elemental_04 LABELS cpython llvm)
175175
RUN(NAME elemental_05 LABELS cpython llvm)
176176
RUN(NAME elemental_06 LABELS cpython llvm)
177+
RUN(NAME elemental_07 LABELS cpython llvm)
178+
RUN(NAME elemental_08 LABELS cpython llvm)
177179
RUN(NAME test_random LABELS cpython llvm)
178180
RUN(NAME test_os LABELS cpython llvm)
179181
RUN(NAME test_builtin LABELS cpython llvm)

integration_tests/elemental_07.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from ltypes import i32, f64, f32
2+
from numpy import empty, tanh, reshape, int32, float64, sin, log10
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(tanh(sin(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[1024] = reshape(array, shape)
18+
result1d: f64[1024] = reshape(result, shape)
19+
20+
for i in range(size):
21+
assert abs((tanh(sin(array1d[i])) + 2)/2 - result1d[i]) <= eps
22+
23+
24+
def elemental_tanh():
25+
i: i32; j: i32; k: i32; l: i32; size: i32;
26+
27+
array1d: f32[80] = empty(80)
28+
tanh1d: f32[80] = empty(80)
29+
30+
for i in range(80):
31+
array1d[i] = i / 10.0
32+
33+
tanh1d = tanh(sin(array1d))
34+
verify1d(array1d, tanh1d, 10)
35+
36+
arraynd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
37+
tanhnd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
38+
size = 16 * 8 * 4 * 2
39+
40+
for i in range(16):
41+
for j in range(8):
42+
for k in range(4):
43+
for l in range(2):
44+
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/size
45+
46+
tanhnd = (tanh(sin(arraynd)) + 2)/2
47+
48+
verifynd(arraynd, tanhnd, 16, 8, 4, 2)
49+
50+
elemental_tanh()

integration_tests/elemental_08.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from ltypes import i32, f64, f32
2+
from numpy import empty, reshape, int32, exp
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(exp(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[1024] = reshape(array, shape)
18+
result1d: f64[1024] = reshape(result, shape)
19+
20+
for i in range(size):
21+
assert abs(( exp(array1d[i]) + exp(array1d[i] / 3) ) / 2 - result1d[i]) <= eps
22+
23+
def elemental_exp():
24+
i: i32; j: i32; k: i32; l: i32; size: i32;
25+
26+
array1d: f32[80] = empty(80)
27+
exp1d: f32[80] = empty(80)
28+
29+
for i in range(80):
30+
array1d[i] = i / 50.0
31+
32+
exp1d = exp(array1d)
33+
verify1d(array1d, exp1d, 80)
34+
35+
arraynd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
36+
expnd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
37+
size = 32
38+
39+
for i in range(16):
40+
for j in range(8):
41+
for k in range(4):
42+
for l in range(2):
43+
arraynd[i, j, k, l] = float( i / 8 + j / 4 + k / 2 + l ) / size
44+
45+
expnd = (exp(arraynd) + exp(arraynd / 3)) / 2
46+
47+
verifynd(arraynd, expnd, 16, 8, 4, 2)
48+
49+
elemental_exp()

src/runtime/lpython_intrinsic_numpy.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,44 @@ def _lfortran_sacos(x: f32) -> f32:
207207
@vectorize
208208
def arccos(x: f32) -> f32:
209209
return _lfortran_sacos(x)
210+
211+
########## tanh ##########
212+
213+
@ccall
214+
def _lfortran_dtanh(x: f64) -> f64:
215+
pass
216+
217+
@overload
218+
@vectorize
219+
def tanh(x: f64) -> f64:
220+
return _lfortran_dtanh(x)
221+
222+
@ccall
223+
def _lfortran_stanh(x: f32) -> f32:
224+
pass
225+
226+
@overload
227+
@vectorize
228+
def tanh(x: f32) -> f32:
229+
return _lfortran_stanh(x)
230+
231+
########## exp ##########
232+
233+
@ccall
234+
def _lfortran_dexp(x: f64) -> f64:
235+
pass
236+
237+
@overload
238+
@vectorize
239+
def exp(x: f64) -> f64:
240+
return _lfortran_dexp(x)
241+
242+
@ccall
243+
def _lfortran_sexp(x: f32) -> f32:
244+
pass
245+
246+
@overload
247+
@vectorize
248+
def exp(x: f32) -> f32:
249+
return _lfortran_sexp(x)
250+

tests/reference/asr-array_01_decl-39cf894.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_01_decl-39cf894.stdout",
9-
"stdout_hash": "5ecf6039e91c9a83a28aeb6a43fdc4d72d9561a3d8f0542ccdf019a3",
9+
"stdout_hash": "581cac2fa1b5469cf1d1ccb3a91fb97ce22413a9b7a3bcb25b5e634c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_01_decl-39cf894.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-array_02_decl-e8f6874.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_02_decl-e8f6874.stdout",
9-
"stdout_hash": "4a38b14377eb0d1bb08af1a3ac8298d87507a01528f564d6c2c77ed0",
9+
"stdout_hash": "8086f9d4d75d8ac5b7819ddc6fa3f7066865eadffe0ad0a77bb3472b",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_02_decl-e8f6874.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-elemental_01-b58df26.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-elemental_01-b58df26.stdout",
9-
"stdout_hash": "90ca8c75d86ea830fb2083b6b5a6813f385c634015b3e890784d6b2e",
9+
"stdout_hash": "572b8546d87ba877caf44a4126b22b93a28cee853c0d5a56e89b8720",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-elemental_01-b58df26.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-test_numpy_03-e600a49.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-test_numpy_03-e600a49.stdout",
9-
"stdout_hash": "47bb384882382f1812820016fd01fa30b8f04c52dd50c731c27b741c",
9+
"stdout_hash": "1db5a5bf604257b691bc633d1e56383142498365050933be5df2a1a5",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-test_numpy_03-e600a49.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-test_numpy_04-ecbb614.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-test_numpy_04-ecbb614.stdout",
9-
"stdout_hash": "52f53b0d09f443f7fcceb3db59b0bcf943f525a8746783bebb6980bb",
9+
"stdout_hash": "92ccd61ff99f3f3187821f5adfaf4e84e3264ea14aa6ad7618e41aa3",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-test_numpy_04-ecbb614.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-vec_01-66ac423.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-vec_01-66ac423.stdout",
9-
"stdout_hash": "27e2d720253a51fcc8522c2c43f572c4fc1d55adf4b351eb407867fe",
9+
"stdout_hash": "4210bc49a30dcc6f81bce09c613c5a6515f2d9bcd311ccba2d8cc895",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-vec_01-66ac423.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/pass_loop_vectorise-vec_01-be9985e.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "pass_loop_vectorise-vec_01-be9985e.stdout",
9-
"stdout_hash": "5e63d66337232a414c2dcc035de82b554cd346fe7d579429deddb397",
9+
"stdout_hash": "90114a0b77cfbb8d9d18385e27a6393ca56659a19440dacf7d0cddca",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/pass_loop_vectorise-vec_01-be9985e.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)