[Numpy-discussion] Comparing arrays with dtypes with field names
Travis Oliphant
oliphant.travis at ieee.org
Thu Jul 6 03:20:46 CDT 2006
Albert Strasheim wrote:
> Hello all
>
> I noticed that arrays with dtypes with field names don't compare correctly
> when using the equality operator.
>
> Example:
>
> In [17]: dt = N.dtype({'names' : ['f1', 'f2'],
> 'formats' : [N.intc, N.intc]})
>
> In [18]: x = N.array([(0,0)], dtype=dt)
>
> In [19]: y = N.array([(0,0)], dtype=dt)
>
> In [20]: x['f1']==y['f1']
> Out[20]: array([True], dtype=bool)
>
> In [21]: x['f2']==y['f2']
> Out[21]: array([True], dtype=bool)
>
> In [22]: x==y
> Out[22]: False
>
> The question arises as to what should be returned when comparing these
> arrays. One option would be an array with dtype=bool which would indicate
> that each item in the array is equal in f1 and f2.
Currently, in SVN, record-arrays now can be compared (== or != only).
The comparison returns a single bool array:
For equality the comparison is basically:
res = True
for name in x.dtype.fields[-1]:
res = logical_and(res, x[name] == y[name])
while for not-equal comparisons it is:
res = False
for name in x.dtype.fields[-1]:
res = logical_or(res, x[name] != y[name])
> Another option would be
> an array with dtype
>
> N.dtype({'names' : ['f1', 'f2'], 'formats' : [bool,bool]})
>
> I think this second option could be very useful for some applications, where
> you might want to know something like: is the (i,j)th element of this array
> the same in f1 and f2 but different in f3?
>
This is a bit too sophisticated and does not work very well for nested
arrays. The implemented comparison works for nested arrays just fine.
You can just test for what you want directly
(x['f1'] == y['f2']) & (x['f1'] != y['f3'])
to handle the fewer particular cases where the other approach might be
useful.
-Travis
More information about the Numpy-discussion
mailing list