Skip to content

ENH: Augment ufunc.reduce with state #8773

@eric-wieser

Description

@eric-wieser

Right now, reduce can be roughly described as:

it = iter_along_axis(arr)  #pseudocode
result = func.identity or next(it)
for i in it:
    result = func(result, i)
return result

Or diagramatically:

       x2     x3     x3      xn
       |      |      |       |
       V      V      V       V
x1 -> (f) -> (f) -> (f) ... (f) --> result

What I'm proposing is a more powerful variant that allows extra state to be preserved between each function call:

it = iter_along_axis(arr)  #pseudocode
result =  func.identity or next(it)
state = None
for i in it:
    result, state = func(result, i, reduce_state=state)
return result
         x2     x3     x3      xn
         |      |      |       |
         V      V      V       V
x1   -> ( ) -> ( ) -> ( ) ... ( ) --> result
        (f)    (f)    (f)     (f)
None -> ( ) -> ( ) -> ( ) ... ( )

This makes ufuncs capable of describing:

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