NumPy & SciPy compatible GPU-accelerated array library for CUDA computing
—
Comprehensive logical operations, element-wise comparisons, truth value testing, and content validation functions for array processing and conditional operations. CuPy provides complete GPU implementations of logical and comparison operations with NumPy compatibility.
Element-wise comparison operations returning boolean arrays for conditional processing.
def equal(x1, x2, out=None, **kwargs):
"""Element-wise equality comparison.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array of equality results
"""
def not_equal(x1, x2, out=None, **kwargs):
"""Element-wise inequality comparison.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array of inequality results
"""
def less(x1, x2, out=None, **kwargs):
"""Element-wise less-than comparison.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array where x1 < x2
"""
def less_equal(x1, x2, out=None, **kwargs):
"""Element-wise less-than-or-equal comparison.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array where x1 <= x2
"""
def greater(x1, x2, out=None, **kwargs):
"""Element-wise greater-than comparison.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array where x1 > x2
"""
def greater_equal(x1, x2, out=None, **kwargs):
"""Element-wise greater-than-or-equal comparison.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array where x1 >= x2
"""Higher-level array comparison functions for testing array equality and similarity.
def array_equal(a1, a2, equal_nan=False):
"""Test if two arrays are element-wise equal.
Args:
a1: First input array
a2: Second input array
equal_nan: Whether to compare NaN values as equal
Returns:
bool: True if arrays are equal
"""
def array_equiv(a1, a2):
"""Test if two arrays are equivalent (broadcastable and equal).
Args:
a1: First input array
a2: Second input array
Returns:
bool: True if arrays are equivalent
"""
def allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
"""Test if two arrays are element-wise equal within tolerance.
Args:
a: First input array
b: Second input array
rtol: Relative tolerance
atol: Absolute tolerance
equal_nan: Whether to compare NaN values as equal
Returns:
bool: True if arrays are close
"""
def isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
"""Element-wise test for closeness within tolerance.
Args:
a: First input array
b: Second input array
rtol: Relative tolerance
atol: Absolute tolerance
equal_nan: Whether to compare NaN values as equal
Returns:
cupy.ndarray: Boolean array of closeness results
"""Element-wise logical operations for boolean algebra and conditional logic.
def logical_and(x1, x2, out=None, **kwargs):
"""Element-wise logical AND operation.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array of AND results
"""
def logical_or(x1, x2, out=None, **kwargs):
"""Element-wise logical OR operation.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array of OR results
"""
def logical_not(x, out=None, **kwargs):
"""Element-wise logical NOT operation.
Args:
x: Input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array of NOT results
"""
def logical_xor(x1, x2, out=None, **kwargs):
"""Element-wise logical XOR operation.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array of XOR results
"""Functions for testing truth values and array conditions across multiple elements.
def all(a, axis=None, out=None, keepdims=False, where=None):
"""Test whether all elements evaluate to True.
Args:
a: Input array
axis: Axis along which to perform test
out: Output array
keepdims: Keep reduced dimensions as 1
where: Boolean array for selective testing
Returns:
cupy.ndarray or bool: Result of all-test
"""
def any(a, axis=None, out=None, keepdims=False, where=None):
"""Test whether any element evaluates to True.
Args:
a: Input array
axis: Axis along which to perform test
out: Output array
keepdims: Keep reduced dimensions as 1
where: Boolean array for selective testing
Returns:
cupy.ndarray or bool: Result of any-test
"""
def alltrue(a, axis=None, out=None, keepdims=False, where=None):
"""Test if all elements are True (alias for all)."""
def sometrue(a, axis=None, out=None, keepdims=False, where=None):
"""Test if any element is True (alias for any)."""Functions for validating array content and detecting special values.
def isfinite(x, out=None, **kwargs):
"""Test for finite values (not infinity, not NaN).
Args:
x: Input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array indicating finite values
"""
def isinf(x, out=None, **kwargs):
"""Test for infinite values.
Args:
x: Input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array indicating infinite values
"""
def isnan(x, out=None, **kwargs):
"""Test for NaN (Not a Number) values.
Args:
x: Input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Boolean array indicating NaN values
"""
def isneginf(x, out=None):
"""Test for negative infinity values.
Args:
x: Input array
out: Output array
Returns:
cupy.ndarray: Boolean array indicating negative infinity
"""
def isposinf(x, out=None):
"""Test for positive infinity values.
Args:
x: Input array
out: Output array
Returns:
cupy.ndarray: Boolean array indicating positive infinity
"""
def isreal(x):
"""Test for real values (imaginary part is zero).
Args:
x: Input array
Returns:
cupy.ndarray: Boolean array indicating real values
"""
def iscomplex(x):
"""Test for complex values (imaginary part is non-zero).
Args:
x: Input array
Returns:
cupy.ndarray: Boolean array indicating complex values
"""Functions for testing membership and performing set-like operations.
def in1d(ar1, ar2, assume_unique=False, invert=False):
"""Test membership of elements in 1D arrays.
Args:
ar1: Input array
ar2: Values to test for membership
assume_unique: Assume input arrays contain unique elements
invert: Return complement of membership
Returns:
cupy.ndarray: Boolean array indicating membership
"""
def isin(element, test_elements, assume_unique=False, invert=False):
"""Test element membership in array.
Args:
element: Element or array to test
test_elements: Array of test values
assume_unique: Assume test_elements contains unique values
invert: Return complement of membership
Returns:
cupy.ndarray: Boolean array indicating membership
"""
def intersect1d(ar1, ar2, assume_unique=False, return_indices=False):
"""Find intersection of two 1D arrays.
Args:
ar1: First input array
ar2: Second input array
assume_unique: Assume input arrays are unique
return_indices: Return indices of intersection
Returns:
cupy.ndarray or tuple: Intersection values and optionally indices
"""
def setdiff1d(ar1, ar2, assume_unique=False):
"""Find set difference between two 1D arrays.
Args:
ar1: First input array
ar2: Second input array
assume_unique: Assume input arrays are unique
Returns:
cupy.ndarray: Values in ar1 but not in ar2
"""
def union1d(ar1, ar2):
"""Find union of two 1D arrays.
Args:
ar1: First input array
ar2: Second input array
Returns:
cupy.ndarray: Unique union values
"""
def setxor1d(ar1, ar2, assume_unique=False):
"""Find symmetric difference of two 1D arrays.
Args:
ar1: First input array
ar2: Second input array
assume_unique: Assume input arrays are unique
Returns:
cupy.ndarray: Values in either array but not both
"""Functions for testing array properties and data types.
def isscalar(element):
"""Test if input is scalar type.
Args:
element: Input to test
Returns:
bool: True if input is scalar
"""
def isrealobj(x):
"""Test if input object has real data type.
Args:
x: Input object
Returns:
bool: True if object is real-valued
"""
def iscomplexobj(x):
"""Test if input object has complex data type.
Args:
x: Input object
Returns:
bool: True if object is complex-valued
"""
def isfortran(a):
"""Test if array is Fortran-contiguous.
Args:
a: Input array
Returns:
bool: True if array is Fortran-contiguous
"""
def issubdtype(arg1, arg2):
"""Test if data type is subtype of another.
Args:
arg1: First data type
arg2: Second data type
Returns:
bool: True if arg1 is subtype of arg2
"""
def issubclass_(arg1, arg2):
"""Test if class is subclass of another.
Args:
arg1: First class
arg2: Second class
Returns:
bool: True if arg1 is subclass of arg2
"""
def issubsctype(arg1, arg2):
"""Test if scalar type is subtype of another.
Args:
arg1: First scalar type
arg2: Second scalar type
Returns:
bool: True if arg1 is subtype of arg2
"""Element-wise bitwise operations for integer arrays and boolean logic.
def bitwise_and(x1, x2, out=None, **kwargs):
"""Element-wise bitwise AND operation.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Bitwise AND result
"""
def bitwise_or(x1, x2, out=None, **kwargs):
"""Element-wise bitwise OR operation.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Bitwise OR result
"""
def bitwise_xor(x1, x2, out=None, **kwargs):
"""Element-wise bitwise XOR operation.
Args:
x1: First input array
x2: Second input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Bitwise XOR result
"""
def bitwise_not(x, out=None, **kwargs):
"""Element-wise bitwise NOT operation.
Args:
x: Input array
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Bitwise NOT result
"""
def invert(x, out=None, **kwargs):
"""Bitwise inversion (alias for bitwise_not)."""
def left_shift(x1, x2, out=None, **kwargs):
"""Element-wise left bit shift.
Args:
x1: Input array
x2: Number of positions to shift
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Left-shifted values
"""
def right_shift(x1, x2, out=None, **kwargs):
"""Element-wise right bit shift.
Args:
x1: Input array
x2: Number of positions to shift
out: Output array
**kwargs: Additional ufunc parameters
Returns:
cupy.ndarray: Right-shifted values
"""Functions for packing and manipulating bits within arrays.
def packbits(a, axis=None, bitorder='big'):
"""Pack binary values into bytes.
Args:
a: Input array of binary values
axis: Axis along which to pack
bitorder: Bit order ('big' or 'little')
Returns:
cupy.ndarray: Packed byte array
"""
def unpackbits(a, axis=None, count=None, bitorder='big'):
"""Unpack bytes into binary values.
Args:
a: Input byte array
axis: Axis along which to unpack
count: Number of bits to unpack
bitorder: Bit order ('big' or 'little')
Returns:
cupy.ndarray: Unpacked binary array
"""
def binary_repr(num, width=None):
"""Return binary representation of number as string.
Args:
num: Input number
width: Minimum width of representation
Returns:
str: Binary representation
"""import cupy as cp
# Create test arrays
a = cp.array([1, 2, 3, 4, 5])
b = cp.array([3, 2, 1, 4, 6])
# Element-wise comparisons
equal_mask = cp.equal(a, b) # [False, True, False, True, False]
less_mask = cp.less(a, b) # [True, False, False, False, True]
greater_mask = cp.greater(a, b) # [False, False, True, False, False]
print(f"Equal: {equal_mask}")
print(f"Less: {less_mask}")
print(f"Greater: {greater_mask}")
# Logical operations
and_result = cp.logical_and(a > 2, b > 2) # [True, False, False, True, True]
or_result = cp.logical_or(a > 4, b > 4) # [False, False, False, True, True]
not_result = cp.logical_not(a > 3) # [True, True, True, False, False]
print(f"AND (a>2 & b>2): {and_result}")
print(f"OR (a>4 | b>4): {or_result}")
print(f"NOT (a>3): {not_result}")
# Truth value testing
all_positive = cp.all(a > 0) # True
any_greater_than_4 = cp.any(a > 4) # True
all_equal = cp.all(cp.equal(a, b)) # False
print(f"All positive: {all_positive}")
print(f"Any > 4: {any_greater_than_4}")
print(f"All equal: {all_equal}")import cupy as cp
# Create array with special values
data = cp.array([1.0, 2.0, cp.inf, cp.nan, -cp.inf, 0.0])
# Test for special values
finite_mask = cp.isfinite(data) # [True, True, False, False, False, True]
inf_mask = cp.isinf(data) # [False, False, True, False, True, False]
nan_mask = cp.isnan(data) # [False, False, False, True, False, False]
posinf_mask = cp.isposinf(data) # [False, False, True, False, False, False]
neginf_mask = cp.isneginf(data) # [False, False, False, False, True, False]
print(f"Original data: {data}")
print(f"Finite values: {finite_mask}")
print(f"Infinite values: {inf_mask}")
print(f"NaN values: {nan_mask}")
# Clean data by removing non-finite values
clean_data = data[cp.isfinite(data)]
print(f"Clean data: {clean_data}")
# Complex number testing
complex_array = cp.array([1+2j, 3+0j, 4-1j, 5])
real_mask = cp.isreal(complex_array) # [False, True, False, True]
complex_mask = cp.iscomplex(complex_array) # [True, False, True, False]
print(f"Complex array: {complex_array}")
print(f"Real elements: {real_mask}")
print(f"Complex elements: {complex_mask}")import cupy as cp
# Set operations example
set1 = cp.array([1, 2, 3, 4, 5])
set2 = cp.array([3, 4, 5, 6, 7])
test_values = cp.array([1, 3, 6, 9])
# Membership testing
in_set1 = cp.isin(test_values, set1) # [True, True, False, False]
in_set2 = cp.isin(test_values, set2) # [False, True, True, False]
not_in_set1 = cp.isin(test_values, set1, invert=True) # [False, False, True, True]
print(f"Test values: {test_values}")
print(f"In set1: {in_set1}")
print(f"In set2: {in_set2}")
print(f"Not in set1: {not_in_set1}")
# Set operations
intersection = cp.intersect1d(set1, set2) # [3, 4, 5]
union = cp.union1d(set1, set2) # [1, 2, 3, 4, 5, 6, 7]
difference = cp.setdiff1d(set1, set2) # [1, 2]
symmetric_diff = cp.setxor1d(set1, set2) # [1, 2, 6, 7]
print(f"Set1: {set1}")
print(f"Set2: {set2}")
print(f"Intersection: {intersection}")
print(f"Union: {union}")
print(f"Difference (set1 - set2): {difference}")
print(f"Symmetric difference: {symmetric_diff}")import cupy as cp
# Create sample data
temperature = cp.array([15.2, 22.1, 28.7, 31.4, 19.8, 25.3, 33.1, 18.9])
humidity = cp.array([65, 70, 45, 40, 80, 55, 35, 75])
pressure = cp.array([1013, 1018, 1020, 1015, 1010, 1022, 1025, 1012])
# Define comfort conditions
temp_comfort = cp.logical_and(temperature >= 20, temperature <= 30)
humidity_comfort = cp.logical_and(humidity >= 40, humidity <= 70)
pressure_comfort = cp.logical_and(pressure >= 1010, pressure <= 1025)
# Overall comfort condition
comfortable = cp.logical_and(cp.logical_and(temp_comfort, humidity_comfort),
pressure_comfort)
print(f"Temperature comfortable: {temp_comfort}")
print(f"Humidity comfortable: {humidity_comfort}")
print(f"Pressure comfortable: {pressure_comfort}")
print(f"Overall comfortable: {comfortable}")
# Count and extract comfortable conditions
comfort_count = cp.sum(comfortable)
comfort_indices = cp.where(comfortable)[0]
print(f"Number of comfortable readings: {comfort_count}")
print(f"Comfortable indices: {comfort_indices}")
# Statistical analysis of comfortable vs uncomfortable
if comfort_count > 0:
avg_temp_comfort = cp.mean(temperature[comfortable])
avg_temp_uncomfort = cp.mean(temperature[~comfortable])
print(f"Average temperature when comfortable: {avg_temp_comfort:.1f}°C")
print(f"Average temperature when uncomfortable: {avg_temp_uncomfort:.1f}°C")import cupy as cp
# Multi-dimensional logical operations
data = cp.random.randn(100, 50)
threshold = 2.0
# Find outliers (values beyond threshold standard deviations)
mean_vals = cp.mean(data, axis=0)
std_vals = cp.std(data, axis=0)
z_scores = cp.abs((data - mean_vals) / std_vals)
outliers = z_scores > threshold
# Count outliers per column
outlier_counts = cp.sum(outliers, axis=0)
columns_with_outliers = cp.any(outliers, axis=0)
print(f"Data shape: {data.shape}")
print(f"Columns with outliers: {cp.sum(columns_with_outliers)}")
print(f"Total outliers: {cp.sum(outliers)}")
print(f"Max outliers in single column: {cp.max(outlier_counts)}")
# Complex condition with multiple criteria
condition1 = data > 1.0 # Positive values above 1
condition2 = z_scores < 1.5 # Not too extreme
condition3 = cp.any(data > 0, axis=1, keepdims=True) # Row has positive values
# Combine conditions
complex_mask = cp.logical_and(cp.logical_and(condition1, condition2), condition3)
# Apply mask and compute statistics
filtered_data = cp.where(complex_mask, data, cp.nan)
valid_count = cp.sum(~cp.isnan(filtered_data), axis=0)
print(f"Valid values per column (min/max): {cp.min(valid_count)}/{cp.max(valid_count)}")
# Boolean indexing for advanced selection
high_variance_cols = std_vals > cp.median(std_vals)
stable_data = data[:, high_variance_cols]
stable_outliers = outliers[:, high_variance_cols]
print(f"High variance columns: {cp.sum(high_variance_cols)}")
print(f"Outliers in high variance data: {cp.sum(stable_outliers)}")import cupy as cp
# Bitwise operations on integers
a = cp.array([5, 3, 8, 1], dtype=cp.uint8) # Binary: [101, 011, 1000, 001]
b = cp.array([3, 5, 2, 7], dtype=cp.uint8) # Binary: [011, 101, 010, 111]
# Bitwise operations
and_result = cp.bitwise_and(a, b) # [1, 1, 0, 1]
or_result = cp.bitwise_or(a, b) # [7, 7, 10, 7]
xor_result = cp.bitwise_xor(a, b) # [6, 6, 10, 6]
not_a = cp.bitwise_not(a) # [250, 252, 247, 254] (8-bit complement)
print(f"a: {a} (binary: {[bin(x) for x in cp.asnumpy(a)]})")
print(f"b: {b} (binary: {[bin(x) for x in cp.asnumpy(b)]})")
print(f"a & b: {and_result}")
print(f"a | b: {or_result}")
print(f"a ^ b: {xor_result}")
print(f"~a: {not_a}")
# Bit shifting
left_shift = cp.left_shift(a, 2) # Multiply by 4
right_shift = cp.right_shift(a, 1) # Divide by 2
print(f"a << 2: {left_shift}")
print(f"a >> 1: {right_shift}")
# Bit packing/unpacking
binary_data = cp.array([0, 1, 1, 0, 1, 0, 0, 1], dtype=cp.uint8)
packed = cp.packbits(binary_data)
unpacked = cp.unpackbits(packed)
print(f"Binary data: {binary_data}")
print(f"Packed: {packed} (binary: {bin(cp.asnumpy(packed)[0])})")
print(f"Unpacked: {unpacked}")
# Using bitwise operations for flags
FLAG_READ = 1 # 001
FLAG_WRITE = 2 # 010
FLAG_EXECUTE = 4 # 100
permissions = cp.array([1, 3, 5, 7, 0, 6]) # Various permission combinations
# Test for specific permissions
can_read = cp.bitwise_and(permissions, FLAG_READ) != 0
can_write = cp.bitwise_and(permissions, FLAG_WRITE) != 0
can_execute = cp.bitwise_and(permissions, FLAG_EXECUTE) != 0
print(f"Permissions: {permissions}")
print(f"Can read: {can_read}")
print(f"Can write: {can_write}")
print(f"Can execute: {can_execute}")
# Set/unset permissions
new_permissions = cp.bitwise_or(permissions, FLAG_WRITE) # Add write
removed_execute = cp.bitwise_and(permissions, ~FLAG_EXECUTE) # Remove execute
print(f"With write permission: {new_permissions}")
print(f"Without execute permission: {removed_execute}")import cupy as cp
import time
# Performance comparison of logical operations
n = 10_000_000
a = cp.random.rand(n) > 0.5 # Random boolean array
b = cp.random.rand(n) > 0.3 # Random boolean array
# Time logical operations
operations = [
('logical_and', lambda: cp.logical_and(a, b)),
('logical_or', lambda: cp.logical_or(a, b)),
('logical_not', lambda: cp.logical_not(a)),
('bitwise_and', lambda: cp.bitwise_and(a, b)),
('element_and', lambda: a & b),
('element_or', lambda: a | b),
]
print(f"Performance comparison on {n:,} elements:")
for name, operation in operations:
start_time = time.perf_counter()
result = operation()
cp.cuda.Stream.null.synchronize() # Ensure GPU completion
end_time = time.perf_counter()
print(f"{name:12}: {(end_time - start_time)*1000:.2f} ms")
# Validation of array equality methods
arr1 = cp.array([1.0, 2.0, 3.0, cp.nan])
arr2 = cp.array([1.0, 2.0, 3.0, cp.nan])
arr3 = cp.array([1.0, 2.0, 3.1, cp.nan])
print(f"\nArray equality validation:")
print(f"arr1 == arr2 (element-wise): {cp.equal(arr1, arr2)}")
print(f"array_equal(arr1, arr2): {cp.array_equal(arr1, arr2)}")
print(f"array_equal(arr1, arr2, equal_nan=True): {cp.array_equal(arr1, arr2, equal_nan=True)}")
print(f"allclose(arr1, arr3, atol=0.1): {cp.allclose(arr1, arr3, atol=0.1)}")
print(f"allclose(arr1, arr3, atol=0.05): {cp.allclose(arr1, arr3, atol=0.05)}")Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda114