[Numpy-discussion] Why is the shape of a singleton array the empty tuple?

David Goldsmith d.l.goldsmith at gmail.com
Mon Mar 8 02:22:14 EST 2010

```On Sun, Mar 7, 2010 at 4:30 AM, Friedrich Romstedt <
friedrichromstedt at gmail.com> wrote:

> First, to David's routine:
>
> 2010/3/7 David Goldsmith <d.l.goldsmith at gmail.com>:
> > def convert_close(arg):
> >     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()
>
> Ok, chaps, let's code:
>
> import numpy
>
> def convert_close(ndarray, atol = 1e-5, rtol = 1e-8):
>    ndarray_abs = abs(ndarray)
>    mask = (ndarray_abs > atol + rtol * ndarray_abs)
>
> > 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.])
>

Great, thanks!

DG
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100307/f05a265e/attachment.html>
```