6

Can anyone please give me a simple example of how to add elements to a triplet matrix using CHOLMOD.

I have tried something like this:

cholmod_triplet *A;
int k;

void add_A_entry(int r, int c, double x)
{
    ((int*)A->i)[k] = r;
    ((int*)A->j)[k] = c;
    ((double*)A->x)[k] = x;
    k++;
}

int main()
{
    k = 0;
    cholmod_common com;
    cholmod_start(&com);

    A = cholmod_allocate_triplet(202, 202, 202*202, -1, CHOLMOD_REAL, &com);
    add_A_entry(2, 2, 1.);
    add_A_entry(4, 1, 2.);
    add_A_entry(2, 10, -1.);

    cholmod_print_triplet(A, "A", &com);

    cholmod_finish(&com);
    return 0;
}

However, this doesn't add any elements to the matrix. I simply get the output:

CHOLMOD triplet: A:  202-by-202, nz 0, lower.  OK

Of course, I have tried to find the solution both by searching and in the CHOLMOD documentation, but I found no help.

timrau
  • 22,578
  • 4
  • 51
  • 64
asny
  • 63
  • 4
  • 1
    What do you mean by saying that it does not add elements to the matrix? Where is k initialized? is it a global variable? – angainor Sep 26 '12 at 18:36
  • If I for example print the matrix elements by cholmod_print_triplet(A, "Triplet", &com); it prints a matrix of zeros. The matrix and the variable k is defined in the scope of a class, but yes, for simplicity let's say they are global variables. – asny Sep 26 '12 at 19:01

1 Answers1

10

cholmod_allocate_triplet() sets A->nzmax, which in your case is 202*202. That just defines the space available to add triplets. The actual number of triplets in the matrix is A->nnz, which gets set to zero by cholmod_allocate_triplet().

The A->nnz should be used instead of your variable k.

Tim Davis (CHOLMOD author)

timrau
  • 22,578
  • 4
  • 51
  • 64
Tim Davis
  • 166
  • 1
  • 2