<br><br><div class="gmail_quote">On Thu, Jan 14, 2010 at 3:49 PM, Warren Weckesser <span dir="ltr"><<a href="mailto:warren.weckesser@enthought.com">warren.weckesser@enthought.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">Yaroslav Halchenko wrote:<br>
> Dear NumPy People,<br>
><br>
> First I want to apologize if I misbehaved on NumPy Trac by reopening the<br>
> closed ticket<br>
> <a href="http://projects.scipy.org/numpy/ticket/1362" target="_blank">http://projects.scipy.org/numpy/ticket/1362</a><br>
> but I still feel strongly that there is misunderstanding<br>
> and the bug/defect is valid.   I would appreciate if someone would waste<br>
> more of his time to persuade me that I am wrong but please first read<br>
> till the end:<br>
><br>
> The issue, as originally reported, is demonstrated with:<br>
><br>
> ,---<br>
> | > python -c 'import numpy as N; print N.__version__; a=N.array([1, (0,1)],dtype=object); print a==1; print a == (0,1),  a[1] == (0,1)'<br>
> | 1.5.0.dev<br>
> | [ True False]<br>
> | [False False] True<br>
> `---<br>
><br>
> whenever I expected the last line to be<br>
><br>
> [False True] True<br>
><br>
> charris (thanks for all the efforts to enlighten me) summarized it as<br>
><br>
> """the result was correct given that the tuple (0,1) was converted to an<br>
> object array with elements 0 and 1. It is *not* converted to an array<br>
> containing a tuple. """<br>
><br>
> and I was trying to argue that it is not the case in my example.  It is<br>
> the case in charris's example though whenever both elements are of<br>
> the same length, or there is just a single tuple, i.e.<br>
><br>
><br>
<br>
</div>The "problem" is that the tuple is converted to an array in the<br>
statement that<br>
does the comparison, not in the construction of the array.  Numpy attempts<br>
to convert the right hand side of the == operator into an array.  It<br>
then does<br>
the comparison using the two arrays.<br>
<br>
One way to get what you want is to create your own array and then do<br>
the comparison:<br>
<br>
In [1]: import numpy as np<br>
<br>
In [2]: a = np.array([1, (0,1)], dtype='O')<br>
<br>
In [3]: t = np.empty(1, dtype='O')<br>
<br>
In [4]: t[0] = (0,1)<br>
<br>
In [5]: a == t<br>
Out[5]: array([False,  True], dtype=bool)<br>
<br>
<br>
In the above code, a numpy array 't' of objects with shape (1,) is created,<br>
and the single element is assigned the value (0,1).  Then the comparison<br>
works as expected.<br>
<br>
More food for thought:<br>
<br>
In [6]: b = np.array([1, (0,1), "foo"], dtype='O')<br>
<br>
In [7]: b == 1<br>
Out[7]: array([ True, False, False], dtype=bool)<br>
<br>
In [8]: b == (0,1)<br>
Out[8]: False<br>
<br></blockquote><div><br>Oooh, that last one is strange. Also<br><br><span style="font-family: courier new,monospace;">In [6]: arange(2) == arange(3)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Out[6]: False</span><br>
<br>So the comparison isn't element-wise. I rather think a  shape mismatch error should be raised in this case.<br><br><snip><br><br>Chuck  <br></div><br></div>