Skip to content

Commit

Permalink
Merge pull request lcompilers#769 from Madhav2310/stats
Browse files Browse the repository at this point in the history
Mean, Geometric Mean & Harmonic mean Functions implemented in Statistics.py module
  • Loading branch information
certik committed Jul 19, 2022
2 parents 14c789e + 13c5291 commit 27af0e1
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ RUN(NAME test_max_min LABELS cpython llvm)
RUN(NAME test_integer_bitnot LABELS cpython llvm)
RUN(NAME test_unary_minus LABELS cpython llvm)
RUN(NAME test_unary_plus LABELS cpython llvm)
RUN(NAME test_statistics LABELS cpython)
RUN(NAME test_bool_binop LABELS cpython llvm)
RUN(NAME test_issue_518 LABELS cpython llvm)
RUN(NAME structs_01 LABELS cpython llvm c)
Expand Down
40 changes: 40 additions & 0 deletions integration_tests/test_statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from statistics import (mean, geometric_mean, harmonic_mean)
from ltypes import i32, f64, i64

eps: f64
eps = 1e-12

def test_mean():
b: list[i32]
b = [9, 4, 10]
j: f64
j = mean(b)
assert abs(j - 7.666666666666667) < eps

c: list[f64]
c = [2.0, 3.1, 11.1]
k: f64
k = mean(c)
assert abs(k - 5.4) < eps

def test_geometric_mean():
c: list[i32]
c = [1,2,3]
k: f64
k = geometric_mean(c)
assert abs(k - 1.8171205928321397) < eps

def test_harmonic_mean():
c: list[i32]
c = [9,2,46]
k: f64
k = harmonic_mean(c)
assert abs(k - 4.740458015267175) < eps


def check():
test_mean()
test_geometric_mean()
test_harmonic_mean()

check()
64 changes: 64 additions & 0 deletions src/runtime/statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from ltypes import i32, f64

@overload
def mean(x: list[i32]) -> f64:
k: i32 = len(x)
if k == 0:
return 0.0
sum: f64
sum = 0.0
i: i32

for i in range(k):
sum += x[i]
ans: f64
ans = sum/k
return ans

@overload
def mean(x: list[f64]) -> f64:
k: i32 = len(x)
if k == 0:
return 0.0
sum: f64
sum = 0.0
i: i32
for i in range(k):
sum += x[i]
ans: f64
ans = sum/k
return ans

@overload
def geometric_mean(x: list[i32]) -> f64:
k: i32 = len(x)
if k == 0:
return 0.0
product: f64
product = 1.0
i: i32
for i in range(k):
if x[i] < 1:
raise ValueError('geometric mean requires a non-empty dataset containing positive numbers')
product *= x[i]
ans: f64
ans = product**(1/k)
return ans

@overload
def harmonic_mean(x: list[i32]) -> f64:
k: i32 = len(x)
if k == 0:
return 0.0
sum: f64
sum = 0.0
i: i32
for i in range(k):
if x[i] < 0:
raise ValueError('harmonic mean does not support negative values')
if x[i] ==0:
return 0.0
sum += 1 / x[i]
ans: f64
ans = k/sum
return ans

0 comments on commit 27af0e1

Please sign in to comment.