CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cupy

NumPy & SciPy-compatible array library for GPU-accelerated computing with Python

Pending
Overview
Eval results
Files

array-creation.mddocs/

Array Creation and Manipulation

Core functionality for creating, reshaping, and manipulating GPU arrays with the same interface as NumPy. All operations create arrays on the GPU and support the same broadcasting and indexing semantics as NumPy.

Capabilities

Basic Array Creation

Create new arrays with specified shapes, data types, and initialization values.

def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0):
    """
    Create a GPU array from existing data.
    
    Parameters:
    - object: array-like, data to convert to CuPy array
    - dtype: data type, if None infer from data
    - copy: bool, whether to copy data
    - order: {'K', 'A', 'C', 'F'}, memory layout
    - subok: bool, whether to allow subclasses
    - ndmin: int, minimum number of dimensions
    
    Returns:
    cupy.ndarray: GPU array
    """

def zeros(shape, dtype=None, order='C'):
    """
    Create array filled with zeros.
    
    Parameters:
    - shape: int or tuple of ints, shape of new array
    - dtype: data type, default float64
    - order: {'C', 'F'}, memory layout
    
    Returns:
    cupy.ndarray: Array of zeros
    """

def ones(shape, dtype=None, order='C'):
    """
    Create array filled with ones.
    
    Parameters:
    - shape: int or tuple of ints, shape of new array
    - dtype: data type, default float64
    - order: {'C', 'F'}, memory layout
    
    Returns:
    cupy.ndarray: Array of ones
    """

def empty(shape, dtype=float32, order='C'):
    """
    Create uninitialized array.
    
    Parameters:
    - shape: int or tuple of ints, shape of new array
    - dtype: data type, default float32
    - order: {'C', 'F'}, memory layout
    
    Returns:
    cupy.ndarray: Uninitialized array
    """

def full(shape, fill_value, dtype=None, order='C'):
    """
    Create array filled with specified value.
    
    Parameters:
    - shape: int or tuple of ints, shape of new array
    - fill_value: scalar, value to fill array with
    - dtype: data type, if None infer from fill_value
    - order: {'C', 'F'}, memory layout
    
    Returns:
    cupy.ndarray: Array filled with fill_value
    """

def eye(N, M=None, k=0, dtype=float, order='C'):
    """
    Create identity matrix or matrix with ones on diagonal.
    
    Parameters:
    - N: int, number of rows
    - M: int, number of columns (default N)
    - k: int, diagonal offset
    - dtype: data type
    - order: {'C', 'F'}, memory layout
    
    Returns:
    cupy.ndarray: Identity or diagonal matrix
    """

def identity(n, dtype=None):
    """
    Create identity matrix of given size.
    
    Parameters:
    - n: int, size of identity matrix
    - dtype: data type, default float64
    
    Returns:
    cupy.ndarray: n x n identity matrix
    """

def diag(v, k=0):
    """
    Extract diagonal or construct diagonal matrix.
    
    Parameters:
    - v: array-like, if 1-D creates diagonal matrix, if 2-D extracts diagonal
    - k: int, diagonal offset
    
    Returns:
    cupy.ndarray: Diagonal array or matrix
    """

def tri(N, M=None, k=0, dtype=float):
    """
    Create array with ones at and below diagonal, zeros elsewhere.
    
    Parameters:
    - N: int, number of rows
    - M: int, number of columns (default N)
    - k: int, diagonal offset
    - dtype: data type
    
    Returns:
    cupy.ndarray: Triangular matrix
    """

Array-like Creation

Create arrays with same shape or properties as 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, reference array
    - dtype: data type, if None use same as a
    - order: {'K', 'A', 'C', 'F'}, memory layout
    - subok: bool, whether to allow subclasses
    - shape: int or tuple of ints, override shape
    
    Returns:
    cupy.ndarray: Array of zeros
    """

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 array filled with value, same shape as existing array."""

Numerical Ranges

Create arrays with evenly or logarithmically spaced values.

def arange(start, stop=None, step=1, dtype=None):
    """
    Create array with evenly spaced values within range.
    
    Parameters:
    - start: number, start value or stop if stop not given
    - stop: number, end value (exclusive)
    - step: number, spacing between values
    - dtype: data type, if None infer from inputs
    
    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 interval.
    
    Parameters:
    - start: scalar, start value
    - stop: scalar, end value
    - num: int, number of samples
    - endpoint: bool, whether to include stop value
    - retstep: bool, whether to return step size
    - dtype: data type
    - axis: int, axis along which to store samples
    
    Returns:
    cupy.ndarray: Array of evenly spaced values
    tuple: (array, step) if retstep=True
    """

def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
    """
    Create array with logarithmically spaced values.
    
    Parameters:
    - start: scalar, base**start is first value
    - stop: scalar, base**stop is last value  
    - num: int, number of samples
    - endpoint: bool, whether to include base**stop
    - base: scalar, logarithm base
    - dtype: data type
    - axis: int, axis along which to store samples
    
    Returns:
    cupy.ndarray: Array of logarithmically spaced values
    """

def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
    """
    Create coordinate arrays from coordinate vectors.
    
    Parameters:
    - xi: array-like, coordinate vectors
    - copy: bool, whether to copy input arrays
    - sparse: bool, whether to return sparse grid
    - indexing: {'xy', 'ij'}, indexing convention
    
    Returns:
    list of cupy.ndarray: Coordinate arrays
    """

Matrix Creation

Create matrices with specific structures like diagonal, triangular, or Vandermonde matrices.

def diag(v, k=0):
    """
    Extract or construct diagonal matrix.
    
    Parameters:
    - v: array-like, diagonal values or matrix to extract diagonal from
    - k: int, diagonal offset
    
    Returns:
    cupy.ndarray: Diagonal matrix or diagonal values
    """

def tri(N, M=None, k=0, dtype=float):
    """
    Create array with ones on and below diagonal.
    
    Parameters:
    - N: int, number of rows
    - M: int, number of columns (default N)
    - k: int, diagonal offset
    - dtype: data type
    
    Returns:
    cupy.ndarray: Triangular matrix
    """

def tril(m, k=0):
    """
    Return lower triangle of array.
    
    Parameters:
    - m: array-like, input array
    - k: int, diagonal offset
    
    Returns:
    cupy.ndarray: Lower triangular matrix
    """

def triu(m, k=0):
    """Return upper triangle of array."""

def vander(x, N=None, increasing=False):
    """
    Generate Vandermonde matrix.
    
    Parameters:
    - x: array-like, input vector
    - N: int, number of columns (default len(x))
    - increasing: bool, order of powers
    
    Returns:
    cupy.ndarray: Vandermonde matrix
    """

Data Conversion

Convert between different data sources and CuPy arrays.

def asarray(a, dtype=None, order=None):
    """
    Convert input to CuPy array.
    
    Parameters:
    - a: array-like, input data
    - dtype: data type, if None preserve existing
    - order: {'C', 'F'}, memory layout
    
    Returns:
    cupy.ndarray: CuPy array view or copy
    """

def asanyarray(a, dtype=None, order=None):
    """Convert input to CuPy array, preserving subclasses."""

def ascontiguousarray(a, dtype=None):
    """Return contiguous array in C order."""

def copy(a, order='K', subok=False):
    """
    Return copy of array.
    
    Parameters:
    - a: array-like, input array
    - order: {'K', 'A', 'C', 'F'}, memory layout
    - subok: bool, whether to allow subclasses
    
    Returns:
    cupy.ndarray: Copy of array
    """

def asnumpy(a, stream=None, order='C', out=None, *, blocking=True):
    """
    Convert CuPy array to NumPy array on CPU.
    
    Parameters:
    - a: cupy.ndarray, input GPU array
    - stream: cupy.cuda.Stream, CUDA stream for transfer
    - order: {'C', 'F', 'A'}, memory layout
    - out: numpy.ndarray, output array to write to
    - blocking: bool, whether to block until transfer complete
    
    Returns:
    numpy.ndarray: CPU array
    """

Shape Manipulation

Modify array shapes and dimensions without changing 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: {'C', 'F', 'A'}, read/write order
    
    Returns:
    cupy.ndarray: Reshaped array
    """

def ravel(a, order='C'):
    """
    Return contiguous flattened array.
    
    Parameters:
    - a: array-like, input array
    - order: {'C', 'F', 'A', 'K'}, flattening order
    
    Returns:
    cupy.ndarray: Flattened array
    """

def squeeze(a, axis=None):
    """
    Remove single-dimensional entries from shape.
    
    Parameters:
    - a: array-like, input array
    - axis: None or int or tuple of ints, axes to squeeze
    
    Returns:
    cupy.ndarray: Squeezed array
    """

def expand_dims(a, axis):
    """
    Expand dimensions of array.
    
    Parameters:
    - a: array-like, input array
    - axis: int or tuple of ints, position of new axes
    
    Returns:
    cupy.ndarray: Array with expanded dimensions
    """

Array Joining

Combine multiple arrays along various axes.

def concatenate(arrays, axis=0, out=None, dtype=None, casting='same_kind'):
    """
    Join arrays along existing axis.
    
    Parameters:
    - arrays: sequence of arrays, arrays to concatenate
    - axis: int, axis along which to concatenate
    - out: cupy.ndarray, output array
    - dtype: data type, type of output array
    - casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, casting rule
    
    Returns:
    cupy.ndarray: Concatenated array
    """

def stack(arrays, axis=0, out=None):
    """
    Join arrays along new axis.
    
    Parameters:
    - arrays: sequence of arrays, arrays to stack
    - axis: int, axis along which to stack
    - out: cupy.ndarray, output array
    
    Returns:
    cupy.ndarray: Stacked array
    """

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."""

Array Splitting

Split arrays into multiple sub-arrays.

def split(ary, indices_or_sections, axis=0):
    """
    Split array into multiple sub-arrays.
    
    Parameters:
    - ary: cupy.ndarray, array to split
    - indices_or_sections: int or 1-D array, split points
    - axis: int, axis along which to split
    
    Returns:
    list of cupy.ndarray: Sub-arrays
    """

def array_split(ary, indices_or_sections, axis=0):
    """Split array into multiple sub-arrays of approximately equal size."""

def hsplit(ary, indices_or_sections):
    """Split array horizontally (column-wise)."""  

def vsplit(ary, indices_or_sections):
    """Split array vertically (row-wise)."""

def dsplit(ary, indices_or_sections):
    """Split array depth-wise (along third axis)."""

Usage Examples

Creating Arrays from Different Sources

import cupy as cp
import numpy as np

# From Python lists
arr = cp.array([1, 2, 3, 4, 5])
matrix = cp.array([[1, 2], [3, 4]])

# From NumPy arrays (transfers to GPU)
np_arr = np.random.random((100, 100))
gpu_arr = cp.asarray(np_arr)

# Uninitialized for performance
big_array = cp.empty((10000, 10000), dtype=cp.float32)

# Ranges for computation
x = cp.linspace(0, 2*cp.pi, 1000)
indices = cp.arange(0, 1000, 2)

Memory-Efficient Array Operations

# Reuse existing arrays when possible
result = cp.zeros_like(input_array)
cp.add(arr1, arr2, out=result)  # Write directly to result

# Create views instead of copies
reshaped = arr.reshape((100, -1))  # View, not copy
flattened = arr.ravel()  # View when possible

Install with Tessl CLI

npx tessl i tessl/pypi-cupy

docs

array-creation.md

cuda-management.md

fft.md

index.md

kernels.md

linear-algebra.md

math-functions.md

random.md

scipy-extensions.md

sparse.md

statistics.md

tile.json