On Sun, Mar 7, 2010 at 4:30 AM, Friedrich Romstedt <friedrichromstedt@gmail.com> wrote:
First, to David's routine:
2010/3/7 David Goldsmith <d.l.goldsmith@gmail.com>:
> def convert_close(arg):Ok, chaps, let's code:
> arg = N.array(arg)
> if not arg.shape:
> arg = N.array((arg,))
> if arg.size:
> t = N.array([0 if N.allclose(temp, 0) else temp for temp in arg])
> if len(t.shape) - 1:
> return N.squeeze(t)
> else:
> return t
> else:
> return N.array()
import numpy
def convert_close(ndarray, atol = 1e-5, rtol = 1e-8):
ndarray_abs = abs(ndarray)
mask = (ndarray_abs > atol + rtol * ndarray_abs)
return ndarray * mask
> python -i close.py
>>> a = numpy.asarray([1e-6])
>>> convert_close(a)
array([ 0.])
>>> a = numpy.asarray([1e-6, 1])
>>> convert_close(a)
array([ 0., 1.])
>>> a = numpy.asarray(1e-6)
>>> convert_close(a)
0.0
>>> a = numpy.asarray([-1e-6, 1])
>>> convert_close(a)
array([ 0., 1.])