CSS product code  0.1
C++ library to estimate distance of CSS codes. Some particular construction of CSS codes are implemented.
mm_read.cpp
Go to the documentation of this file.
1 /*
2 * Matrix Market I/O example program
3 *
4 * Read a real (non-complex) sparse matrix from a Matrix Market (v. 2.0) file.
5 * and copies it to stdout. This porgram does nothing useful, but
6 * illustrates common usage of the Matrix Matrix I/O routines.
7 * (See http://math.nist.gov/MatrixMarket for details.)
8 *
9 * Usage: a.out [filename] > output
10 *
11 *
12 * NOTES:
13 *
14 * 1) Matrix Market files are always 1-based, i.e. the index of the first
15 * element of a matrix is (1,1), not (0,0) as in C. ADJUST THESE
16 * OFFSETS ACCORDINGLY offsets accordingly when reading and writing
17 * to files.
18 *
19 * 2) ANSI C requires one to use the "l" format modifier when reading
20 * double precision floating point numbers in scanf() and
21 * its variants. For example, use "%lf", "%lg", or "%le"
22 * when reading doubles, otherwise errors will occur.
23 */
24 //Weilei: read from the file and return GF2mat
25 //#include <stdio.h>//cout
26 //#include <stdlib.h> //fprintf?
27 #include "mmio.h" //for MM format
28 #include <itpp/itbase.h>
29 //#include <string>
30 #include "mm_read.h"
31 
32 
33 
34 
35 
36 itpp::GF2mat MM_to_GF2mat(std::string file_name){
37  char cstr[file_name.size()+1];
38  strcpy(cstr,file_name.c_str());
39  return MM_to_GF2mat(cstr);
40 }
41 
42 itpp::mat MM_to_mat(std::string file_name){
43  char cstr[file_name.size()+1];
44  strcpy(cstr,file_name.c_str());
45  return MM_to_mat(cstr);
46 }
47 
48 
49 itpp::GF2mat MM_to_GF2mat(char * file_name)
50 {
51  int ret_code;
52  MM_typecode matcode;
53  FILE *f;
54  int M, N, nz;
55  int i, *I, *J;
56  double *val;
57 
58  if ((f = fopen(file_name, "r")) == NULL) {
59  std::cout<<"file open fail:"<<file_name<<std::endl;
60  exit(1);}
61  if (mm_read_banner(f, &matcode) != 0)
62  {
63  printf("Could not process Matrix Market banner.\n");
64  exit(1);
65  }
66 
67  /* This is how one can screen matrix types if their application */
68  /* only supports a subset of the Matrix Market data types. */
69 
70  if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
71  mm_is_sparse(matcode) )
72  {
73  printf("Sorry, this application does not support ");
74  printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
75  exit(1);
76  }
77 
78  /* find out size of sparse matrix .... */
79 
80  if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0)
81  exit(1);
82 
83  /* reseve memory for matrices */
84 
85  I = (int *) malloc(nz * sizeof(int));
86  J = (int *) malloc(nz * sizeof(int));
87  val = (double *) malloc(nz * sizeof(double));
88 
89 
90  /* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
91  /* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
92  /* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
93 
94  for (i=0; i<nz; i++)
95  {
96  if ( fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]) !=3 ){
97  fprintf( stderr, "Expected at least three numbers as input\n");
98  exit(1);
99  }
100  I[i]--; /* adjust from 1-based to 0-based */
101  J[i]--;
102  }
103 
104  if (f !=stdin) fclose(f);
105 
106  /************************/
107  /* now write out matrix */
108  /************************/
109  /*
110  mm_write_banner(stdout, matcode);
111  mm_write_mtx_crd_size(stdout, M, N, nz);
112  for (i=0; i<nz; i++)
113  fprintf(stdout, "%d %d %20.19g\n", I[i]+1, J[i]+1, val[i]);
114  */
115 
116  //read into GF2mat
117  itpp::GF2mat G(M,N);
118  for (int i=0;i<nz;i++){
119  G.set(I[i],J[i],val[i]);
120  }
121  // GF2matPrint(G);
122 
123  return G;
124 }
125 
126 itpp::mat MM_to_mat(char * file_name)
127 {
128  // std::cout<<"debug: MM_to_mat"<<std::endl;
129  int ret_code;
130  MM_typecode matcode;
131  FILE *f;
132  int M, N, nz;
133  int i, *I, *J;
134  double *val;
135 
136  if ((f = fopen(file_name, "r")) == NULL) {
137  std::cout<<"file open fail:"<<file_name<<std::endl;
138  exit(1);}
139  if (mm_read_banner(f, &matcode) != 0)
140  {
141  printf("Could not process Matrix Market banner.\n");
142  exit(1);
143  }
144 
145  /* This is how one can screen matrix types if their application */
146  /* only supports a subset of the Matrix Market data types. */
147 
148  if (! mm_is_sparse(matcode)){
149  if (f !=stdin) fclose(f);
150  //a dense matrix is saved in column format insteead of coordinate format.
151  return dense_MM_to_mat(file_name);
152  }
153 
154  if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
155  mm_is_sparse(matcode) )
156  {
157  printf("Sorry, this application does not support ");
158  printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
159  exit(1);
160  }
161 
162  /* find out size of sparse matrix .... */
163 
164  if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0)
165  exit(1);
166 
167  /* reseve memory for matrices */
168 
169  I = (int *) malloc(nz * sizeof(int));
170  J = (int *) malloc(nz * sizeof(int));
171  val = (double *) malloc(nz * sizeof(double));
172 
173 
174  /* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
175  /* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
176  /* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
177 
178  for (i=0; i<nz; i++)
179  {
180  if ( fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]) !=3){
181  fprintf( stderr, "Expected at least three numbers as input\n");
182  exit(1);
183  }
184  I[i]--; /* adjust from 1-based to 0-based */
185  J[i]--;
186  }
187 
188  if (f !=stdin) fclose(f);
189 
190  /************************/
191  /* now write out matrix */
192  /************************/
193  /*
194  mm_write_banner(stdout, matcode);
195  mm_write_mtx_crd_size(stdout, M, N, nz);
196  for (i=0; i<nz; i++)
197  fprintf(stdout, "%d %d %20.19g\n", I[i]+1, J[i]+1, val[i]);
198  */
199 
200  //read into mat
201  itpp::mat G(M,N);
202  G.zeros();
203  for (int i=0;i<nz;i++){
204  G.set(I[i],J[i],val[i]);
205  }
206  return G;
207 }
208 
209 itpp::mat dense_MM_to_mat(char * file_name){
210  //this is design for dense matrix set in column fashion
211  // std::cout<<"debug: dense_MM_to_mat"<<std::endl;
212 
213  int ret_code;
214  MM_typecode matcode;
215  FILE *f;
216  int M, N, nz;
217  int i;//, *I, *J;
218  double *val;
219 
220  if ((f = fopen(file_name, "r")) == NULL) {
221  std::cout<<"file open fail:"<<file_name<<std::endl;
222  exit(1);}
223  if (mm_read_banner(f, &matcode) != 0)
224  {
225  printf("Could not process Matrix Market banner.\n");
226  exit(1);
227  }
228 
229  // std::cout<<matcode<<std::endl;
230  /* This is how one can screen matrix types if their application */
231  /* only supports a subset of the Matrix Market data types. */
232 
233  if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
234  mm_is_sparse(matcode) )
235  {
236  printf("Sorry, this application does not support ");
237  printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
238  exit(1);
239  }
240 
241  /* find out size of sparse matrix .... */
242 
243  if ((ret_code = mm_read_mtx_array_size(f, &M, &N) !=0))
244  exit(1);
245  nz=M*N;
246  /* reseve memory for matrices */
247 
248  // I = (int *) malloc(nz * sizeof(int));
249  //J = (int *) malloc(nz * sizeof(int));
250 
251  val = (double *) malloc(nz * sizeof(double));
252 
253 
254  /* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
255  /* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
256  /* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
257 
258  // double min_value=100;
259  // int min_index=-1;
260  for (i=0; i<nz; i++)
261  {
262 
263  if ( fscanf(f, " %lg\n", &val[i]) !=1){
264  fprintf( stderr, "Expected at least one numbers as input\n");
265  exit(1);
266  }
267 
268  /*if ( val[i] < min_value){
269  std::cout<< val[i]<<std::endl;
270  min_value = val[i];
271  min_index = i;
272 
273  }*/
274  //I[i]--; /* adjust from 1-based to 0-based */
275  //J[i]--;
276  }
277  // std::cout<<M<<" M N "<<N<<std::endl;
278  //std::cout<<min_value <<"value index " <<min_index<<std::endl;
279  //std::cout<<"val[2190] = "<<val[2190]<<std::endl;
280 
281  if (f !=stdin) fclose(f);
282 
283 
284 
285  //read into mat
286  itpp::mat G(M,N);
287  G.zeros();
288  // double dd=0,value=100;
289  //int index =-1;
290  for (int i=0;i<M;i++){
291  for ( int j=0;j<N;j++){
292  // dd=val[j*M+i];
293  //std::cout<<j*N+i<<std::endl;
294  G.set(i,j,val[j*M+i]);
295  /*if ( val[j*N+i] < value){
296  value = val[j*N+i];
297  index = j*N+i;
298  std::cout<<value <<" value index," <<index<<std::endl;
299  // std::cout<< val[j*N+i]<<","<<G(i,j)<<std::endl;
300  }
301  if ( G(i,j)==0){
302  // std::cout<<i<<" i,j "<<j<<", val = "<<val[j*N+i]<<std::endl;
303  }
304  */
305  // std::cout<<"test "<<std::endl;;
306  }
307  }
308  //std::cout<<M<<" M N "<<N<<std::endl;
309  //std::cout<<G.rows()<<" row col "<<G.cols()<<std::endl;
310  return G;
311 }
312 
mm_typecode_to_str
char * mm_typecode_to_str(MM_typecode matcode)
Definition: mmio.c:453
MM_to_mat
itpp::mat MM_to_mat(std::string file_name)
Definition: mm_read.cpp:42
MM_to_GF2mat
itpp::GF2mat MM_to_GF2mat(std::string file_name)
Definition: mm_read.cpp:36
mm_read_mtx_crd_size
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
Definition: mmio.c:187
mmio.h
mm_read_mtx_array_size
int mm_read_mtx_array_size(FILE *f, int *M, int *N)
Definition: mmio.c:218
MM_typecode
char MM_typecode[4]
Definition: mmio.h:23
dense_MM_to_mat
itpp::mat dense_MM_to_mat(char *file_name)
Definition: mm_read.cpp:209
mm_is_matrix
#define mm_is_matrix(typecode)
Definition: mmio.h:46
mm_is_complex
#define mm_is_complex(typecode)
Definition: mmio.h:53
mm_read.h
mm_read_banner
int mm_read_banner(FILE *f, MM_typecode *matcode)
Definition: mmio.c:94
mm_is_sparse
#define mm_is_sparse(typecode)
Definition: mmio.h:48