CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cupy-cuda101

CuPy: NumPy & SciPy for GPU (CUDA 10.1 version)

Pending
Overview
Eval results
Files

indexing-searching.mddocs/

Indexing and Searching

Advanced indexing, searching, and selection operations including multi-dimensional indexing, conditional selection, and array searching functions. These operations provide comprehensive tools for accessing, extracting, and manipulating specific array elements based on various criteria.

Capabilities

Advanced Indexing

Functions for complex array indexing and element selection.

def take(a, indices, axis=None, out=None, mode='raise'):
    """
    Take elements from an array along an axis.
    
    Parameters:
    - a: array_like, input array
    - indices: array_like, indices of elements to take
    - axis: int, axis over which to select values
    - out: cupy.ndarray, optional output array
    - mode: str, how to handle out-of-bounds indices
    
    Returns:
    cupy.ndarray: array with selected elements
    """

def take_along_axis(arr, indices, axis):
    """
    Take values from the input array by matching 1d index and data slices.
    
    Parameters:
    - arr: cupy.ndarray, input array
    - indices: cupy.ndarray, indices to take along each 1d slice
    - axis: int, axis along which to take 1d slices
    
    Returns:
    cupy.ndarray: array with selected elements
    """

def choose(a, choices, out=None, mode='raise'):
    """
    Construct an array from an index array and a set of arrays to choose from.
    
    Parameters:
    - a: int array, index array specifying which choice to use
    - choices: sequence of arrays, choice arrays
    - out: cupy.ndarray, optional output array
    - mode: str, how to handle out-of-bounds indices
    
    Returns:
    cupy.ndarray: constructed array
    """

def compress(condition, a, axis=None, out=None):
    """
    Return selected slices of an array along given axis.
    
    Parameters:
    - condition: 1-D array of bool, which entries to return
    - a: array_like, input array
    - axis: int, axis along which to take slices
    - out: cupy.ndarray, optional output array
    
    Returns:
    cupy.ndarray: compressed array
    """

def extract(condition, arr):
    """
    Return the elements of an array that satisfy some condition.
    
    Parameters:
    - condition: array_like, condition array
    - arr: array_like, input array
    
    Returns:
    cupy.ndarray: 1-D array with elements where condition is True
    """

def select(condlist, choicelist, default=0):
    """
    Return an array drawn from elements in choicelist, depending on conditions.
    
    Parameters:
    - condlist: list of arrays, conditions determining choice
    - choicelist: list of arrays, arrays to choose from
    - default: scalar, default value
    
    Returns:
    cupy.ndarray: array with selected elements
    """

Index Generation

Functions for generating index arrays and coordinate arrays.

def indices(dimensions, dtype=int, sparse=False):
    """
    Return an array representing the indices of a grid.
    
    Parameters:
    - dimensions: sequence of ints, shape of the grid
    - dtype: dtype, data type of result
    - sparse: bool, return sparse representation
    
    Returns:
    cupy.ndarray or tuple: grid indices
    """

def ix_(*args):
    """
    Construct an open mesh from multiple sequences.
    
    Parameters:
    - args: 1-D sequences, input sequences
    
    Returns:
    tuple of cupy.ndarray: open mesh arrays
    """

def unravel_index(indices, shape, order='C'):
    """
    Converts a flat index or array of flat indices into a tuple of coordinate arrays.
    
    Parameters:
    - indices: array_like, flat indices to convert
    - shape: tuple of ints, shape of the array
    - order: str, index order ('C' or 'F')
    
    Returns:
    tuple of cupy.ndarray: coordinate arrays
    """

def ravel_multi_index(multi_index, dims, mode='raise', order='C'):
    """
    Converts a tuple of index arrays into an array of flat indices.
    
    Parameters:
    - multi_index: tuple of array_like, tuple of index arrays
    - dims: tuple of ints, shape of array
    - mode: str, how to handle out-of-bounds indices
    - order: str, index order ('C' or 'F')
    
    Returns:
    cupy.ndarray: flat indices array
    """

def diag_indices(n, ndim=2):
    """
    Return the indices to access the main diagonal of an array.
    
    Parameters:
    - n: int, size of array for which diagonal indices are returned
    - ndim: int, number of dimensions
    
    Returns:
    tuple of cupy.ndarray: diagonal indices
    """

def diag_indices_from(arr):
    """
    Return the indices to access the main diagonal of an n-dimensional array.
    
    Parameters:
    - arr: array_like, input array
    
    Returns:
    tuple of cupy.ndarray: diagonal indices
    """

Array Searching

Functions for finding elements in arrays.

def where(condition, x=None, y=None):
    """
    Return elements chosen from x or y depending on condition.
    
    Parameters:
    - condition: array_like, bool, where True, yield x, otherwise yield y
    - x, y: array_like, values from which to choose
    
    Returns:
    cupy.ndarray or tuple: selected elements or indices
    """

def nonzero(a):
    """
    Return the indices of the elements that are non-zero.
    
    Parameters:
    - a: array_like, input array
    
    Returns:
    tuple of cupy.ndarray: indices of non-zero elements
    """

def flatnonzero(a):
    """
    Return indices that are non-zero in the flattened version of a.
    
    Parameters:
    - a: array_like, input array
    
    Returns:
    cupy.ndarray: 1-D array of indices
    """

def argwhere(a):
    """
    Find the indices of array elements that are non-zero, grouped by element.
    
    Parameters:
    - a: array_like, input array
    
    Returns:
    cupy.ndarray: indices of non-zero elements
    """

def searchsorted(a, v, side='left', sorter=None):
    """
    Find indices where elements should be inserted to maintain order.
    
    Parameters:
    - a: 1-D array_like, sorted input array
    - v: array_like, values to insert
    - side: str, which side to return insertion point
    - sorter: 1-D array_like, optional array of indices sorting a
    
    Returns:
    cupy.ndarray: insertion indices
    """

Extrema Finding

Functions for finding minimum and maximum elements and their indices.

def argmax(a, axis=None, out=None):
    """
    Returns the indices of the maximum values along an axis.
    
    Parameters:
    - a: array_like, input array
    - axis: int, axis along which to operate
    - out: cupy.ndarray, optional output array
    
    Returns:
    cupy.ndarray: indices of maximum values
    """

def argmin(a, axis=None, out=None):
    """
    Returns the indices of the minimum values along an axis.
    
    Parameters:
    - a: array_like, input array
    - axis: int, axis along which to operate
    - out: cupy.ndarray, optional output array
    
    Returns:
    cupy.ndarray: indices of minimum values
    """

def nanargmax(a, axis=None):
    """
    Return the indices of the maximum values in the specified axis ignoring NaNs.
    
    Parameters:
    - a: array_like, input array
    - axis: int, axis along which to operate
    
    Returns:
    cupy.ndarray: indices of maximum values
    """

def nanargmin(a, axis=None):
    """
    Return the indices of the minimum values in the specified axis ignoring NaNs.
    
    Parameters:
    - a: array_like, input array
    - axis: int, axis along which to operate
    
    Returns:
    cupy.ndarray: indices of minimum values
    """

Array Insertion and Modification

Functions for modifying arrays at specific indices.

def place(arr, mask, vals):
    """
    Change elements of an array based on conditional and input values.
    
    Parameters:
    - arr: cupy.ndarray, array to put data into
    - mask: array_like, boolean mask array
    - vals: array_like, values to put into arr
    """

def put(a, ind, v, mode='raise'):
    """
    Replaces specified elements of an array with given values.
    
    Parameters:
    - a: cupy.ndarray, target array
    - ind: array_like, target indices
    - v: array_like, values to place
    - mode: str, how to handle out-of-bounds indices
    """

def putmask(a, mask, values):
    """
    Changes elements of an array based on conditional and input values.
    
    Parameters:
    - a: cupy.ndarray, target array
    - mask: array_like, boolean mask array
    - values: array_like, values to assign
    """

def fill_diagonal(a, val, wrap=False):
    """
    Fill the main diagonal of the given array of any dimensionality.
    
    Parameters:
    - a: cupy.ndarray, array to modify (modified in-place)
    - val: scalar, value to be written on the diagonal
    - wrap: bool, whether to wrap diagonal for tall matrices
    """

Diagonal Operations

Functions for working with array diagonals.

def diagonal(a, offset=0, axis1=0, axis2=1):
    """
    Return specified diagonals.
    
    Parameters:
    - a: array_like, input array
    - offset: int, diagonal offset
    - axis1: int, first axis
    - axis2: int, second axis
    
    Returns:
    cupy.ndarray: diagonal elements
    """

Iterator Objects

Objects for iterating over arrays.

class flatiter:
    """
    Flat iterator object to iterate over arrays.
    
    A flatiter iterator is returned by flat for any array type.
    """
    def __iter__(self): ...
    def __next__(self): ...
    def __getitem__(self, key): ...
    def __setitem__(self, key, value): ...

Usage Examples

Basic Indexing and Selection

import cupy as cp

# Create test arrays
arr = cp.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
indices = cp.array([0, 2, 1])

# Take elements along axis
result = cp.take(arr, indices, axis=0)
print(result)  # [[1 2 3] [7 8 9] [4 5 6]]

# Extract elements based on condition
condition = arr > 5
selected = cp.extract(condition, arr)
print(selected)  # [6 7 8 9]

Finding Non-zero Elements

import cupy as cp

# Array with some zeros
arr = cp.array([[0, 1, 0], [2, 0, 3], [0, 4, 0]])

# Find non-zero elements
nonzero_indices = cp.nonzero(arr)
print(nonzero_indices)  # (array([0, 1, 1, 2]), array([1, 0, 2, 1]))

# Get non-zero values
nonzero_values = arr[nonzero_indices]
print(nonzero_values)  # [1 2 3 4]

# Alternative using where
where_result = cp.where(arr != 0)
print(where_result)  # Same as nonzero_indices

Conditional Selection

import cupy as cp

# Create arrays for conditional selection
condition = cp.array([True, False, True, False, True])
x = cp.array([1, 2, 3, 4, 5])
y = cp.array([10, 20, 30, 40, 50])

# Select from x where condition is True, y otherwise
result = cp.where(condition, x, y)
print(result)  # [1 20 3 40 5]

Finding Extrema

import cupy as cp

# 2D array
arr = cp.array([[3, 1, 4], [1, 5, 9], [2, 6, 5]])

# Find indices of maximum values
max_indices = cp.argmax(arr, axis=1)
print(max_indices)  # [2 2 1]

# Find global maximum index (flattened)
global_max_idx = cp.argmax(arr)
print(global_max_idx)  # 5 (corresponds to value 9)

# Get the actual maximum values
max_values = cp.max(arr, axis=1)
print(max_values)  # [4 9 6]

Advanced Indexing with Choose

import cupy as cp

# Index array
indices = cp.array([[0, 1], [2, 0]])

# Choice arrays
choices = [cp.array([[10, 11], [12, 13]]),  # choice 0
           cp.array([[20, 21], [22, 23]]),  # choice 1
           cp.array([[30, 31], [32, 33]])]  # choice 2

# Choose elements based on indices
result = cp.choose(indices, choices)
print(result)  # [[10 21] [32 13]]

Searching in Sorted Arrays

import cupy as cp

# Sorted array
sorted_arr = cp.array([1, 3, 5, 7, 9, 11])

# Values to insert
values = cp.array([2, 6, 8, 12])

# Find insertion points
insertion_points = cp.searchsorted(sorted_arr, values)
print(insertion_points)  # [1 3 4 6]

# Insert values to maintain sorted order
result = cp.insert(sorted_arr, insertion_points, values)
print(result)  # [1 2 3 5 6 7 8 9 11 12]

Install with Tessl CLI

npx tessl i tessl/pypi-cupy-cuda101@9.6.1

docs

array-creation.md

array-manipulation.md

binary-operations.md

cuda.md

fft.md

index.md

indexing-searching.md

linalg.md

logic-functions.md

math-functions.md

memory-performance.md

random.md

sorting-counting.md

statistics.md

tile.json