The return shape has dimensions python code many other uses
and cumprod methods of arrays. The following Python code implements an
equivalent of the accumulate method.
This method is a generalization of both reduce and accumulate. It offers the
ability to reduce along an axis but only between certain indices. The indices input must be a one dimensional (index) sequence. Then, if Ik is the kthele-ment of indices, the reduceat method computes <op>.reduce(array[Ik:Ik+1]).
Equivalent Python code is� �� �
>>> i1 = [index exp[:]]*array.ndim
>>> i2 = [index exp[:]]*array.ndim
>>> outshape = list(array.shape)
>>> N = array.shape[axis]
>>> outshape[axis] = len(indices)
>>> result = zeros(outshape, dtype or array.dtype) >>> for k,Ik in enumerate(indices):
i1[axis] = k ...
Example: Suppose a is a two-dimensional array of shape 10 × 20. Then, res=add.reduce (a, [0,3,1]) returns a 3 × 20 array with res[0,:] = add.reduce(a[:,0:3]), res[1,:] = a[:,3], and res[2,:] = add.reduce(a[:,1:]).
9.4.4 Outer
Among many other uses, arithmetic tables can be conveniently built using outer:
>>> multiply.outer([1,7,9,12],arange(5,12))
array([[ | 5, | 6, | 10, | ||||||
---|---|---|---|---|---|---|---|---|---|
[ 35, |
|
|
56, | 63, | 70, | 77], | |||
[ 45, | 72, | 81, | 90, | 99], | |||||
[ 60, | 96, 108, 120, 132]]) |
166