Skip to content

ufunc identity of -inf #7702

Closed
Closed

Description

@mikegraham

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.

Activity

  1. njsmith commented on Jun 3, 2016

    @njsmith
    Member

    Fyi the function already exists as np.logaddexp

  2. mhvk commented on Jun 3, 2016

    @mhvk
    Contributor

    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!

  3. mikegraham commented on Jun 3, 2016

    @mikegraham
    Author

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

  4. mikegraham commented on Jun 3, 2016

    @mikegraham
    Author

    @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.

  5. seberg commented on Jun 5, 2016

    @seberg
    Member

    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).

  6. eric-wieser commented on Apr 16, 2017

    @eric-wieser
    Member

    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

  7. mikegraham commented on Jul 15, 2017

    @mikegraham
    Author

    Many thanks, @eric-wieser

  8. 2 remaining items

  9. mikegraham commented on Jul 15, 2017

    @mikegraham
    Author

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

  10. added a commit that references this issue on Nov 18, 2018
    4c277f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions