~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/math/matrix/mat_io.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)Matrix input/output (various types and shapes)
  2  */
  3 
  4 #include <stdio.h>
  5 #include <tina/sys.h>
  6 #include <tina/math.h>
  7 #include <tina/mathfuncs.h>
  8 
  9 void    imatrix_print(FILE * fp, Matrix * mat);
 10 void    fmatrix_print(FILE * fp, Matrix * mat);
 11 void    dmatrix_print(FILE * fp, Matrix * mat);
 12 void    imatrix_pprint(FILE * fp, Matrix * mat);
 13 void    fmatrix_pprint(FILE * fp, Matrix * mat);
 14 void    dmatrix_pprint(FILE * fp, Matrix * mat);
 15 
 16 void    matrix_print(FILE * fp, Matrix * mat)
 17 {
 18     (void) fprintf(fp, "matrix ");
 19 
 20     if (mat == NULL)
 21     {
 22         (void) fprintf(fp, "nil ");
 23         return;
 24     }
 25     switch (mat->vtype)
 26     {
 27     case int_v:
 28         imatrix_print(fp, mat);
 29         break;
 30     case float_v:
 31         fmatrix_print(fp, mat);
 32         break;
 33     case double_v:
 34         dmatrix_print(fp, mat);
 35         break;
 36     }
 37 }
 38 
 39 void    imatrix_print(FILE * fp, Matrix * mat)
 40 {
 41     int     i, j;
 42     int     m = mat->m;
 43     int     n = mat->n;
 44     int   **el = mat->el.int_v;
 45 
 46     (void) fprintf(fp, "int ");
 47     switch (mat->shape)
 48     {
 49     case matrix_full:
 50         (void) fprintf(fp, "full ");
 51         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
 52         for (i = 0; i < m; ++i)
 53             for (j = 0; j < n; ++j)
 54                 (void) fprintf(fp, "%d ", el[i][j]);
 55         break;
 56     case matrix_lower:
 57         (void) fprintf(fp, "lower ");
 58         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
 59         for (i = 0; i < m; ++i)
 60             for (j = 0; j <= i; ++j)
 61                 (void) fprintf(fp, "%d ", el[i][j]);
 62         break;
 63     case matrix_upper:
 64         (void) fprintf(fp, "upper ");
 65         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
 66         for (i = 0; i < m; ++i)
 67             for (j = i; j < m; ++j)
 68                 (void) fprintf(fp, "%d ", el[i][j]);
 69         break;
 70     case matrix_symmetric:
 71         (void) fprintf(fp, "symmetric ");
 72         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
 73         for (i = 0; i < m; ++i)
 74             for (j = 0; j <= i; ++j)
 75                 (void) fprintf(fp, "%d ", el[i][j]);
 76         break;
 77     default:
 78         {
 79             (void) fprintf(fp, "full ");
 80             (void) fprintf(fp, "%d %d ", mat->m, mat->n);
 81             for (i = 0; i < m; ++i)
 82                 for (j = 0; j < n; ++j)
 83                     (void) fprintf(fp, "%d ", matrix_get(mat, i, j));
 84         }
 85     }
 86 }
 87 
 88 void    fmatrix_print(FILE * fp, Matrix * mat)
 89 {
 90     int     i, j;
 91     int     m = mat->m;
 92     int     n = mat->n;
 93     float **el = mat->el.float_v;
 94 
 95     (void) fprintf(fp, "float ");
 96     switch (mat->shape)
 97     {
 98     case matrix_full:
 99         (void) fprintf(fp, "full ");
100         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
101         for (i = 0; i < m; ++i)
102           for (j = 0; j < n; ++j)
103             (void) fprintf(fp, "%f ", el[i][j]);
104         break;
105     case matrix_lower:
106         (void) fprintf(fp, "lower ");
107         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
108         for (i = 0; i < m; ++i)
109           for (j = 0; j <= i; ++j)
110             (void) fprintf(fp, "%f ", el[i][j]);
111         break;
112     case matrix_upper:
113         (void) fprintf(fp, "upper ");
114         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
115         for (i = 0; i < m; ++i)
116           for (j = i; j < m; ++j)
117             (void) fprintf(fp, "%f ", el[i][j]);
118         break;
119     case matrix_symmetric:
120         (void) fprintf(fp, "symmetric ");
121         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
122         for (i = 0; i < m; ++i)
123           for (j = 0; j <= i; ++j)
124             (void) fprintf(fp, "%f ", el[i][j]);
125         break;
126     default:
127         {
128             (void) fprintf(fp, "full ");
129             (void) fprintf(fp, "%d %d ", mat->m, mat->n);
130             for (i = 0; i < m; ++i)
131               for (j = 0; j < n; ++j)
132                 (void) fprintf(fp, "%f ", matrix_getf(mat, i, j));
133         }
134     }
135 }
136 
137 void    dmatrix_print(FILE * fp, Matrix * mat)
138 {
139     int     i, j;
140     int     m = mat->m;
141     int     n = mat->n;
142     double **el = mat->el.double_v;
143 
144     (void) fprintf(fp, "double ");
145     switch (mat->shape)
146     {
147     case matrix_full:
148         (void) fprintf(fp, "full ");
149         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
150         for (i = 0; i < m; ++i)
151             for (j = 0; j < n; ++j)
152                 (void) fprintf(fp, "%f ", el[i][j]);
153         break;
154     case matrix_lower:
155         (void) fprintf(fp, "lower ");
156         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
157         for (i = 0; i < m; ++i)
158             for (j = 0; j <= i; ++j)
159                 (void) fprintf(fp, "%f ", el[i][j]);
160         break;
161     case matrix_upper:
162         (void) fprintf(fp, "upper ");
163         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
164         for (i = 0; i < m; ++i)
165             for (j = i; j < m; ++j)
166                 (void) fprintf(fp, "%f ", el[i][j]);
167         break;
168     case matrix_symmetric:
169         (void) fprintf(fp, "symmetric ");
170         (void) fprintf(fp, "%d %d ", mat->m, mat->n);
171         for (i = 0; i < m; ++i)
172             for (j = 0; j <= i; ++j)
173                 (void) fprintf(fp, "%f ", el[i][j]);
174         break;
175     default:
176         {
177             (void) fprintf(fp, "full ");
178             (void) fprintf(fp, "%d %d ", mat->m, mat->n);
179             for (i = 0; i < m; ++i)
180                 for (j = 0; j < n; ++j)
181                     (void) fprintf(fp, "%f ", matrix_getf(mat, i, j));
182         }
183     }
184 }
185 
186 void    matrix_pprint(FILE * fp, Matrix * mat)
187 {
188     (void) fprintf(fp, "matrix ");
189 
190     if (mat == NULL)
191     {
192         (void) fprintf(fp, "nil\n");
193         return;
194     }
195     switch (mat->vtype)
196     {
197     case int_v:
198         imatrix_pprint(fp, mat);
199         break;
200     case float_v:
201         fmatrix_pprint(fp, mat);
202         break;
203     case double_v:
204         dmatrix_pprint(fp, mat);
205         break;
206     }
207 }
208 
209 void    imatrix_pprint(FILE * fp, Matrix * mat)
210 {
211     int     i, j;
212     int     m = mat->m;
213     int     n = mat->n;
214     int   **el = mat->el.int_v;
215 
216     (void) fprintf(fp, "int ");
217     switch (mat->shape)
218     {
219     case matrix_full:
220         (void) fprintf(fp, "full ");
221         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
222         for (i = 0; i < m; ++i)
223         {
224             for (j = 0; j < n; ++j)
225                 (void) fprintf(fp, "%10d", el[i][j]);
226             (void) fprintf(fp, "\n");
227         }
228         break;
229     case matrix_lower:
230         (void) fprintf(fp, "lower ");
231         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
232         for (i = 0; i < m; ++i)
233         {
234             for (j = 0; j <= i; ++j)
235                 (void) fprintf(fp, "%10d", el[i][j]);
236             (void) fprintf(fp, "\n");
237         }
238         break;
239     case matrix_upper:
240         (void) fprintf(fp, "upper ");
241         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
242         for (i = 0; i < m; ++i)
243         {
244             for (j = 0; j < i; ++j)
245                 (void) fprintf(fp, "          ");
246             for (j = i; j < m; ++j)
247                 (void) fprintf(fp, "%10d", el[i][j]);
248             (void) fprintf(fp, "\n");
249         }
250         break;
251     case matrix_symmetric:
252         (void) fprintf(fp, "symmetric ");
253         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
254         for (i = 0; i < m; ++i)
255         {
256             for (j = 0; j <= i; ++j)
257                 (void) fprintf(fp, "%10d", el[i][j]);
258             (void) fprintf(fp, "\n");
259         }
260         break;
261     default:
262         (void) fprintf(fp, "full ");
263         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
264         for (i = 0; i < m; ++i)
265         {
266             for (j = 0; j < n; ++j)
267                 (void) fprintf(fp, "%10d", matrix_get(mat, i, j));
268             (void) fprintf(fp, "\n");
269         }
270     }
271     (void) fprintf(fp, "\n");
272 }
273 
274 void    fmatrix_pprint(FILE * fp, Matrix * mat)
275 {
276     int     i, j;
277     int     m = mat->m;
278     int     n = mat->n;
279     float **el = mat->el.float_v;
280 
281     (void) fprintf(fp, "float ");
282     switch (mat->shape)
283     {
284     case matrix_full:
285         (void) fprintf(fp, "full ");
286         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
287         for (i = 0; i < m; ++i)
288         {
289             for (j = 0; j < n; ++j)
290                 (void) fprintf(fp, "%15.6f", el[i][j]);
291             (void) fprintf(fp, "\n");
292         }
293         break;
294     case matrix_lower:
295         (void) fprintf(fp, "lower ");
296         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
297         for (i = 0; i < m; ++i)
298         {
299             for (j = 0; j <= i; ++j)
300                 (void) fprintf(fp, "%15.6f", el[i][j]);
301             (void) fprintf(fp, "\n");
302         }
303         break;
304     case matrix_upper:
305         (void) fprintf(fp, "upper ");
306         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
307         for (i = 0; i < m; ++i)
308         {
309             for (j = 0; j < i; ++j)
310                 (void) fprintf(fp, "               ");
311             for (j = i; j < m; ++j)
312                 (void) fprintf(fp, "%15.6f", el[i][j]);
313             (void) fprintf(fp, "\n");
314         }
315         break;
316     case matrix_symmetric:
317         (void) fprintf(fp, "symmetric ");
318         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
319         for (i = 0; i < m; ++i)
320         {
321             for (j = 0; j <= i; ++j)
322                 (void) fprintf(fp, "%15.6f", el[i][j]);
323             (void) fprintf(fp, "\n");
324         }
325         break;
326     default:
327         (void) fprintf(fp, "full ");
328         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
329         for (i = 0; i < m; ++i)
330         {
331             for (j = 0; j < n; ++j)
332                 (void) fprintf(fp, "%15.6f", matrix_getf(mat, i, j));
333             (void) fprintf(fp, "\n");
334         }
335     }
336     (void) fprintf(fp, "\n");
337 }
338 
339 void    dmatrix_pprint(FILE * fp, Matrix * mat)
340 {
341     int     i, j;
342     int     m = mat->m;
343     int     n = mat->n;
344     double **el = mat->el.double_v;
345 
346     (void) fprintf(fp, "double ");
347     switch (mat->shape)
348     {
349     case matrix_full:
350         (void) fprintf(fp, "full ");
351         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
352         for (i = 0; i < m; ++i)
353         {
354             for (j = 0; j < n; ++j)
355                 (void) fprintf(fp, "%15.6f", el[i][j]);
356             (void) fprintf(fp, "\n");
357         }
358         break;
359     case matrix_lower:
360         (void) fprintf(fp, "lower ");
361         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
362         for (i = 0; i < m; ++i)
363         {
364             for (j = 0; j <= i; ++j)
365                 (void) fprintf(fp, "%15.6f", el[i][j]);
366             (void) fprintf(fp, "\n");
367         }
368         break;
369     case matrix_upper:
370         (void) fprintf(fp, "upper ");
371         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
372         for (i = 0; i < m; ++i)
373         {
374             for (j = 0; j < i; ++j)
375                 (void) fprintf(fp, "               ");
376             for (j = i; j < m; ++j)
377                 (void) fprintf(fp, "%15.6f", el[i][j]);
378             (void) fprintf(fp, "\n");
379         }
380         break;
381     case matrix_symmetric:
382         (void) fprintf(fp, "symmetric ");
383         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
384         for (i = 0; i < m; ++i)
385         {
386             for (j = 0; j <= i; ++j)
387                 (void) fprintf(fp, "%15.6f", el[i][j]);
388             (void) fprintf(fp, "\n");
389         }
390         break;
391     default:
392         (void) fprintf(fp, "full ");
393         (void) fprintf(fp, "%d %d\n", mat->m, mat->n);
394         for (i = 0; i < m; ++i)
395         {
396             for (j = 0; j < n; ++j)
397                 (void) fprintf(fp, "%15.6f", matrix_getf(mat, i, j));
398             (void) fprintf(fp, "\n");
399         }
400     }
401     (void) fprintf(fp, "\n");
402 }
403 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.