CuPy: NumPy & SciPy-compatible array library for GPU-accelerated computing with Python that provides a drop-in replacement for NumPy/SciPy on NVIDIA CUDA platforms.
—
Core array creation functions and array manipulation operations that mirror NumPy's interface but operate on GPU memory. These functions provide the foundation for all GPU-accelerated computations in CuPy.
Create arrays with specific patterns or values, all stored in GPU memory for immediate use in GPU computations.
def array(obj, dtype=None, copy=True, order='K', subok=False, ndmin=0):
"""Create array from array-like object in GPU memory.
Parameters:
- obj: array-like, input object to convert
- dtype: data type for the array
- copy: bool, whether to copy data
- order: memory layout order ('C', 'F', 'A', 'K')
- subok: bool, whether to return subclass
- ndmin: int, minimum number of dimensions
Returns:
cupy.ndarray: Array in GPU memory
"""
def zeros(shape, dtype=float64, order='C'):
"""Create array filled with zeros.
Parameters:
- shape: int or sequence of ints, shape of array
- dtype: data type
- order: memory layout order
Returns:
cupy.ndarray: Array of zeros
"""
def ones(shape, dtype=None, order='C'):
"""Create array filled with ones.
Parameters:
- shape: int or sequence of ints, shape of array
- dtype: data type
- order: memory layout order
Returns:
cupy.ndarray: Array of ones
"""
def empty(shape, dtype=float64, order='C'):
"""Create uninitialized array.
Parameters:
- shape: int or sequence of ints, shape of array
- dtype: data type
- order: memory layout order
Returns:
cupy.ndarray: Uninitialized array
"""
def full(shape, fill_value, dtype=None, order='C'):
"""Create array filled with specified value.
Parameters:
- shape: int or sequence of ints, shape of array
- fill_value: scalar, value to fill array with
- dtype: data type
- order: memory layout order
Returns:
cupy.ndarray: Array filled with fill_value
"""Create new arrays based on the shape and properties of existing arrays.
def zeros_like(a, dtype=None, order='K', subok=True, shape=None):
"""Create array of zeros with same shape as existing array.
Parameters:
- a: array-like, array whose shape to copy
- dtype: data type, defaults to a.dtype
- order: memory layout order
- subok: bool, whether to return subclass
- shape: int or sequence of ints, override shape
Returns:
cupy.ndarray: Array of zeros with same shape as a
"""
def ones_like(a, dtype=None, order='K', subok=True, shape=None):
"""Create array of ones with same shape as existing array."""
def empty_like(a, dtype=None, order='K', subok=True, shape=None):
"""Create uninitialized array with same shape as existing array."""
def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None):
"""Create filled array with same shape as existing array."""Generate arrays with evenly spaced values or specific patterns.
def arange(start, stop=None, step=1, dtype=None):
"""Create array with evenly spaced values within interval.
Parameters:
- start: number, start of interval
- stop: number, end of interval (exclusive)
- step: number, spacing between values
- dtype: data type
Returns:
cupy.ndarray: Array of evenly spaced values
"""
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0):
"""Create array with evenly spaced values over specified interval.
Parameters:
- start: scalar, start value
- stop: scalar, stop value
- num: int, number of samples
- endpoint: bool, whether to include stop value
- retstep: bool, whether to return spacing
- dtype: data type
- axis: int, axis along which to store samples
Returns:
cupy.ndarray: Array of evenly spaced samples
tuple: If retstep is True, (samples, step)
"""
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
"""Create array with logarithmically spaced values."""
def meshgrid(*xi, indexing='xy', sparse=False, copy=True):
"""Create coordinate matrices from coordinate vectors."""Create arrays with specific mathematical structures.
def eye(N, M=None, k=0, dtype=float64, 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
- order: memory layout order
Returns:
cupy.ndarray: Identity-like matrix
"""
def identity(n, dtype=None):
"""Create identity matrix."""
def diag(v, k=0):
"""Extract diagonal or construct diagonal array."""
def tri(N, M=None, k=0, dtype=float64):
"""Create array with ones at and below given diagonal."""Modify array shapes without changing the underlying data.
def reshape(a, newshape, order='C'):
"""Give new shape to array without changing data.
Parameters:
- a: array-like, input array
- newshape: int or tuple of ints, new shape
- order: memory layout order
Returns:
cupy.ndarray: Reshaped array
"""
def ravel(a, order='C'):
"""Return flattened array."""
def flatten(a, order='C'):
"""Return copy of array collapsed to one dimension."""
def squeeze(a, axis=None):
"""Remove single-dimensional entries from shape."""
def expand_dims(a, axis):
"""Expand dimensions of array."""Combine multiple arrays into single arrays.
def concatenate(arrays, axis=0, out=None):
"""Join arrays along existing axis.
Parameters:
- arrays: sequence of arrays, arrays to join
- axis: int, axis along which to join
- out: ndarray, destination array
Returns:
cupy.ndarray: Concatenated array
"""
def stack(arrays, axis=0, out=None):
"""Join arrays along new axis."""
def vstack(tup):
"""Stack arrays vertically (row-wise)."""
def hstack(tup):
"""Stack arrays horizontally (column-wise)."""
def dstack(tup):
"""Stack arrays depth-wise (along third axis)."""
def column_stack(tup):
"""Stack 1-D arrays as columns into 2-D array."""Split single arrays into multiple sub-arrays.
def split(ary, indices_or_sections, axis=0):
"""Split array into multiple sub-arrays.
Parameters:
- ary: ndarray, input array to split
- indices_or_sections: int or 1-D array, split points
- axis: int, axis along which to split
Returns:
list: List of sub-arrays
"""
def array_split(ary, indices_or_sections, axis=0):
"""Split array into multiple sub-arrays (allows unequal division)."""
def hsplit(ary, indices_or_sections):
"""Split array horizontally (along columns)."""
def vsplit(ary, indices_or_sections):
"""Split array vertically (along rows)."""
def dsplit(ary, indices_or_sections):
"""Split array along third axis."""Rearrange array dimensions and transpose operations.
def transpose(a, axes=None):
"""Permute dimensions of array.
Parameters:
- a: array-like, input array
- axes: list of ints, permutation of dimensions
Returns:
cupy.ndarray: Transposed array
"""
def swapaxes(a, axis1, axis2):
"""Interchange two axes of array."""
def moveaxis(a, source, destination):
"""Move axes of array to new positions."""
def rollaxis(a, axis, start=0):
"""Roll specified axis backwards."""Repeat and tile array elements or entire arrays.
def tile(A, reps):
"""Construct array by repeating A given number of times.
Parameters:
- A: array-like, input array
- reps: int or sequence of ints, repetitions along each axis
Returns:
cupy.ndarray: Tiled array
"""
def repeat(a, repeats, axis=None):
"""Repeat elements of array.
Parameters:
- a: array-like, input array
- repeats: int or array of ints, repetitions for each element
- axis: int, axis along which to repeat
Returns:
cupy.ndarray: Array with repeated elements
"""Flip, roll, and rotate array elements.
def flip(m, axis=None):
"""Reverse order of elements in array along axis."""
def fliplr(m):
"""Flip array left to right."""
def flipud(m):
"""Flip array up to down."""
def roll(a, shift, axis=None):
"""Roll array elements along axis."""
def rot90(m, k=1, axes=(0, 1)):
"""Rotate array 90 degrees in plane specified by axes."""import cupy as cp
# Create arrays of different types
zeros_arr = cp.zeros((3, 4))
ones_arr = cp.ones((2, 3), dtype=cp.int32)
random_arr = cp.random.random((5, 5))
# Create from Python lists
data = [[1, 2, 3], [4, 5, 6]]
gpu_array = cp.array(data)
# Create ranges
sequence = cp.arange(0, 10, 0.5)
linear = cp.linspace(0, 1, 100)import cupy as cp
# Create and reshape arrays
original = cp.arange(12)
reshaped = cp.reshape(original, (3, 4))
flattened = cp.ravel(reshaped)
# Add and remove dimensions
expanded = cp.expand_dims(reshaped, axis=2) # Shape: (3, 4, 1)
squeezed = cp.squeeze(expanded) # Shape: (3, 4)import cupy as cp
# Create sample arrays
a = cp.array([[1, 2], [3, 4]])
b = cp.array([[5, 6], [7, 8]])
# Join along different axes
vertical = cp.concatenate([a, b], axis=0) # Shape: (4, 2)
horizontal = cp.concatenate([a, b], axis=1) # Shape: (2, 4)
# Stack along new axis
stacked = cp.stack([a, b], axis=2) # Shape: (2, 2, 2)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda113