Strange error with gsl_matrix_complex type

Dear community,

when using the following code:

#include <gsl/gsl_linalg.h>
#include <iostream>
#include <gsl/gsl_complex.h>

using namespace std;

int main()
{

int rows = 4;
int cols = 5;


gsl_matrix *mm = gsl_matrix_alloc(rows,cols);

for (int ii=0;ii<rows;ii++) {
for (int jj=0;jj<cols;jj++) {
gsl_matrix_set(mm,ii,jj,ii+jj);
}
}

for (int ii=0;ii<rows;ii++) {
cout << endl;
for (int jj=0;jj<cols;jj++) {
cout << gsl_matrix_get(mm,ii,jj) << " ";
}
}

cout << endl;
gsl_matrix_free (mm);

/************************************/
gsl_matrix_complex *m = gsl_matrix_complex_alloc(rows,cols);
gsl_complex a;

for (int ii=0;ii<rows;ii++) {
for (int jj=0;jj<cols;jj++) {
GSL_SET_COMPLEX(&a,ii,jj);
gsl_matrix_complex_set(m,ii,jj,a);
}
}

for (int ii=0;ii<rows;ii++) {
cout << endl;
for (int jj=0;jj<cols;jj++) {
cout << GSL_REAL(gsl_matrix_complex_get(m,ii,jj)) << " ";
//printf("%f",GSL_REAL(gsl_matrix_complex_get(m,ii,jj)));
}
}

return 0;
}

my program crashes after the following output:

0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3
Process returned -1073741819 (0xC0000005) execution time : 1.845 s

Can anyone help me with this? Sorry for the bad formatting, but somehow the buttons didn't work.
Last edited on
Hi

I don't know the gsl library, but it seems to me than you're not initializing properly the complex numbers before adding into the array ... :


Complex numbers are represented using the type gsl_complex. The internal representation of this type may vary across platforms and should not be accessed directly. The functions and macros described below allow complex numbers to be manipulated in a portable way.

For reference, the default form of the gsl_complex type is given by the following struct,

typedef struct
{
double dat[2];
} gsl_complex;
Registered users can post here. Sign in or register to post.