Array Creation Routines¶
Basic creation routines¶

cupy.
empty
(shape, dtype=<type 'float'>, order='C')[source]¶ Returns an array without initializing the elements.
Parameters:  shape (tuple of ints) – Dimensionalities of the array.
 dtype – Data type specifier.
 order ({'C', 'F'}) – Rowmajor (Cstyle) or columnmajor (Fortranstyle) order.
Returns: A new array with elements not initialized.
Return type: See also
numpy.empty()

cupy.
empty_like
(a, dtype=None)[source]¶ Returns a new array with same shape and dtype of a given array.
This function currently does not support
order
andsubok
options.Parameters:  a (cupy.ndarray) – Base array.
 dtype – Data type specifier. The data type of
a
is used by default.
Returns: A new array with same shape and dtype of
a
with elements not initialized.Return type: See also
numpy.empty_like()

cupy.
eye
(N, M=None, k=0, dtype=<type 'float'>)[source]¶ Returns a 2D array with ones on the diagonals and zeros elsewhere.
Parameters: Returns: A 2D array with given diagonals filled with ones and zeros elsewhere.
Return type: See also
numpy.eye()

cupy.
identity
(n, dtype=<type 'float'>)[source]¶ Returns a 2D identity array.
It is equivalent to
eye(n, n, dtype)
.Parameters:  n (int) – Number of rows and columns.
 dtype – Data type specifier.
Returns: A 2D identity array.
Return type: See also
numpy.identity()

cupy.
ones
(shape, dtype=<type 'float'>)[source]¶ Returns a new array of given shape and dtype, filled with ones.
This function currently does not support
order
option.Parameters:  shape (tuple of ints) – Dimensionalities of the array.
 dtype – Data type specifier.
Returns: An array filled with ones.
Return type: See also
numpy.ones()

cupy.
ones_like
(a, dtype=None)[source]¶ Returns an array of ones with same shape and dtype as a given array.
This function currently does not support
order
andsubok
options.Parameters:  a (cupy.ndarray) – Base array.
 dtype – Data type specifier. The dtype of
a
is used by default.
Returns: An array filled with ones.
Return type: See also
numpy.ones_like()

cupy.
zeros
(shape, dtype=<type 'float'>, order='C')[source]¶ Returns a new array of given shape and dtype, filled with zeros.
Parameters:  shape (tuple of ints) – Dimensionalities of the array.
 dtype – Data type specifier.
 order ({'C', 'F'}) – Rowmajor (Cstyle) or columnmajor (Fortranstyle) order.
Returns: An array filled with ones.
Return type: See also
numpy.zeros()

cupy.
zeros_like
(a, dtype=None)[source]¶ Returns an array of zeros with same shape and dtype as a given array.
This function currently does not support
order
andsubok
options.Parameters:  a (cupy.ndarray) – Base array.
 dtype – Data type specifier. The dtype of
a
is used by default.
Returns: An array filled with ones.
Return type: See also
numpy.zeros_like()

cupy.
full
(shape, fill_value, dtype=None)[source]¶ Returns a new array of given shape and dtype, filled with a given value.
This function currently does not support
order
option.Parameters:  shape (tuple of ints) – Dimensionalities of the array.
 fill_value – A scalar value to fill a new array.
 dtype – Data type specifier.
Returns: An array filled with
fill_value
.Return type: See also
numpy.full()

cupy.
full_like
(a, fill_value, dtype=None)[source]¶ Returns a full array with same shape and dtype as a given array.
This function currently does not support
order
andsubok
options.Parameters:  a (cupy.ndarray) – Base array.
 fill_value – A scalar value to fill a new array.
 dtype – Data type specifier. The dtype of
a
is used by default.
Returns: An array filled with
fill_value
.Return type: See also
numpy.full_like()
Creation from other data¶

cupy.
array
(obj, dtype=None, copy=True, ndmin=0)[source]¶ Creates an array on the current device.
This function currently does not support the
order
andsubok
options.Parameters:  obj –
cupy.ndarray
object or any other object that can be passed tonumpy.array()
.  dtype – Data type specifier.
 copy (bool) – If
False
, this function returnsobj
if possible. Otherwise this function always returns a new array.  ndmin (int) – Minimum number of dimensions. Ones are inserted to the head of the shape if needed.
Returns: An array on the current device.
Return type: See also
numpy.array()
 obj –

cupy.
asarray
(a, dtype=None)[source]¶ Converts an object to array.
This is equivalent to
array(a, dtype, copy=False)
. This function currently does not support theorder
option.Parameters:  a – The source object.
 dtype – Data type specifier. It is inferred from the input by default.
Returns: An array on the current device. If
a
is already on the device, no copy is performed.Return type: See also
numpy.asarray()

cupy.
asanyarray
(a, dtype=None)[source]¶ Converts an object to array.
This is currently equivalent to
asarray()
, since there is no subclass of ndarray in CuPy. Note that the originalnumpy.asanyarray()
returns the input array as is if it is an instance of a subtype ofnumpy.ndarray
.See also
cupy.asarray()
,numpy.asanyarray()

cupy.
ascontiguousarray
(a, dtype=None)[source]¶ Returns a Ccontiguous array.
Parameters:  a (cupy.ndarray) – Source array.
 dtype – Data type specifier.
Returns: If no copy is required, it returns
a
. Otherwise, it returns a copy ofa
.Return type: See also
numpy.ascontiguousarray()

cupy.
copy
(a, order='C')[source]¶ Creates a copy of a given array on the current device.
This function allocates the new array on the current device. If the given array is allocated on the different device, then this function tries to copy the contents over the devices.
Parameters:  a (cupy.ndarray) – The source array.
 order ({'C', 'F'}) – Rowmajor (Cstyle) or columnmajor (Fortranstyle) order. This function currently does not support order ‘A’ and ‘K’.
Returns: The copy of
a
on the current device.Return type: See:
numpy.copy()
,cupy.ndarray.copy()
Numerical ranges¶

cupy.
arange
(start, stop=None, step=1, dtype=None)[source]¶ Returns an array with evenly spaced values within a given interval.
Values are generated within the halfopen interval [start, stop). The first three arguments are mapped like the
range
builtin function, i.e. start and step are optional.Parameters:  start – Start of the interval.
 stop – End of the interval.
 step – Step width between each pair of consecutive values.
 dtype – Data type specifier. It is inferred from other arguments by default.
Returns: The 1D array of range values.
Return type: See also
numpy.arange()

cupy.
linspace
(start, stop, num=50, endpoint=True, retstep=False, dtype=None)[source]¶ Returns an array with evenlyspaced values within a given interval.
Instead of specifying the step width like
cupy.arange()
, this function requires the total number of elements specified.Parameters:  start – Start of the interval.
 stop – End of the interval.
 num – Number of elements.
 endpoint (bool) – If
True
, the stop value is included as the last element. Otherwise, the stop value is omitted.  retstep (bool) – If
True
, this function returns (array, step). Otherwise, it returns only the array.  dtype – Data type specifier. It is inferred from the start and stop arguments by default.
Returns: The 1D array of ranged values.
Return type:

cupy.
logspace
(start, stop, num=50, endpoint=True, base=10.0, dtype=None)[source]¶ Returns an array with evenlyspaced values on a logscale.
Instead of specifying the step width like
cupy.arange()
, this function requires the total number of elements specified.Parameters:  start – Start of the interval.
 stop – End of the interval.
 num – Number of elements.
 endpoint (bool) – If
True
, the stop value is included as the last element. Otherwise, the stop value is omitted.  base (float) – Base of the log space. The step sizes between the
elements on a logscale are the same as
base
.  dtype – Data type specifier. It is inferred from the start and stop arguments by default.
Returns: The 1D array of ranged values.
Return type:

cupy.
meshgrid
(*xi, **kwargs)[source]¶ Return coordinate matrices from coordinate vectors.
Given onedimensional coordinate arrays x1, x2, ..., xn, this function makes ND grids.
For onedimensional arrays x1, x2, ..., xn with lengths
Ni = len(xi)
, this function returns(N1, N2, N3, ..., Nn)
shaped arrays if indexing=’ij’ or(N2, N1, N3, ..., Nn)
shaped arrays if indexing=’xy’.Unlike NumPy, CuPy currently only supports 1D arrays as inputs. Also, CuPy does not support
sparse
option yet.Parameters:  xi (tuple of ndarrays) – 1D arrays representing the coordinates of a grid.
 indexing ({'xy', 'ij'}, optional) – Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output.
 copy (bool, optional) – If
False
, a view into the original arrays are returned. Default is True.
Returns: list of cupy.ndarray
See also
numpy.meshgrid()
Matrix creation¶

cupy.
diag
(v, k=0)[source]¶ Returns a diagonal or a diagonal array.
Parameters:  v (arraylike) – Array or arraylike object.
 k (int) – Index of diagonals. Zero indicates the main diagonal, a positive value an upper diagonal, and a negative value a lower diagonal.
Returns: If
v
indicates a 1D array, then it returns a 2D array with the specified diagonal filled byv
. Ifv
indicates a 2D array, then it returns the specified diagonal ofv
. In latter case, ifv
is acupy.ndarray
object, then its view is returned.Return type: See also
numpy.diag()

cupy.
diagflat
(v, k=0)[source]¶ Creates a diagonal array from the flattened input.
Parameters:  v (arraylike) – Array or arraylike object.
 k (int) – Index of diagonals. See
cupy.diag()
for detail.
Returns: A 2D diagonal array with the diagonal copied from
v
.Return type: