[Numpy-discussion] Adding .abs() method to the array object

Sebastian Berg sebastian at sipsolutions.net
Tue Feb 26 04:58:03 EST 2013


On Mon, 2013-02-25 at 22:04 -0500, josef.pktd at gmail.com wrote:
> On Mon, Feb 25, 2013 at 9:58 PM,  <josef.pktd at gmail.com> wrote:
> > On Mon, Feb 25, 2013 at 9:20 PM, Sebastian Berg
> > <sebastian at sipsolutions.net> wrote:
> >> On Mon, 2013-02-25 at 10:50 -0500, Skipper Seabold wrote:
> >>> On Mon, Feb 25, 2013 at 10:43 AM, Till Stensitzki <mail.till at gmx.de>
> >>> wrote:
> >>> >
> >>> > First, sorry that i didnt search for an old thread, but because i
> >>> disagree with
> >>> > conclusion i would at least address my reason:
> >>> >
<snip>
> >> Two small things (not sure if it matters much). But first almost all of
> >> these methods are related to the container and not the elements. Second
> >> actually using a method arr.abs() has a tiny pitfall, since abs would
> >> work on numpy types, but not on python types. This means that:
> >>
> >> np.array([1, 2, 3]).max().abs()
> >>
> >> works, but
> >>
> >> np.array([1, 2, 3], dtype=object).max().abs()
> >>
> >> breaks. Python has a safe name for abs already...
> >
> >>>> (np.array([1, 2, 3], dtype=object)).max()
> > 3
> >>>> (np.array([1, 2, 3], dtype=object)).__abs__().max()
> > 3
> >>>> (np.array([1, 2, '3'], dtype=object)).__abs__()
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > TypeError: bad operand type for abs(): 'str'
> >
> >>>> map(abs, [1, 2, 3])
> > [1, 2, 3]
> >>>> map(abs, [1, 2, '3'])
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > TypeError: bad operand type for abs(): 'str'
> 
> or maybe more useful
> 
> >>> from decimal import Decimal
> >>> d = [Decimal(str(k)) for k in np.linspace(-1, 1, 5)]
> >>> map(abs, d)
> [Decimal('1.0'), Decimal('0.5'), Decimal('0.0'), Decimal('0.5'), Decimal('1.0')]
> 
> >>> np.asarray(d).__abs__()
> array([1.0, 0.5, 0.0, 0.5, 1.0], dtype=object)
> >>> np.asarray(d).__abs__()[0]
> Decimal('1.0')
> 
> Josef
> 
> >
> > I don't see a difference.
> >
> > (I don't expect to use max abs on anything else than numbers.)
> >

The difference is about scalars only. And of course __abs__ is fine, but
if numpy adds an abs method, its scalars would logically have it too.
But then you diverge from python scalars. That has exactly the downside
that you may write code that suddenly stops working for python scalars
without noticing.

I turned around the abs and max order here, so that the abs works on the
scalar, not useful but just as an example.

> > Josef
> >>
> >>
> >>> I find myself typing things like
> >>>
> >>> arr.abs()
> >>>
> >>> and
> >>>
> >>> arr.unique()
> >>>
> >>> quite often.
> >>>
> >>> Skipper
> >>> _______________________________________________
> >>> NumPy-Discussion mailing list
> >>> NumPy-Discussion at scipy.org
> >>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >>
> >>
> >> _______________________________________________
> >> NumPy-Discussion mailing list
> >> NumPy-Discussion at scipy.org
> >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 





More information about the NumPy-Discussion mailing list