[Numpy-discussion] subtract.reduce behavior

Johann Hibschman jhibschman+numpy at gmail.com
Mon Jul 26 09:41:15 EDT 2010


Pauli Virtanen <pav at iki.fi> writes:

>> Returning a *right* identity for an operation that is otherwise a *left*
>> fold is very odd, no matter how you slice it. That is what looks like
>> special casing...
>
> I think I see your point now.

I know this is unlikely to happen, since it would break things for a
mostly-cosmetic (and that probably only in my eyes) improvement, but if
reduce were defined as a *right* fold, then it would make sense for
subtract (and divide) to use the right identity.

A right fold is also perhaps more interesting, since it can be used to
do alternating series, while the regular left-fold of subtract is pretty
pointless.  It also seems more natural that subtract.reduce([]) returns
0, because then we can partition the sequence however we want and
preserve np.subtract.reduce(np.append(x[:i], np.subtract.reduce(x[i:])))
== np.subtract.reduce(x) for any i.

But that's really just idle musing.  This is, by the way, how J (the
APL-derived array language) does it, but there it's very natural to do
right folds since all operations are right-associative.

Cheers,
Johann




More information about the NumPy-Discussion mailing list