scripts: Renamed import math alias m -> mt

Mainly to avoid conflicts with match results m, this frees up the single
letter variables m for other purposes.

Choosing a two letter alias was surprisingly difficult, but mt is nice
in that it somewhat matches it (for itertools) and ft (for functools).
This commit is contained in:
Christopher Haster
2024-11-05 01:58:40 -06:00
parent 96ddc72481
commit 48c2e7784b
20 changed files with 258 additions and 256 deletions
+35 -33
View File
@@ -16,7 +16,7 @@ import collections as co
import csv
import functools as ft
import itertools as it
import math as m
import math as mt
import os
import re
@@ -27,20 +27,22 @@ import re
#
OPS = {
'sum': lambda xs: sum(xs[1:], start=xs[0]),
'prod': lambda xs: m.prod(xs[1:], start=xs[0]),
'prod': lambda xs: mt.prod(xs[1:], start=xs[0]),
'min': min,
'max': max,
'avg': lambda xs: RFloat(sum(float(x) for x in xs) / len(xs)),
'stddev': lambda xs: (
lambda avg: RFloat(
m.sqrt(sum((float(x) - avg)**2 for x in xs) / len(xs)))
mt.sqrt(sum((float(x) - avg)**2 for x in xs) / len(xs)))
)(sum(float(x) for x in xs) / len(xs)),
'gmean': lambda xs: RFloat(m.prod(float(x) for x in xs)**(1/len(xs))),
'gmean': lambda xs: RFloat(mt.prod(float(x) for x in xs)**(1/len(xs))),
'gstddev': lambda xs: (
lambda gmean: RFloat(
m.exp(m.sqrt(sum(m.log(float(x)/gmean)**2 for x in xs) / len(xs)))
if gmean else m.inf)
)(m.prod(float(x) for x in xs)**(1/len(xs))),
mt.exp(mt.sqrt(
sum(mt.log(float(x)/gmean)**2 for x in xs)
/ len(xs)))
if gmean else mt.inf)
)(mt.prod(float(x) for x in xs)**(1/len(xs))),
}
@@ -56,24 +58,24 @@ class RInt(co.namedtuple('RInt', 'x')):
except ValueError:
# also accept +-∞ and +-inf
if re.match('^\s*\+?\s*(?:∞|inf)\s*$', x):
x = m.inf
x = mt.inf
elif re.match('^\s*-\s*(?:∞|inf)\s*$', x):
x = -m.inf
x = -mt.inf
else:
raise
assert isinstance(x, int) or m.isinf(x), x
assert isinstance(x, int) or mt.isinf(x), x
return super().__new__(cls, x)
def __str__(self):
if self.x == m.inf:
if self.x == mt.inf:
return ''
elif self.x == -m.inf:
elif self.x == -mt.inf:
return '-∞'
else:
return str(self.x)
def __int__(self):
assert not m.isinf(self.x)
assert not mt.isinf(self.x)
return self.x
def __float__(self):
@@ -87,9 +89,9 @@ class RInt(co.namedtuple('RInt', 'x')):
new = self.x if self else 0
old = other.x if other else 0
diff = new - old
if diff == +m.inf:
if diff == +mt.inf:
return '%7s' % '+∞'
elif diff == -m.inf:
elif diff == -mt.inf:
return '%7s' % '-∞'
else:
return '%+7d' % diff
@@ -97,16 +99,16 @@ class RInt(co.namedtuple('RInt', 'x')):
def ratio(self, other):
new = self.x if self else 0
old = other.x if other else 0
if m.isinf(new) and m.isinf(old):
if mt.isinf(new) and mt.isinf(old):
return 0.0
elif m.isinf(new):
return +m.inf
elif m.isinf(old):
return -m.inf
elif mt.isinf(new):
return +mt.inf
elif mt.isinf(old):
return -mt.inf
elif not old and not new:
return 0.0
elif not old:
return +m.inf
return +mt.inf
else:
return (new-old) / old
@@ -131,18 +133,18 @@ class RFloat(co.namedtuple('RFloat', 'x')):
except ValueError:
# also accept +-∞ and +-inf
if re.match('^\s*\+?\s*(?:∞|inf)\s*$', x):
x = m.inf
x = mt.inf
elif re.match('^\s*-\s*(?:∞|inf)\s*$', x):
x = -m.inf
x = -mt.inf
else:
raise
assert isinstance(x, float), x
return super().__new__(cls, x)
def __str__(self):
if self.x == m.inf:
if self.x == mt.inf:
return ''
elif self.x == -m.inf:
elif self.x == -mt.inf:
return '-∞'
else:
return '%.1f' % self.x
@@ -157,9 +159,9 @@ class RFloat(co.namedtuple('RFloat', 'x')):
new = self.x if self else 0
old = other.x if other else 0
diff = new - old
if diff == +m.inf:
if diff == +mt.inf:
return '%7s' % '+∞'
elif diff == -m.inf:
elif diff == -mt.inf:
return '%7s' % '-∞'
else:
return '%+7.1f' % diff
@@ -193,8 +195,8 @@ class RFrac(co.namedtuple('RFrac', 'a,b')):
def notes(self):
t = self.a.x/self.b.x if self.b.x else 1.0
return ['%' if t == +m.inf
else '-∞%' if t == -m.inf
return ['%' if t == +mt.inf
else '-∞%' if t == -mt.inf
else '%.1f%%' % (100*t)]
def diff(self, other):
@@ -526,8 +528,8 @@ def table(Result, results, diff_results=None, *,
(getattr(r, k).table()
if getattr(r, k, None) is not None
else types[k].none,
(lambda t: ['+∞%'] if t == +m.inf
else ['-∞%'] if t == -m.inf
(lambda t: ['+∞%'] if t == +mt.inf
else ['-∞%'] if t == -mt.inf
else ['%+.1f%%' % (100*t)])(
types[k].ratio(
getattr(r, k, None),
@@ -546,8 +548,8 @@ def table(Result, results, diff_results=None, *,
(types[k].diff(
getattr(r, k, None),
getattr(diff_r, k, None)),
(lambda t: ['+∞%'] if t == +m.inf
else ['-∞%'] if t == -m.inf
(lambda t: ['+∞%'] if t == +mt.inf
else ['-∞%'] if t == -mt.inf
else ['%+.1f%%' % (100*t)] if t
else [])(
types[k].ratio(