[Numpy-discussion] Deprecate boolean math operators?

Sebastian Berg sebastian at sipsolutions.net
Fri Dec 6 04:39:36 EST 2013


On Thu, 2013-12-05 at 23:02 -0500, josef.pktd at gmail.com wrote:
> On Thu, Dec 5, 2013 at 10:56 PM, Alexander Belopolsky <ndarray at mac.com> wrote:
> > On Thu, Dec 5, 2013 at 5:37 PM, Sebastian Berg <sebastian at sipsolutions.net>
> > wrote:
> >> there was a discussion that for numpy booleans math operators +,-,* (and
> >> the unary -), while defined, are not very helpful.
> >
> > It has been suggested at the Github that there is an area where it is useful
> > to have linear algebra operations like matrix multiplication to be defined
> > over a semiring:
> >
> > http://en.wikipedia.org/wiki/Logical_matrix
> >
> > This still does not justify having unary or binary -, so I suggest that we
> > first discuss deprecation of those.
> 
> Does it make sense to only remove - and maybe / ?
> 
> would python sum still work?   (I almost never use it.)
> 
> >>> sum(mask)
> 2
> >>> sum(mask.tolist())
> 2
> 
> is accumulate the same as sum and would keep working?
> 
> >>> np.add.accumulate(mask)
> array([0, 0, 0, 1, 2])
> 
> 
> In operation with other dtypes, do they still dominate so these work?
> 

Hey,

of course the other types will always dominate interpreting bools as 0
and 1. This would only affect operations with only booleans. There is a
good point that * is well defined however you define it, though. (Btw. /
is not defined for bools, `np.bool_(True)/np.bool_(True)` will upcast to
int8 to do the operation)

However, while well defined, + is not defined like it is for python
bools (which are just ints) so that is the reason to consider
deprecation there (if we allow upcast to int8 -- or maybe the default
int -- in the future, in-place += and -= operations would not behave
differently, since they just cast back...).

I suppose python sum works because it first tries using the C-Api number
protocol, which also means it is not affected. If you were to write a
sum which just uses the `+` operator, it would be affected, but that
would seem good to me.

- Sebastian


> >>> x / mask
> array([0, 0, 0, 3, 4])
> >>> x * 1. / mask
> array([ nan,  inf,  inf,   3.,   4.])
> >>> x**mask
> array([1, 1, 1, 3, 4])
> >>> mask - 5
> array([-5, -5, -5, -4, -4])
> 
> Josef
> 
> >
> > _______________________________________________
> > 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