Search code examples
c++carrayssuitesparse

Indexing into CHOLMOD dense vector array


I have a cholmod_dense data structure:

cholmod_dense* ex = cholmod_l_solve(CHOLMOD_A, L, B, &com);

and I want to extract the values and copy them to another variable. This means I need to index into the double array and copy values over.

for (int k=0; k<ncols; k++) T_x[k]=((double*)ex->x)[k];   

which the compiler is ok with but I get a segmenation fault. Or I think I should be able to do:

double* e_x =(double*)ex->x;
for (int k=0; k<ncols; k++) T_x[k]=*e_x[k];

But the compiler really dislikes this:

error: invalid type argument of unary ‘*’ (have ‘double’)
for (int k=0; k<ncols; k++) T_x[k]= *e_x[k];

According to CHOLMOD userguide:

  1. cholmod dense: A dense matrix, either real, complex or zomplex, in column-major order. This differs from the row-major convention used in C. A dense matrix X contains • X->x, a double array of size X->nzmax or twice that for the complex case. • X->z, a double array of size X->nzmax if X is zomplex.

So I should be able to simply grab ex->x and index into it as a double array, but I cannot do so without getting a segmentation fault. Can anyone help me out?

The CHOLMOD library is written in C and the code that is linking to CHOLMOD library (the code snippet shown above) is c++.


Solution

  • Ok, so it looks likes I made a couple mistakes.

    First of all, I was running into a segmentation fault because I was using cholmod_l_zeros(); which assumes long integers. Instead, I should be using cholmod_zeros(); since I am using doubles.

    After fixing this I ran into the error CHOLMOD error: invalid xtype right after my cholmod_solve(CHOLMOD_A, L, B, &com); statement. This was because my cholmod_factor* L was defined out of scope. After fixing both of these issues, the code successfully copies values from cholmod_dense ex->x double array over to my T_x double vector:

    cholmod_dense* ex = cholmod_solve(CHOLMOD_A, L, B, &com);
    double* e_x = (double*)ex->x;
    for (int k=0; k<ncols; k++) T_x[k] = e_x[k];
    

    I also was unaware that the [] operator automatically dereferences pointers. Good to know!