In addition to my comment, math.isclose uses abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) which is commutative, but at the cost of an additional operation (which could be costly for numpy arrays which need to be stacked in order to find the maximum). It isn't hard to implement your own if you really require this property:
def isclose_comm(a, b, rtol=1e-5, atol=1e-8):
ab = np.stack((a, b))
return np.abs(a - b) <= (atol + rtol * ab.max(axis=0))
x = np.random.random(5)
x
Out[94]: array([0.36007049, 0.86934972, 0.05827216, 0.60794612, 0.24539454])
y = x + np.random.random(5)/1e6
y
Out[96]: array([0.36007072, 0.86934976, 0.05827312, 0.6079464 , 0.24539492])
np.isclose(x, y)
Out[97]: array([ True, True, False, True, True])
isclose_comm(x, y)
Out[98]: array([ True, True, True, True, True])
z = np.zeros(5)
isclose_comm(x, z)
Out[100]: array([False, False, False, False, False])
allclose equivelent:
def allclose_comm(a, b, rtol=1e-5, atol=1e-8):
return isclose_comm(a, b, rtol, atol).all()
Answer from FHTMitchell on Stack OverflowRather than using np.isclose, use np.allclose. This will return a single boolean value for that comparison.
From the documentation - isclose "Returns a boolean array where two arrays are element-wise equal within a tolerance."
Thus, there is an array of boolean values returned which represents the is-close-ness of each value of the 4-element array. It's up to you to convert that array to a single boolean value.
You might, for instance, only care if any of the 4 match, in which case you can use any(); alternatively, if you care if all match, you can use all().