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

Complex Numbers

Tags: None
(comma "," separated)
MinkowskiSpace
Registered Member
Posts
2
Karma
0
OS

Complex Numbers

Tue Jun 21, 2011 10:42 pm
Hello,
(DISCLAIMER: novice programmer)
I am currently writing a program in C++ using Eigen 3 to diagonalize matrices whose entries are complex doubles. In my code, I have a method called "H_matrix" that returns a complex<double> type for each specified entry of the matrix. In my main method, I declare the dynamic-size matrix with complex double entries: MatrixXcd H; . However, when I fill H with values generated from H_matrix and try to spit out these values in the terminal with cout, Eigen gives me the error:


DenseCoeffsBase.h:350: typename Eigen::internal::traits<T>::Scalar& Eigen::DenseCoeffsBase<Derived, 1>::operator()(typename Eigen::internal::traits<T>::Index, typename Eigen::internal::traits<T>::Index) [with Derived = Eigen::Matrix<std::complex<double>, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]: Assertion `row >= 0 && row < rows() && col >= 0 && col < cols()' failed.
Aborted

Oddly enough, if I fill H with random complex double entries with:
MatrixXcd H = MatrixXcd::Random(N,N), where N is some integer, I experience no problem in outputting the matrix. Also, I can use a nested for loop to fill H with values generated from H_matrix only if I fill in the real and imaginary parts separately:

for(int i = 0; i < M; i++)
{
for(int j = 0; j < M; j++)
{

real(H(i,j)) = real(H_matrix(i,j));
imag(H(i,j)) = imag(H_matrix(i,j));
}
}

How can I go about filling H with complex double entries without having to fill in the real and imaginary parts separately?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Complex Numbers

Wed Jun 22, 2011 9:07 am
Note that:

real(H(i,j)) = real(H_matrix(i,j));

is not compatible with c++ standard and only works with GCC. Here is the correct version:



H.resize(M,M);

for()
for()
H(i,j) = H_matrix(i,j);


And without for loops:

H = MatrixXcd::NullaryExpr(M, M, std::ptr_fun(H_matrix));
jitseniesen
Registered Member
Posts
204
Karma
2

Re: Complex Numbers

Wed Jun 22, 2011 9:23 am
It may be worth emphasizing that if you declare a matrix H with "MatrixXcd H;" then the size of H is not specified (in fact; Eigen will set the size to zero-by-zero). Thus, you cannot fill the matrix H at that time. This may be the cause of the error you showed us.

If you fill H with random complex double entries with: "MatrixXcd H = MatrixXcd::Random(N,N)", where N is some integer, then the size of H is specified and you can fill the entries of H with a for loop. Other methods for specifying the size of H which do not involve generating random numbers is to declare H as "MatrixXcd H(N,N);" or to call the resize method, as Gael indicated: "H.resize(N,N);" where H is declared as a MatrixXcd before.
MinkowskiSpace
Registered Member
Posts
2
Karma
0
OS

Re: Complex Numbers

Wed Jun 22, 2011 6:29 pm
Thank you all for your responses. Everything is running smoothly.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar