GenSVM
gensvm_sparse.c
Go to the documentation of this file.
1 
27 #include "gensvm_sparse.h"
28 
39 {
40  struct GenSparse *sp = Malloc(struct GenSparse, 1);
41  sp->nnz = 0;
42  sp->n_row = 0;
43  sp->n_col = 0;
44 
45  sp->values = NULL;
46  sp->ia = NULL;
47  sp->ja = NULL;
48 
49  return sp;
50 }
51 
62 void gensvm_free_sparse(struct GenSparse *sp)
63 {
64  free(sp->values);
65  free(sp->ia);
66  free(sp->ja);
67  free(sp);
68  sp = NULL;
69 }
70 
84 long gensvm_count_nnz(double *A, long rows, long cols)
85 {
86  long i, nnz = 0;
87 
88  for (i=0; i<rows*cols; i++)
89  nnz += (A[i] != 0.0) ? 1 : 0;
90  return nnz;
91 }
92 
105 bool gensvm_nnz_comparison(long nnz, long rows, long cols)
106 {
107  return (nnz < (rows*(cols-1.0)-1.0)/2.0);
108 }
109 
129 bool gensvm_could_sparse(double *A, long rows, long cols)
130 {
131  long nnz = gensvm_count_nnz(A, rows, cols);
132  return gensvm_nnz_comparison(nnz, rows, cols);
133 }
134 
150 struct GenSparse *gensvm_dense_to_sparse(double *A, long rows, long cols)
151 {
152  double value;
153  long row_cnt;
154  long i, j, cnt, nnz = 0;
155  struct GenSparse *spA = NULL;
156 
157  nnz = gensvm_count_nnz(A, rows, cols);
158 
159  spA = gensvm_init_sparse();
160 
161  spA->nnz = nnz;
162  spA->n_row = rows;
163  spA->n_col = cols;
164  spA->values = Calloc(double, nnz);
165  spA->ia = Calloc(long, rows+1);
166  spA->ja = Calloc(long, nnz);
167 
168  cnt = 0;
169  spA->ia[0] = 0;
170  for (i=0; i<rows; i++) {
171  row_cnt = 0;
172  for (j=0; j<cols; j++) {
173  value = matrix_get(A, cols, i, j);
174  if (value != 0) {
175  row_cnt++;
176  spA->values[cnt] = value;
177  spA->ja[cnt] = j;
178  cnt++;
179  }
180  }
181  spA->ia[i+1] = spA->ia[i] + row_cnt;
182  }
183 
184  return spA;
185 }
186 
200 {
201  double value;
202  long i, j, jj;
203  double *B = Calloc(double, (A->n_row)*(A->n_col));
204  for (i=0; i<A->n_row; i++) {
205  for (jj=A->ia[i]; jj<A->ia[i+1]; jj++) {
206  j = A->ja[jj];
207  value = A->values[jj];
208  matrix_set(B, A->n_col, i, j, value);
209  }
210  }
211 
212  return B;
213 }
#define Calloc(type, size)
Definition: gensvm_memory.h:40
long * ja
column indices, should be of length nnz
Definition: gensvm_sparse.h:67
long n_col
number of columns of the original matrix
Definition: gensvm_sparse.h:60
struct GenSparse * gensvm_init_sparse(void)
Initialize a GenSparse structure.
Definition: gensvm_sparse.c:38
long gensvm_count_nnz(double *A, long rows, long cols)
Count the number of nonzeros in a matrix.
Definition: gensvm_sparse.c:84
#define matrix_get(M, cols, i, j)
bool gensvm_nnz_comparison(long nnz, long rows, long cols)
Compare the number of nonzeros is such that sparsity if worth it.
long nnz
number of nonzero elements
Definition: gensvm_sparse.h:56
Header file for gensvm_sparse.c.
#define Malloc(type, size)
Definition: gensvm_memory.h:48
bool gensvm_could_sparse(double *A, long rows, long cols)
Check if it is worthwile to convert to a sparse matrix.
double * values
actual nonzero values, should be of length nnz
Definition: gensvm_sparse.h:63
void gensvm_free_sparse(struct GenSparse *sp)
Free an allocated GenSparse structure.
Definition: gensvm_sparse.c:62
struct GenSparse * gensvm_dense_to_sparse(double *A, long rows, long cols)
Convert a dense matrix to a GenSparse structure if advantageous.
#define matrix_set(M, cols, i, j, val)
double * gensvm_sparse_to_dense(struct GenSparse *A)
Convert a GenSparse structure to a dense matrix.
long * ia
cumulative row lengths, should be of length n_row+1
Definition: gensvm_sparse.h:65
A structure to represent a sparse matrix in CSR format.
Definition: gensvm_sparse.h:55
long n_row
number of rows of the original matrix
Definition: gensvm_sparse.h:58