def _ireduce_ufunc_new_axis(arrays, ufunc, **kwargs):
"""
Reduction operation for arrays, in the direction of a new axis (i.e. stacking).
Parameters
----------
arrays : iterable
Arrays to be reduced.
ufunc : numpy.ufunc
Binary universal function. Must have a signature of the form ufunc(x1, x2, ...)
kwargs
Keyword arguments are passed to ``ufunc``.
Yields
------
reduced : ndarray
"""
arrays = iter(arrays)
first = next(arrays)
kwargs.pop('axis')
dtype = kwargs.get('dtype', None)
if dtype is None:
dtype = first.dtype
else:
kwargs['casting'] = 'unsafe'
# If the out parameter was already given
# we create the accumulator from it
# Otherwise, it is a copy of the first array
accumulator = kwargs.pop('out', None)
if accumulator is not None:
accumulator[:] = first
else:
accumulator = np.array(first, copy = True).astype(dtype)
yield accumulator
for array in arrays:
ufunc(accumulator, array, out = accumulator, **kwargs)
yield accumulator
评论列表
文章目录