projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add inital cut of iter_utils with a coupel of iters I needed. Tweak
[python_utils.git]
/
math_utils.py
diff --git
a/math_utils.py
b/math_utils.py
index dd26cb6f175dea80ae28181c9cf983115b37f323..ed9c2f450f0fb0a9b121069a19d4a0b1fe7acfba 100644
(file)
--- a/
math_utils.py
+++ b/
math_utils.py
@@
-41,6
+41,8
@@
class NumericPopulation(object):
self.lowers, self.highers = [], []
self.aggregate = 0.0
self.sorted_copy: Optional[List[float]] = None
self.lowers, self.highers = [], []
self.aggregate = 0.0
self.sorted_copy: Optional[List[float]] = None
+ self.maximum = None
+ self.minimum = None
def add_number(self, number: float):
"""Adds a number to the population. Runtime complexity of this
def add_number(self, number: float):
"""Adds a number to the population. Runtime complexity of this
@@
-52,6
+54,10
@@
class NumericPopulation(object):
heappush(self.lowers, -number) # for lowers we need a max heap
self.aggregate += number
self._rebalance()
heappush(self.lowers, -number) # for lowers we need a max heap
self.aggregate += number
self._rebalance()
+ if not self.maximum or number > self.maximum:
+ self.maximum = number
+ if not self.minimum or number < self.minimum:
+ self.minimum = number
def _rebalance(self):
if len(self.lowers) - len(self.highers) > 1:
def _rebalance(self):
if len(self.lowers) - len(self.highers) > 1:
@@
-96,7
+102,7
@@
class NumericPopulation(object):
variance += (n - mean) ** 2
for n in self.highers:
variance += (n - mean) ** 2
variance += (n - mean) ** 2
for n in self.highers:
variance += (n - mean) ** 2
- count = len(self.lowers) + len(self.highers)
- 1
+ count = len(self.lowers) + len(self.highers)
return math.sqrt(variance) / count
def _create_sorted_copy_if_needed(self, count: int):
return math.sqrt(variance) / count
def _create_sorted_copy_if_needed(self, count: int):
@@
-120,7
+126,8
@@
class NumericPopulation(object):
self._create_sorted_copy_if_needed(count)
assert self.sorted_copy
index = round(count * (n / 100.0))
self._create_sorted_copy_if_needed(count)
assert self.sorted_copy
index = round(count * (n / 100.0))
- assert 0 <= index < count
+ index = max(0, index)
+ index = min(count - 1, index)
return self.sorted_copy[index]
return self.sorted_copy[index]