137
137
from itertools import groupby , repeat
138
138
from bisect import bisect_left , bisect_right
139
139
from math import hypot , sqrt , fabs , exp , erf , tau , log , fsum
140
+ from functools import reduce
140
141
from operator import mul
141
142
from collections import Counter , namedtuple , defaultdict
142
143
@@ -183,11 +184,12 @@ def _sum(data):
183
184
allowed.
184
185
"""
185
186
count = 0
187
+ types = set ()
188
+ types_add = types .add
186
189
partials = {}
187
190
partials_get = partials .get
188
- T = int
189
191
for typ , values in groupby (data , type ):
190
- T = _coerce ( T , typ ) # or raise TypeError
192
+ types_add ( typ )
191
193
for n , d in map (_exact_ratio , values ):
192
194
count += 1
193
195
partials [d ] = partials_get (d , 0 ) + n
@@ -199,6 +201,7 @@ def _sum(data):
199
201
else :
200
202
# Sum all the partial sums using builtin sum.
201
203
total = sum (Fraction (n , d ) for d , n in partials .items ())
204
+ T = reduce (_coerce , types , int ) # or raise TypeError
202
205
return (T , total , count )
203
206
204
207
@@ -214,11 +217,12 @@ def _ss(data, c=None):
214
217
T , total , count = _sum ((d := x - c ) * d for x in data )
215
218
return (T , total , count )
216
219
count = 0
220
+ types = set ()
221
+ types_add = types .add
217
222
sx_partials = defaultdict (int )
218
223
sxx_partials = defaultdict (int )
219
- T = int
220
224
for typ , values in groupby (data , type ):
221
- T = _coerce ( T , typ ) # or raise TypeError
225
+ types_add ( typ )
222
226
for n , d in map (_exact_ratio , values ):
223
227
count += 1
224
228
sx_partials [d ] += n
@@ -236,6 +240,7 @@ def _ss(data, c=None):
236
240
# This formula has poor numeric properties for floats,
237
241
# but with fractions it is exact.
238
242
total = (count * sxx - sx * sx ) / count
243
+ T = reduce (_coerce , types , int ) # or raise TypeError
239
244
return (T , total , count )
240
245
241
246
0 commit comments