Skip to content

Commit d79ad27

Browse files
authored
Implement arcsin and arccos in NumPy. (#1054)
1 parent 78a008d commit d79ad27

17 files changed

+182
-14
lines changed

integration_tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ RUN(NAME elemental_01 LABELS cpython llvm)
171171
RUN(NAME elemental_02 LABELS cpython llvm)
172172
RUN(NAME elemental_03 LABELS cpython llvm)
173173
RUN(NAME elemental_04 LABELS cpython llvm)
174+
RUN(NAME elemental_06 LABELS cpython llvm)
174175
RUN(NAME test_random LABELS cpython llvm)
175176
RUN(NAME test_os LABELS cpython llvm)
176177
RUN(NAME test_builtin LABELS cpython llvm)

integration_tests/elemental_06.py

+127
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()

src/runtime/lpython_intrinsic_numpy.py

+40
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,43 @@ def log2(x: f64) -> f64:
125125
@vectorize
126126
def log2(x: f32) -> f32:
127127
return _lfortran_slog(x)/_lfortran_slog(2.0)
128+
129+
########## arcsin ##########
130+
131+
@ccall
132+
def _lfortran_dasin(x: f64) -> f64:
133+
pass
134+
135+
@overload
136+
@vectorize
137+
def arcsin(x: f64) -> f64:
138+
return _lfortran_dasin(x)
139+
140+
@ccall
141+
def _lfortran_sasin(x: f32) -> f32:
142+
pass
143+
144+
@overload
145+
@vectorize
146+
def arcsin(x: f32) -> f32:
147+
return _lfortran_sasin(x)
148+
149+
########## arccos ##########
150+
151+
@ccall
152+
def _lfortran_dacos(x: f64) -> f64:
153+
pass
154+
155+
@overload
156+
@vectorize
157+
def arccos(x: f64) -> f64:
158+
return _lfortran_dacos(x)
159+
160+
@ccall
161+
def _lfortran_sacos(x: f32) -> f32:
162+
pass
163+
164+
@overload
165+
@vectorize
166+
def arccos(x: f32) -> f32:
167+
return _lfortran_sacos(x)

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

+1-1
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": "4812dc85fefac0f3095586a421d702202e0da99399fabab2c26a7681",
9+
"stdout_hash": "85bcc57879d6a94bf5e51e106e7ffbe00137da998cdd03ca9771397e",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

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

+1-1
Large diffs are not rendered by default.

tests/reference/asr-array_02_decl-e8f6874.json

+1-1
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": "a7ee4e4069b9cf459e6e0d0d9d4abb0faa788146f3852b27402b3801",
9+
"stdout_hash": "d255150e3a3cab32c1c10b7d87e6bf41a5c08b1a02a3262aa03ca643",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_02_decl-e8f6874.stdout

+1-1
Large diffs are not rendered by default.

tests/reference/asr-elemental_01-b58df26.json

+1-1
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": "371c67412744f45ebbacdd11d04266a89d1948752cb00750d7cc4592",
9+
"stdout_hash": "8196b92c8852e196721b484ed4cbf08597f08e14d03ac317778f7b09",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-elemental_01-b58df26.stdout

+1-1
Large diffs are not rendered by default.

tests/reference/asr-test_numpy_03-e600a49.json

+1-1
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": "a6b22172ece2dbc6543551cfb3f3f7a7d13b0574d18f9ab54ecc4a20",
9+
"stdout_hash": "5b56db1f6eae87e55daa8941552f6b701c87a2b5da2847b4efa88ee7",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-test_numpy_03-e600a49.stdout

+1-1
Large diffs are not rendered by default.

tests/reference/asr-test_numpy_04-ecbb614.json

+1-1
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": "8392f917d487d810c4382a104eb4e44391fd627c0b0ef51baef3c29e",
9+
"stdout_hash": "dd38ca7e26c58fbd295e5114536da8096c4b3d706e70149d033bf015",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-test_numpy_04-ecbb614.stdout

+1-1
Large diffs are not rendered by default.

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

+1-1
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": "f6a1956c4ca8f970d78f8d6a84351f555d3fd0357dd896295c6d3fd8",
9+
"stdout_hash": "5c6c944844a7321b1d638d30ef7bd55ff73157280cdf4be5eb1a8323",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)