From 75d4c2240cc9c66480fd39c11baf6d42f1a5d58b Mon Sep 17 00:00:00 2001 From: wirawan Date: Fri, 28 May 2010 18:43:59 +0000 Subject: [PATCH] * Function ztol: for zeroing out array elements below certain tolerance. --- math/__init__.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/math/__init__.py b/math/__init__.py index 1b60624..236e51b 100644 --- a/math/__init__.py +++ b/math/__init__.py @@ -1,7 +1,30 @@ -# $Id: __init__.py,v 1.1 2010-01-20 03:11:01 wirawan Exp $ +# $Id: __init__.py,v 1.2 2010-05-28 18:43:59 wirawan Exp $ # # wpylib.math main module # Created: 20091204 # Wirawan Purwanto # pass + +import numpy + +ZERO_TOL = 5.0e-16 + +def ztol(val, tol=None, copy=True): + """Rounds down values to zero if they are below tolerance.""" + if tol == None: tol = ZERO_TOL + if "__iter__" not in dir(val): + if numpy.abs(val) < tol: + return 0 + else: + return val + elif isinstance(val, numpy.ndarray): + if copy: + rslt = val.copy() + else: + rslt = val + numpy.putmask(rslt, numpy.abs(rslt) < tol, [0]) + return rslt + else: + raise ValueError, "Unsupported datatype: %s" % str(type(val)) +