Skip to content

Commit

Permalink
Working sum(…). The speed-up is impressive: 5k numbers went from 17 s…
Browse files Browse the repository at this point in the history
… to a fraction of a second. 100k numbers are 1.5 s. 1,000,000 numbers are 15 s. Linear time complexity is observed, indeed! :)
  • Loading branch information
lebigot committed Aug 13, 2016
1 parent 6f68bf7 commit 84a27f5
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions uncertainties/core.py
Expand Up @@ -239,7 +239,7 @@ def to_affine_scalar(x):

if isinstance(x, CONSTANT_TYPES):
# No variable => no derivative:
return Variable(x, [])
return Variable(x, NestedLinearCombination())

# Case of lists, etc.
raise NotUpcast("%s cannot be converted to a number with"
Expand Down Expand Up @@ -707,7 +707,8 @@ def f_with_affine_output(*args, **kwargs):

# The function now returns the necessary linear approximation
# to the function:
return AffineScalarFunc(f_nominal_value, linear_part)
return AffineScalarFunc(f_nominal_value,
NestedLinearCombination(linear_part))

f_with_affine_output = set_doc("""\
Version of %s(...) that returns an affine approximation
Expand Down Expand Up @@ -1435,7 +1436,9 @@ def signif_dgt_to_limit(value, num_signif_d):
# !!!!!!!! This creates a copy of the list, when the expression is
# built: I should probably just store linear_combo. !!!!!!! SAME for
# a new FlatLinearCombination, that I would access explicitly between
# class that know how it works.
# class that know how it works. Using slots should be quite cheap in
# memory. BUT I would pay with attribute access, when doing
# calculations.
class NestedLinearCombination(list):
"""
List of (float_coefficient, NestedLinearCombination) or
Expand Down Expand Up @@ -1475,17 +1478,22 @@ def expand_and_empty(self):
# remaining (and whose size can temporarily grow):
main_factor, main_expr = self.pop()

# print "MAINS", main_factor, main_expr #!!!!!!!!!!!!

if isinstance(main_expr, NestedLinearCombination):

for (factor, expr) in main_expr.iteritems():
for (factor, expr) in main_expr:
# The main_factor is applied to expr:
self.append((main_factor*factor, expr))

else: # We directly have an expanded linear combination

for (factor, var) in main_expr.iteritems():
for (var, factor) in main_expr.iteritems():
derivatives[var] += main_factor*factor

# print "DERIV", derivatives #!!!!!!!!!!!


return derivatives

class AffineScalarFunc(object):
Expand Down Expand Up @@ -1614,6 +1622,7 @@ def derivatives(self):
# Attempts to get the contribution of a variable that the
# function does not depend on raise a KeyError:
self._linear_part.default_factory = None

return self._linear_part

############################################################
Expand Down Expand Up @@ -1711,6 +1720,10 @@ def error_components(self):

for (variable, derivative) in self.derivatives.iteritems():

# print "TYPE", type(variable), type(derivative) #!!!!!!!!!!!!



# Individual standard error due to variable:

# 0 is returned even for a NaN derivative (in this case no
Expand Down

0 comments on commit 84a27f5

Please sign in to comment.