Convert complex to bool

desktoop: <p>When I try to convert a complex data to bool, I found the results are not what I expect.<br>
For example:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; t = torch.tensor([1+1j, 0+1j, 1+0j])
&gt;&gt;&gt; t.to(torch.bool)
</code></pre>
<p>and then I get this:</p>
<pre><code class="lang-auto">&lt;ipython-input-3-666889c1a9fa&gt;:1: UserWarning: Casting complex values to real discards the imaginary part (Triggered internally at  ../aten/src/ATen/native/Copy.cpp:239.)
  a.to(torch.bool)

tensor([ True,  True, False])
</code></pre>
<p>the result I expect is:</p>
<pre><code class="lang-auto">tensor([ True,  True,  True])
</code></pre>
<p>I just wonder why torch discard the imaginary part when converting complex to bool. Is bool type a kind of  real type as the warning said?</p>
<p>As a comparison, here is the result of numpy:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; arr = np.array([1+1j, 0+1j, 1+0j])
&gt;&gt;&gt; np.asarray(arr, bool)

array([ True,  True,  True])
</code></pre>
<p>My python version: ‘3.8.10’<br>
torch version: ‘1.11.0+cu113’<br>
numpy version: ‘1.21.0’</p>

ptrblck: <p>Yes, <code>torch.bool</code> is most likely seen as a “real” <code>dtype</code> as there is no “complex bool” type.<br>
I don’t know the reasoning behind numpy’s interpretation, but could you explain what the transformation of <code>cfloat</code> into <code>bool</code> would represent and why <code>numpy</code>’s approach is the correct one (I’m not claiming PyTorch is right and I would be interested into a proper reasoning)?</p>
<p>Btw. I get <code>tensor([ True, False,  True])</code> which would be the expected output if only the real part is used.</p>

desktoop: <p>Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/laughing.png?v=12" title=":laughing:" class="emoji" alt=":laughing:" loading="lazy" width="20" height="20"></p>
<p>I make this transformation because I want to get adjacent matrix from edge features data. The elements which are non-zero in edge features data indicate there are edges in corresponding positions. There are many methods to get adjacent matrix, I randomly chose this method and found this little problem by accident.</p>
<p>I don’t mean that numpy is correct. I thought pytorch and numpy have similar usage in similar api but it is not.</p>
<pre><code class="lang-auto">&gt;&gt;&gt; t = torch.tensor([1+1j, 0+1j, 1+0j])
&gt;&gt;&gt; t.to(torch.int)
  tensor([1, 0, 1], dtype=torch.int32)
&gt;&gt;&gt; t.to(torch.bool)
  tensor([ True, False,  True])

&gt;&gt;&gt; arr = np.array([1+1j, 0+1j, 1+0j])
&gt;&gt;&gt; arr.astype(int)
  /tmp/ipykernel_3548785/4181001350.py:1: ComplexWarning: Casting complex values to real 
  discards the imaginary part
  array([1, 0, 1])
&gt;&gt;&gt; arr.astype(bool)
  array([ True,  True,  True])
</code></pre>
<p>It seems that torch.bool is indeed more like a real type in pytorch than bool in numpy.</p>

ptrblck: <p>Thanks for the follow up. Yeah, it might make sense to stick to <code>numpy</code> (but I don’t know which conversion makes more sense <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> ). Feel free to create a feature request on <a href="https://github.com/pytorch/pytorch/issues">GitHub</a> so that the code owners could discuss it.</p>