8000 ufunc identity of -inf · Issue #7702 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ufunc identity of -inf #7702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mikegraham opened this issue Jun 3, 2016 · 9 comments
Closed

ufunc identity of -inf #7702

mikegraham opened this issue Jun 3, 2016 · 9 comments

Comments

@mikegraham
Copy link

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.

@njsmith
Copy link
Member
njsmith commented Jun 3, 2016

Fyi the function already exists as np.logaddexp

@mhvk
Copy link
Contributor
mhvk commented Jun 3, 2016

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!

@mikegraham
Copy link
Author

@njsmith Wow, thanks, I had thought I checked well enough... ;)

@mikegraham
Copy link
Author
mikegraham commented 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
Copy link
Member
seberg commented 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
Copy link
Member
eric-wieser commented Apr 16, 2017

If we want to allow arbitrary identity values, I think we should pull out ufunc.reorderable from the C ufunc.identity.

There was another recent PR about identity at #8255

eric-wieser added a commit to eric-wieser/numpy that referenced this issue Apr 17, 2017
There didn't seem to be any value to a `assign_identity` function - all we
actually care about is the value to assign.

This also fixes numpy#8860 as a side-effect, and paves the way for:

* easily adding more values (numpy#7702)
* using the identity in more places (numpy#834)
@mikegraham
Copy link
Author

Many thanks, @eric-wieser

@eric-wieser
Copy link
Member

I'm afraid this is a way off from being merged since it would break the ufunc abi, so I'd prefer to leave this open.

@eric-wieser eric-wieser reopened this Jul 15, 2017
@mikegraham
Copy link
Author

Oops, I misread (I thought it was merged). Thanks all the same :)

eric-wieser added a commit to eric-wieser/numpy that referenced this issue Oct 3, 2017
There didn't seem to be any value to a `assign_identity` function - all we
actually care about is the value to assign.

This also fixes numpy#8860 as a side-effect, and paves the way for:

* easily adding more values (numpy#7702)
* using the identity in more places (numpy#834)
eric-wieser added a commit to eric-wieser/numpy that referenced this issue Nov 12, 2018
liwt31 pushed a commit to liwt31/numpy that referenced this issue Nov 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0