Skip to content

any() and all() should support axis and keep_dims #1555

Open

Description

@DavisVaughan

To match numpy, xt::any() and xt::all() should have an axis argument allowing for multiple axes (it should probably be called axes). Maybe it can be implemented with the reduction framework that already exists?

https://docs.scipy.org/doc/numpy/reference/generated/numpy.any.html
https://docs.scipy.org/doc/numpy/reference/generated/numpy.all.html

The only trouble is making sure you can early exit with things like xt::any(). If you see any true values along the way, exit immediately for performance.

It actually doesn't look like numpy has optimized for the case I mention above, unless _wrapreduction() does something clever that I'm not aware of
https://github.com/numpy/numpy/blob/v1.16.1/numpy/core/fromnumeric.py#L2164

Activity

  1. wolfv commented on Jun 5, 2019

    @wolfv
    Member

    we should move any and all to the reducer framework.

  2. DavisVaughan commented on Jun 5, 2019

    @DavisVaughan
    ContributorAuthor

    i agree!

  3. wolfv commented on Jun 5, 2019

    @wolfv
    Member

    It's actually a "simple" fix - just need to pass a correct reducing lambda or struct to the make reducer macro.

    btw. where was your issue with the cum* functions? I was looking for that :)

  4. wolfv commented on Jun 5, 2019

    @wolfv
    Member

    ah yeah, except for the fast exit ...

  5. DavisVaughan commented on Jun 5, 2019

    @DavisVaughan
    ContributorAuthor

    i believe its this one
    #1334

    linked them all from
    DavisVaughan/rray1#30

  6. Ivorforce commented on Sep 19, 2024

    @Ivorforce

    I was able to get axis support for any / all pretty easily using the reduction macros:

    using namespace xt;
    XTENSOR_REDUCER_FUNCTION(va_any, xt::detail::logical_or, bool, true)
    XTENSOR_REDUCER_FUNCTION(va_all, xt::detail::logical_and, bool, false)

    To make use of the optimized implementation with no axes provided, I currently use:

    #define ReducerAnyAll(Name, fun_name_axes, fun_name_no_axes)\
    	template <typename GivenAxes, typename A>\
    	auto operator()(GivenAxes&& axes, A&& a) const {\
    		return fun_name_axes(std::forward<A>(a), std::forward<GivenAxes>(axes), std::tuple<xt::evaluation_strategy::lazy_type>());\
    	}\
    \
    	template <typename A>\
    	auto operator()(A&& a) const {\
    		return xt::xtensor_fixed<bool, xshape<>>(fun_name_no_axes(std::forward<A>(a)));\
    	}
    
    struct All { ReducerAnyAll(All, va_all, xt::all) };
    struct Any { ReducerAnyAll(Any, va_any, xt::any) };

    It would be nice if the implementations were unified so this workaround can be dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions