Debian Codegraph @

packages/muelu/research/max/MatrixMarket_routines/mmwrite.m

raw
1 function [ err ] = mmwrite(filename,A,comment,field,precision)
2 %
3 % Function: mmwrite(filename,A,comment,field,precision)
4 %
5 % Writes the sparse or dense matrix A to a Matrix Market (MM)
6 % formatted file.
7 %
8 % Required arguments:
9 %
10 % filename - destination file
11 %
12 % A - sparse or full matrix
13 %
14 % Optional arguments:
15 %
16 % comment - matrix of comments to prepend to
17 % the MM file. To build a comment matrix,
18 % use str2mat. For example:
19 %
20 % comment = str2mat(' Comment 1' ,...
21 % ' Comment 2',...
22 % ' and so on.',...
23 % ' to attach a date:',...
24 % [' ',date]);
25 % If ommitted, a single line date stamp comment
26 % will be included.
27 %
28 % field - 'real'
29 % 'complex'
30 % 'integer'
31 % 'pattern'
32 % If ommitted, data will determine type.
33 %
34 % precision - number of digits to display for real
35 % or complex values
36 % If ommitted, full working precision is used.
37 %
38
39 if ( nargin == 5)
40 precision = 16;
41 elseif ( nargin == 4)
42 precision = 16;
43 elseif ( nargin == 3)
44 mattype = 'real'; % placeholder, will check after FIND-ing A
45 precision = 16;
46 elseif ( nargin == 2)
47 comment = '';
48 % Check whether there is an imaginary part:
49 mattype = 'real'; % placeholder, will check after FIND-ing A
50 precision = 16;
51 end
52
53 mmfile = fopen([filename],'w');
54 if ( mmfile == -1 )
55 error('Cannot open file for output');
56 end;
57
58
59 [M,N] = size(A);
60
61 %%%%%%%%%%%%% This part for sparse matrices %%%%%%%%%%%%%%%%
62 if ( issparse(A) )
63
64 [I,J,V] = find(A);
65 if ( sum(abs(imag(nonzeros(V)))) > 0 )
66 Vreal = 0;
67 else
68 Vreal = 1;
69 end
70
71 if ( ~ strcmp(mattype,'pattern') & Vreal )
72 mattype = 'real';
73 elseif ( ~ strcmp(mattype,'pattern') )
74 mattype = 'complex';
75 end
76 %
77 % Determine symmetry:
78 %
79 if ( M ~= N )
80 symm = 'general';
81 issymm = 0;
82 NZ = length(V);
83 else
84 issymm = 1;
85 NZ = length(V);
86 for i=1:NZ
87 if ( A(J(i),I(i)) ~= V(i) )
88 issymm = 0;
89 break;
90 end
91 end
92 if ( issymm )
93 symm = 'symmetric';
94 ATEMP = tril(A);
95 [I,J,V] = find(ATEMP);
96 NZ = nnz(ATEMP);
97 else
98 isskew = 1;
99 for i=1:NZ
100 if ( A(J(i),I(i)) ~= - V(i) )
101 isskew = 0;
102 break;
103 end
104 end
105 if ( isskew )
106 symm = 'skew-symmetric';
107 ATEMP = tril(A);
108 [I,J,V] = find(ATEMP);
109 NZ = nnz(ATEMP);
110 elseif ( strcmp(mattype,'complex') )
111 isherm = 1;
112 for i=1:NZ
113 if ( A(J(i),I(i)) ~= conj(V(i)) )
114 isherm = 0;
115 break;
116 end
117 end
118 if ( isherm )
119 symm = 'hermitian';
120 ATEMP = tril(A);
121 [I,J,V] = find(ATEMP);
122 NZ = nnz(ATEMP);
123 else
124 symm = 'general';
125 NZ = nnz(A);
126 end
127 else
128 symm = 'general';
129 NZ = nnz(A);
130 end
131 end
132 end
133
134 % Sparse coordinate format:
135
136 rep = 'coordinate';
137
138
139 fprintf(mmfile,'%%%%MatrixMarket matrix %s %s %s\n',rep,mattype,symm);
140 [MC,NC] = size(comment);
141 if ( MC == 0 )
142 fprintf(mmfile,'%% Generated %s\n',[date]);
143 else
144 for i=1:MC,
145 fprintf(mmfile,'%%%s\n',comment(i,:));
146 end
147 end
148 fprintf(mmfile,'%d %d %d\n',M,N,NZ);
149 cplxformat = sprintf('%%d %%d %% .%dg %% .%dg\n',precision,precision);
150 realformat = sprintf('%%d %%d %% .%dg\n',precision);
151 if ( strcmp(mattype,'real') )
152 for i=1:NZ
153 fprintf(mmfile,realformat,I(i),J(i),V(i));
154 end;
155 elseif ( strcmp(mattype,'complex') )
156 for i=1:NZ
157 fprintf(mmfile,cplxformat,I(i),J(i),real(V(i)),imag(V(i)));
158 end;
159 elseif ( strcmp(mattype,'pattern') )
160 for i=1:NZ
161 fprintf(mmfile,'%d %d\n',I(i),J(i));
162 end;
163 else
164 err = -1;
165 disp('Unsupported mattype:')
166 mattype
167 end;
168
169 %%%%%%%%%%%%% This part for dense matrices %%%%%%%%%%%%%%%%
170 else
171 if ( sum(abs(imag(nonzeros(A)))) > 0 )
172 Areal = 0;
173 else
174 Areal = 1;
175 end
176 if ( ~strcmp(mattype,'pattern') & Areal )
177 mattype = 'real';
178 elseif ( ~strcmp(mattype,'pattern') )
179 mattype = 'complex';
180 end
181 %
182 % Determine symmetry:
183 %
184 if ( M ~= N )
185 issymm = 0;
186 symm = 'general';
187 else
188 issymm = 1;
189 for j=1:N
190 for i=j+1:N
191 if (A(i,j) ~= A(j,i) )
192 issymm = 0;
193 break;
194 end
195 end
196 if ( ~ issymm ) break; end
197
198 end
199 if ( issymm )
200 symm = 'symmetric';
201 else
202 isskew = 1;
203 for j=1:N
204 for i=j+1:N
205 if (A(i,j) ~= - A(j,i) )
206 isskew = 0;
207 break;
208 end
209 end
210 if ( ~ isskew ) break; end
211 end
212 if ( isskew )
213 symm = 'skew-symmetric';
214 elseif ( strcmp(mattype,'complex') )
215 isherm = 1;
216 for j=1:N
217 for i=j+1:N
218 if (A(i,j) ~= conj(A(j,i)) )
219 isherm = 0;
220 break;
221 end
222 end
223 if ( ~ isherm ) break; end
224 end
225 if ( isherm )
226 symm = 'hermitian';
227 else
228 symm = 'general';
229 end
230 else
231 symm = 'general';
232 end
233 end
234 end
235
236 % Dense array format:
237
238 rep = 'array';
239 [MC,NC] = size(comment);
240 fprintf(mmfile,'%%%%MatrixMarket matrix %s %s %s\n',rep,mattype,symm);
241 for i=1:MC,
242 fprintf(mmfile,'%%%s\n',comment(i,:));
243 end;
244 fprintf(mmfile,'%d %d\n',M,N);
245 cplxformat = sprintf('%% .%dg %% .%dg\n', precision,precision);
246 realformat = sprintf('%% .%dg\n', precision);
247 if ( ~ strcmp(symm,'general') )
248 rowloop = 'j';
249 else
250 rowloop = '1';
251 end
252 if ( strcmp(mattype,'real') )
253 for j=1:N
254 for i=eval(rowloop):M
255 fprintf(mmfile,realformat,A(i,j));
256 end
257 end
258 elseif ( strcmp(mattype,'complex') )
259 for j=1:N
260 for i=eval(rowloop):M
261 fprintf(mmfile,cplxformat,real(A(i,j)),imag(A(i,j)));
262 end
263 end
264 elseif ( strcmp(mattype,'pattern') )
265 err = -2
266 disp('Pattern type inconsistent with dense matrix')
267 else
268 err = -2
269 disp('Unknown matrix type:')
270 mattype
271 end
272 end
273
274 fclose(mmfile);