forked from ProbShin/mpiCG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmwrite.m
274 lines (258 loc) · 6.69 KB
/
mmwrite.m
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
function [ err ] = mmwrite(filename,A,comment,field,precision)
%
% Function: mmwrite(filename,A,comment,field,precision)
%
% Writes the sparse or dense matrix A to a Matrix Market (MM)
% formatted file.
%
% Required arguments:
%
% filename - destination file
%
% A - sparse or full matrix
%
% Optional arguments:
%
% comment - matrix of comments to prepend to
% the MM file. To build a comment matrix,
% use str2mat. For example:
%
% comment = str2mat(' Comment 1' ,...
% ' Comment 2',...
% ' and so on.',...
% ' to attach a date:',...
% [' ',date]);
% If ommitted, a single line date stamp comment
% will be included.
%
% field - 'real'
% 'complex'
% 'integer'
% 'pattern'
% If ommitted, data will determine type.
%
% precision - number of digits to display for real
% or complex values
% If ommitted, full working precision is used.
%
if ( nargin == 5)
precision = 16;
elseif ( nargin == 4)
precision = 16;
elseif ( nargin == 3)
mattype = 'real'; % placeholder, will check after FIND-ing A
precision = 16;
elseif ( nargin == 2)
comment = '';
% Check whether there is an imaginary part:
mattype = 'real'; % placeholder, will check after FIND-ing A
precision = 16;
end
mmfile = fopen([filename],'w');
if ( mmfile == -1 )
error('Cannot open file for output');
end;
[M,N] = size(A);
%%%%%%%%%%%%% This part for sparse matrices %%%%%%%%%%%%%%%%
if ( issparse(A) )
[I,J,V] = find(A);
if ( sum(abs(imag(nonzeros(V)))) > 0 )
Vreal = 0;
else
Vreal = 1;
end
if ( ~ strcmp(mattype,'pattern') & Vreal )
mattype = 'real';
elseif ( ~ strcmp(mattype,'pattern') )
mattype = 'complex';
end
%
% Determine symmetry:
%
if ( M ~= N )
symm = 'general';
issymm = 0;
NZ = length(V);
else
issymm = 1;
NZ = length(V);
for i=1:NZ
if ( A(J(i),I(i)) ~= V(i) )
issymm = 0;
break;
end
end
if ( issymm )
symm = 'symmetric';
ATEMP = tril(A);
[I,J,V] = find(ATEMP);
NZ = nnz(ATEMP);
else
isskew = 1;
for i=1:NZ
if ( A(J(i),I(i)) ~= - V(i) )
isskew = 0;
break;
end
end
if ( isskew )
symm = 'skew-symmetric';
ATEMP = tril(A);
[I,J,V] = find(ATEMP);
NZ = nnz(ATEMP);
elseif ( strcmp(mattype,'complex') )
isherm = 1;
for i=1:NZ
if ( A(J(i),I(i)) ~= conj(V(i)) )
isherm = 0;
break;
end
end
if ( isherm )
symm = 'hermitian';
ATEMP = tril(A);
[I,J,V] = find(ATEMP);
NZ = nnz(ATEMP);
else
symm = 'general';
NZ = nnz(A);
end
else
symm = 'general';
NZ = nnz(A);
end
end
end
% Sparse coordinate format:
rep = 'coordinate';
fprintf(mmfile,'%%%%MatrixMarket matrix %s %s %s\n',rep,mattype,symm);
[MC,NC] = size(comment);
if ( MC == 0 )
fprintf(mmfile,'%% Generated %s\n',[date]);
else
for i=1:MC,
fprintf(mmfile,'%%%s\n',comment(i,:));
end
end
fprintf(mmfile,'%d %d %d\n',M,N,NZ);
cplxformat = sprintf('%%d %%d %% .%dg %% .%dg\n',precision,precision);
realformat = sprintf('%%d %%d %% .%dg\n',precision);
if ( strcmp(mattype,'real') )
for i=1:NZ
fprintf(mmfile,realformat,I(i),J(i),V(i));
end;
elseif ( strcmp(mattype,'complex') )
for i=1:NZ
fprintf(mmfile,cplxformat,I(i),J(i),real(V(i)),imag(V(i)));
end;
elseif ( strcmp(mattype,'pattern') )
for i=1:NZ
fprintf(mmfile,'%d %d\n',I(i),J(i));
end;
else
err = -1;
disp('Unsupported mattype:')
mattype
end;
%%%%%%%%%%%%% This part for dense matrices %%%%%%%%%%%%%%%%
else
if ( sum(abs(imag(nonzeros(A)))) > 0 )
Areal = 0;
else
Areal = 1;
end
if ( ~strcmp(mattype,'pattern') & Areal )
mattype = 'real';
elseif ( ~strcmp(mattype,'pattern') )
mattype = 'complex';
end
%
% Determine symmetry:
%
if ( M ~= N )
issymm = 0;
symm = 'general';
else
issymm = 1;
for j=1:N
for i=j+1:N
if (A(i,j) ~= A(j,i) )
issymm = 0;
break;
end
end
if ( ~ issymm ) break; end
end
if ( issymm )
symm = 'symmetric';
else
isskew = 1;
for j=1:N
for i=j+1:N
if (A(i,j) ~= - A(j,i) )
isskew = 0;
break;
end
end
if ( ~ isskew ) break; end
end
if ( isskew )
symm = 'skew-symmetric';
elseif ( strcmp(mattype,'complex') )
isherm = 1;
for j=1:N
for i=j+1:N
if (A(i,j) ~= conj(A(j,i)) )
isherm = 0;
break;
end
end
if ( ~ isherm ) break; end
end
if ( isherm )
symm = 'hermitian';
else
symm = 'general';
end
else
symm = 'general';
end
end
end
% Dense array format:
rep = 'array';
[MC,NC] = size(comment);
fprintf(mmfile,'%%%%MatrixMarket matrix %s %s %s\n',rep,mattype,symm);
for i=1:MC,
fprintf(mmfile,'%%%s\n',comment(i,:));
end;
fprintf(mmfile,'%d %d\n',M,N);
cplxformat = sprintf('%% .%dg %% .%dg\n', precision,precision);
realformat = sprintf('%% .%dg\n', precision);
if ( ~ strcmp(symm,'general') )
rowloop = 'j';
else
rowloop = '1';
end
if ( strcmp(mattype,'real') )
for j=1:N
for i=eval(rowloop):M
fprintf(mmfile,realformat,A(i,j));
end
end
elseif ( strcmp(mattype,'complex') )
for j=1:N
for i=eval(rowloop):M
fprintf(mmfile,cplxformat,real(A(i,j)),imag(A(i,j)));
end
end
elseif ( strcmp(mattype,'pattern') )
err = -2
disp('Pattern type inconsistant with dense matrix')
else
err = -2
disp('Unknown matrix type:')
mattype
end
end
fclose(mmfile);