Skip to content

Implement arcsin and arccos in Numpy. #1054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ RUN(NAME elemental_01 LABELS cpython llvm)
RUN(NAME elemental_02 LABELS cpython llvm)
RUN(NAME elemental_03 LABELS cpython llvm)
RUN(NAME elemental_04 LABELS cpython llvm)
RUN(NAME elemental_06 LABELS cpython llvm)
RUN(NAME test_random LABELS cpython llvm)
RUN(NAME test_os LABELS cpython llvm)
RUN(NAME test_builtin LABELS cpython llvm)
Expand Down
127 changes: 127 additions & 0 deletions integration_tests/elemental_06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from ltypes import i32, f32, f64
from numpy import empty, arcsin, arccos, sin, cos, sqrt
from math import pi

def verify1d_same(array: f32[:], result: f32[:], size: i32):
i: i32
eps: f32
eps = 1e-6
for i in range(size):
assert abs(array[i] - result[i]) <= eps

def verify_arcsin_1d(array: f32[:], result: f32[:], size: i32):
i: i32
eps: f32
eps = 1e-6
for i in range(size):
assert abs(arcsin(array[i])**2 - result[i]) <= eps

def verify_arcsin_2d(array: f64[:, :], result: f64[:, :], size1:i32, size2:i32):
i: i32
j: i32
eps: f64
eps = 1e-12
for i in range(size1):
for j in range(size2):
assert abs(arcsin(array[i, j])**2 - result[i, j]) <= eps

def verify_arccos_1d(array: f32[:], result: f32[:], size: i32):
i: i32
eps: f32
eps = 1e-6
for i in range(size):
assert abs(arccos(array[i])**2 - result[i]) <= eps

def verify_arccos_2d(array: f64[:, :], result: f64[:, :], size1:i32, size2:i32):
i: i32
j: i32
eps: f64
eps = 1e-12
for i in range(size1):
for j in range(size2):
assert abs(arccos(array[i, j])**2 - result[i, j]) <= eps

def elemental_arcsin():
i: i32
j: i32
array1d: f32[201] = empty(201)
arcsin1d: f32[201] = empty(201)
for i in range(201):
array1d[i] = float((i - 100)/100)
arcsin1d = arcsin(array1d) ** 2
verify_arcsin_1d(array1d, arcsin1d, 201)

array2d: f64[64, 64] = empty((64, 64))
arcsin2d: f64[64, 64] = empty((64, 64))
for i in range(64):
for j in range(64): # 2048 = 64 * 32
array2d[i,j]= float((i * 64 + j - 2048 )/2048)

arcsin2d = arcsin(array2d) ** 2
verify_arcsin_2d(array2d, arcsin2d, 64, 64)

def elemental_arccos():
i: i32
j: i32
array1d: f32[201] = empty(201)
arccos1d: f32[201] = empty(201)
for i in range(201):
array1d[i] = float((i - 100)/100)
arccos1d = arccos(array1d) ** 2
verify_arccos_1d(array1d, arccos1d, 201)

array2d: f64[64, 64] = empty((64, 64))
arccos2d: f64[64, 64] = empty((64, 64))
for i in range(64):
for j in range(64): # 2048 = 64 * 32
array2d[i,j]= float((i * 64 + j - 2048 )/2048)

arccos2d = arccos(array2d) ** 2
verify_arccos_2d(array2d, arccos2d, 64, 64)

def elemental_trig_identity():
i: i32
eps: f32
eps = 1e-6
array1d: f32[201] = empty(201)
observed1d: f32[201] = empty(201)
for i in range(201):
array1d[i] = float((i - 100)/100)

observed1d = arcsin(array1d) + arccos(array1d)
for i in range(201):
assert abs(observed1d[i] - pi / 2) <= eps

def elemental_reverse():
i: i32
array1d: f32[201] = empty(201)
observed1d: f32[201] = empty(201)
for i in range(201):
array1d[i] = float((i - 100)/100)
observed1d = sin(arcsin(array1d))
verify1d_same(observed1d, array1d, 201)

observed1d = cos(arccos(array1d))
verify1d_same(observed1d, array1d, 201)

def elemental_trig_identity_extra():
i: i32
array1d: f32[201] = empty(201)
array_x: f32[201] = empty(201)
array_y: f32[201] = empty(201)
for i in range(201):
array1d[i] = float((i - 100)/100)
array_x = sin(arccos(array1d))
array_y = cos(arcsin(array1d))
for i in range(201):
array1d[i] = 1 - array1d[i] ** 2
array1d = sqrt(array1d)
verify1d_same(array_x, array_y, 201)
verify1d_same(array_x, array1d, 201)


elemental_arcsin()
elemental_arccos()
elemental_trig_identity()
elemental_reverse()
elemental_trig_identity_extra()
40 changes: 40 additions & 0 deletions src/runtime/lpython_intrinsic_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,43 @@ def log2(x: f64) -> f64:
@vectorize
def log2(x: f32) -> f32:
return _lfortran_slog(x)/_lfortran_slog(2.0)

########## arcsin ##########

@ccall
def _lfortran_dasin(x: f64) -> f64:
pass

@overload
@vectorize
def arcsin(x: f64) -> f64:
return _lfortran_dasin(x)

@ccall
def _lfortran_sasin(x: f32) -> f32:
pass

@overload
@vectorize
def arcsin(x: f32) -> f32:
return _lfortran_sasin(x)

########## arccos ##########

@ccall
def _lfortran_dacos(x: f64) -> f64:
pass

@overload
@vectorize
def arccos(x: f64) -> f64:
return _lfortran_dacos(x)

@ccall
def _lfortran_sacos(x: f32) -> f32:
pass

@overload
@vectorize
def arccos(x: f32) -> f32:
return _lfortran_sacos(x)
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "4812dc85fefac0f3095586a421d702202e0da99399fabab2c26a7681",
"stdout_hash": "85bcc57879d6a94bf5e51e106e7ffbe00137da998cdd03ca9771397e",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_02_decl-e8f6874.stdout",
"stdout_hash": "a7ee4e4069b9cf459e6e0d0d9d4abb0faa788146f3852b27402b3801",
"stdout_hash": "d255150e3a3cab32c1c10b7d87e6bf41a5c08b1a02a3262aa03ca643",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-elemental_01-b58df26.stdout",
"stdout_hash": "371c67412744f45ebbacdd11d04266a89d1948752cb00750d7cc4592",
"stdout_hash": "8196b92c8852e196721b484ed4cbf08597f08e14d03ac317778f7b09",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_03-e600a49.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_numpy_03-e600a49.stdout",
"stdout_hash": "a6b22172ece2dbc6543551cfb3f3f7a7d13b0574d18f9ab54ecc4a20",
"stdout_hash": "5b56db1f6eae87e55daa8941552f6b701c87a2b5da2847b4efa88ee7",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_03-e600a49.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_04-ecbb614.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_numpy_04-ecbb614.stdout",
"stdout_hash": "8392f917d487d810c4382a104eb4e44391fd627c0b0ef51baef3c29e",
"stdout_hash": "dd38ca7e26c58fbd295e5114536da8096c4b3d706e70149d033bf015",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_04-ecbb614.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-vec_01-66ac423.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-vec_01-66ac423.stdout",
"stdout_hash": "f6a1956c4ca8f970d78f8d6a84351f555d3fd0357dd896295c6d3fd8",
"stdout_hash": "5c6c944844a7321b1d638d30ef7bd55ff73157280cdf4be5eb1a8323",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading