[Numpy-discussion] How to compare an array of arrays elementwise to None in

Sebastian Berg sebastian at sipsolutions.net
Wed Jul 19 08:55:18 EDT 2017


On Wed, 2017-07-19 at 08:31 +0000, Martin.Gfeller at swisscom.com wrote:
> Thank you for your help!
> 
> Sebastian, I couldn't agree more with someone's bug being someone
> else's feature! A fast identity ufunc would be useful, though. 
> 

An `object_identity` ufunc should be very easy to implement, the bigger
work is likely to actually decide on it and the name. Also should
probably check back with the PyPy guys to make sure it would also work
on PyPy.

- Sebastian


> Actually, numpy.frompyfunc(operator.is_,2,1) is much faster than the
> numpy.vectorize approach - only about 35% slower on quick
> measurement 
> than the direct ==, as opposed to 62% slower with vectorize (with
> otypes hint). 
> 
> Robert, yes, that's what I already did provisionally. 
> 
> Eric, that is a nice puzzle - but I agree with Robert about
> understanding by code maintainers. 
> 
> Thanks again, and best regards,
> Martin
> 
> 
> 
> 
> On Mon, 17 Jul 2017 11:41 Sebastian Berg <sebastian at sipsolutions.net>
> write
> 
> > Yes, I guess ones bug is someone elses feature :(, if it is very
> > bad, we could delay the deprecation probably. For a solutions,
> > maybe 
> > we could add a ufunc  for elementwise `is` on object arrays (dunno
> > about the name, maybe `object_identity`.
> > Just some quick thoughts.
> > - Sebastian
> 
> On Mon, 17 Jul 2017 at 17:45 Robert Kern <robert.kern at gmail.com>
> wrote:
> 
> > Wrap the clumsiness up in a documented, tested utility function
> > with a descriptive name and use that function everywhere instead.
> > Robert Kern
> 
> On Mon, Jul 17, 2017 at 10:52 AM, Eric Wieser <wieser.eric+numpy at gmai
> l.com>
> wrote:
> 
> > Here's a hack that lets you keep using ==:
> > 
> > class IsCompare:
> >     __array_priority__ = 999999  # needed to make it work on either
> > side of `==`
> >     def __init__(self, val): self._val = val
> >     def __eq__(self, other): return other is self._val
> >     def __neq__(self, other): return other is not self._val
> > 
> > a == IsCompare(None)  # a is None
> > a == np.array(IsCompare(None)) # broadcasted a is None
> > 
> > 
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170719/6baecbbd/attachment.sig>


More information about the NumPy-Discussion mailing list