ATSuite C++  v1.0
Scientific C++ routines originally developed by Alexis Tantet
atio.hpp
Go to the documentation of this file.
1 #ifndef ATIO_HPP
2 #define ATIO_HPP
3 
4 #include <vector>
5 #include <Eigen/Dense>
6 #include <Eigen/Sparse>
7 #include <gsl/gsl_vector.h>
8 #include <gsl/gsl_matrix.h>
9 
23 // Typedef declaration
25 typedef Eigen::SparseMatrix<double, Eigen::RowMajor> SpMatCSR;
27 typedef Eigen::SparseMatrix<double, Eigen::ColMajor> SpMatCSC;
29 typedef Eigen::Triplet<double> Tri;
30 
31 
32 // Declarations
33 void Eigen2Pajek(FILE *, const SpMatCSR *, const char *);
34 void Eigen2Compressed(FILE *, const SpMatCSC *, const char *);
35 void Eigen2Compressed(FILE *, const SpMatCSR *, const char *);
36 SpMatCSC * pajek2Eigen(FILE *, const char *);
37 SpMatCSR * Compressed2Eigen(FILE *, const char *);
38 gsl_matrix * Compressed2EdgeList(FILE *, const char *);
39 void Compressed2EdgeList(FILE *, FILE *, const char *);
40 SpMatCSR * CSC2CSR(const SpMatCSC *T);
41 SpMatCSC * CSR2CSC(const SpMatCSR *T);
42 std::vector<Tri> Eigen2Triplet(const SpMatCSC *);
43 std::vector<Tri> Eigen2Triplet(const SpMatCSR *);
44 void fprintfEigen(FILE *, const SpMatCSR *, const char *);
45 size_t lineCount(FILE *);
46 
47 // Definitions
57 void
58 Eigen2Pajek(FILE *fp, const SpMatCSR *P, const char *dataFormat="%lf"){
59  int N = P->rows();
60  int E = P->nonZeros();
61 
62  // Write vertices
63  fprintf(fp, "*Vertices %d\n", N);
64  for (int k = 0; k < N; k++)
65  fprintf(fp, "%d \"%d\"\n", k, k);
66 
67  // Write Edges
68  fprintf(fp, "Edges %d\n", E);
69  for (int i = 0; i < P->rows(); i++){
70  for (SpMatCSR::InnerIterator it(*P, i); it; ++it){
71  fprintf(fp, "%d %d ", i, it.col());
72  fprintf(fp, dataFormat, it.value());
73  fprintf(fp, "\n");
74  }
75  }
76 
77  return;
78 }
79 
89 SpMatCSC *
90 pajek2Eigen(FILE *fp, const char *dataFormat="%lf"){
91  char label[20];
92  int N, E;
93  int i, j, i0;
94  double x;
95 
96  std::vector<Tri> tripletList;
97 
98  // Read vertices
99  fscanf(fp, "%s %d", label, &N);
100 
101  // Define sparse matrix
102  SpMatCSC *T = new SpMatCSC(N, N);
103 
104  // Read first (assume monotonous)
105  fscanf(fp, "%d %s", &i0, label);
106  for (int k = 1; k < N; k++){
107  fscanf(fp, "%d %s", &i, label);
108  }
109 
110  // Read Edges
111  fscanf(fp, "%s %d", label, &E);
112 
113  // Reserve triplet capacity
114  tripletList.reserve(E);
115 
116  for (int k = 0; k < E; k++){
117  fscanf(fp, "%d %d", &i, &j);
118  fscanf(fp, dataFormat, &x);
119  tripletList.push_back(Tri(i - i0, j - i0, x));
120  }
121 
122  // Assign matrix elements
123  T->setFromTriplets(tripletList.begin(), tripletList.end());
124 
125  return T;
126 }
127 
136 void
137 Eigen2Compressed(FILE *fp, const SpMatCSC *P, const char *dataFormat="%lf"){
138  char sparseType[] = "CSC";
139 
140  // Write type, inner size, outer size and number of non-zeros
141  fprintf(fp, "%s %d %d %d\n", sparseType, P->innerSize(), P->outerSize(), P->nonZeros());
142 
143  // Write values
144  for (int nz = 0; nz < P->nonZeros(); nz++){
145  fprintf(fp, dataFormat, (P->valuePtr())[nz]);
146  fprintf(fp, " ");
147  }
148  fprintf(fp, "\n");
149 
150  // Write row indices
151  for (int nz = 0; nz < P->nonZeros(); nz++)
152  fprintf(fp, "%d ", (P->innerIndexPtr())[nz]);
153  fprintf(fp, "\n");
154 
155  // Write first element of column pointer
156  for (int outer = 0; outer < P->outerSize()+1; outer++)
157  fprintf(fp, "%d ", (P->outerIndexPtr())[outer]);
158  fprintf(fp, "\n");
159 
160  return;
161 }
162 
171 void
172 Eigen2Compressed(FILE *fp, const SpMatCSR *P, const char *dataFormat="%lf"){
173  char sparseType[] = "CSR";
174 
175  // Write type, inner size, outer size and number of non-zeros
176  fprintf(fp, "%s %d %d %d\n", sparseType, P->innerSize(), P->outerSize(), P->nonZeros());
177 
178  // Write values
179  for (int nz = 0; nz < P->nonZeros(); nz++){
180  fprintf(fp, dataFormat, (P->valuePtr())[nz]);
181  fprintf(fp, " ");
182  }
183  fprintf(fp, "\n");
184 
185  // Write column indices
186  for (int nz = 0; nz < P->nonZeros(); nz++)
187  fprintf(fp, "%d ", (P->innerIndexPtr())[nz]);
188  fprintf(fp, "\n");
189 
190  // Write first element of row pointer
191  for (int outer = 0; outer < P->outerSize()+1; outer++)
192  fprintf(fp, "%d ", (P->outerIndexPtr())[outer]);
193  fprintf(fp, "\n");
194 
195  return;
196 }
197 
206 SpMatCSR *
207 Compressed2Eigen(FILE *fp, const char *dataFormat="%lf")
208 {
209  int innerSize, outerSize, nnz;
210  char sparseType[4];
211  double *nzval;
212  int *innerIndexPtr, *outerIndexPtr;
213  SpMatCSR *T;
214  std::vector<Tri> tripletList;
215 
216  // Read type, inner size, outer size and number of non-zeros and allocate
217  fscanf(fp, "%s %d %d %d", sparseType, &outerSize, &innerSize, &nnz);
218  nzval = new double [nnz];
219  innerIndexPtr = new int [nnz];
220  outerIndexPtr = new int[outerSize+1];
221  T = new SpMatCSR(outerSize, innerSize);
222  Eigen::VectorXf innerNNZ(outerSize);
223 
224  // Read values
225  for (int nz = 0; nz < nnz; nz++)
226  fscanf(fp, dataFormat, &nzval[nz]);
227 
228  // Read inner indices (column)
229  for (int nz = 0; nz < nnz; nz++)
230  fscanf(fp, "%d ", &innerIndexPtr[nz]);
231 
232  // Read first element of column pointer
233  fscanf(fp, "%d ", &outerIndexPtr[0]);
234  for (int outer = 1; outer < outerSize+1; outer++){
235  fscanf(fp, "%d ", &outerIndexPtr[outer]);
236  innerNNZ(outer-1) = outerIndexPtr[outer] - outerIndexPtr[outer-1];
237  }
238  T->reserve(innerNNZ);
239 
240  // Insert elements
241  for (int outer = 0; outer < outerSize; outer++)
242  for (int nzInner = outerIndexPtr[outer]; nzInner < outerIndexPtr[outer+1]; nzInner++)
243  T->insertBackUncompressed(outer, innerIndexPtr[nzInner]) = nzval[nzInner];
244  //T.sumupDuplicates();
245 
246  delete nzval;
247  delete innerIndexPtr;
248  delete outerIndexPtr;
249 
250  return T;
251 }
252 
262 gsl_matrix *
263 Compressed2EdgeList(FILE *fp, const char *dataFormat="%lf")
264 {
265  int innerSize, outerSize, nnz;
266  char sparseType[4];
267  double *nzval;
268  int *idx, *ptr;
269  gsl_matrix *EdgeList;
270  int iOuter, iInner;
271 
272  // Read type, inner size, outer size and number of non-zeros and allocate
273  fscanf(fp, "%s %d %d %d", sparseType, &innerSize, &outerSize, &nnz);
274  nzval = new double [nnz];
275  idx = new int [nnz];
276  ptr = new int[outerSize+1];
277  EdgeList = gsl_matrix_alloc(nnz, 3);
278  if (strcmp(sparseType, "CSR") == 0)
279  iOuter = 0;
280  else if (strcmp(sparseType, "CSR") == 0)
281  iOuter = 1;
282  else{
283  std::cerr << "Invalid sparse matrix type." << std::endl;
284  exit(EXIT_FAILURE);
285  }
286  iInner = (iOuter + 1)%2;
287 
288  // Read values
289  for (int nz = 0; nz < nnz; nz++)
290  fscanf(fp, dataFormat, &nzval[nz]);
291 
292  // Read row indices
293  for (int nz = 0; nz < nnz; nz++)
294  fscanf(fp, "%d ", &idx[nz]);
295 
296  // Read first element of column pointer
297  for (int outer = 0; outer < outerSize+1; outer++)
298  fscanf(fp, "%d ", &ptr[outer]);
299 
300  int nz = 0;
301  for (int outer = 0; outer < outerSize; outer++){
302  for (int inner = ptr[outer]; inner < ptr[outer+1]; inner++){
303  gsl_matrix_set(EdgeList, nz, iOuter, outer);
304  gsl_matrix_set(EdgeList, nz, iInner, idx[inner]);
305  gsl_matrix_set(EdgeList, nz, 2, nzval[inner]);
306  nz++;
307  }
308  }
309 
310  delete(nzval);
311  delete(idx);
312  delete(ptr);
313  return EdgeList;
314 }
315 
324 void
325 Compressed2EdgeList(FILE *src, FILE *dst, const char *dataFormat="%lf")
326 {
327  gsl_matrix *EdgeList = Compressed2EdgeList(src);
328  size_t nnz = EdgeList->size1;
329 
330  for (size_t nz = 0; nz < nnz; nz++){
331  fprintf(dst, "%d\t%d\t", (int) gsl_matrix_get(EdgeList, nz, 0),
332  (int) gsl_matrix_get(EdgeList, nz, 1));
333  fprintf(dst, dataFormat, gsl_matrix_get(EdgeList, nz, 2));
334  fprintf(dst, "\n");
335  }
336 
337  gsl_matrix_free(EdgeList);
338  return;
339 }
340 
348 SpMatCSR *
349 CSC2CSR(const SpMatCSC *T){
350  int N = T->rows();
351 
352  // Define sparse matrix
353  SpMatCSR *TCSR = new SpMatCSR(N, N);
354 
355  // Get triplet list
356  std::vector<Tri> tripletList = Eigen2Triplet(T);
357 
358  // Assign matrix elements
359  TCSR->setFromTriplets(tripletList.begin(), tripletList.end());
360 
361  return TCSR;
362 }
363 
371 SpMatCSC *
372 CSR2CSC(const SpMatCSR *T){
373  int N = T->rows();
374 
375  // Define sparse matrix
376  SpMatCSC *TCSC = new SpMatCSC(N, N);
377 
378  // Get triplet list
379  std::vector<Tri> tripletList = Eigen2Triplet(T);
380 
381  // Assign matrix elements
382  TCSC->setFromTriplets(tripletList.begin(), tripletList.end());
383 
384  return TCSC;
385 }
386 
394 std::vector<Tri>
396 {
397  // Works for column major only
398  int nnz = T->nonZeros();
399  std::vector<Tri> tripletList;
400  tripletList.reserve(nnz);
401 
402  for (int beta = 0; beta < T->outerSize(); ++beta)
403  for (SpMatCSC::InnerIterator it(*T, beta); it; ++it)
404  tripletList.push_back(Tri(it.row(), it.col(), it.value()));
405 
406  return tripletList;
407 }
408 
416 std::vector<Tri>
417 Eigen2Triplet(const SpMatCSR *T)
418 {
419  // Works for column major only
420  int nnz = T->nonZeros();
421  std::vector<Tri> tripletList;
422  tripletList.reserve(nnz);
423 
424  for (int beta = 0; beta < T->outerSize(); ++beta)
425  for (SpMatCSR::InnerIterator it(*T, beta); it; ++it)
426  tripletList.push_back(Tri(it.row(), it.col(), it.value()));
427 
428  return tripletList;
429 }
430 
439 void
440 fprintfEigen(FILE *fp, const SpMatCSR *T, const char *dataFormat)
441 {
442  int count;
443  for (int irow = 0; irow < T->outerSize(); ++irow){
444  count = 0;
445  for (SpMatCSR::InnerIterator it(*T, irow); it; ++it){
446  while (count < it.col()){
447  fprintf(fp, "---\t");
448  count++;
449  }
450  fprintf(fp, dataFormat, it.value());
451  fprintf(fp, "\t");
452  count++;
453  }
454  while (count < T->innerSize()){
455  fprintf(fp, "---\t");
456  count++;
457  }
458  fprintf(fp, "\n");
459  }
460  return;
461 }
462 
470 size_t
471 lineCount(FILE *fp)
472 {
473  size_t count = 0;
474  int ch;
475 
476  // Count lines
477  do {
478  ch = fgetc(fp);
479  if (ch == '\n')
480  count++;
481  } while (ch != EOF);
482 
483  return count;
484 }
485 
486 #endif
SpMatCSR * Compressed2Eigen(FILE *, const char *)
Scan an Eigen CSR matrix from a file in compressed format.
Definition: atio.hpp:207
SpMatCSC * pajek2Eigen(FILE *, const char *)
Scans an Eigen CSC matrix from a Pajek file.
Definition: atio.hpp:90
Eigen::Triplet< double > Tri
Eigen triplet of double.
Definition: atio.hpp:29
SpMatCSR * CSC2CSR(const SpMatCSC *T)
Convert an Eigen CSC matrix to an Eigen CSR matrix.
Definition: atio.hpp:349
SpMatCSC * CSR2CSC(const SpMatCSR *T)
Convert an Eigen CSR matrix to an Eigen CSC matrix.
Definition: atio.hpp:372
Eigen::SparseMatrix< double, Eigen::RowMajor > SpMatCSR
Eigen sparse CSR matrix of double type.
Definition: atio.hpp:25
size_t lineCount(FILE *)
Count the number of lines in a file.
Definition: atio.hpp:471
void Eigen2Compressed(FILE *, const SpMatCSC *, const char *)
Print an Eigen CSC matrix to file in compressed format.
Definition: atio.hpp:137
std::vector< Tri > Eigen2Triplet(const SpMatCSC *)
Convert an Eigen CSC matrix to a vector of Eigen triplet.
Definition: atio.hpp:395
void Eigen2Pajek(FILE *, const SpMatCSR *, const char *)
Print an Eigen CSR matrix in Pajek format.
Definition: atio.hpp:58
void fprintfEigen(FILE *, const SpMatCSR *, const char *)
Print an Eigen CSR matrix as a dense matrix.
Definition: atio.hpp:440
gsl_matrix * Compressed2EdgeList(FILE *, const char *)
Scan an edge list from a file in compressed format.
Definition: atio.hpp:263
Eigen::SparseMatrix< double, Eigen::ColMajor > SpMatCSC
Eigen sparse CSC matrix of double type.
Definition: atio.hpp:27