-1

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.

Dohyun
  • 642
  • 4
  • 15

1 Answers1

2

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

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Thanks. I knew that there is eigen which supports sparse matrix manipulation, but I didn't know that there is equivalent function. I read the documentation and I found that `setFromTriplets` is what I wanted. Thanks! – Dohyun Nov 14 '16 at 03:51