tibsar - 10 months ago 62

C Question

I am trying to use the GSL library in C and am running into trouble.

I am allocating space for my matrices and vectors:

`A = gsl_matrix_alloc(nr, nc); /* Data matrix */`

tau = gsl_vector_alloc(nc); /* required place holder for GSL */

Then I am initializing

`A`

`for(i = 0; i < nr; i++){`

gsl_matrix_set(A, i, 0, 1.0);

}

for(i = 1; i < nr; i++){

for(j = 0; j < nc; j++){

gsl_matrix_set(A, i, j, 1.0 / (double)(i + j + 1));

}

}

I am then erroring on this line:

`gsl_linalg_QR_decomp(A, tau);`

with the following error:

`==901== Conditional jump or move depends on uninitialised value(s)`

==901== at 0x3E8920AE0E: cblas_dnrm2 (in /usr/lib64/libgslcblas.so.0.0.0)

==901== by 0x3E88EA779A: gsl_linalg_householder_transform (in /usr/lib64/libgsl.so.0.14.0)

==901== by 0x3E88EA1305: gsl_linalg_QR_decomp (in /usr/lib64/libgsl.so.0.14.0)

==901== by 0x400FF3: GE_FindPoint (hw8.c:145)

==901== by 0x400DF4: main (hw8.c:66)

==901== Uninitialised value was created by a heap allocation

==901== at 0x4A06A2E: malloc (vg_replace_malloc.c:270)

==901== by 0x3E88E4CFD4: gsl_block_alloc (in /usr/lib64/libgsl.so.0.14.0)

==901== by 0x3E88EB1768: gsl_matrix_alloc (in /usr/lib64/libgsl.so.0.14.0)

==901== by 0x400EBB: GE_FindPoint (hw8.c:110)

==901== by 0x400DF4: main (hw8.c:66)

Line 110 points to the line allocating

`A`

`A = gsl_matrix_alloc(nr, nc);`

In this case,

`nc = 3`

`nr = 3840`

Answer Source

In the second part of your initialization, the start values of `i`

and `j`

are mismatched. So you call `gsl_matrix_set( A, i, 0, ... )`

twice for 1 <= i < nr and you never call `gsl_matrix_set( A, 0, j, ... )`

for 1 <= j < nc.

Just change that part of the initialization to

```
for(i = 0; i < nr; i++){
for(j = 1; j < nc; j++){
gsl_matrix_set(A, i, j, 1.0 / (double)(i + j + 1));
}
}
```