Skip to content
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

ENH: Allow arrays to be passed into the initial kwarg of reductions #15975

Closed
wants to merge 1 commit into from

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Apr 14, 2020

This enables something like:

bound_upper = np.full((3,), -np.inf)
bound_lower = np.full((3,), np.inf)
for vertex_array in vertex_arrays:
    data_arr.max(out=bound_upper, initial=bound_upper)
    data_arr.min(out=bound_lower, initial=bound_lower)

It also resolves #15905, as same_kind casting is now used:

In [7]: np.array([-10,20,30,-25]).min(initial=np.inf)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-966c5d8ff062> in <module>
----> 1 np.array([-10,20,30,-25]).min(initial=np.inf)

~\Repos\numeric-python\numpy\build\testenv\Lib\site-packages\numpy\core\_methods.py in _amin(a, axis, out, keepdims, initial, where)
     41 def _amin(a, axis=None, out=None, keepdims=False,
     42           initial=_NoValue, where=True):
---> 43     return umr_minimum(a, axis, None, out, keepdims, initial, where)
     44
     45 def _sum(a, axis=None, dtype=None, out=None, keepdims=False,

TypeError: Cannot cast scalar from dtype('float64') to dtype('int32') according to the rule 'same_kind'

}
else {
/* infer the dtype, ReduceWrapper will perform same_kind casting to check later */
initial_arr = (PyArrayObject *)PyArray_FROM_O(initial);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember if PyArray_FROM_O makes a copy, but my intent was that it not do so.

@eric-wieser eric-wieser force-pushed the initial-as-array branch 5 times, most recently from 5c9e3fd to 012677f Compare April 15, 2020 10:23
Note that this changes the behavior of `initial=sequence`.
@eric-wieser
Copy link
Member Author

Tests fail because initial is not normalized wrt keepdims. We could use conform_reduce_result, but that doesn't support broadcasting, which is probably useful.

@eric-wieser eric-wieser marked this pull request as draft April 15, 2020 11:15
@seberg
Copy link
Member

seberg commented Apr 15, 2020

This would probably be simpler with gh-15162, but pushing that through may be a bit more work...

Base automatically changed from master to main March 4, 2021 02:04
@charris charris added the 52 - Inactive Pending author response label Apr 6, 2022
@charris charris closed this Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible bug in np.min, np.max, np.ndarray.min, np.ndarray.max
3 participants