# Indexing Routines¶

cupy.take(a, indices, axis=None, out=None)[source]

Takes elements of an array at specified indices along an axis.

This is an implementation of “fancy indexing” at single axis.

This function does not support mode option.

Parameters: a (cupy.ndarray) – Array to extract elements. indices (int or array-like) – Indices of elements that this function takes. axis (int) – The axis along which to select indices. The flattened input is used by default. out (cupy.ndarray) – Output array. If provided, it should be of appropriate shape and dtype. The result of fancy indexing. cupy.ndarray
cupy.diagonal(a, offset=0, axis1=0, axis2=1)[source]

Returns specified diagonals.

This function extracts the diagonals along two specified axes. The other axes are not changed. This function returns a writable view of this array as NumPy 1.10 will do.

Parameters: a (cupy.ndarray) – Array from which the diagonals are taken. offset (int) – Index of the diagonals. Zero indicates the main diagonals, a positive value upper diagonals, and a negative value lower diagonals. axis1 (int) – The first axis to take diagonals from. axis2 (int) – The second axis to take diagonals from. A view of the diagonals of a. cupy.ndarray
cupy.ix_(*args)[source]

Construct an open mesh from multiple sequences.

This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions.

Using ix_ one can quickly construct index arrays that will index the cross product. a[cupy.ix_([1,3],[2,5])] returns the array [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

Parameters: *args – 1-D sequences N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh. tuple of ndarrays

Examples

>>> a = cupy.arange(10).reshape(2, 5)
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> ixgrid = cupy.ix_([0,1], [2,4])
>>> ixgrid
(array([[0],
[1]]), array([[2, 4]]))

cupy.fill_diagonal(a, val, wrap=False)[source]

Fill the main diagonal of the given array of any dimensionality.

For an array a with a.ndim > 2, the diagonal is the list of locations with indices a[i, i, ..., i] all identical. This function modifies the input array in-place, it does not return a value.

Parameters: a (cupy.ndarray) – The array, at least 2-D. val (scalar) – The value to be written on the diagonal. Its type must be compatible with that of the array a. wrap (bool) – If specified, the diagonal is “wrapped” after N columns. This affects only tall matrices.

Examples

>>> a = cupy.zeros((3, 3), int)
>>> cupy.fill_diagonal(a, 5)
>>> a
array([[5, 0, 0],
[0, 5, 0],
[0, 0, 5]])

cupy.c_ = <cupy.indexing.generate.CClass object>

Translates slice objects to concatenation along the second axis.

This is a CuPy object that corresponds to cupy.r_(), which is useful because of its common occurrence. In particular, arrays will be stacked along their last axis after being upgraded to at least 2-D with 1’s post-pended to the shape (column vectors made out of 1-D arrays).

For detailed documentation, see r_().

This implementation is partially borrowed from NumPy’s one.

Parameters: a function, so takes no parameters (Not) – Joined array. cupy.ndarray

numpy.c_()

Examples

>>> a = cupy.array([[1, 2, 3]], dtype=np.int32)
>>> b = cupy.array([[4, 5, 6]], dtype=np.int32)
>>> cupy.c_[a, 0, 0, b]
array([[1, 2, 3, 0, 0, 4, 5, 6]], dtype=int32)

cupy.r_ = <cupy.indexing.generate.RClass object>

Translates slice objects to concatenation along the first axis.

This is a simple way to build up arrays quickly. If the index expression contains comma separated arrays, then stack them along their first axis.

This object can build up from normal CuPy arrays. Therefore, the other objects (e.g. writing strings like ‘2,3,4’, or using imaginary numbers like [1,2,3j], or using string integers like ‘-1’) are not implemented yet compared with NumPy.

This implementation is partially borrowed from NumPy’s one.

Parameters: a function, so takes no parameters (Not) – Joined array. cupy.ndarray

numpy.r_()

Examples

>>> a = cupy.array([1, 2, 3], dtype=np.int32)
>>> b = cupy.array([4, 5, 6], dtype=np.int32)
>>> cupy.r_[a, 0, 0, b]
array([1, 2, 3, 0, 0, 4, 5, 6], dtype=int32)