MCPcopy Create free account
hub / github.com/DataArcTech/DataArc-SynData-Toolkit / _array_equal

Function _array_equal

verl/protocol.py:124–148  ·  view source on GitHub ↗

Recursively compares two NumPy arrays for strict equality, with special handling for object-dtype arrays, NaN values, and circular references. This function assumes that the two arguments provided are NumPy arrays. Args: array1: The first NumPy array. array2: The se

(array1: np.ndarray, array2: np.ndarray, visited: set[int])

Source from the content-addressed store, hash-verified

122
123
124def _array_equal(array1: np.ndarray, array2: np.ndarray, visited: set[int]) -> bool:
125 """
126 Recursively compares two NumPy arrays for strict equality, with special
127 handling for object-dtype arrays, NaN values, and circular references.
128 This function assumes that the two arguments provided are NumPy arrays.
129
130 Args:
131 array1: The first NumPy array.
132 array2: The second NumPy array.
133
134 Returns:
135 True if the arrays' dtypes, shapes, and all elements are equal.
136 """
137 # Check dtype and shape first, as this is the fastest failure path.
138 if array1.dtype != array2.dtype or array1.shape != array2.shape:
139 return False
140
141 # For non-object dtypes, use NumPy's implementation with equal_nan=True.
142 if array1.dtype != "object":
143 return np.array_equal(array1, array2, equal_nan=True)
144
145 # For object-dtype arrays, we must recursively compare each element.
146 # We delegate to _deep_equal to handle elements, as they could be any
147 # type, including other nested arrays or NaNs.
148 return all(_deep_equal(x, y, visited) for x, y in zip(array1.flat, array2.flat, strict=False))
149
150
151def _deep_equal(a: Any, b: Any, visited: set[int]) -> bool:

Callers 1

_deep_equalFunction · 0.85

Calls 1

_deep_equalFunction · 0.85

Tested by

no test coverage detected