Debian Codegraph @

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

raw
1 function [ err ] = mmwriteNonOptimized(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
80 symm = 'general';
81 issymm = 0;
82 NZ = length(V);
83
84 % Sparse coordinate format:
85
86 rep = 'coordinate';
87
88
89 fprintf(mmfile,'%%%%MatrixMarket matrix %s %s %s\n',rep,mattype,symm);
90 % [MC,NC] = size(comment);
91 % if ( MC == 0 )
92 % fprintf(mmfile,'%% Generated %s\n',[date]);
93 % else
94 % for i=1:MC,
95 % fprintf(mmfile,'%%%s\n',comment(i,:));
96 % end
97 % end
98 fprintf(mmfile,'%d %d %d\n',M,N,NZ);
99 cplxformat = sprintf('%%d %%d %% .%dg %% .%dg\n',precision,precision);
100 realformat = sprintf('%%d %%d %% .%dg\n',precision);
101 if ( strcmp(mattype,'real') )
102 for i=1:NZ
103 fprintf(mmfile,realformat,I(i),J(i),V(i));
104 end;
105 elseif ( strcmp(mattype,'complex') )
106 for i=1:NZ
107 fprintf(mmfile,cplxformat,I(i),J(i),real(V(i)),imag(V(i)));
108 end;
109 elseif ( strcmp(mattype,'pattern') )
110 for i=1:NZ
111 fprintf(mmfile,'%d %d\n',I(i),J(i));
112 end;
113 else
114 err = -1;
115 disp('Unsupported mattype:')
116 mattype
117 end;
118
119 %%%%%%%%%%%%% This part for dense matrices %%%%%%%%%%%%%%%%
120 else
121 if ( sum(abs(imag(nonzeros(A)))) > 0 )
122 Areal = 0;
123 else
124 Areal = 1;
125 end
126 if ( ~strcmp(mattype,'pattern') & Areal )
127 mattype = 'real';
128 elseif ( ~strcmp(mattype,'pattern') )
129 mattype = 'complex';
130 end
131 %
132 % Determine symmetry:
133 %
134
135 issymm = 0;
136 symm = 'general';
137
138
139 % Dense array format:
140
141 rep = 'array';
142 [MC,NC] = size(comment);
143 fprintf(mmfile,'%%%%MatrixMarket matrix %s %s %s\n',rep,mattype,symm);
144 for i=1:MC,
145 fprintf(mmfile,'%%%s\n',comment(i,:));
146 end;
147 fprintf(mmfile,'%d %d\n',M,N);
148 cplxformat = sprintf('%% .%dg %% .%dg\n', precision,precision);
149 realformat = sprintf('%% .%dg\n', precision);
150 if ( ~ strcmp(symm,'general') )
151 rowloop = 'j';
152 else
153 rowloop = '1';
154 end
155 if ( strcmp(mattype,'real') )
156 for j=1:N
157 for i=eval(rowloop):M
158 fprintf(mmfile,realformat,A(i,j));
159 end
160 end
161 elseif ( strcmp(mattype,'complex') )
162 for j=1:N
163 for i=eval(rowloop):M
164 fprintf(mmfile,cplxformat,real(A(i,j)),imag(A(i,j)));
165 end
166 end
167 elseif ( strcmp(mattype,'pattern') )
168 err = -2
169 disp('Pattern type inconsistent with dense matrix')
170 else
171 err = -2
172 disp('Unknown matrix type:')
173 mattype
174 end
175 end
176
177 fclose(mmfile);