From ed1242dc6684cd2deb3eb68f213f43daf208f9be Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Wed, 2 Oct 2013 14:30:55 -0400 Subject: [PATCH] * Added "lazy string printout" which will speed up errorbar algebra--i.e. we only need to evaluate the string representation when the string printout isrequired. --- math/stats/errorbar.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/math/stats/errorbar.py b/math/stats/errorbar.py index 84fd27a..bb086a8 100644 --- a/math/stats/errorbar.py +++ b/math/stats/errorbar.py @@ -151,6 +151,7 @@ class errorbar(object): This value is custom-made.""" ebproc = staticmethod(compress_errorbar_cxx) + lazy_string = True def __init__(self, val, err, eb=None, ebproc=None): self.val = val @@ -158,7 +159,8 @@ class errorbar(object): if ebproc != None: self.ebproc = ebproc if eb == None: - self.eb = self.ebproc(val, err) + if not self.lazy_string: + self.eb = self.ebproc(val, err) else: self.eb = eb @@ -173,13 +175,24 @@ class errorbar(object): def __str__(self): if getattr(self, "eb", None): return self.eb + elif getattr(self, "eb", None) == None and getattr(self, "ebproc", None) != None: + self.eb = self.ebproc(self.val, self.err) + return self.eb else: return "%g +- %g" % (self.val, self.err) display = __str__ def __repr__(self): return "errorbar(%s,%s,'%s')" % (self.val, self.err, self.display()) + def update(self, val, err): + self.val = val + self.err = err + self.ebupdate() + return self def ebupdate(self): - self.eb = self.ebproc(self.val, self.err) + if self.lazy_string: + self.eb = None + else: + self.eb = self.ebproc(self.val, self.err) def copy(self): return self.__class__(self.val, self.err, self.eb, self.ebproc) # Some algebraic operations with scalars are defined here: