CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for AMD ROCm 4.3 platform
—
Logical operations, comparisons, and truth value testing for GPU arrays. These functions provide element-wise logical operations, array comparisons, and content testing essential for array manipulation and filtering.
Element-wise logical operations on boolean and numeric arrays.
def logical_and(x1, x2):
"""
Compute truth value of x1 AND x2 element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean result of logical AND
"""
def logical_or(x1, x2):
"""
Compute truth value of x1 OR x2 element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean result of logical OR
"""
def logical_not(x):
"""
Compute truth value of NOT x element-wise.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Boolean result of logical NOT
"""
def logical_xor(x1, x2):
"""
Compute truth value of x1 XOR x2 element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean result of logical XOR
"""Element-wise comparison operations returning boolean arrays.
def equal(x1, x2):
"""
Return (x1 == x2) element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean array of equality comparison
"""
def not_equal(x1, x2):
"""
Return (x1 != x2) element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean array of inequality comparison
"""
def less(x1, x2):
"""
Return (x1 < x2) element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean array of less-than comparison
"""
def less_equal(x1, x2):
"""
Return (x1 <= x2) element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean array of less-equal comparison
"""
def greater(x1, x2):
"""
Return (x1 > x2) element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean array of greater-than comparison
"""
def greater_equal(x1, x2):
"""
Return (x1 >= x2) element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Boolean array of greater-equal comparison
"""Functions to compare entire arrays for equivalence and closeness within tolerance.
def array_equal(a1, a2, equal_nan=False):
"""
True if two arrays have the same shape and elements, False otherwise.
Parameters:
- a1, a2: cupy.ndarray, input arrays
- equal_nan: bool, whether to compare NaN values as equal
Returns:
bool: True if arrays are equal
"""
def array_equiv(a1, a2):
"""
Returns True if arrays are element-wise equal within broadcasting.
Parameters:
- a1, a2: array-like, input arrays
Returns:
bool: True if arrays are equivalent
"""
def allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
"""
Returns True if two arrays are element-wise equal within tolerance.
Parameters:
- a, b: array-like, input arrays
- rtol: float, relative tolerance parameter
- atol: float, absolute tolerance parameter
- equal_nan: bool, whether to compare NaN values as equal
Returns:
bool: True if arrays are close within tolerance
"""
def isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
"""
Returns boolean array where two arrays are element-wise equal within tolerance.
Parameters:
- a, b: array-like, input arrays
- rtol: float, relative tolerance parameter
- atol: float, absolute tolerance parameter
- equal_nan: bool, whether to compare NaN values as equal
Returns:
cupy.ndarray: Boolean array indicating closeness element-wise
"""Functions to test truth values across arrays and determine overall boolean state.
def all(a, axis=None, out=None, keepdims=False):
"""
Test whether all array elements along given axis evaluate to True.
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis along which logical AND is performed
- out: cupy.ndarray, alternative output array
- keepdims: bool, whether to retain reduced dimensions
Returns:
cupy.ndarray or bool: True if all elements are True
"""
def any(a, axis=None, out=None, keepdims=False):
"""
Test whether any array element along given axis evaluates to True.
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis along which logical OR is performed
- out: cupy.ndarray, alternative output array
- keepdims: bool, whether to retain reduced dimensions
Returns:
cupy.ndarray or bool: True if any element is True
"""
def alltrue(a, axis=None, out=None, keepdims=False):
"""
Test whether all array elements along given axis evaluate to True.
Alias for all().
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis along which to test
- out: cupy.ndarray, alternative output array
- keepdims: bool, whether to retain reduced dimensions
Returns:
cupy.ndarray or bool: True if all elements are True
"""
def sometrue(a, axis=None, out=None, keepdims=False):
"""
Test whether some array elements along given axis evaluate to True.
Alias for any().
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis along which to test
- out: cupy.ndarray, alternative output array
- keepdims: bool, whether to retain reduced dimensions
Returns:
cupy.ndarray or bool: True if any element is True
"""Functions to test array content for special values like NaN, infinity, and data types.
def isfinite(x):
"""
Test finiteness element-wise (not infinity or NaN).
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Boolean array indicating finite elements
"""
def isinf(x):
"""
Test for positive or negative infinity element-wise.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Boolean array indicating infinite elements
"""
def isnan(x):
"""
Test for NaN element-wise.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Boolean array indicating NaN elements
"""
def isneginf(x, out=None):
"""
Test for negative infinity element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, alternative output array
Returns:
cupy.ndarray: Boolean array indicating negative infinity
"""
def isposinf(x, out=None):
"""
Test for positive infinity element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, alternative output array
Returns:
cupy.ndarray: Boolean array indicating positive infinity
"""
def isreal(x):
"""
Returns True for real elements, False for complex elements.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Boolean array indicating real elements
"""
def iscomplex(x):
"""
Returns True for complex elements, False for real elements.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Boolean array indicating complex elements
"""
def isrealobj(x):
"""
Return True if x is not a complex type or an array of complex numbers.
Parameters:
- x: array-like, input array or scalar
Returns:
bool: True if object is real (not complex)
"""
def iscomplexobj(x):
"""
Return True if x is a complex type or an array of complex numbers.
Parameters:
- x: array-like, input array or scalar
Returns:
bool: True if object is complex
"""
def isfortran(a):
"""
Check if array is Fortran contiguous but not C contiguous.
Parameters:
- a: cupy.ndarray, input array
Returns:
bool: True if array is Fortran contiguous
"""Functions for set-like operations on arrays including membership testing and set algebra.
def in1d(ar1, ar2, assume_unique=False, invert=False):
"""
Test whether each element of ar1 is also present in ar2.
Parameters:
- ar1: array-like, input array
- ar2: array-like, values against which to test
- assume_unique: bool, whether input arrays are unique
- invert: bool, whether to invert the result
Returns:
cupy.ndarray: Boolean array of same shape as ar1
"""
def isin(element, test_elements, assume_unique=False, invert=False):
"""
Calculates element in test_elements, broadcasting over element only.
Parameters:
- element: array-like, input array
- test_elements: array-like, values against which to test
- assume_unique: bool, whether test_elements is unique
- invert: bool, whether to invert the result
Returns:
cupy.ndarray: Boolean array of same shape as element
"""
def intersect1d(ar1, ar2, assume_unique=False, return_indices=False):
"""
Find intersection of two arrays.
Parameters:
- ar1, ar2: array-like, input arrays
- assume_unique: bool, whether input arrays are unique
- return_indices: bool, whether to return indices
Returns:
cupy.ndarray: sorted 1D array of common and unique elements
"""
def setdiff1d(ar1, ar2, assume_unique=False):
"""
Find set difference of two arrays.
Parameters:
- ar1: array-like, input array
- ar2: array-like, values to remove from ar1
- assume_unique: bool, whether input arrays are unique
Returns:
cupy.ndarray: 1D array of values in ar1 that are not in ar2
"""
def setxor1d(ar1, ar2, assume_unique=False):
"""
Find set exclusive-or of two arrays.
Parameters:
- ar1, ar2: array-like, input arrays
- assume_unique: bool, whether input arrays are unique
Returns:
cupy.ndarray: sorted 1D array of unique values in only one array
"""
def union1d(ar1, ar2):
"""
Find union of two arrays.
Parameters:
- ar1, ar2: array-like, input arrays
Returns:
cupy.ndarray: unique, sorted union of input arrays
"""import cupy as cp
# Logical operations
a = cp.array([True, False, True, False])
b = cp.array([True, True, False, False])
logical_and_result = cp.logical_and(a, b) # [True, False, False, False]
logical_or_result = cp.logical_or(a, b) # [True, True, True, False]
logical_not_result = cp.logical_not(a) # [False, True, False, True]
logical_xor_result = cp.logical_xor(a, b) # [False, True, True, False]
# Comparison operations
x = cp.array([1, 2, 3, 4, 5])
y = cp.array([1, 3, 2, 4, 6])
equal_result = cp.equal(x, y) # [True, False, False, True, False]
less_result = cp.less(x, y) # [False, True, False, False, True]
greater_result = cp.greater(x, y) # [False, False, True, False, False]
# Array comparison
arr1 = cp.array([[1, 2], [3, 4]])
arr2 = cp.array([[1, 2], [3, 4]])
arr3 = cp.array([[1.0, 2.0], [3.0, 4.0001]])
arrays_equal = cp.array_equal(arr1, arr2) # True
arrays_close = cp.allclose(arr1, arr3, rtol=1e-3) # True
# Element-wise closeness
close_elements = cp.isclose(arr1, arr3, rtol=1e-3) # [[True, True], [True, True]]
# Truth value testing
bool_array = cp.array([True, False, True, True])
all_true = cp.all(bool_array) # False
any_true = cp.any(bool_array) # True
# Content testing
float_array = cp.array([1.0, cp.inf, cp.nan, -cp.inf, 2.5])
finite_mask = cp.isfinite(float_array) # [True, False, False, False, True]
inf_mask = cp.isinf(float_array) # [False, True, False, True, False]
nan_mask = cp.isnan(float_array) # [False, False, True, False, False]
# Complex number testing
complex_array = cp.array([1+2j, 3, 4+0j, 5j])
real_mask = cp.isreal(complex_array) # [False, True, True, False]
complex_mask = cp.iscomplex(complex_array) # [True, False, False, True]
# Set operations
set1 = cp.array([1, 2, 3, 4, 5])
set2 = cp.array([3, 4, 5, 6, 7])
membership = cp.in1d(set1, set2) # [False, False, True, True, True]
intersection = cp.intersect1d(set1, set2) # [3, 4, 5]
difference = cp.setdiff1d(set1, set2) # [1, 2]
union = cp.union1d(set1, set2) # [1, 2, 3, 4, 5, 6, 7]Install with Tessl CLI
npx tessl i tessl/pypi-cupy-rocm-4-3