I am implementing a sparse linear equation solver such as Lx = b. For this, I initialize a reference x vector named x_ref and generate an RHS vector b accordingly. However, both pointers seem empty somehow.
VALUE_TYPE is a macro and set to double
VALUE_TYPE *x_ref = (VALUE_TYPE *)malloc(sizeof(VALUE_TYPE) * n);
VALUE_TYPE *b = (VALUE_TYPE *)malloc(sizeof(VALUE_TYPE) * n);
for ( int i = 0; i < n; i++)
x_ref[i] = rand() % 10 + 1;
for (int i = 0; i < n; i++)
{
for (int j = cscColPtrTR[i]; j < cscColPtrTR[i+1]; j++)
{
int rowid = cscRowIdxTR[j]; //printf("rowid = %i\n", rowid);
b[rowid] += cscValTR[j] * x_ref[i];
}
}
After I print them as;
for(int i = 0; i < n; i++)
{
printf("%d\t\t%d\n", x_ref[i], b[i]);
}
The results are:
4226166 1977719296
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
I cannot understand what is wrong with it.
sizeof
instead of types VALUE_TYPE *x_ref = (VALUE_TYPE *)malloc(sizeof(*x_ref) * n);
printf
formats. %d
is used to output integers, not doubles.#define VALUE_TYPE double
int main(void)
{
size_t n = 20;
VALUE_TYPE *x_ref = malloc(sizeof(*x_ref) * n);
VALUE_TYPE *b = malloc(sizeof(*b) * n);
for ( int i = 0; i < n; i++)
x_ref[i] = rand() % 10 + 1;
for ( int i = 0; i < n; i++)
printf("%d - %f\n", i, x_ref[i]);
}