How to add elements to a triplet matrix using CHOLMOD?
-
04-07-2021 - |
Question
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.
Solution
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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow