Q:

How do I work with complex numbers in Eigen (C++)?

I am successfully working with Eigen and I'm trying to understand a few details with complex numbers.

How do I multiply a Matrix or Vector by a complex constant?  Multiplying to complex matrices is easy; likewise with inner products of complex vectors and matrices.
How do I assign a complex value to a matrix element?  I've tried:

This works fine (Visual Studio)
kx.real()(0, 0) = 1.0;

This throws a compiler error
kz_r.imag()(0, ii) =1.0

The error I get is:

Severity  Code    Description Project File    Line    Suppression State
  Error   C2440   'return': cannot convert from 'double' to 'double &'        \eigen\src\core\mathfunctions.h 919 

A:

Just use std::complex<double> (or float):
std::complex<double> c(1,1);
Eigen::MatrixXd R1; R1.setRandom(2,2);
Eigen::MatrixXcd C1 = c*R1; // multiply complex*real
Eigen::MatrixXcd C2 = c*C1; // complex scalar times complex matrix
C1(0,0) = c; // assign complex value.

