This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Mix complex matrix and real matrix

Tags: None
(comma "," separated)
xding
Registered Member
Posts
3
Karma
0

Mix complex matrix and real matrix

Sat Apr 02, 2016 1:37 pm
Hi,

I want to add a real matrix to a complex matrix and store the result in another complex matrix. It seems that Eigen does not support such operation.
The following code failed at compilation stage. The reason I want to use such feature is that, in a template function, one input matrix can be real or
complex, and I want a uniform code to deal with it. Why doesn't Eigen support such operation ?
Code: Select all
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;

int main(){

    MatrixXd A(2,2);
    MatrixXcd B(2,2);
    A << -1, -2, -3, -4;
    B.real() << 1, 2, 3, 4;
    B.imag() << 5, 6, 7, 8;
   
    MatrixXcd C(2, 2);
    C = B + A;

    return 0;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Agree: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=279

A workaround is to explicitly cast:

template<typename A, typename B> ...
A res, a;
B b;
res = a + b.cast<typename A::Scalar>();

which is a no-op if A::Scalar == B::Scalar.


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient