def putmask(array, mask, values): """putmask() sets elements of 'array' for which 'mask' is non-zero to the corresponding element in 'values'. 'array' must be an array. """ bmask = _nc.asarray(mask) bvalues = _nc.asarray(values) if bmask.nelements() == array.nelements(): if bmask._shape != array._shape: bmask = bmask.view() bmask.setshape(array.getshape()) else: bmask = _broadcast_or_resize(array, bmask) bvalues = _broadcast_or_resize(array, bvalues) choose(bmask != 0, (array, bvalues), array)
def repeat(array, repeats, axis=0): """repeat() returns a new array with each element 'a[i]' repeated 'r[i]' times. """ if axis == 0: return _repeat(_nc.asarray(array), repeats) else: return swapaxes( _repeat(swapaxes(array, 0, axis), repeats), 0, axis)
def _repeat(array, repeats): if ufunc._isScalar(repeats): repeats = (repeats,)*len(array) else: repeats = _nc.asarray(repeats, type=_nt.MaybeLong) total = ufunc.add.reduce(repeats) newshape = (total,)+array._shape[1:] newarray = array.__class__(shape=newshape, type=array._type) newi = 0; for i in range(len(repeats)): limit = repeats[i] for j in range(limit): newarray[newi+j] = array[i] newi += limit return newarray