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'}) – Row-major (C-style) or column-major (Fortran-style) order.
Returns:

A new array with elements not initialized.

Return type:

cupy.ndarray

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 and subok 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:

cupy.ndarray

cupy.eye(N, M=None, k=0, dtype=<type 'float'>)[source]

Returns a 2-D array with ones on the diagonals and zeros elsewhere.

Parameters:
  • N (int) – Number of rows.
  • M (int) – Number of columns. M == N by default.
  • k (int) – Index of the diagonal. Zero indicates the main diagonal, a positive index an upper diagonal, and a negative index a lower diagonal.
  • dtype – Data type specifier.
Returns:

A 2-D array with given diagonals filled with ones and zeros elsewhere.

Return type:

cupy.ndarray

See also

numpy.eye()

cupy.identity(n, dtype=<type 'float'>)[source]

Returns a 2-D identity array.

It is equivalent to eye(n, n, dtype).

Parameters:
  • n (int) – Number of rows and columns.
  • dtype – Data type specifier.
Returns:

A 2-D identity array.

Return type:

cupy.ndarray

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:

cupy.ndarray

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 and subok 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:

cupy.ndarray

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'}) – Row-major (C-style) or column-major (Fortran-style) order.
Returns:

An array filled with ones.

Return type:

cupy.ndarray

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 and subok 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:

cupy.ndarray

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:

cupy.ndarray

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 and subok 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:

cupy.ndarray

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 and subok options.

Parameters:
  • objcupy.ndarray object or any other object that can be passed to numpy.array().
  • dtype – Data type specifier.
  • copy (bool) – If False, this function returns obj 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:

cupy.ndarray

See also

numpy.array()

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 the order 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:

cupy.ndarray

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 original numpy.asanyarray() returns the input array as is if it is an instance of a subtype of numpy.ndarray.

cupy.ascontiguousarray(a, dtype=None)[source]

Returns a C-contiguous 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 of a.

Return type:

cupy.ndarray

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'}) – Row-major (C-style) or column-major (Fortran-style) order. This function currently does not support order ‘A’ and ‘K’.
Returns:

The copy of a on the current device.

Return type:

cupy.ndarray

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 half-open interval [start, stop). The first three arguments are mapped like the range built-in 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 1-D array of range values.

Return type:

cupy.ndarray

See also

numpy.arange()

cupy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)[source]

Returns an array with evenly-spaced 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 1-D array of ranged values.

Return type:

cupy.ndarray

cupy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)[source]

Returns an array with evenly-spaced values on a log-scale.

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 log-scale are the same as base.
  • dtype – Data type specifier. It is inferred from the start and stop arguments by default.
Returns:

The 1-D array of ranged values.

Return type:

cupy.ndarray

Matrix creation

cupy.diag(v, k=0)[source]

Returns a diagonal or a diagonal array.

Parameters:
  • v (array-like) – Array or array-like 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 1-D array, then it returns a 2-D array with the specified diagonal filled by v. If v indicates a 2-D array, then it returns the specified diagonal of v. In latter case, if v is a cupy.ndarray object, then its view is returned.

Return type:

cupy.ndarray

See also

numpy.diag()

cupy.diagflat(v, k=0)[source]

Creates a diagonal array from the flattened input.

Parameters:
  • v (array-like) – Array or array-like object.
  • k (int) – Index of diagonals. See cupy.diag() for detail.
Returns:

A 2-D diagonal array with the diagonal copied from v.

Return type:

cupy.ndarray