JWS C Library
C language utility library
matrix_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/matrix/matrix.h"
00060 #include "jwsc/matrix/matrix_io.h"
00061 
00062 #ifdef JWSC_HAVE_DMALLOC
00063 #include <dmalloc.h>
00064 #endif
00065 
00066 
00068 static int read_matrix_line(char** line, FILE* fp)
00069 {
00070     static const uint32_t block_size = 1024;
00071 
00072     uint32_t num_elts;
00073     uint32_t blocks;
00074 
00075     char c;
00076 
00077     num_elts = 0;
00078     blocks   = 0;
00079 
00080     while ((c = fgetc(fp)) != EOF && c != '\n') 
00081     {
00082         if (num_elts % block_size == 0)
00083         {
00084             *line = realloc(*line, (++blocks)*block_size*sizeof(char));
00085             assert(*line);
00086         }
00087 
00088         (*line)[ num_elts++ ] = c;
00089     }
00090 
00091     if (num_elts % block_size == 0)
00092     {
00093         *line = realloc(*line, (blocks*block_size+1)*sizeof(char));
00094         assert(*line);
00095     }
00096     (*line)[ num_elts ] = '\0';
00097 
00098     return (c == EOF);
00099 }
00100 
00101 
00103 static uint32_t count_matrix_line_columns(const char* line)
00104 {
00105     uint32_t count;
00106     uint32_t done;
00107     uint32_t in_column;
00108 
00109     const char* c;
00110 
00111     count     = 0;
00112     done      = 0;
00113     in_column = 0;
00114 
00115     c = line;
00116 
00117     while(!done)
00118     {
00119         if (*c == '\0')
00120         {
00121             done = 1;
00122         }
00123         else if (isspace(*c))
00124         {
00125             in_column = 0;
00126         }
00127         else if (!in_column)
00128         {
00129             count++;
00130             in_column = 1;
00131         }
00132         c++;
00133     }
00134 
00135     return count;
00136 }
00137 
00138 
00140 static const char* advance_line_column_ptr(const char* col)
00141 {
00142     while (*col != ' ' && *col != '\0') col++;
00143 
00144     while (*col == ' ' && *col != '\0') col++;
00145 
00146     return col;
00147 }
00148 
00149 
00150 
00151 
00181 Error* read_matrix_u32(Matrix_u32** m_out, const char* fname)
00182 {
00183     FILE*  fp;
00184     Error* e;
00185 
00186     if ((fp = fopen(fname, "r")) == NULL)
00187     {
00188         free_matrix_u32(*m_out); *m_out = NULL;
00189         return JWSC_EIO(strerror(errno));
00190     }
00191 
00192     if ((e = read_matrix_fp_u32(m_out, fp)) != NULL)
00193     {
00194         fclose(fp);
00195         return e;
00196     }
00197 
00198     if (fclose(fp) != 0)
00199     {
00200         free_matrix_u32(*m_out); *m_out = NULL;
00201         return JWSC_EIO(strerror(errno));
00202     }
00203 
00204     return NULL;
00205 }
00206 
00229 Error* read_matrix_i32(Matrix_i32** m_out, const char* fname)
00230 {
00231     FILE*  fp;
00232     Error* e;
00233 
00234     if ((fp = fopen(fname, "r")) == NULL)
00235     {
00236         free_matrix_i32(*m_out); *m_out = NULL;
00237         return JWSC_EIO(strerror(errno));
00238     }
00239 
00240     if ((e = read_matrix_fp_i32(m_out, fp)) != NULL)
00241     {
00242         fclose(fp);
00243         return e;
00244     }
00245 
00246     if (fclose(fp) != 0)
00247     {
00248         free_matrix_i32(*m_out); *m_out = NULL;
00249         return JWSC_EIO(strerror(errno));
00250     }
00251 
00252     return NULL;
00253 }
00254 
00277 Error* read_matrix_i64(Matrix_i64** m_out, const char* fname)
00278 {
00279     FILE*  fp;
00280     Error* e;
00281 
00282     if ((fp = fopen(fname, "r")) == NULL)
00283     {
00284         free_matrix_i64(*m_out); *m_out = NULL;
00285         return JWSC_EIO(strerror(errno));
00286     }
00287 
00288     if ((e = read_matrix_fp_i64(m_out, fp)) != NULL)
00289     {
00290         fclose(fp);
00291         return e;
00292     }
00293 
00294     if (fclose(fp) != 0)
00295     {
00296         free_matrix_i64(*m_out); *m_out = NULL;
00297         return JWSC_EIO(strerror(errno));
00298     }
00299 
00300     return NULL;
00301 }
00302 
00325 Error* read_matrix_f(Matrix_f** m_out, const char* fname)
00326 {
00327     FILE*  fp;
00328     Error* e;
00329 
00330     if ((fp = fopen(fname, "r")) == NULL)
00331     {
00332         free_matrix_f(*m_out); *m_out = NULL;
00333         return JWSC_EIO(strerror(errno));
00334     }
00335 
00336     if ((e = read_matrix_fp_f(m_out, fp)) != NULL)
00337     {
00338         fclose(fp);
00339         return e;
00340     }
00341 
00342     if (fclose(fp) != 0)
00343     {
00344         free_matrix_f(*m_out); *m_out = NULL;
00345         return JWSC_EIO(strerror(errno));
00346     }
00347 
00348     return NULL;
00349 }
00350 
00373 Error* read_matrix_d(Matrix_d** m_out, const char* fname)
00374 {
00375     FILE*  fp;
00376     Error* e;
00377 
00378     if ((fp = fopen(fname, "r")) == NULL)
00379     {
00380         free_matrix_d(*m_out); *m_out = NULL;
00381         return JWSC_EIO(strerror(errno));
00382     }
00383 
00384     if ((e = read_matrix_fp_d(m_out, fp)) != NULL)
00385     {
00386         fclose(fp);
00387         return e;
00388     }
00389 
00390     if (fclose(fp) != 0)
00391     {
00392         free_matrix_d(*m_out); *m_out = NULL;
00393         return JWSC_EIO(strerror(errno));
00394     }
00395 
00396     return NULL;
00397 }
00398 
00421 Error* read_matrix_cf(Matrix_cf** m_out, const char* fname)
00422 {
00423     FILE*  fp;
00424     Error* e;
00425 
00426     if ((fp = fopen(fname, "r")) == NULL)
00427     {
00428         free_matrix_cf(*m_out); *m_out = NULL;
00429         return JWSC_EIO(strerror(errno));
00430     }
00431 
00432     if ((e = read_matrix_fp_cf(m_out, fp)) != NULL)
00433     {
00434         fclose(fp);
00435         return e;
00436     }
00437 
00438     if (fclose(fp) != 0)
00439     {
00440         free_matrix_cf(*m_out); *m_out = NULL;
00441         return JWSC_EIO(strerror(errno));
00442     }
00443 
00444     return NULL;
00445 }
00446 
00469 Error* read_matrix_cd(Matrix_cd** m_out, const char* fname)
00470 {
00471     FILE*  fp;
00472     Error* e;
00473 
00474     if ((fp = fopen(fname, "r")) == NULL)
00475     {
00476         free_matrix_cd(*m_out); *m_out = NULL;
00477         return JWSC_EIO(strerror(errno));
00478     }
00479 
00480     if ((e = read_matrix_fp_cd(m_out, fp)) != NULL)
00481     {
00482         fclose(fp);
00483         return e;
00484     }
00485 
00486     if (fclose(fp) != 0)
00487     {
00488         free_matrix_cd(*m_out); *m_out = NULL;
00489         return JWSC_EIO(strerror(errno));
00490     }
00491 
00492     return NULL;
00493 }
00494 
00529 Error* read_matrix_fp_u32(Matrix_u32** m_out, FILE* fp)
00530 {
00531     static const uint32_t block_size = 1024;
00532 
00533     uint32_t num_elts, num_rows, num_cols;
00534     uint32_t elt, col;
00535     uint32_t blocks;
00536     uint32_t done;
00537 
00538     char* line = NULL;
00539     const char* line_col;
00540 
00541     uint32_t  val;
00542     uint32_t* elts   = NULL;
00543     uint32_t* m_elts = NULL;
00544 
00545     if (skip_fp_spaces_and_comments(fp) == 0)
00546     {
00547         free_matrix_u32(*m_out); *m_out = NULL;
00548         return JWSC_EIO("Nothing to read");
00549     }
00550 
00551     num_elts = 0;
00552     num_cols = 0;
00553     num_rows = 0;
00554     blocks   = 0;
00555     done     = 0;
00556 
00557     while (!done)
00558     {
00559         read_matrix_line(&line, fp);
00560 
00561         if (num_cols == 0)
00562         {
00563             if ((num_cols = count_matrix_line_columns(line)) == 0)
00564             {
00565                 free(line);
00566                 free_matrix_u32(*m_out); *m_out = NULL;
00567                 return JWSC_EIO("Matrix to read not formatted properly");
00568             }
00569         }
00570         else
00571         {
00572             if (count_matrix_line_columns(line) != num_cols)
00573             {
00574                 free(line);
00575                 free(elts);
00576                 free_matrix_u32(*m_out); *m_out = NULL;
00577                 return JWSC_EIO("Matrix to read not formatted properly");
00578             }
00579         }
00580 
00581         line_col = line;
00582 
00583         for (col = 0; col < num_cols; col++)
00584         {
00585             if (sscanf(line_col, "%u", &val) != 1)
00586             {
00587                 free(line);
00588                 free(elts);
00589                 free_matrix_u32(*m_out); *m_out = NULL;
00590                 return JWSC_EIO("Matrix to read not formatted properly");
00591             }
00592 
00593             line_col = advance_line_column_ptr(line_col);
00594 
00595             if (num_elts % block_size == 0)
00596             {
00597                 elts = realloc(elts, (++blocks)*block_size*
00598                         sizeof(uint32_t));
00599                 assert(elts);
00600             }
00601 
00602             elts[ num_elts++ ] = val;
00603         }
00604 
00605         num_rows++;
00606 
00607         done = !skip_fp_spaces_and_comments(fp);
00608     }
00609 
00610     create_matrix_u32(m_out, num_rows, num_cols);
00611     m_elts = *((*m_out)->elts);
00612 
00613     for (elt = 0; elt < num_elts; elt++)
00614     {
00615         m_elts[ elt ] = elts[ elt ];
00616     }
00617 
00618     free(line);
00619     free(elts);
00620 
00621     return NULL;
00622 }
00623 
00646 Error* read_matrix_fp_i32(Matrix_i32** m_out, FILE* fp)
00647 {
00648     static const uint32_t block_size = 1024;
00649 
00650     uint32_t num_elts, num_rows, num_cols;
00651     uint32_t elt, col;
00652     uint32_t blocks;
00653     uint32_t done;
00654 
00655     char* line = NULL;
00656     const char* line_col;
00657 
00658     int32_t  val;
00659     int32_t* elts   = NULL;
00660     int32_t* m_elts = NULL;
00661 
00662     if (skip_fp_spaces_and_comments(fp) == 0)
00663     {
00664         free_matrix_i32(*m_out); *m_out = NULL;
00665         return JWSC_EIO("Nothing to read");
00666     }
00667 
00668     num_elts = 0;
00669     num_cols = 0;
00670     num_rows = 0;
00671     blocks   = 0;
00672     done     = 0;
00673 
00674     while (!done)
00675     {
00676         read_matrix_line(&line, fp);
00677 
00678         if (num_cols == 0)
00679         {
00680             if ((num_cols = count_matrix_line_columns(line)) == 0)
00681             {
00682                 free(line);
00683                 free_matrix_i32(*m_out); *m_out = NULL;
00684                 return JWSC_EIO("Matrix to read not formatted properly");
00685             }
00686         }
00687         else
00688         {
00689             if (count_matrix_line_columns(line) != num_cols)
00690             {
00691                 free(line);
00692                 free(elts);
00693                 free_matrix_i32(*m_out); *m_out = NULL;
00694                 return JWSC_EIO("Matrix to read not formatted properly");
00695             }
00696         }
00697 
00698         line_col = line;
00699 
00700         for (col = 0; col < num_cols; col++)
00701         {
00702             if (sscanf(line_col, "%d", &val) != 1)
00703             {
00704                 free(line);
00705                 free(elts);
00706                 free_matrix_i32(*m_out); *m_out = NULL;
00707                 return JWSC_EIO("Matrix to read not formatted properly");
00708             }
00709 
00710             line_col = advance_line_column_ptr(line_col);
00711 
00712             if (num_elts % block_size == 0)
00713             {
00714                 elts = realloc(elts, (++blocks)*block_size*sizeof(int32_t));
00715                 assert(elts);
00716             }
00717 
00718             elts[ num_elts++ ] = val;
00719         }
00720 
00721         num_rows++;
00722 
00723         done = !skip_fp_spaces_and_comments(fp);
00724     }
00725 
00726     create_matrix_i32(m_out, num_rows, num_cols);
00727     m_elts = *((*m_out)->elts);
00728 
00729     for (elt = 0; elt < num_elts; elt++)
00730     {
00731         m_elts[ elt ] = elts[ elt ];
00732     }
00733 
00734     free(line);
00735     free(elts);
00736 
00737     return NULL;
00738 }
00739 
00762 Error* read_matrix_fp_i64(Matrix_i64** m_out, FILE* fp)
00763 {
00764     static const uint32_t block_size = 1024;
00765 
00766     uint32_t num_elts, num_rows, num_cols;
00767     uint32_t elt, col;
00768     uint32_t blocks;
00769     uint32_t done;
00770 
00771     char* line = NULL;
00772     const char* line_col;
00773 
00774     int64_t  val;
00775     int64_t* elts   = NULL;
00776     int64_t* m_elts = NULL;
00777 
00778     if (skip_fp_spaces_and_comments(fp) == 0)
00779     {
00780         free_matrix_i64(*m_out); *m_out = NULL;
00781         return JWSC_EIO("Nothing to read");
00782     }
00783 
00784     num_elts = 0;
00785     num_cols = 0;
00786     num_rows = 0;
00787     blocks   = 0;
00788     done     = 0;
00789 
00790     while (!done)
00791     {
00792         read_matrix_line(&line, fp);
00793 
00794         if (num_cols == 0)
00795         {
00796             if ((num_cols = count_matrix_line_columns(line)) == 0)
00797             {
00798                 free(line);
00799                 free_matrix_i64(*m_out); *m_out = NULL;
00800                 return JWSC_EIO("Matrix to read not formatted properly");
00801             }
00802         }
00803         else
00804         {
00805             if (count_matrix_line_columns(line) != num_cols)
00806             {
00807                 free(line);
00808                 free(elts);
00809                 free_matrix_i64(*m_out); *m_out = NULL;
00810                 return JWSC_EIO("Matrix to read not formatted properly");
00811             }
00812         }
00813 
00814         line_col = line;
00815 
00816         for (col = 0; col < num_cols; col++)
00817         {
00818             if (sscanf(line_col, "%" SCNd64, &val) != 1)
00819             {
00820                 free(line);
00821                 free(elts);
00822                 free_matrix_i64(*m_out); *m_out = NULL;
00823                 return JWSC_EIO("Matrix to read not formatted properly");
00824             }
00825 
00826             line_col = advance_line_column_ptr(line_col);
00827 
00828             if (num_elts % block_size == 0)
00829             {
00830                 elts = realloc(elts, (++blocks)*block_size*sizeof(int64_t));
00831                 assert(elts);
00832             }
00833 
00834             elts[ num_elts++ ] = val;
00835         }
00836 
00837         num_rows++;
00838 
00839         done = !skip_fp_spaces_and_comments(fp);
00840     }
00841 
00842     create_matrix_i64(m_out, num_rows, num_cols);
00843     m_elts = *((*m_out)->elts);
00844 
00845     for (elt = 0; elt < num_elts; elt++)
00846     {
00847         m_elts[ elt ] = elts[ elt ];
00848     }
00849 
00850     free(line);
00851     free(elts);
00852 
00853     return NULL;
00854 }
00855 
00878 Error* read_matrix_fp_f(Matrix_f** m_out, FILE* fp)
00879 {
00880     static const uint32_t block_size = 1024;
00881 
00882     uint32_t num_elts, num_rows, num_cols;
00883     uint32_t elt, col;
00884     uint32_t blocks;
00885     uint32_t done;
00886 
00887     char* line = NULL;
00888     const char* line_col;
00889 
00890     float  val;
00891     float* elts   = NULL;
00892     float* m_elts = NULL;
00893 
00894     if (skip_fp_spaces_and_comments(fp) == 0)
00895     {
00896         free_matrix_f(*m_out); *m_out = NULL;
00897         return JWSC_EIO("Nothing to read");
00898     }
00899 
00900     num_elts = 0;
00901     num_cols = 0;
00902     num_rows = 0;
00903     blocks   = 0;
00904     done     = 0;
00905 
00906     while (!done)
00907     {
00908         read_matrix_line(&line, fp);
00909 
00910         if (num_cols == 0)
00911         {
00912             if ((num_cols = count_matrix_line_columns(line)) == 0)
00913             {
00914                 free(line);
00915                 free_matrix_f(*m_out); *m_out = NULL;
00916                 return JWSC_EIO("Matrix to read not formatted properly");
00917             }
00918         }
00919         else
00920         {
00921             if (count_matrix_line_columns(line) != num_cols)
00922             {
00923                 free(line);
00924                 free(elts);
00925                 free_matrix_f(*m_out); *m_out = NULL;
00926                 return JWSC_EIO("Matrix to read not formatted properly");
00927             }
00928         }
00929 
00930         line_col = line;
00931 
00932         for (col = 0; col < num_cols; col++)
00933         {
00934             if (sscanf(line_col, "%f", &val) != 1)
00935             {
00936                 free(line);
00937                 free(elts);
00938                 free_matrix_f(*m_out); *m_out = NULL;
00939                 return JWSC_EIO("Matrix to read not formatted properly");
00940             }
00941 
00942             line_col = advance_line_column_ptr(line_col);
00943 
00944             if (num_elts % block_size == 0)
00945             {
00946                 elts = realloc(elts, (++blocks)*block_size*sizeof(float));
00947                 assert(elts);
00948             }
00949 
00950             elts[ num_elts++ ] = val;
00951         }
00952 
00953         num_rows++;
00954 
00955         done = !skip_fp_spaces_and_comments(fp);
00956     }
00957 
00958     create_matrix_f(m_out, num_rows, num_cols);
00959     m_elts = *((*m_out)->elts);
00960 
00961     for (elt = 0; elt < num_elts; elt++)
00962     {
00963         m_elts[ elt ] = elts[ elt ];
00964     }
00965 
00966     free(line);
00967     free(elts);
00968 
00969     return NULL;
00970 }
00971 
00994 Error* read_matrix_fp_d(Matrix_d** m_out, FILE* fp)
00995 {
00996     static const uint32_t block_size = 1024;
00997 
00998     uint32_t num_elts, num_rows, num_cols;
00999     uint32_t elt, col;
01000     uint32_t blocks;
01001     uint32_t done;
01002 
01003     char* line = NULL;
01004     const char* line_col;
01005 
01006     double  val;
01007     double* elts   = NULL;
01008     double* m_elts = NULL;
01009 
01010     if (skip_fp_spaces_and_comments(fp) == 0)
01011     {
01012         free_matrix_d(*m_out); *m_out = NULL;
01013         return JWSC_EIO("Nothing to read");
01014     }
01015 
01016     num_elts = 0;
01017     num_cols = 0;
01018     num_rows = 0;
01019     blocks   = 0;
01020     done     = 0;
01021 
01022     while (!done)
01023     {
01024         read_matrix_line(&line, fp);
01025 
01026         if (num_cols == 0)
01027         {
01028             if ((num_cols = count_matrix_line_columns(line)) == 0)
01029             {
01030                 free(line);
01031                 free_matrix_d(*m_out); *m_out = NULL;
01032                 return JWSC_EIO("Matrix to read not formatted properly");
01033             }
01034         }
01035         else
01036         {
01037             if (count_matrix_line_columns(line) != num_cols)
01038             {
01039                 free(line);
01040                 free(elts);
01041                 free_matrix_d(*m_out); *m_out = NULL;
01042                 return JWSC_EIO("Matrix to read not formatted properly");
01043             }
01044         }
01045 
01046         line_col = line;
01047 
01048         for (col = 0; col < num_cols; col++)
01049         {
01050             if (sscanf(line_col, "%lf", &val) != 1)
01051             {
01052                 free(line);
01053                 free(elts);
01054                 free_matrix_d(*m_out); *m_out = NULL;
01055                 return JWSC_EIO("Matrix to read not formatted properly");
01056             }
01057 
01058             line_col = advance_line_column_ptr(line_col);
01059 
01060             if (num_elts % block_size == 0)
01061             {
01062                 elts = realloc(elts, (++blocks)*block_size*sizeof(double));
01063                 assert(elts);
01064             }
01065 
01066             elts[ num_elts++ ] = val;
01067         }
01068 
01069         num_rows++;
01070 
01071         done = !skip_fp_spaces_and_comments(fp);
01072     }
01073 
01074     create_matrix_d(m_out, num_rows, num_cols);
01075     m_elts = *((*m_out)->elts);
01076 
01077     for (elt = 0; elt < num_elts; elt++)
01078     {
01079         m_elts[ elt ] = elts[ elt ];
01080     }
01081 
01082     free(line);
01083     free(elts);
01084 
01085     return NULL;
01086 }
01087 
01110 Error* read_matrix_fp_cf(Matrix_cf** m_out, FILE* fp)
01111 {
01112     static const uint32_t block_size = 1024;
01113 
01114     uint32_t num_elts, num_rows, num_cols;
01115     uint32_t elt, col;
01116     uint32_t blocks;
01117     uint32_t done;
01118 
01119     char* line = NULL;
01120     const char* line_col;
01121 
01122     Complex_f  val;
01123     Complex_f* elts   = NULL;
01124     Complex_f* m_elts = NULL;
01125 
01126     if (skip_fp_spaces_and_comments(fp) == 0)
01127     {
01128         free_matrix_cf(*m_out); *m_out = NULL;
01129         return JWSC_EIO("Nothing to read");
01130     }
01131 
01132     num_elts = 0;
01133     num_cols = 0;
01134     num_rows = 0;
01135     blocks   = 0;
01136     done     = 0;
01137 
01138     while (!done)
01139     {
01140         read_matrix_line(&line, fp);
01141 
01142         if (num_cols == 0)
01143         {
01144             if ((num_cols = count_matrix_line_columns(line)) == 0)
01145             {
01146                 free(line);
01147                 free_matrix_cf(*m_out); *m_out = NULL;
01148                 return JWSC_EIO("Matrix to read not formatted properly");
01149             }
01150             num_cols /= 2;
01151         }
01152         else
01153         {
01154             if (count_matrix_line_columns(line) != num_cols*2)
01155             {
01156                 free(line);
01157                 free(elts);
01158                 free_matrix_cf(*m_out); *m_out = NULL;
01159                 return JWSC_EIO("Matrix to read not formatted properly");
01160             }
01161         }
01162 
01163         line_col = line;
01164 
01165         for (col = 0; col < num_cols; col++)
01166         {
01167             if (sscanf(line_col, "%f %f", &(val.r), &(val.i)) != 2)
01168             {
01169                 free(line);
01170                 free(elts);
01171                 free_matrix_cf(*m_out); *m_out = NULL;
01172                 return JWSC_EIO("Matrix to read not formatted properly");
01173             }
01174 
01175             line_col = advance_line_column_ptr(line_col);
01176             line_col = advance_line_column_ptr(line_col);
01177 
01178             if (num_elts % block_size == 0)
01179             {
01180                 elts = realloc(elts, (++blocks)*block_size*
01181                         sizeof(Complex_f));
01182                 assert(elts);
01183             }
01184 
01185             elts[ num_elts++ ] = val;
01186         }
01187 
01188         num_rows++;
01189 
01190         done = !skip_fp_spaces_and_comments(fp);
01191     }
01192 
01193     create_matrix_cf(m_out, num_rows, num_cols);
01194     m_elts = *((*m_out)->elts);
01195 
01196     for (elt = 0; elt < num_elts; elt++)
01197     {
01198         m_elts[ elt ] = elts[ elt ];
01199     }
01200 
01201     free(line);
01202     free(elts);
01203 
01204     return NULL;
01205 }
01206 
01229 Error* read_matrix_fp_cd(Matrix_cd** m_out, FILE* fp)
01230 {
01231     static const uint32_t block_size = 1024;
01232 
01233     uint32_t num_elts, num_rows, num_cols;
01234     uint32_t elt, col;
01235     uint32_t blocks;
01236     uint32_t done;
01237 
01238     char* line = NULL;
01239     const char* line_col;
01240 
01241     Complex_d  val;
01242     Complex_d* elts   = NULL;
01243     Complex_d* m_elts = NULL;
01244 
01245     if (skip_fp_spaces_and_comments(fp) == 0)
01246     {
01247         free_matrix_cd(*m_out); *m_out = NULL;
01248         return JWSC_EIO("Nothing to read");
01249     }
01250 
01251     num_elts = 0;
01252     num_cols = 0;
01253     num_rows = 0;
01254     blocks   = 0;
01255     done     = 0;
01256 
01257     while (!done)
01258     {
01259         read_matrix_line(&line, fp);
01260 
01261         if (num_cols == 0)
01262         {
01263             if ((num_cols = count_matrix_line_columns(line)) == 0)
01264             {
01265                 free(line);
01266                 free_matrix_cd(*m_out); *m_out = NULL;
01267                 return JWSC_EIO("Matrix to read not formatted properly");
01268             }
01269             num_cols /= 2;
01270         }
01271         else
01272         {
01273             if (count_matrix_line_columns(line) != num_cols*2)
01274             {
01275                 free(line);
01276                 free(elts);
01277                 free_matrix_cd(*m_out); *m_out = NULL;
01278                 return JWSC_EIO("Matrix to read not formatted properly");
01279             }
01280         }
01281 
01282         line_col = line;
01283 
01284         for (col = 0; col < num_cols; col++)
01285         {
01286             if (sscanf(line_col, "%lf %lf", &(val.r), &(val.i)) != 2)
01287             {
01288                 free(line);
01289                 free(elts);
01290                 free_matrix_cd(*m_out); *m_out = NULL;
01291                 return JWSC_EIO("Matrix to read not formatted properly");
01292             }
01293 
01294             line_col = advance_line_column_ptr(line_col);
01295             line_col = advance_line_column_ptr(line_col);
01296 
01297             if (num_elts % block_size == 0)
01298             {
01299                 elts = realloc(elts, (++blocks)*block_size*
01300                         sizeof(Complex_d));
01301                 assert(elts);
01302             }
01303 
01304             elts[ num_elts++ ] = val;
01305         }
01306 
01307         num_rows++;
01308 
01309         done = !skip_fp_spaces_and_comments(fp);
01310     }
01311 
01312     create_matrix_cd(m_out, num_rows, num_cols);
01313     m_elts = *((*m_out)->elts);
01314 
01315     for (elt = 0; elt < num_elts; elt++)
01316     {
01317         m_elts[ elt ] = elts[ elt ];
01318     }
01319 
01320     free(line);
01321     free(elts);
01322 
01323     return NULL;
01324 }
01325 
01355 Error* write_matrix_u32(const Matrix_u32* m, const char* fname)
01356 {
01357     FILE*  fp;
01358     Error* e;
01359 
01360     if ((fp = fopen(fname, "w")) == NULL)
01361     {
01362         return JWSC_EIO(strerror(errno));
01363     }
01364 
01365     if ((e = write_matrix_fp_u32(m, fp)) != NULL)
01366     {
01367         fclose(fp);
01368         return e;
01369     }
01370 
01371     if (fclose(fp) != 0)
01372     {
01373         return JWSC_EIO(strerror(errno));
01374     }
01375 
01376     return NULL;
01377 }
01378 
01396 Error* write_matrix_i32(const Matrix_i32* m, const char* fname)
01397 {
01398     FILE*  fp;
01399     Error* e;
01400 
01401     if ((fp = fopen(fname, "w")) == NULL)
01402     {
01403         return JWSC_EIO(strerror(errno));
01404     }
01405 
01406     if ((e = write_matrix_fp_i32(m, fp)) != NULL)
01407     {
01408         fclose(fp);
01409         return e;
01410     }
01411 
01412     if (fclose(fp) != 0)
01413     {
01414         return JWSC_EIO(strerror(errno));
01415     }
01416 
01417     return NULL;
01418 }
01419 
01437 Error* write_matrix_i64(const Matrix_i64* m, const char* fname)
01438 {
01439     FILE*  fp;
01440     Error* e;
01441 
01442     if ((fp = fopen(fname, "w")) == NULL)
01443     {
01444         return JWSC_EIO(strerror(errno));
01445     }
01446 
01447     if ((e = write_matrix_fp_i64(m, fp)) != NULL)
01448     {
01449         fclose(fp);
01450         return e;
01451     }
01452 
01453     if (fclose(fp) != 0)
01454     {
01455         return JWSC_EIO(strerror(errno));
01456     }
01457 
01458     return NULL;
01459 }
01460 
01478 Error* write_matrix_f(const Matrix_f* m, const char* fname)
01479 {
01480     FILE*  fp;
01481     Error* e;
01482 
01483     if ((fp = fopen(fname, "w")) == NULL)
01484     {
01485         return JWSC_EIO(strerror(errno));
01486     }
01487 
01488     if ((e = write_matrix_fp_f(m, fp)) != NULL)
01489     {
01490         fclose(fp);
01491         return e;
01492     }
01493 
01494     if (fclose(fp) != 0)
01495     {
01496         return JWSC_EIO(strerror(errno));
01497     }
01498 
01499     return NULL;
01500 }
01501 
01519 Error* write_matrix_d(const Matrix_d* m, const char* fname)
01520 {
01521     FILE*  fp;
01522     Error* e;
01523 
01524     if ((fp = fopen(fname, "w")) == NULL)
01525     {
01526         return JWSC_EIO(strerror(errno));
01527     }
01528 
01529     if ((e = write_matrix_fp_d(m, fp)) != NULL)
01530     {
01531         fclose(fp);
01532         return e;
01533     }
01534 
01535     if (fclose(fp) != 0)
01536     {
01537         return JWSC_EIO(strerror(errno));
01538     }
01539 
01540     return NULL;
01541 }
01542 
01560 Error* write_matrix_cf(const Matrix_cf* m, const char* fname)
01561 {
01562     FILE*  fp;
01563     Error* e;
01564 
01565     if ((fp = fopen(fname, "w")) == NULL)
01566     {
01567         return JWSC_EIO(strerror(errno));
01568     }
01569 
01570     if ((e = write_matrix_fp_cf(m, fp)) != NULL)
01571     {
01572         fclose(fp);
01573         return e;
01574     }
01575 
01576     if (fclose(fp) != 0)
01577     {
01578         return JWSC_EIO(strerror(errno));
01579     }
01580 
01581     return NULL;
01582 }
01583 
01601 Error* write_matrix_cd(const Matrix_cd* m, const char* fname)
01602 {
01603     FILE*  fp;
01604     Error* e;
01605 
01606     if ((fp = fopen(fname, "w")) == NULL)
01607     {
01608         return JWSC_EIO(strerror(errno));
01609     }
01610 
01611     if ((e = write_matrix_fp_cd(m, fp)) != NULL)
01612     {
01613         fclose(fp);
01614         return e;
01615     }
01616 
01617     if (fclose(fp) != 0)
01618     {
01619         return JWSC_EIO(strerror(errno));
01620     }
01621 
01622     return NULL;
01623 }
01624 
01654 Error* write_matrix_fp_u32(const Matrix_u32* m, FILE* fp)
01655 {
01656     uint32_t num_rows, num_cols;
01657     uint32_t row, col;
01658 
01659     num_rows = m->num_rows;
01660     num_cols = m->num_cols;
01661     for (row = 0; row < num_rows; row++)
01662     {
01663         for (col = 0; col < num_cols; col++)
01664         {
01665             fprintf(fp, "%u ", m->elts[ row ][ col ]);
01666         }
01667         fprintf(fp, "\n");
01668     }
01669 
01670     return NULL;
01671 }
01672 
01690 Error* write_matrix_fp_i32(const Matrix_i32* m, FILE* fp)
01691 {
01692     uint32_t num_rows, num_cols;
01693     uint32_t row, col;
01694 
01695     num_rows = m->num_rows;
01696     num_cols = m->num_cols;
01697     for (row = 0; row < num_rows; row++)
01698     {
01699         for (col = 0; col < num_cols; col++)
01700         {
01701             fprintf(fp, "%d ", m->elts[ row ][ col ]);
01702         }
01703         fprintf(fp, "\n");
01704     }
01705 
01706     return NULL;
01707 }
01708 
01726 Error* write_matrix_fp_i64(const Matrix_i64* m, FILE* fp)
01727 {
01728     uint32_t num_rows, num_cols;
01729     uint32_t row, col;
01730 
01731     num_rows = m->num_rows;
01732     num_cols = m->num_cols;
01733     for (row = 0; row < num_rows; row++)
01734     {
01735         for (col = 0; col < num_cols; col++)
01736         {
01737             fprintf(fp, "%" PRId64 " ", m->elts[ row ][ col ]);
01738         }
01739         fprintf(fp, "\n");
01740     }
01741 
01742     return NULL;
01743 }
01744 
01762 Error* write_matrix_fp_f(const Matrix_f* m, FILE* fp)
01763 {
01764     uint32_t num_rows, num_cols;
01765     uint32_t row, col;
01766 
01767     num_rows = m->num_rows;
01768     num_cols = m->num_cols;
01769     for (row = 0; row < num_rows; row++)
01770     {
01771         for (col = 0; col < num_cols; col++)
01772         {
01773             fprintf(fp, "%f ", m->elts[ row ][ col ]);
01774         }
01775         fprintf(fp, "\n");
01776     }
01777 
01778     return NULL;
01779 }
01780 
01798 Error* write_matrix_fp_d(const Matrix_d* m, FILE* fp)
01799 {
01800     uint32_t num_rows, num_cols;
01801     uint32_t row, col;
01802 
01803     num_rows = m->num_rows;
01804     num_cols = m->num_cols;
01805     for (row = 0; row < num_rows; row++)
01806     {
01807         for (col = 0; col < num_cols; col++)
01808         {
01809             fprintf(fp, "%.8e ", m->elts[ row ][ col ]);
01810         }
01811         fprintf(fp, "\n");
01812     }
01813 
01814     return NULL;
01815 }
01816 
01834 Error* write_matrix_fp_cf(const Matrix_cf* m, FILE* fp)
01835 {
01836     uint32_t num_rows, num_cols;
01837     uint32_t row, col;
01838 
01839     num_rows = m->num_rows;
01840     num_cols = m->num_cols;
01841     for (row = 0; row < num_rows; row++)
01842     {
01843         for (col = 0; col < num_cols; col++)
01844         {
01845             fprintf(fp, "%f %f ", m->elts[ row ][ col ].r, 
01846                                   m->elts[ row ][ col ].i);
01847         }
01848         fprintf(fp, "\n");
01849     }
01850 
01851     return NULL;
01852 }
01853 
01871 Error* write_matrix_fp_cd(const Matrix_cd* m, FILE* fp)
01872 {
01873     uint32_t num_rows, num_cols;
01874     uint32_t row, col;
01875 
01876     num_rows = m->num_rows;
01877     num_cols = m->num_cols;
01878     for (row = 0; row < num_rows; row++)
01879     {
01880         for (col = 0; col < num_cols; col++)
01881         {
01882             fprintf(fp, "%lf %lf ", m->elts[ row ][ col ].r,
01883                                     m->elts[ row ][ col ].i);
01884         }
01885         fprintf(fp, "\n");
01886     }
01887 
01888     return NULL;
01889 }
01890 
01927 Error* read_matrix_with_header_u32(Matrix_u32** m_out, const char* fname)
01928 {
01929     FILE*  fp;
01930     Error* e;
01931 
01932     if ((fp = fopen(fname, "r")) == NULL)
01933     {
01934         free_matrix_u32(*m_out); *m_out = NULL;
01935         return JWSC_EIO(strerror(errno));
01936     }
01937 
01938     if ((e = read_matrix_with_header_fp_u32(m_out, fp)) != NULL)
01939     {
01940         fclose(fp);
01941         return e;
01942     }
01943 
01944     if (fclose(fp) != 0)
01945     {
01946         free_matrix_u32(*m_out); *m_out = NULL;
01947         return JWSC_EIO(strerror(errno));
01948     }
01949 
01950     return NULL;
01951 }
01952 
01977 Error* read_matrix_with_header_i32(Matrix_i32** m_out, const char* fname)
01978 {
01979     FILE*  fp;
01980     Error* e;
01981 
01982     if ((fp = fopen(fname, "r")) == NULL)
01983     {
01984         free_matrix_i32(*m_out); *m_out = NULL;
01985         return JWSC_EIO(strerror(errno));
01986     }
01987 
01988     if ((e = read_matrix_with_header_fp_i32(m_out, fp)) != NULL)
01989     {
01990         fclose(fp);
01991         return e;
01992     }
01993 
01994     if (fclose(fp) != 0)
01995     {
01996         free_matrix_i32(*m_out); *m_out = NULL;
01997         return JWSC_EIO(strerror(errno));
01998     }
01999 
02000     return NULL;
02001 }
02002 
02027 Error* read_matrix_with_header_i64(Matrix_i64** m_out, const char* fname)
02028 {
02029     FILE*  fp;
02030     Error* e;
02031 
02032     if ((fp = fopen(fname, "r")) == NULL)
02033     {
02034         free_matrix_i64(*m_out); *m_out = NULL;
02035         return JWSC_EIO(strerror(errno));
02036     }
02037 
02038     if ((e = read_matrix_with_header_fp_i64(m_out, fp)) != NULL)
02039     {
02040         fclose(fp);
02041         return e;
02042     }
02043 
02044     if (fclose(fp) != 0)
02045     {
02046         free_matrix_i64(*m_out); *m_out = NULL;
02047         return JWSC_EIO(strerror(errno));
02048     }
02049 
02050     return NULL;
02051 }
02052 
02077 Error* read_matrix_with_header_f(Matrix_f** m_out, const char* fname)
02078 {
02079     FILE*  fp;
02080     Error* e;
02081 
02082     if ((fp = fopen(fname, "r")) == NULL)
02083     {
02084         free_matrix_f(*m_out); *m_out = NULL;
02085         return JWSC_EIO(strerror(errno));
02086     }
02087 
02088     if ((e = read_matrix_with_header_fp_f(m_out, fp)) != NULL)
02089     {
02090         fclose(fp);
02091         return e;
02092     }
02093 
02094     if (fclose(fp) != 0)
02095     {
02096         free_matrix_f(*m_out); *m_out = NULL;
02097         return JWSC_EIO(strerror(errno));
02098     }
02099 
02100     return NULL;
02101 }
02102 
02127 Error* read_matrix_with_header_d(Matrix_d** m_out, const char* fname)
02128 {
02129     FILE*  fp;
02130     Error* e;
02131 
02132     if ((fp = fopen(fname, "r")) == NULL)
02133     {
02134         free_matrix_d(*m_out); *m_out = NULL;
02135         return JWSC_EIO(strerror(errno));
02136     }
02137 
02138     if ((e = read_matrix_with_header_fp_d(m_out, fp)) != NULL)
02139     {
02140         fclose(fp);
02141         return e;
02142     }
02143 
02144     if (fclose(fp) != 0)
02145     {
02146         free_matrix_d(*m_out); *m_out = NULL;
02147         return JWSC_EIO(strerror(errno));
02148     }
02149 
02150     return NULL;
02151 }
02152 
02177 Error* read_matrix_with_header_cf(Matrix_cf** m_out, const char* fname)
02178 {
02179     FILE*  fp;
02180     Error* e;
02181 
02182     if ((fp = fopen(fname, "r")) == NULL)
02183     {
02184         free_matrix_cf(*m_out); *m_out = NULL;
02185         return JWSC_EIO(strerror(errno));
02186     }
02187 
02188     if ((e = read_matrix_with_header_fp_cf(m_out, fp)) != NULL)
02189     {
02190         fclose(fp);
02191         return e;
02192     }
02193 
02194     if (fclose(fp) != 0)
02195     {
02196         free_matrix_cf(*m_out); *m_out = NULL;
02197         return JWSC_EIO(strerror(errno));
02198     }
02199 
02200     return NULL;
02201 }
02202 
02227 Error* read_matrix_with_header_cd(Matrix_cd** m_out, const char* fname)
02228 {
02229     FILE*  fp;
02230     Error* e;
02231 
02232     if ((fp = fopen(fname, "r")) == NULL)
02233     {
02234         free_matrix_cd(*m_out); *m_out = NULL;
02235         return JWSC_EIO(strerror(errno));
02236     }
02237 
02238     if ((e = read_matrix_with_header_fp_cd(m_out, fp)) != NULL)
02239     {
02240         fclose(fp);
02241         return e;
02242     }
02243 
02244     if (fclose(fp) != 0)
02245     {
02246         free_matrix_cd(*m_out); *m_out = NULL;
02247         return JWSC_EIO(strerror(errno));
02248     }
02249 
02250     return NULL;
02251 }
02252 
02289 Error* read_matrix_with_header_fp_u32(Matrix_u32** m_out, FILE* fp)
02290 {
02291     uint32_t    num_rows, num_cols;
02292     uint32_t    row, col;
02293     Matrix_u32* m;
02294 
02295     if (skip_fp_spaces_and_comments(fp) == 0)
02296     {
02297         free_matrix_u32(*m_out); *m_out = NULL;
02298         return JWSC_EIO("No matrix to read in file");
02299     }
02300 
02301     if (fscanf(fp, "num_rows=%u num_cols=%u\n", &num_rows, &num_cols) != 2)
02302     {
02303         free_matrix_u32(*m_out); *m_out = NULL;
02304         return JWSC_EIO("File to read matrix from not formatted properly");
02305     }
02306 
02307     create_matrix_u32(m_out, num_rows, num_cols);
02308     m = *m_out;
02309 
02310     for (row = 0; row < num_rows; row++)
02311     {
02312         for (col = 0; col < num_cols; col++)
02313         {
02314             if (fscanf(fp, "%u", &(m->elts[ row ][ col ])) != 1)
02315             {
02316                 free_matrix_u32(*m_out); *m_out = NULL;
02317                 return JWSC_EIO("File to read matrix from not formatted properly");
02318             }
02319         }
02320     }
02321 
02322     return NULL;
02323 }
02324 
02349 Error* read_matrix_with_header_fp_i32(Matrix_i32** m_out, FILE* fp)
02350 {
02351     uint32_t    num_rows, num_cols;
02352     uint32_t    row, col;
02353     Matrix_i32* m;
02354 
02355     if (skip_fp_spaces_and_comments(fp) == 0)
02356     {
02357         free_matrix_i32(*m_out); *m_out = NULL;
02358         return JWSC_EIO("No matrix to read in file");
02359     }
02360 
02361     if (fscanf(fp, "num_rows=%u num_cols=%u\n", &num_rows, &num_cols) != 2)
02362     {
02363         free_matrix_i32(*m_out); *m_out = NULL;
02364         return JWSC_EIO("File to read matrix from not formatted properly");
02365     }
02366 
02367     create_matrix_i32(m_out, num_rows, num_cols);
02368     m = *m_out;
02369 
02370     for (row = 0; row < num_rows; row++)
02371     {
02372         for (col = 0; col < num_cols; col++)
02373         {
02374             if (fscanf(fp, "%d", &(m->elts[ row ][ col ])) != 1)
02375             {
02376                 free_matrix_i32(*m_out); *m_out = NULL;
02377                 return JWSC_EIO("File to read matrix from not formatted properly");
02378             }
02379         }
02380     }
02381 
02382     return NULL;
02383 }
02384 
02409 Error* read_matrix_with_header_fp_i64(Matrix_i64** m_out, FILE* fp)
02410 {
02411     uint32_t    num_rows, num_cols;
02412     uint32_t    row, col;
02413     Matrix_i64* m;
02414 
02415     if (skip_fp_spaces_and_comments(fp) == 0)
02416     {
02417         free_matrix_i64(*m_out); *m_out = NULL;
02418         return JWSC_EIO("No matrix to read in file");
02419     }
02420 
02421     if (fscanf(fp, "num_rows=%u num_cols=%u\n", &num_rows, &num_cols) != 2)
02422     {
02423         free_matrix_i64(*m_out); *m_out = NULL;
02424         return JWSC_EIO("File to read matrix from not formatted properly");
02425     }
02426 
02427     create_matrix_i64(m_out, num_rows, num_cols);
02428     m = *m_out;
02429 
02430     for (row = 0; row < num_rows; row++)
02431     {
02432         for (col = 0; col < num_cols; col++)
02433         {
02434             if (fscanf(fp, "%" SCNd64, &(m->elts[ row ][ col ])) != 1)
02435             {
02436                 free_matrix_i64(*m_out); *m_out = NULL;
02437                 return JWSC_EIO("File to read matrix from not formatted properly");
02438             }
02439         }
02440     }
02441 
02442     return NULL;
02443 }
02444 
02469 Error* read_matrix_with_header_fp_f(Matrix_f** m_out, FILE* fp)
02470 {
02471     uint32_t  num_rows, num_cols;
02472     uint32_t  row, col;
02473     Matrix_f* m;
02474 
02475     if (skip_fp_spaces_and_comments(fp) == 0)
02476     {
02477         free_matrix_f(*m_out); *m_out = NULL;
02478         return JWSC_EIO("No matrix to read in file");
02479     }
02480 
02481     if (fscanf(fp, "num_rows=%u num_cols=%u\n", &num_rows, &num_cols) != 2)
02482     {
02483         free_matrix_f(*m_out); *m_out = NULL;
02484         return JWSC_EIO("File to read matrix from not formatted properly");
02485     }
02486 
02487     create_matrix_f(m_out, num_rows, num_cols);
02488     m = *m_out;
02489 
02490     for (row = 0; row < num_rows; row++)
02491     {
02492         for (col = 0; col < num_cols; col++)
02493         {
02494             if (fscanf(fp, "%f", &(m->elts[ row ][ col ])) != 1)
02495             {
02496                 free_matrix_f(*m_out); *m_out = NULL;
02497                 return JWSC_EIO("File to read matrix from not formatted properly");
02498             }
02499         }
02500     }
02501 
02502     return NULL;
02503 }
02504 
02529 Error* read_matrix_with_header_fp_d(Matrix_d** m_out, FILE* fp)
02530 {
02531     uint32_t  num_rows, num_cols;
02532     uint32_t  row, col;
02533     Matrix_d* m;
02534 
02535     if (skip_fp_spaces_and_comments(fp) == 0)
02536     {
02537         free_matrix_d(*m_out); *m_out = NULL;
02538         return JWSC_EIO("No matrix to read in file");
02539     }
02540 
02541     if (fscanf(fp, "num_rows=%u num_cols=%u\n", &num_rows, &num_cols) != 2)
02542     {
02543         free_matrix_d(*m_out); *m_out = NULL;
02544         return JWSC_EIO("File to read matrix from not formatted properly");
02545     }
02546 
02547     create_matrix_d(m_out, num_rows, num_cols);
02548     m = *m_out;
02549 
02550     for (row = 0; row < num_rows; row++)
02551     {
02552         for (col = 0; col < num_cols; col++)
02553         {
02554             if (fscanf(fp, "%lf", &(m->elts[ row ][ col ])) != 1)
02555             {
02556                 free_matrix_d(*m_out); *m_out = NULL;
02557                 return JWSC_EIO("File to read matrix from not formatted properly");
02558             }
02559         }
02560     }
02561 
02562     return NULL;
02563 }
02564 
02589 Error* read_matrix_with_header_fp_cf(Matrix_cf** m_out, FILE* fp)
02590 {
02591     uint32_t   num_rows, num_cols;
02592     uint32_t   row, col;
02593     Matrix_cf* m;
02594 
02595     if (skip_fp_spaces_and_comments(fp) == 0)
02596     {
02597         free_matrix_cf(*m_out); *m_out = NULL;
02598         return JWSC_EIO("No matrix to read in file");
02599     }
02600 
02601     if (fscanf(fp, "num_rows=%u num_cols=%u\n", &num_rows, &num_cols) != 2)
02602     {
02603         free_matrix_cf(*m_out); *m_out = NULL;
02604         return JWSC_EIO("File to read matrix from not formatted properly");
02605     }
02606 
02607     create_matrix_cf(m_out, num_rows, num_cols);
02608     m = *m_out;
02609 
02610     for (row = 0; row < num_rows; row++)
02611     {
02612         for (col = 0; col < num_cols; col++)
02613         {
02614             if (fscanf(fp, "%f %f", &(m->elts[ row ][ col ].r), 
02615                                     &(m->elts[ row ][ col ].i)) != 2)
02616             {
02617                 free_matrix_cf(*m_out); *m_out = NULL;
02618                 return JWSC_EIO("File to read matrix from not formatted properly");
02619             }
02620         }
02621     }
02622 
02623     return NULL;
02624 }
02625 
02650 Error* read_matrix_with_header_fp_cd(Matrix_cd** m_out, FILE* fp)
02651 {
02652     uint32_t   num_rows, num_cols;
02653     uint32_t   row, col;
02654     Matrix_cd* m;
02655 
02656     if (skip_fp_spaces_and_comments(fp) == 0)
02657     {
02658         free_matrix_cd(*m_out); *m_out = NULL;
02659         return JWSC_EIO("No matrix to read in file");
02660     }
02661 
02662     if (fscanf(fp, "num_rows=%u num_cols=%u\n", &num_rows, &num_cols) != 2)
02663     {
02664         free_matrix_cd(*m_out); *m_out = NULL;
02665         return JWSC_EIO("File to read matrix from not formatted properly");
02666     }
02667 
02668     create_matrix_cd(m_out, num_rows, num_cols);
02669     m = *m_out;
02670 
02671     for (row = 0; row < num_rows; row++)
02672     {
02673         for (col = 0; col < num_cols; col++)
02674         {
02675             if (fscanf(fp, "%lf %lf", &(m->elts[ row ][ col ].r),
02676                                       &(m->elts[ row ][ col ].i)) == 2)
02677             {
02678                 free_matrix_cd(*m_out); *m_out = NULL;
02679                 return JWSC_EIO("File to read matrix from not formatted properly");
02680             }
02681         }
02682     }
02683 
02684     return NULL;
02685 }
02686 
02717 Error* write_matrix_with_header_u32(const Matrix_u32* m, const char* fname)
02718 {
02719     FILE* fp;
02720 
02721     if ((fp = fopen(fname, "w")) == NULL)
02722     {
02723         return JWSC_EIO(strerror(errno));
02724     }
02725 
02726     write_matrix_with_header_fp_u32(m, fp);
02727 
02728     if (fclose(fp) != 0)
02729     {
02730         return JWSC_EIO(strerror(errno));
02731     }
02732 
02733     return NULL;
02734 }
02735 
02754 Error* write_matrix_with_header_i32(const Matrix_i32* m, const char* fname)
02755 {
02756     FILE* fp;
02757 
02758     if ((fp = fopen(fname, "w")) == NULL)
02759     {
02760         return JWSC_EIO(strerror(errno));
02761     }
02762 
02763     write_matrix_with_header_fp_i32(m, fp);
02764 
02765     if (fclose(fp) != 0)
02766     {
02767         return JWSC_EIO(strerror(errno));
02768     }
02769 
02770     return NULL;
02771 }
02772 
02791 Error* write_matrix_with_header_i64(const Matrix_i64* m, const char* fname)
02792 {
02793     FILE* fp;
02794 
02795     if ((fp = fopen(fname, "w")) == NULL)
02796     {
02797         return JWSC_EIO(strerror(errno));
02798     }
02799 
02800     write_matrix_with_header_fp_i64(m, fp);
02801 
02802     if (fclose(fp) != 0)
02803     {
02804         return JWSC_EIO(strerror(errno));
02805     }
02806 
02807     return NULL;
02808 }
02809 
02828 Error* write_matrix_with_header_f(const Matrix_f* m, const char* fname)
02829 {
02830     FILE* fp;
02831 
02832     if ((fp = fopen(fname, "w")) == NULL)
02833     {
02834         return JWSC_EIO(strerror(errno));
02835     }
02836 
02837     write_matrix_with_header_fp_f(m, fp);
02838 
02839     if (fclose(fp) != 0)
02840     {
02841         return JWSC_EIO(strerror(errno));
02842     }
02843 
02844     return NULL;
02845 }
02846 
02865 Error* write_matrix_with_header_d(const Matrix_d* m, const char* fname)
02866 {
02867     FILE* fp;
02868 
02869     if ((fp = fopen(fname, "w")) == NULL)
02870     {
02871         return JWSC_EIO(strerror(errno));
02872     }
02873 
02874     write_matrix_with_header_fp_d(m, fp);
02875 
02876     if (fclose(fp) != 0)
02877     {
02878         return JWSC_EIO(strerror(errno));
02879     }
02880 
02881     return NULL;
02882 }
02883 
02902 Error* write_matrix_with_header_cf(const Matrix_cf* m, const char* fname)
02903 {
02904     FILE* fp;
02905 
02906     if ((fp = fopen(fname, "w")) == NULL)
02907     {
02908         return JWSC_EIO(strerror(errno));
02909     }
02910 
02911     write_matrix_with_header_fp_cf(m, fp);
02912 
02913     if (fclose(fp) != 0)
02914     {
02915         return JWSC_EIO(strerror(errno));
02916     }
02917 
02918     return NULL;
02919 }
02920 
02939 Error* write_matrix_with_header_cd(const Matrix_cd* m, const char* fname)
02940 {
02941     FILE* fp;
02942 
02943     if ((fp = fopen(fname, "w")) == NULL)
02944     {
02945         return JWSC_EIO(strerror(errno));
02946     }
02947 
02948     write_matrix_with_header_fp_cd(m, fp);
02949 
02950     if (fclose(fp) != 0)
02951     {
02952         return JWSC_EIO(strerror(errno));
02953     }
02954 
02955     return NULL;
02956 }
02957 
02988 Error* write_matrix_with_header_fp_u32(const Matrix_u32* m, FILE* fp)
02989 {
02990     uint32_t num_rows, num_cols;
02991     uint32_t row, col;
02992 
02993     num_rows = m->num_rows;
02994     num_cols = m->num_cols;
02995     fprintf(fp, "num_rows=%u num_cols=%u\n", num_rows, num_cols);
02996 
02997     for (row = 0; row < num_rows; row++)
02998     {
02999         for (col = 0; col < num_cols; col++)
03000         {
03001             fprintf(fp, "%u ", m->elts[ row ][ col ]);
03002         }
03003         fprintf(fp, "\n");
03004     }
03005 
03006     return NULL;
03007 }
03008 
03027 Error* write_matrix_with_header_fp_i32(const Matrix_i32* m, FILE* fp)
03028 {
03029     uint32_t num_rows, num_cols;
03030     uint32_t row, col;
03031 
03032     num_rows = m->num_rows;
03033     num_cols = m->num_cols;
03034     fprintf(fp, "num_rows=%u num_cols=%u\n", num_rows, num_cols);
03035 
03036     for (row = 0; row < num_rows; row++)
03037     {
03038         for (col = 0; col < num_cols; col++)
03039         {
03040             fprintf(fp, "%d ", m->elts[ row ][ col ]);
03041         }
03042         fprintf(fp, "\n");
03043     }
03044 
03045     return NULL;
03046 }
03047 
03066 Error* write_matrix_with_header_fp_i64(const Matrix_i64* m, FILE* fp)
03067 {
03068     uint32_t num_rows, num_cols;
03069     uint32_t row, col;
03070 
03071     num_rows = m->num_rows;
03072     num_cols = m->num_cols;
03073     fprintf(fp, "num_rows=%u num_cols=%u\n", num_rows, num_cols);
03074 
03075     for (row = 0; row < num_rows; row++)
03076     {
03077         for (col = 0; col < num_cols; col++)
03078         {
03079             fprintf(fp, "%" PRId64 " ", m->elts[ row ][ col ]);
03080         }
03081         fprintf(fp, "\n");
03082     }
03083 
03084     return NULL;
03085 }
03086 
03105 Error* write_matrix_with_header_fp_f(const Matrix_f* m, FILE* fp)
03106 {
03107     uint32_t num_rows, num_cols;
03108     uint32_t row, col;
03109 
03110     num_rows = m->num_rows;
03111     num_cols = m->num_cols;
03112     fprintf(fp, "num_rows=%u num_cols=%u\n", num_rows, num_cols);
03113 
03114     for (row = 0; row < num_rows; row++)
03115     {
03116         for (col = 0; col < num_cols; col++)
03117         {
03118             fprintf(fp, "%f ", m->elts[ row ][ col ]);
03119         }
03120         fprintf(fp, "\n");
03121     }
03122 
03123     return NULL;
03124 }
03125 
03144 Error* write_matrix_with_header_fp_d(const Matrix_d* m, FILE* fp)
03145 {
03146     uint32_t num_rows, num_cols;
03147     uint32_t row, col;
03148 
03149     num_rows = m->num_rows;
03150     num_cols = m->num_cols;
03151     fprintf(fp, "num_rows=%u num_cols=%u\n", num_rows, num_cols);
03152 
03153     for (row = 0; row < num_rows; row++)
03154     {
03155         for (col = 0; col < num_cols; col++)
03156         {
03157             fprintf(fp, "%.8e ", m->elts[ row ][ col ]);
03158         }
03159         fprintf(fp, "\n");
03160     }
03161 
03162     return NULL;
03163 }
03164 
03183 Error* write_matrix_with_header_fp_cf(const Matrix_cf* m, FILE* fp)
03184 {
03185     uint32_t num_rows, num_cols;
03186     uint32_t row, col;
03187 
03188     num_rows = m->num_rows;
03189     num_cols = m->num_cols;
03190     fprintf(fp, "num_rows=%u num_cols=%u\n", num_rows, num_cols);
03191 
03192     for (row = 0; row < num_rows; row++)
03193     {
03194         for (col = 0; col < num_cols; col++)
03195         {
03196             fprintf(fp, "%f %f ", m->elts[ row ][ col ].r,
03197                                   m->elts[ row ][ col ].i);
03198         }
03199         fprintf(fp, "\n");
03200     }
03201 
03202     return NULL;
03203 }
03204 
03223 Error* write_matrix_with_header_fp_cd(const Matrix_cd* m, FILE* fp)
03224 {
03225     uint32_t num_rows, num_cols;
03226     uint32_t row, col;
03227 
03228     num_rows = m->num_rows;
03229     num_cols = m->num_cols;
03230     fprintf(fp, "num_rows=%u num_cols=%u\n", num_rows, num_cols);
03231 
03232     for (row = 0; row < num_rows; row++)
03233     {
03234         for (col = 0; col < num_cols; col++)
03235         {
03236             fprintf(fp, "%lf %lf ", m->elts[ row ][ col ].r,
03237                                     m->elts[ row ][ col ].i);
03238         }
03239         fprintf(fp, "\n");
03240     }
03241 
03242     return NULL;
03243 }
03244