NumPy & SciPy-compatible GPU-accelerated computing library for CUDA 11.2 environments
—
Core functionality for creating, reshaping, and manipulating N-dimensional arrays on GPU memory. These operations provide the foundation for all GPU-accelerated computations in CuPy with full NumPy compatibility.
Creates new arrays with specified shapes and initial values, allocating memory on the GPU.
def empty(shape, dtype=float, order='C'):
"""
Create an uninitialized array of given shape and type on GPU.
Parameters:
- shape: int or tuple of ints, shape of the array
- dtype: data type, optional
- order: memory layout ('C' for C-contiguous, 'F' for Fortran-contiguous)
Returns:
cupy.ndarray
"""
def zeros(shape, dtype=float, order='C'):
"""
Create array filled with zeros.
Parameters:
- shape: int or tuple of ints
- dtype: data type, optional
- order: memory layout, optional
Returns:
cupy.ndarray
"""
def ones(shape, dtype=float, order='C'):
"""
Create array filled with ones.
Parameters:
- shape: int or tuple of ints
- dtype: data type, optional
- order: memory layout, optional
Returns:
cupy.ndarray
"""
def full(shape, fill_value, dtype=None, order='C'):
"""
Create array filled with specified value.
Parameters:
- shape: int or tuple of ints
- fill_value: scalar, value to fill the array
- dtype: data type, optional
- order: memory layout, optional
Returns:
cupy.ndarray
"""
def identity(n, dtype=None):
"""
Create identity matrix.
Parameters:
- n: int, number of rows/columns
- dtype: data type, optional
Returns:
cupy.ndarray
"""
def eye(N, M=None, k=0, dtype=float, order='C'):
"""
Create 2-D array with ones on diagonal and zeros elsewhere.
Parameters:
- N: int, number of rows
- M: int, number of columns (defaults to N)
- k: int, diagonal offset
- dtype: data type, optional
- order: memory layout, optional
Returns:
cupy.ndarray
"""Creates arrays from existing data sources, enabling conversion from various input formats.
def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0):
"""
Create array from array-like object.
Parameters:
- object: array-like, input data
- dtype: data type, optional
- copy: bool, whether to copy data
- order: memory layout
- subok: bool, allow subclasses
- ndmin: int, minimum dimensions
Returns:
cupy.ndarray
"""
def asarray(a, dtype=None, order=None):
"""
Convert input to array, no copy if already ndarray.
Parameters:
- a: array-like
- dtype: data type, optional
- order: memory layout, optional
Returns:
cupy.ndarray
"""
def copy(a, order='K', subok=False):
"""
Create copy of array.
Parameters:
- a: array-like
- order: memory layout
- subok: bool, allow subclasses
Returns:
cupy.ndarray
"""
def frombuffer(buffer, dtype=float, count=-1, offset=0):
"""
Create array from buffer object.
Parameters:
- buffer: buffer object
- dtype: data type
- count: int, number of items
- offset: int, byte offset
Returns:
cupy.ndarray
"""
def fromfunction(function, shape, **kwargs):
"""
Create array by executing function over each coordinate.
Parameters:
- function: callable, function to call
- shape: tuple of ints
- kwargs: additional arguments
Returns:
cupy.ndarray
"""Creates arrays with regularly spaced values for numerical computations.
def arange(start, stop=None, step=1, dtype=None):
"""
Create array with evenly spaced values within given interval.
Parameters:
- start: number, start of interval
- stop: number, end of interval
- step: number, spacing between values
- dtype: data type, optional
Returns:
cupy.ndarray
"""
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0):
"""
Create array with evenly spaced numbers over specified interval.
Parameters:
- start: scalar, starting value
- stop: scalar, ending value
- num: int, number of samples
- endpoint: bool, include endpoint
- retstep: bool, return step size
- dtype: data type, optional
- axis: int, axis along which to store samples
Returns:
cupy.ndarray or (cupy.ndarray, float)
"""
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
"""
Create array with numbers spaced evenly on log scale.
Parameters:
- start: scalar, base**start is starting value
- stop: scalar, base**stop is ending value
- num: int, number of samples
- endpoint: bool, include endpoint
- base: scalar, base of log space
- dtype: data type, optional
- axis: int, axis along which to store samples
Returns:
cupy.ndarray
"""
def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
"""
Create coordinate matrices from coordinate vectors.
Parameters:
- xi: array-like, 1-D arrays representing coordinates
- copy: bool, copy arrays
- sparse: bool, return sparse grid
- indexing: str, Cartesian ('xy') or matrix ('ij') indexing
Returns:
list of cupy.ndarray
"""Operations for changing array shapes and dimensions without modifying data.
def reshape(a, newshape, order='C'):
"""
Give new shape to array without changing data.
Parameters:
- a: array-like
- newshape: int or tuple of ints
- order: memory layout
Returns:
cupy.ndarray
"""
def ravel(a, order='C'):
"""
Return flattened array.
Parameters:
- a: array-like
- order: memory layout
Returns:
cupy.ndarray
"""
def transpose(a, axes=None):
"""
Permute dimensions of array.
Parameters:
- a: array-like
- axes: tuple of ints, permutation of dimensions
Returns:
cupy.ndarray
"""
def swapaxes(a, axis1, axis2):
"""
Interchange two axes of array.
Parameters:
- a: array-like
- axis1: int, first axis
- axis2: int, second axis
Returns:
cupy.ndarray
"""
def expand_dims(a, axis):
"""
Expand shape of array by inserting new axes.
Parameters:
- a: array-like
- axis: int or tuple of ints, position of new axes
Returns:
cupy.ndarray
"""
def squeeze(a, axis=None):
"""
Remove single-dimensional entries from shape.
Parameters:
- a: array-like
- axis: int or tuple of ints, axes to squeeze
Returns:
cupy.ndarray
"""Operations for combining multiple arrays into single arrays.
def concatenate(arrays, axis=0, out=None, dtype=None, casting="same_kind"):
"""
Join sequence of arrays along existing axis.
Parameters:
- arrays: sequence of array-like
- axis: int, axis to concatenate along
- out: ndarray, destination array
- dtype: data type
- casting: casting mode
Returns:
cupy.ndarray
"""
def stack(arrays, axis=0, out=None):
"""
Join sequence of arrays along new axis.
Parameters:
- arrays: sequence of array-like
- axis: int, axis to stack along
- out: ndarray, destination array
Returns:
cupy.ndarray
"""
def vstack(tup):
"""
Stack arrays vertically (row-wise).
Parameters:
- tup: sequence of arrays
Returns:
cupy.ndarray
"""
def hstack(tup):
"""
Stack arrays horizontally (column-wise).
Parameters:
- tup: sequence of arrays
Returns:
cupy.ndarray
"""
def dstack(tup):
"""
Stack arrays depth-wise (along third axis).
Parameters:
- tup: sequence of arrays
Returns:
cupy.ndarray
"""Operations for dividing arrays into multiple sub-arrays.
def split(ary, indices_or_sections, axis=0):
"""
Split array into sub-arrays.
Parameters:
- ary: ndarray, array to split
- indices_or_sections: int or array of ints, split points
- axis: int, axis to split along
Returns:
list of cupy.ndarray
"""
def hsplit(ary, indices_or_sections):
"""
Split array horizontally.
Parameters:
- ary: ndarray
- indices_or_sections: int or array of ints
Returns:
list of cupy.ndarray
"""
def vsplit(ary, indices_or_sections):
"""
Split array vertically.
Parameters:
- ary: ndarray
- indices_or_sections: int or array of ints
Returns:
list of cupy.ndarray
"""
def array_split(ary, indices_or_sections, axis=0):
"""
Split array into sub-arrays allowing unequal sizes.
Parameters:
- ary: ndarray
- indices_or_sections: int or array of ints
- axis: int, axis to split along
Returns:
list of cupy.ndarray
"""Operations for repeating and tiling arrays to create larger arrays.
def tile(A, reps):
"""
Construct array by repeating array given number of times.
Parameters:
- A: array-like
- reps: int or tuple of ints, repetitions along each axis
Returns:
cupy.ndarray
"""
def repeat(a, repeats, axis=None):
"""
Repeat elements of array.
Parameters:
- a: array-like
- repeats: int or array of ints, number of repetitions
- axis: int, axis along which to repeat
Returns:
cupy.ndarray
"""Operations for controlling memory layout and contiguity of arrays.
def ascontiguousarray(a, dtype=None):
"""
Return contiguous array in C order.
Parameters:
- a: array-like
- dtype: data type, optional
Returns:
cupy.ndarray
"""
def asfortranarray(a, dtype=None):
"""
Return contiguous array in Fortran order.
Parameters:
- a: array-like
- dtype: data type, optional
Returns:
cupy.ndarray
"""
def require(a, dtype=None, requirements=None):
"""
Return array satisfying requirements.
Parameters:
- a: array-like
- dtype: data type, optional
- requirements: str or list of str, array requirements
Returns:
cupy.ndarray
"""import cupy as cp
# Create basic arrays
zeros_array = cp.zeros((3, 4))
ones_matrix = cp.ones((2, 2), dtype=cp.float32)
identity_mat = cp.identity(3)
# Create from data
data = [[1, 2, 3], [4, 5, 6]]
gpu_array = cp.array(data)
# Create ranges
sequence = cp.arange(10)
linear_space = cp.linspace(0, 1, 100)import cupy as cp
# Create and reshape
original = cp.arange(12)
reshaped = cp.reshape(original, (3, 4))
transposed = cp.transpose(reshaped)
# Add/remove dimensions
expanded = cp.expand_dims(original, axis=0)
squeezed = cp.squeeze(expanded)import cupy as cp
# Stack arrays
a = cp.array([1, 2, 3])
b = cp.array([4, 5, 6])
horizontal = cp.hstack([a, b])
vertical = cp.vstack([a, b])
stacked = cp.stack([a, b], axis=0)
# Concatenate along axis
c = cp.array([[1, 2], [3, 4]])
d = cp.array([[5, 6], [7, 8]])
combined = cp.concatenate([c, d], axis=1)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda112