You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to write a function that, given log(x) and log(y), will compute log(x + y). The identity for this is log(0) = -inf. Currently, numpy only allows the values of 0, 1, -1, and None, as far as I can tell. It would be nice for my purposes if -inf or an arbitrary value could be used.
I guess the question then becomes whetherlogaddexp.identity could be -inf (currently it is None). Although I'm not quite sure whether it would help all that much for logaddexp(np.array([])) to yield -inf. ... ... Except perhaps for MaskArray where reducing over an axis could well have parts for which there are no elements ... ... Which led me to think why don't I check np.ma.logaddexp, only to find it doesn't exist!
@mhvk I don't necessarily agree: someone could be indexing a plain array to filter instead of using a ma, or someone could be initializing a result for a generic reduce where some of the loops are plain-old for loops (not over an array that actually exists) so you don't have to build the whole thing in memory. (The latter was my application.) It's a little more work to specialcase the first value even if I require it to exist.
It does look like it might be more trouble / complication-adding than it's worth to add the ability to have an identity of -inf, but I think there is some utility to it.
Adding -inf from within numpy is reasonably easy, we recently added the -1, so you could find a template on how to do it. I would not mind if we had a way of defining the identity more specific, though. One could also think about an identity=... kwarg, to override the identity, or define one for functions where it is unclear what the identity should be (i.e. objects it is common, or np.nan could be a desired result to avoid the error).
Activity
njsmith commentedon Jun 3, 2016
Fyi the function already exists as np.logaddexp
mhvk commentedon Jun 3, 2016
I guess the question then becomes whether
logaddexp.identitycould be-inf(currently it isNone). Although I'm not quite sure whether it would help all that much forlogaddexp(np.array([]))to yield-inf. ... ... Except perhaps forMaskArraywhere reducing over an axis could well have parts for which there are no elements ... ... Which led me to think why don't I checknp.ma.logaddexp, only to find it doesn't exist!mikegraham commentedon Jun 3, 2016
@njsmith Wow, thanks, I had thought I checked well enough... ;)
mikegraham commentedon Jun 3, 2016
@mhvk I don't necessarily agree: someone could be indexing a plain array to filter instead of using a ma, or someone could be initializing a result for a generic reduce where some of the loops are plain-old for loops (not over an array that actually exists) so you don't have to build the whole thing in memory. (The latter was my application.) It's a little more work to specialcase the first value even if I require it to exist.
It does look like it might be more trouble / complication-adding than it's worth to add the ability to have an identity of -inf, but I think there is some utility to it.
seberg commentedon Jun 5, 2016
Adding -inf from within numpy is reasonably easy, we recently added the -1, so you could find a template on how to do it. I would not mind if we had a way of defining the identity more specific, though. One could also think about an
identity=...kwarg, to override the identity, or define one for functions where it is unclear what the identity should be (i.e. objects it is common, or np.nan could be a desired result to avoid the error).eric-wieser commentedon Apr 16, 2017
If we want to allow arbitrary identity values, I think we should pull out
ufunc.reorderablefrom the Cufunc.identity.There was another recent PR about identity at #8255
mikegraham commentedon Jul 15, 2017
Many thanks, @eric-wieser
2 remaining items
mikegraham commentedon Jul 15, 2017
Oops, I misread (I thought it was merged). Thanks all the same :)
ENH: Correct identities for logical ufuncs and logaddexp