@@ -5081,23 +5081,27 @@ def __float__(self):
5081
5081
return self .data .__float__ ()
5082
5082
5083
5083
@deprecate_kwarg ('nan_equals' , 'nans_equal' )
5084
- def equals (self , other , rtol = 0 , atol = 0 , nans_equal = False ):
5085
- """
5084
+ def equals (self , other , rtol = 0 , atol = 0 , nans_equal = False , check_axes = False ):
5085
+ r """
5086
5086
Compares self with another array and returns True if they have the same axes and elements, False otherwise.
5087
5087
5088
5088
Parameters
5089
5089
----------
5090
- other: LArray-like
5090
+ other : LArray-like
5091
5091
Input array. aslarray() is used on a non-LArray input.
5092
5092
rtol : float or int, optional
5093
5093
The relative tolerance parameter (see Notes). Defaults to 0.
5094
5094
atol : float or int, optional
5095
5095
The absolute tolerance parameter (see Notes). Defaults to 0.
5096
- nans_equal: boolean, optional
5096
+ nans_equal : boolean, optional
5097
5097
Whether or not to consider nan values at the same positions in the two arrays as equal.
5098
5098
By default, an array containing nan values is never equal to another array, even if that other array
5099
5099
also contains nan values at the same positions. The reason is that a nan value is different from
5100
5100
*anything*, including itself. Defaults to False.
5101
+ check_axes : boolean, optional
5102
+ Whether or not to check that the set of axes and their order is the same on both sides. Defaults to False.
5103
+ If False, two arrays with compatible axes (and the same data) will compare equal, even if some axis is
5104
+ missing on either side or if the axes are in a different order.
5101
5105
5102
5106
Returns
5103
5107
-------
@@ -5121,17 +5125,17 @@ def equals(self, other, rtol=0, atol=0, nans_equal=False):
5121
5125
--------
5122
5126
>>> arr1 = ndtest((2, 3))
5123
5127
>>> arr1
5124
- a\\ b b0 b1 b2
5128
+ a\b b0 b1 b2
5125
5129
a0 0 1 2
5126
5130
a1 3 4 5
5127
5131
>>> arr2 = arr1.copy()
5128
- >>> arr1 .equals(arr2 )
5132
+ >>> arr2 .equals(arr1 )
5129
5133
True
5130
5134
>>> arr2['b1'] += 1
5131
- >>> arr1 .equals(arr2 )
5135
+ >>> arr2 .equals(arr1 )
5132
5136
False
5133
5137
>>> arr3 = arr1.set_labels('a', ['x0', 'x1'])
5134
- >>> arr1 .equals(arr3 )
5138
+ >>> arr3 .equals(arr1 )
5135
5139
False
5136
5140
5137
5141
Test equality between two arrays within a given tolerance range.
@@ -5145,36 +5149,68 @@ def equals(self, other, rtol=0, atol=0, nans_equal=False):
5145
5149
>>> arr2
5146
5150
a a0 a1
5147
5151
5.999 8.001
5148
- >>> arr1 .equals(arr2 )
5152
+ >>> arr2 .equals(arr1 )
5149
5153
False
5150
- >>> arr1 .equals(arr2 , atol=0.01)
5154
+ >>> arr2 .equals(arr1 , atol=0.01)
5151
5155
True
5152
- >>> arr1 .equals(arr2 , rtol=0.01)
5156
+ >>> arr2 .equals(arr1 , rtol=0.01)
5153
5157
True
5154
5158
5155
5159
Arrays with nan values
5156
5160
5157
5161
>>> arr1 = ndtest((2, 3), dtype=float)
5158
5162
>>> arr1['a1', 'b1'] = nan
5159
5163
>>> arr1
5160
- a\\ b b0 b1 b2
5164
+ a\b b0 b1 b2
5161
5165
a0 0.0 1.0 2.0
5162
5166
a1 3.0 nan 5.0
5163
5167
>>> arr2 = arr1.copy()
5164
5168
>>> # By default, an array containing nan values is never equal to another array,
5165
5169
>>> # even if that other array also contains nan values at the same positions.
5166
5170
>>> # The reason is that a nan value is different from *anything*, including itself.
5167
- >>> arr1 .equals(arr2 )
5171
+ >>> arr2 .equals(arr1 )
5168
5172
False
5169
5173
>>> # set flag nans_equal to True to overwrite this behavior
5170
- >>> arr1.equals(arr2, nans_equal=True)
5174
+ >>> arr2.equals(arr1, nans_equal=True)
5175
+ True
5176
+
5177
+ Arrays with the same data but different axes
5178
+
5179
+ >>> arr1 = ndtest((2, 2))
5180
+ >>> arr1
5181
+ a\b b0 b1
5182
+ a0 0 1
5183
+ a1 2 3
5184
+ >>> arr2 = arr1.transpose()
5185
+ >>> arr2
5186
+ b\a a0 a1
5187
+ b0 0 2
5188
+ b1 1 3
5189
+ >>> arr2.equals(arr1)
5190
+ True
5191
+ >>> arr2.equals(arr1, check_axes=True)
5192
+ False
5193
+ >>> arr2 = arr1.expand('c=c0,c1')
5194
+ >>> arr2
5195
+ a b\c c0 c1
5196
+ a0 b0 0 0
5197
+ a0 b1 1 1
5198
+ a1 b0 2 2
5199
+ a1 b1 3 3
5200
+ >>> arr2.equals(arr1)
5171
5201
True
5202
+ >>> arr2.equals(arr1, check_axes=True)
5203
+ False
5172
5204
"""
5173
5205
try :
5174
5206
other = aslarray (other )
5175
5207
except Exception :
5176
5208
return False
5177
- return self .axes == other .axes and all (self .eq (other , rtol = rtol , atol = atol , nans_equal = nans_equal ))
5209
+ try :
5210
+ axes_equal = self .axes == other .axes if check_axes else True
5211
+ return axes_equal and all (self .eq (other , rtol = rtol , atol = atol , nans_equal = nans_equal ))
5212
+ except ValueError :
5213
+ return False
5178
5214
5179
5215
@deprecate_kwarg ('nan_equals' , 'nans_equal' )
5180
5216
def eq (self , other , rtol = 0 , atol = 0 , nans_equal = False ):
@@ -5183,13 +5219,13 @@ def eq(self, other, rtol=0, atol=0, nans_equal=False):
5183
5219
5184
5220
Parameters
5185
5221
----------
5186
- other: LArray-like
5222
+ other : LArray-like
5187
5223
Input array. aslarray() is used on a non-LArray input.
5188
5224
rtol : float or int, optional
5189
5225
The relative tolerance parameter (see Notes). Defaults to 0.
5190
5226
atol : float or int, optional
5191
5227
The absolute tolerance parameter (see Notes). Defaults to 0.
5192
- nans_equal: boolean, optional
5228
+ nans_equal : boolean, optional
5193
5229
Whether or not to consider nan values at the same positions in the two arrays as equal.
5194
5230
By default, an array containing nan values is never equal to another array, even if that other array
5195
5231
also contains nan values at the same positions. The reason is that a nan value is different from
0 commit comments