[Numpy-discussion] structured array comparisons?

Matthew Brett matthew.brett@gmail....
Sat Mar 7 13:09:09 CST 2009


Hi,

I'm having some difficulty understanding how these work and would be
grateful for any help.

In the simple case, I get what I expect:

In [42]: a = np.zeros((), dtype=[('f1', 'f8'),('f2', 'f8')])

In [43]: a == a
Out[43]: True

If one of the fields is itself an array, and the other is a scalar,
the shape of the truth value appears to be based on the comparison of
that array, ignoring the scalar:

In [44]: a = np.zeros((), dtype=[('f1', 'f8', 8),('f2', 'f8')])

In [45]: a == a
Out[45]: array([ True,  True,  True,  True,  True,  True,  True,
True], dtype=bool)

If the scalar is different, then the shape is from the array, but the
truth value is from the scalar:

In [46]: b = a.copy()

In [47]: b['f2'] = 3

In [48]: a == b
Out[48]: array([False, False, False, False, False, False, False,
False], dtype=bool)

If there are two arrays, it blows up, even comparing to itself:

In [49]: a = np.zeros((), dtype=[('f1', 'f8', 8),('f2', 'f8', 2)])

In [50]: a == a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/mb312/<ipython console> in <module>()

ValueError: shape mismatch: objects cannot be broadcast to a single shape

Is this all expected by someone?

Thanks a lot,

Matthew


More information about the Numpy-discussion mailing list