Re: [eigen] Assignment of Complex Number |

[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]

Hi,
> It might be an easy task for you guys, but I couldn't figure it out by
> myself.
> I am hoping one of you can help me on my problem.
> I define a complex double vector, but I am not able to assign a value to
> both real and imaginary part of an element in that vector.
> Here is an example of what I do.
> Please show me a way to complete this simple task.
>
> *// Fill Complex Vector from data Vectors*
> for (int i = 0; i < 10; i++)
> ComplexVector(i) = (FirstVector(i), SecondVector(i));
The comma (",") operator in C/C++ is tricky: it just evaluates to the
last operhand. So i = (3, 6); will be equivalent to i = 6; To represent
complex numbers in C++, you need the std::complex data type (that is
what Eigen users internally for VectorXcd).
What you want to do is construct a new std::complex (double in this
case) with specific real and imaginary parts:
ComplexVector(i) = std::complex<double>(FirstVector(i), SecondVector(i));
Or, even easier (and probably slightly faster), you could replace the
entire loop by the following two statements, Eigen will take care of the
rest internally:
ComplexVector.real() = FirstVector;
ComplexVector.imag() = SecondVector;
Regards,
Christian