Skip to content

Commit 08b04b0

Browse files
authored
Merge pull request #456 from Aasthaengg/math_funcs_list
Add dist() function in math.py
2 parents e17007a + 96a36ce commit 08b04b0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

integration_tests/test_math.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow,
22
ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc,
3-
modf, fsum, prod)
3+
modf, fsum, prod, dist)
44
import math
55
from ltypes import i32, i64, f32, f64
66

@@ -220,6 +220,14 @@ def test_prod():
220220
res = prod(arr_f64)
221221
assert abs(res - 80.64) < eps
222222

223+
def test_dist():
224+
x: list[f64]
225+
y: list[f64]
226+
eps: f64 = 1e-12
227+
x = [1.0, 2.2, 3.333, 4.0, 5.0]
228+
y = [6.1, 7.2, 8.0, 9.0, 10.0]
229+
assert abs(dist(x, y) - 11.081105044173166) < eps
230+
223231
def test_modf():
224232
i: f64
225233
i = 3.14
@@ -268,6 +276,7 @@ def check():
268276
test_trunc()
269277
test_fsum()
270278
test_prod()
279+
test_dist()
271280
test_modf()
272281
test_issue_1242()
273282

src/runtime/math.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,21 @@ def prod(arr: list[f64]) -> f64:
207207
return result
208208

209209

210+
def dist(x: list[f64], y: list[f64]) -> f64:
211+
"""
212+
Return euclidean distance between `x` and `y` points.
213+
"""
214+
if len(x) != len(y):
215+
raise ValueError("Length of lists should be same")
216+
res: f64
217+
res = 0.0
218+
219+
i: i32
220+
for i in range(len(x)):
221+
res += (x[i] - y[i]) * (x[i] - y[i])
222+
return res**0.5
223+
224+
210225
def comb(n: i32, k: i32) -> i32:
211226
"""
212227
Computes the result of `nCk`, i.e, the number of ways to choose `k`

0 commit comments

Comments
 (0)