@@ -5225,14 +5225,18 @@ def __int__(self):
5225
5225
def __float__ (self ):
5226
5226
return self .data .__float__ ()
5227
5227
5228
- def equals (self , other , nan_equals = False ):
5228
+ def equals (self , other , rtol = 0 , atol = 0 , nan_equals = False ):
5229
5229
"""
5230
5230
Compares self with another array and returns True if they have the same axes and elements, False otherwise.
5231
5231
5232
5232
Parameters
5233
5233
----------
5234
5234
other: LArray-like
5235
5235
Input array. aslarray() is used on a non-LArray input.
5236
+ rtol : float or int, optional
5237
+ The relative tolerance parameter (see Notes). Defaults to 0.
5238
+ atol : float or int, optional
5239
+ The absolute tolerance parameter (see Notes). Defaults to 0.
5236
5240
nan_equals: boolean, optional
5237
5241
Whether or not to consider nan values at the same positions in the two arrays as equal.
5238
5242
By default, an array containing nan values is never equal to another array, even if that other array
@@ -5244,6 +5248,15 @@ def equals(self, other, nan_equals=False):
5244
5248
bool
5245
5249
Returns True if self is equal to other.
5246
5250
5251
+ Notes
5252
+ -----
5253
+ For finite values, equals uses the following equation to test whether two values are equal:
5254
+
5255
+ absolute(array1 - array2) <= (atol + rtol * absolute(array2))
5256
+
5257
+ The above equation is not symmetric in array1 and array2, so that equals(array1, array2) might be different
5258
+ from equals(array2, array1) in some rare cases.
5259
+
5247
5260
Examples
5248
5261
--------
5249
5262
>>> arr1 = ndtest((2, 3))
@@ -5261,6 +5274,24 @@ def equals(self, other, nan_equals=False):
5261
5274
>>> arr1.equals(arr3)
5262
5275
False
5263
5276
5277
+ Test equality between two arrays within a given tolerance range.
5278
+ Return True if absolute(array1 - array2) <= (atol + rtol * absolute(array2)).
5279
+
5280
+ >>> arr1 = LArray([6., 8.], "a=a0,a1")
5281
+ >>> arr1
5282
+ a a0 a1
5283
+ 6.0 8.0
5284
+ >>> arr2 = LArray([5.999, 8.001], "a=a0,a1")
5285
+ >>> arr2
5286
+ a a0 a1
5287
+ 5.999 8.001
5288
+ >>> arr1.equals(arr2)
5289
+ False
5290
+ >>> arr1.equals(arr2, atol=0.01)
5291
+ True
5292
+ >>> arr1.equals(arr2, rtol=0.01)
5293
+ True
5294
+
5264
5295
Arrays with nan values
5265
5296
5266
5297
>>> arr1 = ndtest((2, 3), dtype=float)
@@ -5283,10 +5314,13 @@ def equals(self, other, nan_equals=False):
5283
5314
other = aslarray (other )
5284
5315
except Exception :
5285
5316
return False
5286
- if nan_equals :
5287
- return self .axes == other .axes and all (nan_equal (self , other ))
5317
+ if rtol == 0 and atol == 0 :
5318
+ if nan_equals :
5319
+ return self .axes == other .axes and all (nan_equal (self , other ))
5320
+ else :
5321
+ return self .axes == other .axes and np .array_equal (np .asarray (self ), np .asarray (other ))
5288
5322
else :
5289
- return self .axes == other .axes and np .array_equal (np .asarray (self ), np .asarray (other ))
5323
+ return self .axes == other .axes and np .allclose (np .asarray (self ), np .asarray (other ), rtol , atol , nan_equals )
5290
5324
5291
5325
def divnot0 (self , other ):
5292
5326
"""Divides array by other, but returns 0.0 where other is 0.
0 commit comments