On Sun, Mar 7, 2010 at 4:30 AM, Friedrich Romstedt <span dir="ltr"><<a href="mailto:friedrichromstedt@gmail.com">friedrichromstedt@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
First, to David's routine:<br>
<br>
2010/3/7 David Goldsmith <<a href="mailto:d.l.goldsmith@gmail.com">d.l.goldsmith@gmail.com</a>>:<br>
<div class="im">> def convert_close(arg):<br>
>     arg = N.array(arg)<br>
>     if not arg.shape:<br>
>         arg = N.array((arg,))<br>
>     if arg.size:<br>
>         t = N.array([0 if N.allclose(temp, 0) else temp for temp in arg])<br>
>         if len(t.shape) - 1:<br>
>             return N.squeeze(t)<br>
>         else:<br>
>             return t<br>
>     else:<br>
>         return N.array()<br>
<br>
</div>Ok, chaps, let's code:<br>
<br>
import numpy<br>
<br>
def convert_close(ndarray, atol = 1e-5, rtol = 1e-8):<br>
    ndarray_abs = abs(ndarray)<br>
    mask = (ndarray_abs > atol + rtol * ndarray_abs)<br>
    return ndarray * mask<br>
<br>
> python -i close.py<br>
>>> a = numpy.asarray([1e-6])<br>
>>> convert_close(a)<br>
array([ 0.])<br>
>>> a = numpy.asarray([1e-6, 1])<br>
>>> convert_close(a)<br>
array([ 0.,  1.])<br>
>>> a = numpy.asarray(1e-6)<br>
>>> convert_close(a)<br>
0.0<br>
>>> a = numpy.asarray([-1e-6, 1])<br>
>>> convert_close(a)<br>
array([ 0.,  1.])<br></blockquote><div><br>Great, thanks!<br><br>DG<br></div><br></div>