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

Skip to content

Implemented hypot in numpy #2574

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 1 commit into from
Mar 5, 2024
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
41 changes: 40 additions & 1 deletion integration_tests/elemental_06.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lpython import i32, f32, f64
from numpy import empty, arcsin, arccos, sin, cos, sqrt, arctan, tan, degrees, radians, float32, float64
from numpy import empty, arcsin, arccos, sin, cos, sqrt, arctan, tan, degrees, radians, hypot, float32, float64
from math import pi

def verify1d_same(array: f32[:], result: f32[:], size: i32):
Expand Down Expand Up @@ -57,6 +57,15 @@ def verify_arctan_2d(array: f64[:, :], result: f64[:, :], size1:i32, size2:i32):
for j in range(size2):
assert abs(arctan(array[i, j])**2.0 - result[i, j]) <= eps

def verify_hypot_2d(array1: f64[:, :], array2: 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(hypot(array1[i, j], array2[i, j]) - result[i, j]) <= eps

def elemental_arcsin():
i: i32
j: i32
Expand Down Expand Up @@ -222,6 +231,35 @@ def elemental_radians():
for j in range(64):
assert abs(radians2d[i, j] - cos(radians(array2d[i, j]))) <= eps_64

def elemental_hypot():
i: i32
j: i32
eps_32: f32
eps_32 = f32(1e-6)

hypot1d: f32[200] = empty(200, dtype=float32)
array1d1: f32[200] = empty(200, dtype=float32)
array1d2: f32[200] = empty(200, dtype=float32)
for i in range(200):
array1d1[i] = f32(i)
array1d2[i] = f32(i+10)

hypot1d = hypot(array1d1, array1d2)

for i in range(200):
assert abs(hypot1d[i] - hypot(array1d1[i], array1d2[i])) <= eps_32

array2d1: f64[64, 64] = empty((64, 64), dtype=float64)
array2d2: f64[64, 64] = empty((64, 64), dtype=float64)
hypot2d: f64[64, 64] = empty((64, 64), dtype=float64)
for i in range(64):
for j in range(64):
array2d1[i,j]= float(i * j)
array2d2[i,j]= float(64*i + j - 2048)

hypot2d = hypot(array2d1, array2d2)
verify_hypot_2d(array2d1, array2d2, hypot2d, 64, 64)


elemental_arcsin()
elemental_arccos()
Expand All @@ -231,3 +269,4 @@ def elemental_radians():
elemental_trig_identity()
elemental_reverse()
elemental_trig_identity_extra()
elemental_hypot()
13 changes: 13 additions & 0 deletions src/runtime/lpython_intrinsic_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,19 @@ def ceil(x: f32) -> f32:
return resultf
return resultf + f32(1)

########## hypot ##########


@overload
@vectorize
def hypot(x: f64, y: f64) -> f64:
return sqrt(f64(1.0)*f64(x**f64(2) + y**f64(2)))

@overload
@vectorize
def hypot(x: f32, y: f32) -> f32:
return sqrt(f32(1)*(x**f32(2) + y**f32(2)))

########## trunc ##########

@ccall
Expand Down
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": "5d4751789e2ddcd882c4d6026f801ba32cfc227fafff7395a788bdd9",
"stdout_hash": "d167f932ab4a4bb559ae3b4bb3b983cd6153a105cfb6180474e64ae2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading