-
Notifications
You must be signed in to change notification settings - Fork 170
Fmean, mean overload functions implemented #877
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
Changes from all commits
928408b
a914135
d535b29
88201d5
a3af2ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
from ltypes import i32, f64 | ||
from ltypes import i32, f64, overload | ||
|
||
|
||
@overload | ||
def mean(x: list[i32]) -> f64: | ||
""" | ||
Returns the arithmetic mean of a data sequence of numbers | ||
""" | ||
k: i32 = len(x) | ||
if k == 0: | ||
return 0.0 | ||
|
@@ -11,54 +15,123 @@ def mean(x: list[i32]) -> f64: | |
|
||
for i in range(k): | ||
sum += x[i] | ||
ans: f64 | ||
ans = sum/k | ||
return ans | ||
return sum/k | ||
|
||
|
||
@overload | ||
def mean(x: list[i64]) -> f64: | ||
""" | ||
Returns the arithmetic mean of a data sequence of numbers | ||
""" | ||
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] | ||
|
||
return sum/k | ||
|
||
|
||
@overload | ||
def mean(x: list[f32]) -> f64: | ||
""" | ||
Returns the arithmetic mean of a data sequence of numbers | ||
""" | ||
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] | ||
return sum/k | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, these are great candidates to try using the new templates. The templates are still experimental, and we still need to implement restrictions, so things will change, but the current state should be able to compile such functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. @ansharlubis @Oshanath May be, you can try templates on these functions and reduce the implementation size? What do you folks say? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be a little bit early, since it will slow @ansharlubis down if we have to do any refactoring, which we will. We can however do it on a branch / Draft PR that is not merged. That would be great, just to see if it works, and to iron out any bugs, but I would not merge into master yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It kind of work in an example I added in PR #921, it still uses the hard-coded restrictions though and don't do much type checks. |
||
|
||
|
||
@overload | ||
def mean(x: list[f64]) -> f64: | ||
""" | ||
Returns the arithmetic mean of a data sequence of numbers | ||
""" | ||
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 | ||
return sum/k | ||
|
||
|
||
@overload | ||
def fmean(x: list[i32]) -> f64: | ||
""" | ||
Returns the floating type arithmetic mean of a data sequence of numbers | ||
""" | ||
return mean(x) | ||
|
||
|
||
@overload | ||
def fmean(x: list[i64]) -> f64: | ||
""" | ||
Returns the floating type arithmetic mean of a data sequence of numbers | ||
""" | ||
return mean(x) | ||
|
||
|
||
@overload | ||
def fmean(x: list[f64]) -> f64: | ||
""" | ||
Returns the floating type arithmetic mean of a data sequence of numbers | ||
""" | ||
return mean(x) | ||
|
||
|
||
@overload | ||
def fmean(x: list[f32]) -> f64: | ||
""" | ||
Returns the floating type arithmetic mean of a data sequence of numbers | ||
""" | ||
return mean(x) | ||
|
||
|
||
def geometric_mean(x: list[i32]) -> f64: | ||
""" | ||
Returns the geometric mean of a data sequence of numbers | ||
""" | ||
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 | ||
return product**(1/k) | ||
|
||
|
||
def harmonic_mean(x: list[i32]) -> f64: | ||
""" | ||
Returns the harmonic mean of a data sequence of numbers | ||
""" | ||
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: | ||
if x[i] == 0: | ||
return 0.0 | ||
sum += 1 / x[i] | ||
ans: f64 | ||
ans = k/sum | ||
return ans | ||
return k/sum |
Uh oh!
There was an error while loading. Please reload this page.