CuPy: NumPy & SciPy for GPU (CUDA 10.1 version)
—
Logical operations and comparison functions including element-wise and array-wise logical operations, truth value testing, and type checking functions. These operations provide comprehensive tools for conditional logic, array comparison, and data validation on GPU arrays.
Logical operations applied element-wise to arrays.
def logical_and(x1, x2, out=None, **kwargs):
"""
Compute the truth value of x1 AND x2 element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with logical AND results
"""
def logical_or(x1, x2, out=None, **kwargs):
"""
Compute the truth value of x1 OR x2 element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with logical OR results
"""
def logical_xor(x1, x2, out=None, **kwargs):
"""
Compute the truth value of x1 XOR x2 element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with logical XOR results
"""
def logical_not(x, out=None, **kwargs):
"""
Compute the truth value of NOT x element-wise.
Parameters:
- x: array_like, input array
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with logical NOT results
"""Element-wise comparison functions returning boolean arrays.
def equal(x1, x2, out=None, **kwargs):
"""
Return (x1 == x2) element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with equality comparison results
"""
def not_equal(x1, x2, out=None, **kwargs):
"""
Return (x1 != x2) element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with inequality comparison results
"""
def less(x1, x2, out=None, **kwargs):
"""
Return (x1 < x2) element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with less-than comparison results
"""
def less_equal(x1, x2, out=None, **kwargs):
"""
Return (x1 <= x2) element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with less-equal comparison results
"""
def greater(x1, x2, out=None, **kwargs):
"""
Return (x1 > x2) element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with greater-than comparison results
"""
def greater_equal(x1, x2, out=None, **kwargs):
"""
Return (x1 >= x2) element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array with greater-equal comparison results
"""Functions for comparing entire arrays and testing for approximate equality.
def allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
"""
Returns True if two arrays are element-wise equal within a tolerance.
Parameters:
- a, b: array_like, input arrays to compare
- rtol: float, relative tolerance parameter
- atol: float, absolute tolerance parameter
- equal_nan: bool, whether to compare NaN's as equal
Returns:
bool: True if arrays are close 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: array_like, input arrays
- equal_nan: bool, whether to compare NaN's as equal
Returns:
bool: True if arrays are equal
"""
def isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
"""
Returns a boolean array where two arrays are element-wise equal within tolerance.
Parameters:
- a, b: array_like, input arrays to compare
- rtol: float, relative tolerance parameter
- atol: float, absolute tolerance parameter
- equal_nan: bool, whether to compare NaN's as equal
Returns:
cupy.ndarray: boolean array indicating closeness
"""Functions for testing array content for special values.
def isfinite(x, out=None, **kwargs):
"""
Test finiteness element-wise (not infinity or not Not a Number).
Parameters:
- x: array_like, input array
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array indicating finite elements
"""
def isinf(x, out=None, **kwargs):
"""
Test for positive or negative infinity element-wise.
Parameters:
- x: array_like, input array
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array indicating infinite elements
"""
def isnan(x, out=None, **kwargs):
"""
Test for NaN element-wise and return result as a boolean array.
Parameters:
- x: array_like, input array
- out: cupy.ndarray, optional output array
- **kwargs: additional keyword arguments
Returns:
cupy.ndarray: boolean array indicating NaN elements
"""Functions for testing array data type properties.
def iscomplex(x):
"""
Returns a bool array, where True if input element is complex.
Parameters:
- x: array_like, input array
Returns:
cupy.ndarray: boolean array indicating complex elements
"""
def iscomplexobj(x):
"""
Check for a complex type or an array of complex numbers.
Parameters:
- x: array_like, input array
Returns:
bool: True if x is complex type
"""
def isreal(x):
"""
Returns a bool array, where True if input element is real.
Parameters:
- x: array_like, input array
Returns:
cupy.ndarray: boolean array indicating real elements
"""
def isrealobj(x):
"""
Return True if x is a not complex type or an array of complex numbers.
Parameters:
- x: array_like, input array
Returns:
bool: True if x is real type
"""
def isfortran(a):
"""
Check if the array is Fortran contiguous but *not* C contiguous.
Parameters:
- a: cupy.ndarray, input array
Returns:
bool: True if array is Fortran contiguous
"""
def isscalar(element):
"""
Returns True if the type of element is a scalar type.
Parameters:
- element: any, input element
Returns:
bool: True if element is scalar
"""Functions for testing array truth values and membership.
def all(a, axis=None, out=None, keepdims=False):
"""
Test whether all array elements along a given axis evaluate to True.
Parameters:
- a: array_like, input array
- axis: None, int, or tuple of ints, axis along which to operate
- out: cupy.ndarray, optional output array
- keepdims: bool, whether to keep dimensions
Returns:
cupy.ndarray or bool: result of all operation
"""
def any(a, axis=None, out=None, keepdims=False):
"""
Test whether any array element along a given axis evaluates to True.
Parameters:
- a: array_like, input array
- axis: None, int, or tuple of ints, axis along which to operate
- out: cupy.ndarray, optional output array
- keepdims: bool, whether to keep dimensions
Returns:
cupy.ndarray or bool: result of any operation
"""
def in1d(ar1, ar2, assume_unique=False, invert=False):
"""
Test whether each element of a 1-D array is also present in a second array.
Parameters:
- ar1: array_like, input array
- ar2: array_like, values against which to test each element of ar1
- assume_unique: bool, assume input arrays are unique
- invert: bool, invert the boolean return array
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 each element
- assume_unique: bool, assume input arrays are unique
- invert: bool, invert the boolean return array
Returns:
cupy.ndarray: boolean array of same shape as element
"""import cupy as cp
# Create test arrays
a = cp.array([True, False, True, False])
b = cp.array([True, True, False, False])
# Logical AND
result_and = cp.logical_and(a, b)
print(result_and) # [True False False False]
# Logical OR
result_or = cp.logical_or(a, b)
print(result_or) # [True True True False]
# Logical NOT
result_not = cp.logical_not(a)
print(result_not) # [False True False True]import cupy as cp
# Create numeric arrays
x = cp.array([1, 2, 3, 4, 5])
y = cp.array([1, 3, 2, 4, 6])
# Element-wise comparisons
print(cp.equal(x, y)) # [True False False True False]
print(cp.less(x, y)) # [False True False False True]
print(cp.greater(x, y)) # [False False True False False]import cupy as cp
# Array with special values
arr = cp.array([1.0, cp.inf, cp.nan, -cp.inf, 0.0])
# Test for finite values
finite_mask = cp.isfinite(arr)
print(finite_mask) # [True False False False True]
# Test for infinite values
inf_mask = cp.isinf(arr)
print(inf_mask) # [False True False True False]
# Test for NaN values
nan_mask = cp.isnan(arr)
print(nan_mask) # [False False True False False]import cupy as cp
# Boolean array
bool_arr = cp.array([[True, True], [False, True]])
# Test if all elements are True
all_true = cp.all(bool_arr)
print(all_true) # False
# Test if any elements are True
any_true = cp.any(bool_arr)
print(any_true) # True
# Test along specific axis
all_axis0 = cp.all(bool_arr, axis=0)
print(all_axis0) # [False True]import cupy as cp
# Test array membership
elements = cp.array([1, 2, 3, 4, 5])
test_values = cp.array([2, 4, 6])
# Test which elements are in test_values
mask = cp.isin(elements, test_values)
print(mask) # [False True False True False]
# Get elements that are in test_values
result = elements[mask]
print(result) # [2 4]Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda101@9.6.1