update:
forgot to mention that for the loop 1-51 are ok to run but any number larger. will not....
The error is
gsl: ../gsl/gsl_matrix_double.h:275: ERROR: first index out of range
Default GSL error handler invoked.
Aborted
The structure of my program is as following. When I draw sample once, the program work fine. when I add the for() to run it 10000 times, there is the error I mentioned above.
void drawSample(cov,z,x,p){...}
int main(){
...for(int i=0;i<10000;i++){//error occures when I have the circle
drawSample(cov,z,x,p);
}
}
I suppose there is something wrong happened in the loop, but I can not figure it out.
To make it easier for you to understand better, I paste the related part, and some simple explanation. I suppose the code is not important, the bug should happen because of the for circle.
- data: original data file,nxp matrix
- p: number of variable in data
- cov: pxp Covariance matrix of data
- tri:low trianlge matrix deposited from gsl_linalg_cholesky_decomp function
- p: px1 vector, randomly generated from N(0,1)
- x: tri*p
- sample: to record 10000x
code:
//1.2 draw one sample
void drawSample(gsl_matrix* cov,gsl_matrix* z,gsl_matrix* x, int p)
{
gsl_matrix* tri = gsl_matrix_alloc(p, p);
gsl_matrix_memcpy (tri, cov);
gsl_linalg_cholesky_decomp (tri);
for(int i=0;i<p;i++){
for(int j=0;j<p;j++){
if(i<j){
gsl_matrix_set(tri,i,j,0);}
}
}
const gsl_rng_type* T;
gsl_rng * r;
double a;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
for(int i=0;i<p;i++){
a = gsl_ran_gaussian (r, 1);
gsl_matrix_set(z,i,0,a);
}
matrixproduct(tri,z,x);
gsl_rng_free(r);
gsl_matrix_free(tri);
}