CSS product code  0.1
C++ library to estimate distance of CSS codes. Some particular construction of CSS codes are implemented.
mm_write.cpp
Go to the documentation of this file.
1 
6 //recomend file format for Matrix Market file is .mm
7 //the segmentation fault is finally solved by reserve memory for I,J and Vval. May 2018
8 //trouble shooting: segmentation fault: directory not exist; wrong folder name
9 
10 #include "mmio.h"
11 //#include <stdio.h>
12 //#include <stdlib.h>
13 //#include <fstream> //ofstream ifstream
14 #include <itpp/itbase.h>
15 
16 
17 //int GF2mat_to_MM(GF2mat G, char* file_name="mm_temp.dat")
18 int GF2mat_to_MM(itpp::GF2mat G, char* file_name, int debug)
19 {
20  int M=G.rows(),N=G.cols(),nt=M*N;//nt is the total number of elements, will find nz later. nz is the number of non-zero elements
21  //make nt smaller; the size of int[] should be less than about 2000000+; otherwise it creat segmentation fault
22  //this could be fixed by seperate int[] into smaller ones, but in this case the matrix is already too bigfor any calculation
23  // std::cout<<"density of GF2mat: "<<G.density()<<endl;
24  nt=floor(1+nt*(G.density()+0.001) );
25 
26  int *I, *J;
27  I = (int *) malloc(nt * sizeof(int));
28  J = (int *) malloc(nt * sizeof(int));
29  double * val;
30  val = (double *) malloc(nt * sizeof(double));
31 
32  // int I[nt],J[nt];//the location of nonzero elements
33  //usually nt is much larger than nz, where nz/nt is the density of the code. There is some wasted memory here.
34  // double val[nt];//have to use double to save the binary value in order to match the format of the file, didn't affect the result so far
35  int pos=0;
36  for(int m=0;m<M;m++){
37  // std::cout<<"\t m="<<m<<",";
38  for(int n=0;n<N;n++){
39  if(G.get(m,n)){//if it is nonzero
40  I[pos]=m;
41  J[pos]=n;
42  val[pos]=1;//we know it is one. so dont use G.get(m,n)
43  pos++;
44  }
45  }
46  }
47  int nz=pos;//number of nonzero elements
48  //std::cout<<"number of ones in the matrix is nz= "<<nz<<std::endl;
49  MM_typecode matcode;
50  mm_initialize_typecode(&matcode);
51  mm_set_matrix(&matcode);
52  mm_set_coordinate(&matcode);
53  mm_set_real(&matcode);
54 
55  //create a file and wrote into it.
56  FILE *fout;
57  fout=fopen(file_name,"w");
58  mm_write_banner(fout, matcode);
59  mm_write_mtx_crd_size(fout, M, N, nz);
60 
61  /* NOTE: matrix market files use 1-based indices, i.e. first element
62  of a vector has index 1, not 0. */
63 
64  //it is not necessary to seperate the reading and writing in 2 loops
65  for (int i=0; i<nz; i++)
66  fprintf(fout, "%d %d %10.3g\n", I[i]+1, J[i]+1, val[i]);
67 
68  fclose(fout);
69  if ( debug ) std::cout<<"wrote the matrix (density:"<<G.density()<<") into file "<<file_name<<std::endl;
70  return 0;
71 
72 }
73 
74 //int mat_to_MM(mat G, char* filename="mm_temp.dat")
75 int mat_to_MM(itpp::mat G, char * filename){//use this when G is not a binary matrix
76  //dose not work sometimes and not sure why. Segmentation fault in line: mm_write_banner(fout, matcode);
77  int M=G.rows(),N=G.cols(),nt=M*N;//nt is the total number of elements, will find nz later. nz is the number of non-zero elements
78  //make nt smaller; the size of int[] should be less than about 2000000+
79  //nt=floor(1+nt*G.density());//design to be not sparse and small size
80  MM_typecode matcode;
81  int I[nt],J[nt];//the location of nonzero elements
82  //usually nt is much larger than nz, where nz/nt is the density of the code. There is some wasted memory here.
83  double val[nt];//have to use double to save the binary value in order to match the format of the file, didn't affect the result so far
84  int pos=0;
85  for(int m=0;m<M;m++){
86  for(int n=0;n<N;n++){
87  if(G.get(m,n)){//if it is nonzero
88  I[pos]=m;
89  J[pos]=n;
90  val[pos]=G.get(m,n);
91  pos++;
92  }
93  }
94  }
95  int nz=pos;//number of nonzero elements
96  mm_initialize_typecode(&matcode);
97  mm_set_matrix(&matcode);
98  mm_set_coordinate(&matcode);
99  mm_set_real(&matcode);
100 
101  //create a file and wrote into it.
102  // std::cout<<filename<<", "<<M<<", "<<N<<", "<<nz<<std::endl;
103  FILE *fout;
104  fout=fopen(filename,"w");
105  mm_write_banner(fout, matcode);
106  mm_write_mtx_crd_size(fout, M, N, nz);
107 
108  /* NOTE: matrix market files use 1-based indices, i.e. first element
109  of a vector has index 1, not 0. */
110 
111  //it is not necessary to seperate the reading and writing in 2 loops
112  for (int i=0; i<nz; i++){
113  fprintf(fout, "%d %d %g\n", I[i]+1, J[i]+1, val[i]);
114  //originally use %10.3g, now use %g
115  }
116  fclose(fout);
117  std::cout<<"wrote the matrix into file "<<filename<<std::endl;
118  return 0;
119 
120 }
121 
122 
123 int GF2mat_to_MM(itpp::mat G, std::string file_name){
124  char temp[file_name.length()+1];
125  strcpy(temp, file_name.c_str());
126  return GF2mat_to_MM(G, temp);
127 }
128 
129 
130 int mat_to_MM(itpp::mat G, std::string file_name){
131  char temp[file_name.length()+1];
132  strcpy(temp, file_name.c_str());
133  return mat_to_MM(G, temp);
134 }
mm_set_real
#define mm_set_real(typecode)
Definition: mmio.h:75
mmio.h
GF2mat_to_MM
int GF2mat_to_MM(itpp::GF2mat G, char *file_name, int debug)
Definition: mm_write.cpp:18
mm_write_mtx_crd_size
int mm_write_mtx_crd_size(FILE *f, int M, int N, int nz)
Definition: mmio.c:179
mat_to_MM
int mat_to_MM(itpp::mat G, char *filename)
Definition: mm_write.cpp:75
mm_set_matrix
#define mm_set_matrix(typecode)
Definition: mmio.h:68
MM_typecode
char MM_typecode[4]
Definition: mmio.h:23
mm_write_banner
int mm_write_banner(FILE *f, MM_typecode matcode)
Definition: mmio.c:384
mm_initialize_typecode
#define mm_initialize_typecode(typecode)
Definition: mmio.h:88
mm_set_coordinate
#define mm_set_coordinate(typecode)
Definition: mmio.h:69