How to efficiently extract real / imaginary parts of a complex matrix in the Eigen3 library?

I have complex, dense vectors / matrices in an Eigen3 library, and I want to extract the real and imaginary parts into separate arrays. In matlab, I could do something like

cplxFoo = [1, 1i; -1i -1]
re = real(cplxFoo)
im = imag(cplxFoo)

which gives expectation

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

Is there something like Matlab functions real()and imag()in Eigen3?

Right now, the only thing I know will work, something similar to

MatrixXcd cplxFoo = ...;
MatrixXd re(cplxFoo.rows(), cplxFoo.cols());
MatrixXd im(cplxFoo.rows(), cplxFoo.cols());

for(size_t j=0; j<cplxFoo.cols(); ++j) {
    for(size_t i=0; i<cplxFoo.rows(); ++i) {
        re(i, j) = cplxFoo(i,j).real();
        im(i, j) = cplxFoo(i,j).imag();
    }
}

It works, and I can even put it in a function, but then I was stuck to do my own loop vectorization, expansion, etc., and I need to make an extra copy.

, Map<MatrixXd> cplxFoo, . , MatrixXcd std::complex<double>, , . , std::complex<T> struct {T real; T imag;};, , std::complex<T> ( SO), ++? AFAICT, ++ struct {T imag; T real;}; ( ) - ,

class {
    T radius;
    T angle;

public:
    T real() const { return radius * cos(angle); }
    T imag() const { return radius * sin(angle); }
    /* ... */
};

, Map<MatrixXd> cplxFoo? , ?

, Eigen ?

, , Eigen MATLAB, , - .

+4
1

, .real() .imag():

MatrixXcd M;
MatrixXd r, i;
r = M.real();
i.imag();

, M.real() MatrixXd.

+6

Source: https://habr.com/ru/post/1531141/


All Articles