def put(a, ind, v, mode='raise'): """ Replaces specified elements of an array with given values. The indexing works on the flattened target array. `put` is roughly equivalent to: :: a.flat[ind] = v Parameters ---------- a : ndarray Target array. ind : array_like Target indices, interpreted as integers. v : array_like Values to place in `a` at target indices. If `v` is shorter than `ind` it will be repeated as necessary. mode : {'raise', 'wrap', 'clip'}, optional Specifies how out-of-bounds indices will behave. * 'raise' -- raise an error (default) * 'wrap' -- wrap around * 'clip' -- clip to the range 'clip' mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers. See Also -------- putmask, place, take Examples -------- >>> a = np.arange(5) >>> np.put(a, [0, 2], [-44, -55]) >>> a array([-44, 1, -55, 3, 4]) >>> a = np.arange(5) >>> np.put(a, 22, -5, mode='clip') >>> a array([ 0, 1, 2, 3, -5]) """ if ind.size == 0: return # Nothing to insert! if not bhary.check(a): return numpy.put(a, ind.astype(numpy.int64), v, mode=mode) if mode != "raise": warnings.warn( "Bohrium only supports the 'raise' mode not '%s', " "it will be handled by the original NumPy." % mode, UserWarning, 2) return numpy.put(a, ind, v, mode=mode) indexes = array_manipulation.flatten(array_create.array( ind, dtype=numpy.uint64), always_copy=False) values = array_manipulation.flatten(array_create.array(v, dtype=a.dtype), always_copy=False) # Now let's make the shape of 'indexes' and 'values' match if indexes.size > values.size: if values.size == 1: # When 'values' is a scalar, we can broadcast it to match 'indexes' values = numpy_backport.as_strided(values, shape=indexes.shape, strides=(0, )) else: # else we repeat 'values' enough times to be larger than 'indexes' values = numpy_backport.as_strided( values, shape=(indexes.size // values.size + 2, values.size), strides=(0, values.itemsize)) values = array_manipulation.flatten(values, always_copy=False) # When 'values' is too large, we simple cut the end off if values.size > indexes.size: values = values[0:indexes.size] # Now that 'indexes' and 'values' have the same shape, we can call 'scatter' scatter(a, indexes, values)
def put(a, ind, v, mode='raise'): """ Replaces specified elements of an array with given values. The indexing works on the flattened target array. `put` is roughly equivalent to: :: a.flat[ind] = v Parameters ---------- a : ndarray Target array. ind : array_like Target indices, interpreted as integers. v : array_like Values to place in `a` at target indices. If `v` is shorter than `ind` it will be repeated as necessary. mode : {'raise', 'wrap', 'clip'}, optional Specifies how out-of-bounds indices will behave. * 'raise' -- raise an error (default) * 'wrap' -- wrap around * 'clip' -- clip to the range 'clip' mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers. See Also -------- putmask, place, take Examples -------- >>> a = np.arange(5) >>> np.put(a, [0, 2], [-44, -55]) >>> a array([-44, 1, -55, 3, 4]) >>> a = np.arange(5) >>> np.put(a, 22, -5, mode='clip') >>> a array([ 0, 1, 2, 3, -5]) """ if ind.size == 0: return # Nothing to insert! if not bhary.check(a): return numpy.put(a, ind.astype(numpy.int64), v, mode=mode) if mode != "raise": warnings.warn("Bohrium only supports the 'raise' mode not '%s', " "it will be handled by the original NumPy." % mode, UserWarning, 2) return numpy.put(a, ind, v, mode=mode) indexes = array_manipulation.flatten(array_create.array(ind, dtype=numpy.uint64), always_copy=False) values = array_manipulation.flatten(array_create.array(v, dtype=a.dtype), always_copy=False) # Now let's make the shape of 'indexes' and 'values' match if indexes.size > values.size: if values.size == 1: # When 'values' is a scalar, we can broadcast it to match 'indexes' values = numpy_backport.as_strided(values, shape=indexes.shape, strides=(0,)) else: # else we repeat 'values' enough times to be larger than 'indexes' values = numpy_backport.as_strided(values, shape=(indexes.size // values.size + 2, values.size), strides=(0, values.itemsize)) values = array_manipulation.flatten(values, always_copy=False) # When 'values' is too large, we simple cut the end off if values.size > indexes.size: values = values[0:indexes.size] # Now that 'indexes' and 'values' have the same shape, we can call 'scatter' scatter(a, indexes, values)