Skip to content

Implement arctan, degrees, radians in Numpy. #1062

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
Sep 2, 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
108 changes: 107 additions & 1 deletion integration_tests/elemental_06.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ltypes import i32, f32, f64
from numpy import empty, arcsin, arccos, sin, cos, sqrt
from numpy import empty, arcsin, arccos, sin, cos, sqrt, arctan, tan, degrees, radians
from math import pi

def verify1d_same(array: f32[:], result: f32[:], size: i32):
Expand Down Expand Up @@ -41,6 +41,22 @@ def verify_arccos_2d(array: f64[:, :], result: f64[:, :], size1:i32, size2:i32):
for j in range(size2):
assert abs(arccos(array[i, j])**2 - result[i, j]) <= eps

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

def verify_arctan_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(arctan(array[i, j])**2 - result[i, j]) <= eps

def elemental_arcsin():
i: i32
j: i32
Expand Down Expand Up @@ -79,6 +95,35 @@ def elemental_arccos():
arccos2d = arccos(array2d) ** 2
verify_arccos_2d(array2d, arccos2d, 64, 64)

def elemental_arctan():
i: i32
j: i32
eps: f32
eps = 1e-6
array1d: f32[201] = empty(201)
array1d_rec: f32[201] = empty(201)
arctan1d: f32[201] = empty(201)
for i in range(201):
array1d[i] = float(i-100)
arctan1d = arctan(array1d) ** 2
verify_arctan_1d(array1d, arctan1d, 201)

for i in range(201):
array1d[i] = float(i+1)
array1d_rec[i] = float(1.0/(i+1))
arctan1d = arctan(array1d) + arctan(array1d_rec)
for i in range(201):
assert abs(arctan1d[i] - pi / 2) <= eps

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

arctan2d = arctan(array2d) ** 2
verify_arctan_2d(array2d, arctan2d, 64, 64)

def elemental_trig_identity():
i: i32
eps: f32
Expand All @@ -104,6 +149,12 @@ def elemental_reverse():
observed1d = cos(arccos(array1d))
verify1d_same(observed1d, array1d, 201)

observed1d = tan(arctan(array1d))
verify1d_same(observed1d, array1d, 201)

observed1d = degrees(radians(array1d))
verify1d_same(observed1d, array1d, 201)

def elemental_trig_identity_extra():
i: i32
array1d: f32[201] = empty(201)
Expand All @@ -119,9 +170,64 @@ def elemental_trig_identity_extra():
verify1d_same(array_x, array_y, 201)
verify1d_same(array_x, array1d, 201)

def elemental_degrees():
i: i32
j: i32
eps_32: f32
eps_64: f64
eps_32 = 1e-6
eps_64 = 1e-12
array1d: f32[200] = empty(200)
degrees1d: f32[200] = empty(200)
for i in range(200):
array1d[i] = float(i)
degrees1d = sin(degrees(array1d))

for i in range(200):
assert abs(degrees1d[i]-sin(degrees(array1d[i]))) <= eps_32

array2d: f64[64, 64] = empty((64, 64))
degrees2d: f64[64, 64] = empty((64, 64))
for i in range(64):
for j in range(64):
array2d[i,j]= float(i*64+j)
degrees2d = sin(degrees(array2d))
for i in range(64):
for j in range(64):
assert abs(degrees2d[i,j]-sin(degrees(array2d[i,j]))) <= eps_64

def elemental_radians():
i: i32
j: i32
eps_32: f32
eps_64: f64
eps_32 = 1e-6
eps_64 = 1e-12
array1d: f32[200] = empty(200)
radians1d: f32[200] = empty(200)
for i in range(200):
array1d[i] = float(i)
radians1d = cos(radians(array1d))

for i in range(200):
assert abs(radians1d[i]-cos(radians(array1d[i]))) <= eps_32

array2d: f64[64, 64] = empty((64, 64))
radians2d: f64[64, 64] = empty((64, 64))
for i in range(64):
for j in range(64):
array2d[i,j]= float(i*64+j)
radians2d = cos(radians(array2d))
for i in range(64):
for j in range(64):
assert abs(radians2d[i,j]-cos(radians(array2d[i,j]))) <= eps_64


elemental_arcsin()
elemental_arccos()
elemental_arctan()
elemental_degrees()
elemental_radians()
elemental_trig_identity()
elemental_reverse()
elemental_trig_identity_extra()
46 changes: 46 additions & 0 deletions src/runtime/lpython_intrinsic_numpy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from ltypes import f64, f32, ccall, vectorize, overload

pi_64: f64 = 3.141592653589793238462643383279502884197
pi_32: f32 = 3.141592653589793238462643383279502884197

########## sin ##########

@ccall
Expand Down Expand Up @@ -248,3 +251,46 @@ def _lfortran_sexp(x: f32) -> f32:
def exp(x: f32) -> f32:
return _lfortran_sexp(x)

########## arctan ##########

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

@overload
@vectorize
def arctan(x: f64) -> f64:
return _lfortran_datan(x)

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

@overload
@vectorize
def arctan(x: f32) -> f32:
return _lfortran_satan(x)

########## degrees ##########

@overload
@vectorize
def degrees(x: f64) -> f64:
return x*180/pi_64

@overload
@vectorize
def degrees(x: f32) -> f32:
return x*180/pi_32

########## radians ##########

@overload
@vectorize
def radians(x: f64) -> f64:
return x*pi_64/180

@overload
@vectorize
def radians(x: f32) -> f32:
return x*pi_32/180
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": "581cac2fa1b5469cf1d1ccb3a91fb97ce22413a9b7a3bcb25b5e634c",
"stdout_hash": "c28cc5ab9eeaa418b37fb8a3d48be3afe9ee53f1a798c002c1872410",
"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": "8086f9d4d75d8ac5b7819ddc6fa3f7066865eadffe0ad0a77bb3472b",
"stdout_hash": "f4d6d5213a91087f4c3542434b6b73f4d10c40f873b3b469bf298967",
"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": "572b8546d87ba877caf44a4126b22b93a28cee853c0d5a56e89b8720",
"stdout_hash": "fc03717d767bbc5233186c7b8665d6f60a7682f1970027d6fdfadce7",
"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": "1db5a5bf604257b691bc633d1e56383142498365050933be5df2a1a5",
"stdout_hash": "c08922c2a3f4bc67de3ec220bedcaed820f22b4903347fcb4b80fe2b",
"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": "92ccd61ff99f3f3187821f5adfaf4e84e3264ea14aa6ad7618e41aa3",
"stdout_hash": "a0db0d2668c824adc7b7f26e3ca6c10cb1464701b7a5aa12729cb349",
"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": "4210bc49a30dcc6f81bce09c613c5a6515f2d9bcd311ccba2d8cc895",
"stdout_hash": "961e945caf73473a20009a18102dccf72e054150d4dd95092df236cb",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-vec_01-66ac423.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/pass_loop_vectorise-vec_01-be9985e.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "pass_loop_vectorise-vec_01-be9985e.stdout",
"stdout_hash": "90114a0b77cfbb8d9d18385e27a6393ca56659a19440dacf7d0cddca",
"stdout_hash": "bb71b8bc42b0b1d9d96f6dfb97e38e87936d53e95a56e6db8306307b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/pass_loop_vectorise-vec_01-be9985e.stdout

Large diffs are not rendered by default.