Skip to content

Commit ae1bcd5

Browse files
authored
Merge pull request #2574 from Kishan-Ved/hypot
Implemented `hypot` in numpy
2 parents 71c95ce + c8b617e commit ae1bcd5

26 files changed

Lines changed: 1209 additions & 1157 deletions

integration_tests/elemental_06.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from lpython import i32, f32, f64
2-
from numpy import empty, arcsin, arccos, sin, cos, sqrt, arctan, tan, degrees, radians, float32, float64
2+
from numpy import empty, arcsin, arccos, sin, cos, sqrt, arctan, tan, degrees, radians, hypot, float32, float64
33
from math import pi
44

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

60+
def verify_hypot_2d(array1: f64[:, :], array2: f64[:, :], result: f64[:, :], size1:i32, size2:i32):
61+
i: i32
62+
j: i32
63+
eps: f64
64+
eps = 1e-12
65+
for i in range(size1):
66+
for j in range(size2):
67+
assert abs(hypot(array1[i, j], array2[i, j]) - result[i, j]) <= eps
68+
6069
def elemental_arcsin():
6170
i: i32
6271
j: i32
@@ -222,6 +231,35 @@ def elemental_radians():
222231
for j in range(64):
223232
assert abs(radians2d[i, j] - cos(radians(array2d[i, j]))) <= eps_64
224233

234+
def elemental_hypot():
235+
i: i32
236+
j: i32
237+
eps_32: f32
238+
eps_32 = f32(1e-6)
239+
240+
hypot1d: f32[200] = empty(200, dtype=float32)
241+
array1d1: f32[200] = empty(200, dtype=float32)
242+
array1d2: f32[200] = empty(200, dtype=float32)
243+
for i in range(200):
244+
array1d1[i] = f32(i)
245+
array1d2[i] = f32(i+10)
246+
247+
hypot1d = hypot(array1d1, array1d2)
248+
249+
for i in range(200):
250+
assert abs(hypot1d[i] - hypot(array1d1[i], array1d2[i])) <= eps_32
251+
252+
array2d1: f64[64, 64] = empty((64, 64), dtype=float64)
253+
array2d2: f64[64, 64] = empty((64, 64), dtype=float64)
254+
hypot2d: f64[64, 64] = empty((64, 64), dtype=float64)
255+
for i in range(64):
256+
for j in range(64):
257+
array2d1[i,j]= float(i * j)
258+
array2d2[i,j]= float(64*i + j - 2048)
259+
260+
hypot2d = hypot(array2d1, array2d2)
261+
verify_hypot_2d(array2d1, array2d2, hypot2d, 64, 64)
262+
225263

226264
elemental_arcsin()
227265
elemental_arccos()
@@ -231,3 +269,4 @@ def elemental_radians():
231269
elemental_trig_identity()
232270
elemental_reverse()
233271
elemental_trig_identity_extra()
272+
elemental_hypot()

src/runtime/lpython_intrinsic_numpy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,19 @@ def ceil(x: f32) -> f32:
411411
return resultf
412412
return resultf + f32(1)
413413

414+
########## hypot ##########
415+
416+
417+
@overload
418+
@vectorize
419+
def hypot(x: f64, y: f64) -> f64:
420+
return sqrt(f64(1.0)*f64(x**f64(2) + y**f64(2)))
421+
422+
@overload
423+
@vectorize
424+
def hypot(x: f32, y: f32) -> f32:
425+
return sqrt(f32(1)*(x**f32(2) + y**f32(2)))
426+
414427
########## trunc ##########
415428

416429
@ccall

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

0 commit comments

Comments
 (0)