CuPy: NumPy & SciPy for GPU (CUDA 10.1 version)
—
Array manipulation operations that transform array structure, shape, and organization. These functions provide comprehensive tools for reshaping, transposing, joining, splitting, and rearranging GPU arrays while maintaining optimal memory layout and performance.
Functions for changing array dimensions and structure.
def reshape(a, newshape, order='C'):
"""
Gives a new shape to an array without changing its data.
Parameters:
- a: array_like, input array
- newshape: int or tuple of ints, new shape
- order: {'C', 'F', 'A'}, read elements order
Returns:
cupy.ndarray: reshaped array
"""
def ravel(a, order='C'):
"""
Return a contiguous flattened array.
Parameters:
- a: array_like, input array
- order: {'C', 'F', 'A', 'K'}, read elements order
Returns:
cupy.ndarray: 1-D array
"""
def shape(a):
"""
Return the shape of an array.
Parameters:
- a: array_like, input array
Returns:
tuple of ints: shape of array
"""Functions for rearranging array axes.
def transpose(a, axes=None):
"""
Reverse or permute the axes of an array.
Parameters:
- a: array_like, input array
- axes: tuple or list of ints, permutation of axes
Returns:
cupy.ndarray: transposed array
"""
def swapaxes(a, axis1, axis2):
"""
Interchange two axes of an array.
Parameters:
- a: array_like, input array
- axis1: int, first axis
- axis2: int, second axis
Returns:
cupy.ndarray: array with swapped axes
"""
def moveaxis(a, source, destination):
"""
Move axes of an array to new positions.
Parameters:
- a: array_like, input array
- source: int or sequence of int, original positions
- destination: int or sequence of int, destination positions
Returns:
cupy.ndarray: array with moved axes
"""
def rollaxis(a, axis, start=0):
"""
Roll the specified axis backwards.
Parameters:
- a: array_like, input array
- axis: int, axis to roll backwards
- start: int, position to roll before
Returns:
cupy.ndarray: array with rolled axis
"""Functions for adding or removing array dimensions.
def expand_dims(a, axis):
"""
Expand the shape of an array by inserting new axes.
Parameters:
- a: array_like, input array
- axis: int or tuple of ints, position of new axes
Returns:
cupy.ndarray: array with expanded dimensions
"""
def squeeze(a, axis=None):
"""
Remove single-dimensional entries from array shape.
Parameters:
- a: array_like, input array
- axis: None, int, or tuple of ints, axes to squeeze
Returns:
cupy.ndarray: squeezed array
"""
def atleast_1d(*arys):
"""
Convert inputs to arrays with at least one dimension.
Parameters:
- arys: array_like, input arrays
Returns:
list of cupy.ndarray: arrays with at least 1-D
"""
def atleast_2d(*arys):
"""
View inputs as arrays with at least two dimensions.
Parameters:
- arys: array_like, input arrays
Returns:
list of cupy.ndarray: arrays with at least 2-D
"""
def atleast_3d(*arys):
"""
View inputs as arrays with at least three dimensions.
Parameters:
- arys: array_like, input arrays
Returns:
list of cupy.ndarray: arrays with at least 3-D
"""Functions for broadcasting arrays to compatible shapes.
def broadcast(*args):
"""
Produce an object that mimics broadcasting.
Parameters:
- args: array_like, input arrays
Returns:
broadcast object
"""
def broadcast_arrays(*args, **kwargs):
"""
Broadcast any number of arrays against each other.
Parameters:
- args: array_like, input arrays
Returns:
list of cupy.ndarray: broadcasted arrays
"""
def broadcast_to(array, shape, subok=False):
"""
Broadcast an array to a new shape.
Parameters:
- array: array_like, input array
- shape: tuple, target shape
- subok: bool, whether to allow subclasses
Returns:
cupy.ndarray: broadcasted array
"""Functions for combining multiple arrays.
def concatenate(arrays, axis=0, out=None, dtype=None, casting="same_kind"):
"""
Join a sequence of arrays along an existing axis.
Parameters:
- arrays: sequence of array_like, arrays to concatenate
- axis: int, axis along which arrays are joined
- out: cupy.ndarray, optional output array
- dtype: str or dtype, output array data type
- casting: str, casting rule
Returns:
cupy.ndarray: concatenated array
"""
def stack(arrays, axis=0, out=None):
"""
Join a sequence of arrays along a new axis.
Parameters:
- arrays: sequence of array_like, arrays to stack
- axis: int, axis along which arrays are stacked
- out: cupy.ndarray, optional output array
Returns:
cupy.ndarray: stacked array
"""
def vstack(arrays):
"""
Stack arrays in sequence vertically (row wise).
Parameters:
- arrays: sequence of array_like, arrays to stack
Returns:
cupy.ndarray: stacked array
"""
def hstack(arrays):
"""
Stack arrays in sequence horizontally (column wise).
Parameters:
- arrays: sequence of array_like, arrays to stack
Returns:
cupy.ndarray: stacked array
"""
def dstack(arrays):
"""
Stack arrays in sequence depth wise (along third axis).
Parameters:
- arrays: sequence of array_like, arrays to stack
Returns:
cupy.ndarray: stacked array
"""
def column_stack(arrays):
"""
Stack 1-D arrays as columns into a 2-D array.
Parameters:
- arrays: sequence of 1-D array_like, arrays to stack
Returns:
cupy.ndarray: 2-D stacked array
"""Functions for dividing arrays into sub-arrays.
def split(ary, indices_or_sections, axis=0):
"""
Split an array into multiple sub-arrays.
Parameters:
- ary: cupy.ndarray, input array
- 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 an array into multiple sub-arrays of equal or near-equal size.
Parameters:
- ary: cupy.ndarray, input array
- 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 hsplit(ary, indices_or_sections):
"""
Split an array into multiple sub-arrays horizontally.
Parameters:
- ary: cupy.ndarray, input array
- indices_or_sections: int or 1-D array, split points
Returns:
list of cupy.ndarray: sub-arrays
"""
def vsplit(ary, indices_or_sections):
"""
Split an array into multiple sub-arrays vertically.
Parameters:
- ary: cupy.ndarray, input array
- indices_or_sections: int or 1-D array, split points
Returns:
list of cupy.ndarray: sub-arrays
"""
def dsplit(ary, indices_or_sections):
"""
Split array into multiple sub-arrays along the 3rd axis (depth).
Parameters:
- ary: cupy.ndarray, input array
- indices_or_sections: int or 1-D array, split points
Returns:
list of cupy.ndarray: sub-arrays
"""Functions for creating repetitions of arrays.
def tile(A, reps):
"""
Construct an array by repeating A the number of times given by reps.
Parameters:
- A: array_like, input array
- reps: int or sequence of ints, repetitions
Returns:
cupy.ndarray: tiled array
"""
def repeat(a, repeats, axis=None):
"""
Repeat elements of an 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: repeated array
"""Functions for modifying array contents.
def append(arr, values, axis=None):
"""
Append values to the end of an array.
Parameters:
- arr: array_like, input array
- values: array_like, values to append
- axis: int, axis along which to append
Returns:
cupy.ndarray: array with appended values
"""
def resize(a, new_shape):
"""
Return a new array with the specified shape.
Parameters:
- a: array_like, input array
- new_shape: int or tuple of int, new shape
Returns:
cupy.ndarray: resized array
"""
def unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None):
"""
Find the unique elements of an array.
Parameters:
- ar: array_like, input array
- return_index: bool, return indices of unique elements
- return_inverse: bool, return inverse indices
- return_counts: bool, return counts of unique elements
- axis: int, axis to operate on
Returns:
cupy.ndarray or tuple: unique elements and optional arrays
"""
def trim_zeros(filt, trim='fb'):
"""
Trim the leading and/or trailing zeros from a 1-D array.
Parameters:
- filt: 1-D array_like, input array
- trim: str, trimming behavior
Returns:
cupy.ndarray: trimmed array
"""Functions for reordering array elements.
def flip(m, axis=None):
"""
Reverse the order of elements in an array along given axis.
Parameters:
- m: array_like, input array
- axis: None, int, or tuple of ints, axis to flip
Returns:
cupy.ndarray: flipped array
"""
def fliplr(m):
"""
Flip array in the left/right direction.
Parameters:
- m: array_like, input array
Returns:
cupy.ndarray: flipped array
"""
def flipud(m):
"""
Flip array in the up/down direction.
Parameters:
- m: array_like, input array
Returns:
cupy.ndarray: flipped array
"""
def roll(a, shift, axis=None):
"""
Roll array elements along a given axis.
Parameters:
- a: array_like, input array
- shift: int or tuple of int, number of places to shift
- axis: int or tuple of int, axis to roll
Returns:
cupy.ndarray: rolled array
"""
def rot90(m, k=1, axes=(0, 1)):
"""
Rotate an array by 90 degrees in the plane specified by axes.
Parameters:
- m: array_like, input array
- k: int, number of times to rotate
- axes: tuple of int, plane of rotation
Returns:
cupy.ndarray: rotated array
"""Functions for controlling array memory layout and data types.
def asfortranarray(a, dtype=None):
"""
Return an array laid out in Fortran order in memory.
Parameters:
- a: array_like, input array
- dtype: str or dtype, output data type
Returns:
cupy.ndarray: Fortran-ordered array
"""
def require(a, dtype=None, requirements=None):
"""
Return an array which satisfies requirements.
Parameters:
- a: array_like, input array
- dtype: str or dtype, required data type
- requirements: str or list of str, requirements
Returns:
cupy.ndarray: array satisfying requirements
"""
def copyto(dst, src, casting='same_kind', where=True):
"""
Copies values from one array to another, broadcasting as necessary.
Parameters:
- dst: cupy.ndarray, destination array
- src: array_like, source array
- casting: str, casting rule
- where: array_like of bool, where to copy
"""Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda101@9.6.1