title: Implementing complex-valued matrix equation in OpenCV
body: <p>I am attempting to implement a complex-valued matrix equation in OpenCV. I've prototyped in MATLAB which works fine. Starting off with the equation (exact MATLAB implementation):</p>
  
  <pre><code>kernel = exp(1i .* k .* Circ3D) .* z ./ (1i .* lambda .* Circ3D .* Circ3D)
  </code></pre>
  
  <p>In which</p>
  
  <pre><code>1i = complex number
  k = constant (float)
  Circ3D = real-valued matrix of known size
  lambda = constant (float)
  .* = element-wise multiplication
  ./ = element-wise division
  </code></pre>
  
  <p>The result is a complex-valued matrix. I succeeded in generating the necessary Circ3D matrix as a CV_32F, however the multiplication by the complex number i is giving me trouble. From the OpenCV documentation I understand that a complex matrix is simply a two-channel matrix (CV_32FC2).</p>
  
  <p>The real trouble comes from how to define i. I've tried several options, among which defining i as</p>
  
  <pre><code>cv::Vec2d complex = cv::Vec2d(0,1);
  </code></pre>
  
  <p>and then multiplying by the matrix</p>
  
  <pre><code>kernel = complex * Circ3D
  </code></pre>
  
  <p>But this doesn't work (although I didn't expect it to). I suspect I need to do something with std::complex but I have no idea what (<a href="http://docs.opencv.org/modules/core/doc/basic_structures.html" rel="nofollow">http://docs.opencv.org/modules/core/doc/basic_structures.html</a>).</p>
  
  <p>Thanks in advance for any help.</p>
  
  <p>Edit: Just after writing this post I did make some progress, by defining i as follows:</p>
  
  <pre><code>std::complex&lt;float&gt; complex(0,1)
  </code></pre>
  
  <p>I am then able to assign complex values as follows:</p>
  
  <pre><code>kernel.at&lt;std::complex&lt;float&gt;&gt;(i,j) = cv::exp(complex * k * Circ3D.at&lt;float&gt;(i,j)) * ...
  z / (complex * lambda * pow(Circ3D.at&lt;float&gt;(i,j),2));
  </code></pre>
  
  <p>However, this works in a loop, which makes the procedure incredibly slow. Any way to do it in one go?</p>