JWS C Library
C language utility library
matblock_io.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <jwsc/config.h>
00047 
00048 #include <stdlib.h>
00049 #include <stdio.h>
00050 #include <string.h>
00051 #include <errno.h>
00052 #include <inttypes.h>
00053 #include <assert.h>
00054 #include <ctype.h>
00055 
00056 #include "jwsc/base/error.h"
00057 #include "jwsc/base/file_io.h"
00058 #include "jwsc/math/complex.h"
00059 #include "jwsc/matblock/matblock.h"
00060 #include "jwsc/matblock/matblock_io.h"
00061 
00062 
00094 Error* read_matblock_with_header_u32(Matblock_u32** m_out, const char* fname)
00095 {
00096     FILE*  fp;
00097     Error* e;
00098 
00099     if ((fp = fopen(fname, "r")) == NULL)
00100     {
00101         free_matblock_u32(*m_out); *m_out = NULL;
00102         return JWSC_EIO(strerror(errno));
00103     }
00104 
00105     if ((e = read_matblock_with_header_fp_u32(m_out, fp)) != NULL)
00106     {
00107         fclose(fp);
00108         return e;
00109     }
00110 
00111     if (fclose(fp) != 0)
00112     {
00113         free_matblock_u32(*m_out); *m_out = NULL;
00114         return JWSC_EIO(strerror(errno));
00115     }
00116 
00117     return NULL;
00118 }
00119 
00144 Error* read_matblock_with_header_i32(Matblock_i32** m_out, const char* fname)
00145 {
00146     FILE*  fp;
00147     Error* e;
00148 
00149     if ((fp = fopen(fname, "r")) == NULL)
00150     {
00151         free_matblock_i32(*m_out); *m_out = NULL;
00152         return JWSC_EIO(strerror(errno));
00153     }
00154 
00155     if ((e = read_matblock_with_header_fp_i32(m_out, fp)) != NULL)
00156     {
00157         fclose(fp);
00158         return e;
00159     }
00160 
00161     if (fclose(fp) != 0)
00162     {
00163         free_matblock_i32(*m_out); *m_out = NULL;
00164         return JWSC_EIO(strerror(errno));
00165     }
00166 
00167     return NULL;
00168 }
00169 
00194 Error* read_matblock_with_header_i64(Matblock_i64** m_out, const char* fname)
00195 {
00196     FILE*  fp;
00197     Error* e;
00198 
00199     if ((fp = fopen(fname, "r")) == NULL)
00200     {
00201         free_matblock_i64(*m_out); *m_out = NULL;
00202         return JWSC_EIO(strerror(errno));
00203     }
00204 
00205     if ((e = read_matblock_with_header_fp_i64(m_out, fp)) != NULL)
00206     {
00207         fclose(fp);
00208         return e;
00209     }
00210 
00211     if (fclose(fp) != 0)
00212     {
00213         free_matblock_i64(*m_out); *m_out = NULL;
00214         return JWSC_EIO(strerror(errno));
00215     }
00216 
00217     return NULL;
00218 }
00219 
00244 Error* read_matblock_with_header_f(Matblock_f** m_out, const char* fname)
00245 {
00246     FILE*  fp;
00247     Error* e;
00248 
00249     if ((fp = fopen(fname, "r")) == NULL)
00250     {
00251         free_matblock_f(*m_out); *m_out = NULL;
00252         return JWSC_EIO(strerror(errno));
00253     }
00254 
00255     if ((e = read_matblock_with_header_fp_f(m_out, fp)) != NULL)
00256     {
00257         fclose(fp);
00258         return e;
00259     }
00260 
00261     if (fclose(fp) != 0)
00262     {
00263         free_matblock_f(*m_out); *m_out = NULL;
00264         return JWSC_EIO(strerror(errno));
00265     }
00266 
00267     return NULL;
00268 }
00269 
00294 Error* read_matblock_with_header_d(Matblock_d** m_out, const char* fname)
00295 {
00296     FILE*  fp;
00297     Error* e;
00298 
00299     if ((fp = fopen(fname, "r")) == NULL)
00300     {
00301         free_matblock_d(*m_out); *m_out = NULL;
00302         return JWSC_EIO(strerror(errno));
00303     }
00304 
00305     if ((e = read_matblock_with_header_fp_d(m_out, fp)) != NULL)
00306     {
00307         fclose(fp);
00308         return e;
00309     }
00310 
00311     if (fclose(fp) != 0)
00312     {
00313         free_matblock_d(*m_out); *m_out = NULL;
00314         return JWSC_EIO(strerror(errno));
00315     }
00316 
00317     return NULL;
00318 }
00319 
00344 Error* read_matblock_with_header_cf(Matblock_cf** m_out, const char* fname)
00345 {
00346     FILE*  fp;
00347     Error* e;
00348 
00349     if ((fp = fopen(fname, "r")) == NULL)
00350     {
00351         free_matblock_cf(*m_out); *m_out = NULL;
00352         return JWSC_EIO(strerror(errno));
00353     }
00354 
00355     if ((e = read_matblock_with_header_fp_cf(m_out, fp)) != NULL)
00356     {
00357         fclose(fp);
00358         return e;
00359     }
00360 
00361     if (fclose(fp) != 0)
00362     {
00363         free_matblock_cf(*m_out); *m_out = NULL;
00364         return JWSC_EIO(strerror(errno));
00365     }
00366 
00367     return NULL;
00368 }
00369 
00394 Error* read_matblock_with_header_cd(Matblock_cd** m_out, const char* fname)
00395 {
00396     FILE*  fp;
00397     Error* e;
00398 
00399     if ((fp = fopen(fname, "r")) == NULL)
00400     {
00401         free_matblock_cd(*m_out); *m_out = NULL;
00402         return JWSC_EIO(strerror(errno));
00403     }
00404 
00405     if ((e = read_matblock_with_header_fp_cd(m_out, fp)) != NULL)
00406     {
00407         fclose(fp);
00408         return e;
00409     }
00410 
00411     if (fclose(fp) != 0)
00412     {
00413         free_matblock_cd(*m_out); *m_out = NULL;
00414         return JWSC_EIO(strerror(errno));
00415     }
00416 
00417     return NULL;
00418 }
00419 
00456 Error* read_matblock_with_header_fp_u32(Matblock_u32** m_out, FILE* fp)
00457 {
00458     uint32_t num_mats, num_rows, num_cols;
00459     uint32_t mat, row, col;
00460     Matblock_u32* m;
00461 
00462     if (skip_fp_spaces_and_comments(fp) == 0)
00463     {
00464         free_matblock_u32(*m_out); *m_out = NULL;
00465         return JWSC_EIO("No matblock to read in file");
00466     }
00467 
00468     if (fscanf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", &num_mats,
00469                 &num_rows, &num_cols) != 3)
00470     {
00471         free_matblock_u32(*m_out); *m_out = NULL;
00472         return JWSC_EIO("File to read matblock from not formatted properly");
00473     }
00474 
00475     create_matblock_u32(m_out, num_mats, num_rows, num_cols);
00476     m = *m_out;
00477 
00478     for (mat = 0; mat < num_mats; mat++)
00479     {
00480         for (row = 0; row < num_rows; row++)
00481         {
00482             for (col = 0; col < num_cols; col++)
00483             {
00484                 if (fscanf(fp, "%u", &(m->elts[ mat ][ row ][ col ])) != 1)
00485                 {
00486                     free_matblock_u32(*m_out); *m_out = NULL;
00487                     return JWSC_EIO("File to read matblock from not formatted properly");
00488                 }
00489             }
00490         }
00491     }
00492 
00493     return NULL;
00494 }
00495 
00520 Error* read_matblock_with_header_fp_i32(Matblock_i32** m_out, FILE* fp)
00521 {
00522     uint32_t num_mats, num_rows, num_cols;
00523     uint32_t mat, row, col;
00524     Matblock_i32* m;
00525 
00526     if (skip_fp_spaces_and_comments(fp) == 0)
00527     {
00528         free_matblock_i32(*m_out); *m_out = NULL;
00529         return JWSC_EIO("No matblock to read in file");
00530     }
00531 
00532     if (fscanf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", &num_mats,
00533                 &num_rows, &num_cols) != 3)
00534     {
00535         free_matblock_i32(*m_out); *m_out = NULL;
00536         return JWSC_EIO("File to read matblock from not formatted properly");
00537     }
00538 
00539     create_matblock_i32(m_out, num_mats, num_rows, num_cols);
00540     m = *m_out;
00541 
00542     for (mat = 0; mat < num_mats; mat++)
00543     {
00544         for (row = 0; row < num_rows; row++)
00545         {
00546             for (col = 0; col < num_cols; col++)
00547             {
00548                 if (fscanf(fp, "%d", &(m->elts[ mat ][ row ][ col ])) != 1)
00549                 {
00550                     free_matblock_i32(*m_out); *m_out = NULL;
00551                     return JWSC_EIO("File to read matblock from not formatted properly");
00552                 }
00553             }
00554         }
00555     }
00556 
00557     return NULL;
00558 }
00559 
00584 Error* read_matblock_with_header_fp_i64(Matblock_i64** m_out, FILE* fp)
00585 {
00586     uint32_t num_mats, num_rows, num_cols;
00587     uint32_t mat, row, col;
00588     Matblock_i64* m;
00589 
00590     if (skip_fp_spaces_and_comments(fp) == 0)
00591     {
00592         free_matblock_i64(*m_out); *m_out = NULL;
00593         return JWSC_EIO("No matblock to read in file");
00594     }
00595 
00596     if (fscanf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", &num_mats,
00597                 &num_rows, &num_cols) != 3)
00598     {
00599         free_matblock_i64(*m_out); *m_out = NULL;
00600         return JWSC_EIO("File to read matblock from not formatted properly");
00601     }
00602 
00603     create_matblock_i64(m_out, num_mats, num_rows, num_cols);
00604     m = *m_out;
00605 
00606     for (mat = 0; mat < num_mats; mat++)
00607     {
00608         for (row = 0; row < num_rows; row++)
00609         {
00610             for (col = 0; col < num_cols; col++)
00611             {
00612                 if (fscanf(fp, "%" SCNd64, &(m->elts[ mat ][ row ][ col ])) != 1)
00613                 {
00614                     free_matblock_i64(*m_out); *m_out = NULL;
00615                     return JWSC_EIO("File to read matblock from not formatted properly");
00616                 }
00617             }
00618         }
00619     }
00620 
00621     return NULL;
00622 }
00623 
00648 Error* read_matblock_with_header_fp_f(Matblock_f** m_out, FILE* fp)
00649 {
00650     uint32_t num_mats, num_rows, num_cols;
00651     uint32_t mat, row, col;
00652     Matblock_f* m;
00653 
00654     if (skip_fp_spaces_and_comments(fp) == 0)
00655     {
00656         free_matblock_f(*m_out); *m_out = NULL;
00657         return JWSC_EIO("No matblock to read in file");
00658     }
00659 
00660     if (fscanf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", &num_mats,
00661                 &num_rows, &num_cols) != 3)
00662     {
00663         free_matblock_f(*m_out); *m_out = NULL;
00664         return JWSC_EIO("File to read matblock from not formatted properly");
00665     }
00666 
00667     create_matblock_f(m_out, num_mats, num_rows, num_cols);
00668     m = *m_out;
00669 
00670     for (mat = 0; mat < num_mats; mat++)
00671     {
00672         for (row = 0; row < num_rows; row++)
00673         {
00674             for (col = 0; col < num_cols; col++)
00675             {
00676                 if (fscanf(fp, "%f", &(m->elts[ mat ][ row ][ col ])) != 1)
00677                 {
00678                     free_matblock_f(*m_out); *m_out = NULL;
00679                     return JWSC_EIO("File to read matblock from not formatted properly");
00680                 }
00681             }
00682         }
00683     }
00684 
00685     return NULL;
00686 }
00687 
00712 Error* read_matblock_with_header_fp_d(Matblock_d** m_out, FILE* fp)
00713 {
00714     uint32_t num_mats, num_rows, num_cols;
00715     uint32_t mat, row, col;
00716     Matblock_d* m;
00717 
00718     if (skip_fp_spaces_and_comments(fp) == 0)
00719     {
00720         free_matblock_d(*m_out); *m_out = NULL;
00721         return JWSC_EIO("No matblock to read in file");
00722     }
00723 
00724     if (fscanf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", &num_mats,
00725                 &num_rows, &num_cols) != 3)
00726     {
00727         free_matblock_d(*m_out); *m_out = NULL;
00728         return JWSC_EIO("File to read matblock from not formatted properly");
00729     }
00730 
00731     create_matblock_d(m_out, num_mats, num_rows, num_cols);
00732     m = *m_out;
00733 
00734     for (mat = 0; mat < num_mats; mat++)
00735     {
00736         for (row = 0; row < num_rows; row++)
00737         {
00738             for (col = 0; col < num_cols; col++)
00739             {
00740                 if (fscanf(fp, "%lf", &(m->elts[ mat ][ row ][ col ])) != 1)
00741                 {
00742                     free_matblock_d(*m_out); *m_out = NULL;
00743                     return JWSC_EIO("File to read matblock from not formatted properly");
00744                 }
00745             }
00746         }
00747     }
00748 
00749     return NULL;
00750 }
00751 
00776 Error* read_matblock_with_header_fp_cf(Matblock_cf** m_out, FILE* fp)
00777 {
00778     uint32_t num_mats, num_rows, num_cols;
00779     uint32_t mat, row, col;
00780     Matblock_cf* m;
00781 
00782     if (skip_fp_spaces_and_comments(fp) == 0)
00783     {
00784         free_matblock_cf(*m_out); *m_out = NULL;
00785         return JWSC_EIO("No matblock to read in file");
00786     }
00787 
00788     if (fscanf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", &num_mats,
00789                 &num_rows, &num_cols) != 3)
00790     {
00791         free_matblock_cf(*m_out); *m_out = NULL;
00792         return JWSC_EIO("File to read matblock from not formatted properly");
00793     }
00794 
00795     create_matblock_cf(m_out, num_mats, num_rows, num_cols);
00796     m = *m_out;
00797 
00798     for (mat = 0; mat < num_mats; mat++)
00799     {
00800         for (row = 0; row < num_rows; row++)
00801         {
00802             for (col = 0; col < num_cols; col++)
00803             {
00804                 if (fscanf(fp, "%f %f", &(m->elts[ mat ][ row ][ col ].r), 
00805                             &(m->elts[ mat ][ row ][ col ].i)) != 3)
00806                 {
00807                     free_matblock_cf(*m_out); *m_out = NULL;
00808                     return JWSC_EIO("File to read matblock from not formatted properly");
00809                 }
00810             }
00811         }
00812     }
00813 
00814     return NULL;
00815 }
00816 
00841 Error* read_matblock_with_header_fp_cd(Matblock_cd** m_out, FILE* fp)
00842 {
00843     uint32_t num_mats, num_rows, num_cols;
00844     uint32_t mat, row, col;
00845     Matblock_cd* m;
00846 
00847     if (skip_fp_spaces_and_comments(fp) == 0)
00848     {
00849         free_matblock_cd(*m_out); *m_out = NULL;
00850         return JWSC_EIO("No matblock to read in file");
00851     }
00852 
00853     if (fscanf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", &num_mats,
00854                 &num_rows, &num_cols) != 3)
00855     {
00856         free_matblock_cd(*m_out); *m_out = NULL;
00857         return JWSC_EIO("File to read matblock from not formatted properly");
00858     }
00859 
00860     create_matblock_cd(m_out, num_mats, num_rows, num_cols);
00861     m = *m_out;
00862 
00863     for (mat = 0; mat < num_mats; mat++)
00864     {
00865         for (row = 0; row < num_rows; row++)
00866         {
00867             for (col = 0; col < num_cols; col++)
00868             {
00869                 if (fscanf(fp, "%lf %lf", &(m->elts[ mat ][ row ][ col ].r),
00870                             &(m->elts[ mat ][ row ][ col ].i)) == 2)
00871                 {
00872                     free_matblock_cd(*m_out); *m_out = NULL;
00873                     return JWSC_EIO("File to read matblock from not formatted properly");
00874                 }
00875             }
00876         }
00877     }
00878 
00879     return NULL;
00880 }
00881 
00912 Error* write_matblock_with_header_u32(const Matblock_u32* m, const char* fname)
00913 {
00914     FILE* fp;
00915 
00916     if ((fp = fopen(fname, "w")) == NULL)
00917     {
00918         return JWSC_EIO(strerror(errno));
00919     }
00920 
00921     write_matblock_with_header_fp_u32(m, fp);
00922 
00923     if (fclose(fp) != 0)
00924     {
00925         return JWSC_EIO(strerror(errno));
00926     }
00927 
00928     return NULL;
00929 }
00930 
00949 Error* write_matblock_with_header_i32(const Matblock_i32* m, const char* fname)
00950 {
00951     FILE* fp;
00952 
00953     if ((fp = fopen(fname, "w")) == NULL)
00954     {
00955         return JWSC_EIO(strerror(errno));
00956     }
00957 
00958     write_matblock_with_header_fp_i32(m, fp);
00959 
00960     if (fclose(fp) != 0)
00961     { return JWSC_EIO(strerror(errno));
00962     }
00963 
00964     return NULL;
00965 }
00966 
00985 Error* write_matblock_with_header_i64(const Matblock_i64* m, const char* fname)
00986 {
00987     FILE* fp;
00988 
00989     if ((fp = fopen(fname, "w")) == NULL)
00990     {
00991         return JWSC_EIO(strerror(errno));
00992     }
00993 
00994     write_matblock_with_header_fp_i64(m, fp);
00995 
00996     if (fclose(fp) != 0)
00997     {
00998         return JWSC_EIO(strerror(errno));
00999     }
01000 
01001     return NULL;
01002 }
01003 
01022 Error* write_matblock_with_header_f(const Matblock_f* m, const char* fname)
01023 {
01024     FILE* fp;
01025 
01026     if ((fp = fopen(fname, "w")) == NULL)
01027     {
01028         return JWSC_EIO(strerror(errno));
01029     }
01030 
01031     write_matblock_with_header_fp_f(m, fp);
01032 
01033     if (fclose(fp) != 0)
01034     {
01035         return JWSC_EIO(strerror(errno));
01036     }
01037 
01038     return NULL;
01039 }
01040 
01059 Error* write_matblock_with_header_d(const Matblock_d* m, const char* fname)
01060 {
01061     FILE* fp;
01062 
01063     if ((fp = fopen(fname, "w")) == NULL)
01064     {
01065         return JWSC_EIO(strerror(errno));
01066     }
01067 
01068     write_matblock_with_header_fp_d(m, fp);
01069 
01070     if (fclose(fp) != 0)
01071     {
01072         return JWSC_EIO(strerror(errno));
01073     }
01074 
01075     return NULL;
01076 }
01077 
01096 Error* write_matblock_with_header_cf(const Matblock_cf* m, const char* fname)
01097 {
01098     FILE* fp;
01099 
01100     if ((fp = fopen(fname, "w")) == NULL)
01101     {
01102         return JWSC_EIO(strerror(errno));
01103     }
01104 
01105     write_matblock_with_header_fp_cf(m, fp);
01106 
01107     if (fclose(fp) != 0)
01108     {
01109         return JWSC_EIO(strerror(errno));
01110     }
01111 
01112     return NULL;
01113 }
01114 
01133 Error* write_matblock_with_header_cd(const Matblock_cd* m, const char* fname)
01134 {
01135     FILE* fp;
01136 
01137     if ((fp = fopen(fname, "w")) == NULL)
01138     {
01139         return JWSC_EIO(strerror(errno));
01140     }
01141 
01142     write_matblock_with_header_fp_cd(m, fp);
01143 
01144     if (fclose(fp) != 0)
01145     {
01146         return JWSC_EIO(strerror(errno));
01147     }
01148 
01149     return NULL;
01150 }
01151 
01182 Error* write_matblock_with_header_fp_u32(const Matblock_u32* m, FILE* fp)
01183 {
01184     uint32_t num_mats, num_rows, num_cols;
01185     uint32_t mat, row, col;
01186 
01187     num_mats = m->num_mats;
01188     num_rows = m->num_rows;
01189     num_cols = m->num_cols;
01190     fprintf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", num_mats, num_rows,
01191             num_cols);
01192 
01193     for (mat = 0; mat < num_mats; mat++)
01194     {
01195         for (row = 0; row < num_rows; row++)
01196         {
01197             for (col = 0; col < num_cols; col++)
01198             {
01199                 fprintf(fp, "%u ", m->elts[ mat ][ row ][ col ]);
01200             }
01201             fprintf(fp, "\n");
01202         }
01203     }
01204 
01205     return NULL;
01206 }
01207 
01226 Error* write_matblock_with_header_fp_i32(const Matblock_i32* m, FILE* fp)
01227 {
01228     uint32_t num_mats, num_rows, num_cols;
01229     uint32_t mat, row, col;
01230 
01231     num_mats = m->num_mats;
01232     num_rows = m->num_rows;
01233     num_cols = m->num_cols;
01234     fprintf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", num_mats, num_rows,
01235             num_cols);
01236 
01237     for (mat = 0; mat < num_mats; mat++)
01238     {
01239         for (row = 0; row < num_rows; row++)
01240         {
01241             for (col = 0; col < num_cols; col++)
01242             {
01243                 fprintf(fp, "%d ", m->elts[ mat ][ row ][ col ]);
01244             }
01245             fprintf(fp, "\n");
01246         }
01247     }
01248 
01249     return NULL;
01250 }
01251 
01270 Error* write_matblock_with_header_fp_i64(const Matblock_i64* m, FILE* fp)
01271 {
01272     uint32_t num_mats, num_rows, num_cols;
01273     uint32_t mat, row, col;
01274 
01275     num_mats = m->num_mats;
01276     num_rows = m->num_rows;
01277     num_cols = m->num_cols;
01278     fprintf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", num_mats, num_rows,
01279             num_cols);
01280 
01281     for (mat = 0; mat < num_mats; mat++)
01282     {
01283         for (row = 0; row < num_rows; row++)
01284         {
01285             for (col = 0; col < num_cols; col++)
01286             {
01287                 fprintf(fp, "%" PRId64 " ", m->elts[ mat ][ row ][ col ]);
01288             }
01289             fprintf(fp, "\n");
01290         }
01291     }
01292 
01293     return NULL;
01294 }
01295 
01314 Error* write_matblock_with_header_fp_f(const Matblock_f* m, FILE* fp)
01315 {
01316     uint32_t num_mats, num_rows, num_cols;
01317     uint32_t mat, row, col;
01318 
01319     num_mats = m->num_mats;
01320     num_rows = m->num_rows;
01321     num_cols = m->num_cols;
01322     fprintf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", num_mats, num_rows,
01323             num_cols);
01324 
01325     for (mat = 0; mat < num_mats; mat++)
01326     {
01327         for (row = 0; row < num_rows; row++)
01328         {
01329             for (col = 0; col < num_cols; col++)
01330             {
01331                 fprintf(fp, "%f ", m->elts[ mat ][ row ][ col ]);
01332             }
01333             fprintf(fp, "\n");
01334         }
01335     }
01336 
01337     return NULL;
01338 }
01339 
01358 Error* write_matblock_with_header_fp_d(const Matblock_d* m, FILE* fp)
01359 {
01360     uint32_t num_mats, num_rows, num_cols;
01361     uint32_t mat, row, col;
01362 
01363     num_mats = m->num_mats;
01364     num_rows = m->num_rows;
01365     num_cols = m->num_cols;
01366     fprintf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", num_mats, num_rows,
01367             num_cols);
01368 
01369     for (mat = 0; mat < num_mats; mat++)
01370     {
01371         for (row = 0; row < num_rows; row++)
01372         {
01373             for (col = 0; col < num_cols; col++)
01374             {
01375                 fprintf(fp, "%.8e ", m->elts[ mat ][ row ][ col ]);
01376             }
01377             fprintf(fp, "\n");
01378         }
01379     }
01380 
01381     return NULL;
01382 }
01383 
01402 Error* write_matblock_with_header_fp_cf(const Matblock_cf* m, FILE* fp)
01403 {
01404     uint32_t num_mats, num_rows, num_cols;
01405     uint32_t mat, row, col;
01406 
01407     num_mats = m->num_mats;
01408     num_rows = m->num_rows;
01409     num_cols = m->num_cols;
01410     fprintf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", num_mats, num_rows,
01411             num_cols);
01412 
01413     for (mat = 0; mat < num_mats; mat++)
01414     {
01415         for (row = 0; row < num_rows; row++)
01416         {
01417             for (col = 0; col < num_cols; col++)
01418             {
01419                 fprintf(fp, "%f %f ", m->elts[ mat ][ row ][ col ].r,
01420                         m->elts[ mat ][ row ][ col ].i);
01421             }
01422             fprintf(fp, "\n");
01423         }
01424     }
01425 
01426     return NULL;
01427 }
01428 
01447 Error* write_matblock_with_header_fp_cd(const Matblock_cd* m, FILE* fp)
01448 {
01449     uint32_t num_mats, num_rows, num_cols;
01450     uint32_t mat, row, col;
01451 
01452     num_mats = m->num_mats;
01453     num_rows = m->num_rows;
01454     num_cols = m->num_cols;
01455     fprintf(fp, "num_mats=%u num_rows=%u num_cols=%u\n", num_mats, num_rows,
01456             num_cols);
01457 
01458     for (mat = 0; mat < num_mats; mat++)
01459     {
01460         for (row = 0; row < num_rows; row++)
01461         {
01462             for (col = 0; col < num_cols; col++)
01463             {
01464                 fprintf(fp, "%lf %lf ", m->elts[ mat ][ row ][ col ].r,
01465                         m->elts[ mat ][ row ][ col ].i);
01466             }
01467             fprintf(fp, "\n");
01468         }
01469     }
01470 
01471     return NULL;
01472 }
01473