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

Complex matrices documentation

Tags: None
(comma "," separated)
linello
Registered Member
Posts
56
Karma
0
OS

Complex matrices documentation

Tue Aug 16, 2011 8:27 pm
Hi think the documentation lacks of information on usage of matrices defined with std::complex scalar type.

For example, how can I extract the imaginary part and save it on a new matrix starting from

Code: Select all
Matrix< complex<float>,3,3 > A=Matrix< complex<float>,3,3 >::Random();


without explicitly looping over the matrix with for loops?

Code: Select all
Matrix< float,3,3 > I;
for (int i=0; i<9; i++)
I(i)= std::imag(A(i));
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
A.imag()
A.real()
Akkawe
Registered Member
Posts
35
Karma
0
OS

Re: Complex matrices documentation

Thu Jan 26, 2012 4:34 pm
if I had two sparse matrix A and B, could I obtain a third matrix C as:
complex<double> C(A,B) ?
If guess that's not working well.
Any idea?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Complex matrices documentation

Thu Jan 26, 2012 4:55 pm
In two steps:

MatrixXc C(m,n);
c.real() = A;
c.imag() = B;
Akkawe
Registered Member
Posts
35
Karma
0
OS

Re: Complex matrices documentation

Wed Mar 28, 2012 11:41 am
ggael wrote:In two steps:

MatrixXc C(m,n);
c.real() = A;
c.imag() = B;


The same procedure doesn't work if C is a sparse matrix.
I have:
Code: Select all
SparseMatrix<double> A(dim,dim);
SparseMatrix<double> B(dim,dim);
SparseMatrix<complex<double> > C(dim,dim);
C.real()=A;
C.imag()=B;

Why?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Complex matrices documentation

Thu Mar 29, 2012 9:55 pm
Indeed with a sparse representation such operations are not possible in general, thus not allowed. A workaround:

C = A.cast<complex<double> >() + B.cast<complex<double> >();

If A and B have the same sparcity pattern, this should be pretty fast (I hope the compiler will be able to remove the additions with 0).


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]