Array Manipulation Routines¶
Basic manipulations¶

cupy.
copyto
(dst, src, casting='same_kind', where=None)[source]¶ Copies values from one array to another with broadcasting.
This function can be called for arrays on different devices. In this case, casting,
where
, and broadcasting is not supported, and an exception is raised if these are used.Parameters:  dst (cupy.ndarray) – Target array.
 src (cupy.ndarray) – Source array.
 casting (str) – Casting rule. See
numpy.can_cast()
for detail.  where (cupy.ndarray of bool) – If specified, this array acts as a mask,
and an element is copied only if the corresponding element of
where
is True.
See also
Shape manipulation¶

cupy.
reshape
(a, newshape)[source]¶ Returns an array with new shape and same elements.
It tries to return a view if possible, otherwise returns a copy.
This function currently does not support
order
option.Parameters:  a (cupy.ndarray) – Array to be reshaped.
 newshape (int or tuple of ints) – The new shape of the array to return.
If it is an integer, then it is treated as a tuple of length one.
It should be compatible with
a.size
. One of the elements can be 1, which is automatically replaced with the appropriate value to make the shape compatible witha.size
.
Returns: A reshaped view of
a
if possible, otherwise a copy.Return type: See also

cupy.
ravel
(a)[source]¶ Returns a flattened array.
It tries to return a view if possible, otherwise returns a copy.
This function currently does not support
order
option.Parameters: a (cupy.ndarray) – Array to be flattened. Returns: A flattened view of a
if possible, otherwise a copy.Return type: cupy.ndarray See also
Transposition¶

cupy.
rollaxis
(a, axis, start=0)[source]¶ Moves the specified axis backwards to the given place.
Parameters:  a (cupy.ndarray) – Array to move the axis.
 axis (int) – The axis to move.
 start (int) – The place to which the axis is moved.
Returns: A view of
a
that the axis is moved tostart
.Return type: See also

cupy.
swapaxes
(a, axis1, axis2)[source]¶ Swaps the two axes.
Parameters:  a (cupy.ndarray) – Array to swap the axes.
 axis1 (int) – The first axis to swap.
 axis2 (int) – The second axis to swap.
Returns: A view of
a
that the two axes are swapped.Return type: See also

cupy.
transpose
(a, axes=None)[source]¶ Permutes the dimensions of an array.
Parameters:  a (cupy.ndarray) – Array to permute the dimensions.
 axes (tuple of ints) – Permutation of the dimensions. This function reverses the shape by default.
Returns: A view of
a
that the dimensions are permuted.Return type: See also
Edit dimensionalities¶

cupy.
atleast_1d
(*arys)[source]¶ Converts arrays to arrays with dimensions >= 1.
Parameters: arys (tuple of arrays) – Arrays to be converted. All arguments must be cupy.ndarray
objects. Only zerodimensional array is affected.Returns: If there are only one input, then it returns its converted version. Otherwise, it returns a list of converted arrays. See also

cupy.
atleast_2d
(*arys)[source]¶ Converts arrays to arrays with dimensions >= 2.
If an input array has dimensions less than two, then this function inserts new axes at the head of dimensions to make it have two dimensions.
Parameters: arys (tuple of arrays) – Arrays to be converted. All arguments must be cupy.ndarray
objects.Returns: If there are only one input, then it returns its converted version. Otherwise, it returns a list of converted arrays. See also

cupy.
atleast_3d
(*arys)[source]¶ Converts arrays to arrays with dimensions >= 3.
If an input array has dimensions less than three, then this function inserts new axes to make it have three dimensions. The place of the new axes are following:
 If its shape is
()
, then the shape of output is(1, 1, 1)
.  If its shape is
(N,)
, then the shape of output is(1, N, 1)
.  If its shape is
(M, N)
, then the shape of output is(M, N, 1)
.  Otherwise, the output is the input array itself.
Parameters: arys (tuple of arrays) – Arrays to be converted. All arguments must be cupy.ndarray
objects.Returns: If there are only one input, then it returns its converted version. Otherwise, it returns a list of converted arrays. See also
 If its shape is

class
cupy.
broadcast
¶ Object that performs broadcasting.
CuPy actually uses this class to support broadcasting in various operations. Note that this class does not provide an iterator.
Parameters: arrays (tuple of arrays) – Arrays to be broadcasted.
Variables: See also

cupy.
broadcast_arrays
(*args)[source]¶ Broadcasts given arrays.
Parameters: args (tuple of arrays) – Arrays to broadcast for each other. Returns: A list of broadcasted arrays. Return type: list See also

cupy.
broadcast_to
(array, shape)[source]¶ Broadcast an array to a given shape.
Parameters:  array (cupy.ndarray) – Array to broadcast.
 shape (tuple of int) – The shape of the desired array.
Returns: Broadcasted view.
Return type: See also

cupy.
expand_dims
(a, axis)[source]¶ Expands given arrays.
Parameters:  a (cupy.ndarray) – Array to be expanded.
 axis (int) – Position where new axis is to be inserted.
Returns:  The number of dimensions is one greater than that of
the input array.
Return type: See also

cupy.
squeeze
(a, axis=None)[source]¶ Removes sizeone axes from the shape of an array.
Parameters:  a (cupy.ndarray) – Array to be reshaped.
 axis (int or tuple of ints) – Axes to be removed. This function removes all sizeone axes by default. If one of the specified axes is not of size one, an exception is raised.
Returns: An array without (specified) sizeone axes.
Return type: See also
Changing kind of array¶
Joining arrays along axis¶

cupy.
column_stack
(tup)[source]¶ Stacks 1D and 2D arrays as columns into a 2D array.
A 1D array is first converted to a 2D column array. Then, the 2D arrays are concatenated along the second axis.
Parameters: tup (sequence of arrays) – 1D or 2D arrays to be stacked. Returns: A new 2D array of stacked columns. Return type: cupy.ndarray See also

cupy.
concatenate
(tup, axis=0)[source]¶ Joins arrays along an axis.
Parameters:  tup (sequence of arrays) – Arrays to be joined. All of these should have same dimensionalities except the specified axis.
 axis (int) – The axis to join arrays along.
Returns: Joined array.
Return type: See also

cupy.
vstack
(tup)[source]¶ Stacks arrays vertically.
If an input array has one dimension, then the array is treated as a horizontal vector and stacked along the additional axis at the head. Otherwise, the array is stacked along the first axis.
Parameters: tup (sequence of arrays) – Arrays to be stacked. Each array is converted by cupy.atleast_2d()
before stacking.Returns: Stacked array. Return type: cupy.ndarray See also

cupy.
hstack
(tup)[source]¶ Stacks arrays horizontally.
If an input array has one dimension, then the array is treated as a horizontal vector and stacked along the first axis. Otherwise, the array is stacked along the second axis.
Parameters: tup (sequence of arrays) – Arrays to be stacked. Returns: Stacked array. Return type: cupy.ndarray See also

cupy.
dstack
(tup)[source]¶ Stacks arrays along the third axis.
Parameters: tup (sequence of arrays) – Arrays to be stacked. Each array is converted by cupy.atleast_3d()
before stacking.Returns: Stacked array. Return type: cupy.ndarray See also
Splitting arrays along axis¶

cupy.
array_split
(ary, indices_or_sections, axis=0)[source]¶ Splits an array into multiple sub arrays along a given axis.
This function is almost equivalent to
cupy.split()
. The only difference is that this function allows an integer sections that does not evenly divide the axis.See also
cupy.split()
for more detail,numpy.array_split()

cupy.
split
(ary, indices_or_sections, axis=0)[source]¶ Splits an array into multiple sub arrays along a given axis.
Parameters:  ary (cupy.ndarray) – Array to split.
 indices_or_sections (int or sequence of ints) – A value indicating how to divide the axis. If it is an integer, then is treated as the number of sections, and the axis is evenly divided. Otherwise, the integers indicate indices to split at. Note that the sequence on the device memory is not allowed.
 axis (int) – Axis along which the array is split.
Returns: A list of sub arrays. Each array is a view of the corresponding input array.
See also

cupy.
vsplit
(ary, indices_or_sections)[source]¶ Splits an array into multiple sub arrays along the first axis.
This is equivalent to
split
withaxis=0
.See also
cupy.split()
for more detail,numpy.dsplit()

cupy.
hsplit
(ary, indices_or_sections)[source]¶ Splits an array into multiple sub arrays horizontally.
This is equivalent to
split
withaxis=0
ifary
has one dimension, and otherwise that withaxis=1
.See also
cupy.split()
for more detail,numpy.hsplit()

cupy.
dsplit
(ary, indices_or_sections)[source]¶ Splits an array into multiple sub arrays along the third axis.
This is equivalent to
split
withaxis=2
.See also
cupy.split()
for more detail,numpy.dsplit()
Repeating part of arrays along axis¶

cupy.
tile
(A, reps)[source]¶ Construct an array by repeating A the number of times given by reps.
Parameters:  A (cupy.ndarray) – Array to transform.
 reps (int or tuple) – The number of repeats.
Returns: Transformed array with repeats.
Return type: See also
Rearranging elements¶

cupy.
flip
(a, axis)[source]¶ Reverse the order of elements in an array along the given axis.
Note that
flip
function has been introduced since NumPy v1.12. The contents of this document is the same as the original one.Parameters: Returns: Output array.
Return type: See also

cupy.
fliplr
(a)[source]¶ Flip array in the left/right direction.
Flip the entries in each row in the left/right direction. Columns are preserved, but appear in a different order than before.
Parameters: a (ndarray) – Input array. Returns: Output array. Return type: ndarray See also

cupy.
flipud
(a)[source]¶ Flip array in the up/down direction.
Flip the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before.
Parameters: a (ndarray) – Input array. Returns: Output array. Return type: ndarray See also

cupy.
roll
(a, shift, axis=None)[source]¶ Roll array elements along a given axis.
Parameters: Returns: Output array.
Return type: See also