Complex Tensor in C++ API?

username: kaihsinwu
cooked: <p>Hi everyone,</p>
<p>I am currently trying to integrate PyTorch into my project on c++ side. I notice that It seems there is no complex type similarity of k (ex: kfloat…), but the pytorch implementation does have complex…</p>
<p>I am wondering if it is possible to initialize a complex type tensor/scalar/storage?</p>

username: ptrblck
cooked: <p>The complex type is implemented as <code>kComplexHalf</code>, <code>kComplexFloat</code>, and <code>kComplexDouble</code> as seen <a href="https://github.com/pytorch/pytorch/blob/c371542efc31b1abfe6f388042aa3ab0cef935f2/test/cpp/api/tensor.cpp#L174-L192">here</a>.</p>

username: BruceDai003
cooked: <aside class="quote no-group" data-username="kaihsinwu" data-post="1" data-topic="117148">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kaihsinwu/48/36742_2.png" class="avatar"> kaihsinwu:</div>
<blockquote>
<p>integrate PyTorch into my project on c++</p>
</blockquote>
</aside>
<p>Thanks, I was wondering how to create an Scalar with complex64 dtype in C++, can you give me an example, e.g.</p>
<pre><code class="lang-auto">a = 3+5j
</code></pre>
<p>Somthing like this?</p>

username: ptrblck
cooked: <p>This should work:</p>
<pre><code class="lang-python">tensor = at::tensor(c10::complex&lt;float&gt;(1.0, 2.0), at::dtype(at::kComplexFloat))
</code></pre>

username: BruceDai003
cooked: <p>Thanks, I only want to get the Scalar, not Scalar to tensor. So basically just do this?</p>
<pre><code class="lang-auto">auto base = c10::Scalar(c10::complex&lt;double&gt;(1.0, 2.0));
</code></pre>
<p>Is this equivilent to the python code of:</p>
<pre><code class="lang-auto">base = 1.0 + 2.0j;
</code></pre>
<p>Since the default dtype for scalar is double not float.</p>
<p>Another question is that, when I try to print out the complex tensor using the <code>std::cout</code> method, the imaginary part didn’t get printed out.<br>
Here is a simple example:</p>
<pre><code class="lang-auto">auto a = at::tensor({c10::complex&lt;double&gt;(1, 0.5), c10::complex&lt;double&gt;(1, 0.5)}, at::dtype(at::kComplexFloat));

    std::cout &lt;&lt; a &lt;&lt; std::endl;
</code></pre>
<p>What I got:</p>
<pre><code class="lang-auto">[W Copy.cpp:219] Warning: Casting complex values to real discards the imaginary part (function operator())
 1
 1
</code></pre>
<p>So, how can I print out the value of a complex tensor including the imaginary part?<br>
Much appreciated.</p>

username: BruceDai003
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Can you help me with this? Thanks.</p>

username: ptrblck
cooked: <p>These approaches work for me:</p>
<pre><code class="lang-auto">#include &lt;iostream&gt;
#include &lt;ATen/ATen.h&gt;

using std::cout;
using namespace at;

int main() {
  auto format = [] (Scalar a) {
    std::ostringstream str;
    str &lt;&lt; a;
    return str.str();
  };

  std::cout &lt;&lt; format(Scalar(c10::complex&lt;double&gt;(2.0, 3.1))) &lt;&lt; std::endl;
  std::cout &lt;&lt; Scalar(c10::complex&lt;double&gt;(3.2, 4.4)) &lt;&lt; std::endl;
  std::cout &lt;&lt; c10::complex&lt;float&gt;(4.5, 5.6) &lt;&lt; std::endl;

  return 0;
}
</code></pre>
<p>Build:</p>
<pre><code class="lang-auto">./build# cmake -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'` .. &amp;&amp; cmake --build . --config Release
</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">./build# ./example-app 
(2,3.1)
(3.2,4.4)
(4.5,5.6)
</code></pre>

username: anxingle820
cooked: <p>I find your example show  how to create complex number( or scalar), but <a class="mention" href="/u/brucedai003">@BruceDai003</a>  want to create a complex tensor (and print it ).</p>