Skip to content
Open
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
61 changes: 61 additions & 0 deletions maths/harmonic_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
The Harmonic Mean of n numbers is defined as n divided by the sum of the
reciprocals of those numbers. It is used to calculate average rates and ratios.
https://en.wikipedia.org/wiki/Harmonic_mean
"""

from typing import Union


def compute_harmonic_mean(*args: Union[int, float]) -> float:

Check failure on line 10 in maths/harmonic_mean.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP007)

maths/harmonic_mean.py:10:34: UP007 Use `X | Y` for type annotations

Check failure on line 10 in maths/harmonic_mean.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PYI041)

maths/harmonic_mean.py:10:34: PYI041 Use `float` instead of `int | float`
"""
Return the harmonic mean of the argument numbers.

The harmonic mean is particularly useful for rates (e.g., speed, density).

>>> compute_harmonic_mean(1, 2, 4)
1.7142857142857142
>>> compute_harmonic_mean(2, 4)
2.6666666666666665
>>> compute_harmonic_mean('a', 4)
Traceback (most recent call last):
...
TypeError: Not a Number
>>> compute_harmonic_mean(1, 0)
Traceback (most recent call last):
...
ValueError: Cannot compute harmonic mean with zero values
>>> compute_harmonic_mean(1, -1)
Traceback (most recent call last):
...
ValueError: Cannot compute harmonic mean with non-positive values
>>> compute_harmonic_mean()
Traceback (most recent call last):
...
ValueError: At least one number is required
>>> compute_harmonic_mean(4, 4, 4)
4.0
>>> compute_harmonic_mean(1, 2)
1.3333333333333333
"""
if len(args) == 0:
raise ValueError("At least one number is required")

reciprocal_sum = 0
for number in args:
if not isinstance(number, (int, float)):
raise TypeError("Not a Number")
if number == 0:
raise ValueError("Cannot compute harmonic mean with zero values")
if number < 0:
raise ValueError("Cannot compute harmonic mean with non-positive values")
reciprocal_sum += 1 / number

return len(args) / reciprocal_sum


if __name__ == "__main__":
from doctest import testmod

testmod(name="compute_harmonic_mean")
print(compute_harmonic_mean(1, 2, 4))
Loading