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