0

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);

}

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • is `drawSample` using any global variable that you are not reinitializing between each iteration? – quantdev Jun 04 '14 at 21:15
  • @Wolf yes, 2 works. And actually. 2-51 are all work, but I do not understand why. – user3708204 Jun 04 '14 at 21:17
  • 1
    Did you debug this so you know which line of code that causes the error ? Do that, and point us to that code, and show the relevant values of the variables involved. – nos Jun 04 '14 at 21:24
  • @nos yes, I know, and I pointed out already. `for(int i=0;i<10000;i++){//error occures when I have the circle `.also, if i changed it to i<2,it worked or any from 1-51, it work. Any larger number will not work out. – user3708204 Jun 04 '14 at 21:29
  • @user3708204 No, that's not it. Your error clearly comes from a gsl function call. There is no such function call in that loop. Probably one of the gsl_xxx() functions within your drawSample() function fails, or possibly something within matrixproduct(). Find the point of failure. – nos Jun 04 '14 at 21:33

0 Answers0