I tried to port m
code to c
or cpp
.
In my code there is a line
A = sparse(I,J,IA,nR,nC);
which converts row index I
, col index J
, and data IA
to sparse matrix A
with size nR x nC
.
Is there any equivalent code with C++ or C?
An naïve algorithm to duplicate result in full matrix is
double *A;
A = malloc(sizeof(double)*nR*nC);
memset(A, 0, sizeof(double));
for(k=0; k<size_of_IA; k++)
A[I[k]*nC + J[k]] += IA[k];
Note that if there is common indices, the value is not over overwritten, but accumulated.
Eigen is an example of a C++ math matrix library that cobtains sparse matrices. It overloads operators to make it feel like a built in feature.
There are many C and C++ matrix libraries. None ship as part of std
, nor is there anything built in.
Writing a good sparse matrix library would be quite hard; your best bet is finding a pre-written one. Recommendation questions are off topic