title: How to efficiently extract real/imaginary parts of complex matrix in Eigen3 library?
body: <p>I have some complex, dense vectors/matrices in the Eigen3 library, and I want to extract the real and imaginary parts into separate arrays. In Matlab, I could do something like</p>
  
  <pre><code>cplxFoo = [1, 1i; -1i -1]
  re = real(cplxFoo)
  im = imag(cplxFoo)
  </code></pre>
  
  <p>which expectantly yields</p>
  
  <pre><code>cplxFoo =
     1.0000 + 0.0000i   0.0000 + 1.0000i
     0.0000 - 1.0000i  -1.0000 + 0.0000i
  re =
       1     0
       0    -1
  im =
       0     1
      -1     0
  </code></pre>
  
  <p>Is there anything like the <code>real()</code> and <code>imag()</code> Matlab functions in Eigen3?</p>
  
  <p>Right now, the only thing I <em>know</em> will work is something akin to</p>
  
  <pre><code>MatrixXcd cplxFoo = ...;
  MatrixXd re(cplxFoo.rows(), cplxFoo.cols());
  MatrixXd im(cplxFoo.rows(), cplxFoo.cols());
  
  for(size_t j=0; j&lt;cplxFoo.cols(); ++j) {
      for(size_t i=0; i&lt;cplxFoo.rows(); ++i) {
          re(i, j) = cplxFoo(i,j).real();
          im(i, j) = cplxFoo(i,j).imag();
      }
  }
  </code></pre>
  
  <p>It works, and I can put it in a function even, but then I'm stuck having to do my own loop vectorizing, unrolling, etc., and I have to make an extra copy.</p>
  
  <p>What I would like to be able to do is wrap a couple of <code>Map&lt;MatrixXd&gt;</code> with appropriate strides around <code>cplxFoo</code> to get the real and imaginary parts. But the problem is that the elements of <code>MatrixXcd</code> are <code>std::complex&lt;double&gt;</code>, and I'm not sure what the layout of that is. My <em>guess</em> is that <code>std::complex&lt;T&gt;</code> is essentially laid out like <code>struct {T real; T imag;};</code> so that real and imaginary parts are tightly packed and interleaved when you make an array of <code>std::complex&lt;T&gt;</code> (and that also seems to be the consensus at <a href="https://stackoverflow.com/questions/5317777/is-stdcomplex-stored-in-an-interleaved-fashion">this SO question</a>), but is that guaranteed by the C++ standard? AFAICT, a compliant C++ compiler could lay it out like <code>struct {T imag; T real;};</code> (note the changed order), or something more exotic like</p>
  
  <pre><code>class {
      T radius;
      T angle;
  
  public:
      T real() const { return radius * cos(angle); }
      T imag() const { return radius * sin(angle); }
      /* ... */
  };
  </code></pre>
  
  <p>So, is it ok to wrap a couple of <code>Map&lt;MatrixXd&gt;</code> with appropriate strides around <code>cplxFoo</code>? If so, how do I set up the strides correctly?</p>
  
  <p>Alternatively, is there any way to get Eigen's complex data types to use separate chunks of memory for the real and imaginary parts?</p>
  
  <p>For what it's worth, the reason I need to do this is because I need to interface the Eigen library with MATLAB which can only handle separate arrays for real and imaginary parts, not interleaved in any way.</p>