Skip to content
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

Mean, Geometric Mean & Harmonic mean Functions implemented in Statistics.py module #769

Merged
merged 4 commits into from
Jul 19, 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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not llvm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm tests weren't passing, I think it had something to do with list implementation in the backend

Copy link
Contributor Author

@Madhav2310 Madhav2310 Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was getting the following error:

semantic error: Arguments do not match for any generic procedure, mean
  --> /home/madhav/lpython/integration_tests/test_statistics.py:11:9
   |
11 |     j = mean(b)
   |         ^^^^^^^ 


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