Internal Function. Call `func` with `a` as first argument swapping the axes to use extended axis on functions that don't support it natively. Returns result and a.shape with axis dims set to 1. Parameters ---------- a : array_like Input array or object that can
(a, func, keepdims=False, **kwargs)
| 3761 | |
| 3762 | |
| 3763 | def _ureduce(a, func, keepdims=False, **kwargs): |
| 3764 | """ |
| 3765 | Internal Function. |
| 3766 | Call `func` with `a` as first argument swapping the axes to use extended |
| 3767 | axis on functions that don't support it natively. |
| 3768 | |
| 3769 | Returns result and a.shape with axis dims set to 1. |
| 3770 | |
| 3771 | Parameters |
| 3772 | ---------- |
| 3773 | a : array_like |
| 3774 | Input array or object that can be converted to an array. |
| 3775 | func : callable |
| 3776 | Reduction function capable of receiving a single axis argument. |
| 3777 | It is called with `a` as first argument followed by `kwargs`. |
| 3778 | kwargs : keyword arguments |
| 3779 | additional keyword arguments to pass to `func`. |
| 3780 | |
| 3781 | Returns |
| 3782 | ------- |
| 3783 | result : tuple |
| 3784 | Result of func(a, **kwargs) and a.shape with axis dims set to 1 |
| 3785 | which can be used to reshape the result to the same shape a ufunc with |
| 3786 | keepdims=True would produce. |
| 3787 | |
| 3788 | """ |
| 3789 | a = np.asanyarray(a) |
| 3790 | axis = kwargs.get('axis', None) |
| 3791 | out = kwargs.get('out', None) |
| 3792 | |
| 3793 | if keepdims is np._NoValue: |
| 3794 | keepdims = False |
| 3795 | |
| 3796 | nd = a.ndim |
| 3797 | if axis is not None: |
| 3798 | axis = _nx.normalize_axis_tuple(axis, nd) |
| 3799 | |
| 3800 | if keepdims: |
| 3801 | if out is not None: |
| 3802 | index_out = tuple( |
| 3803 | 0 if i in axis else slice(None) for i in range(nd)) |
| 3804 | kwargs['out'] = out[(Ellipsis, ) + index_out] |
| 3805 | |
| 3806 | if len(axis) == 1: |
| 3807 | kwargs['axis'] = axis[0] |
| 3808 | else: |
| 3809 | keep = set(range(nd)) - set(axis) |
| 3810 | nkeep = len(keep) |
| 3811 | # swap axis that should not be reduced to front |
| 3812 | for i, s in enumerate(sorted(keep)): |
| 3813 | a = a.swapaxes(i, s) |
| 3814 | # merge reduced axis |
| 3815 | a = a.reshape(a.shape[:nkeep] + (-1,)) |
| 3816 | kwargs['axis'] = -1 |
| 3817 | else: |
| 3818 | if keepdims: |
| 3819 | if out is not None: |
| 3820 | index_out = (0, ) * nd |