@@ -445,10 +445,10 @@ diskio::convert_token(eT& val, const std::string& token)
445445 {
446446 const size_t N = size_t (token.length ());
447447
448- if (N == 0 ) { val = eT (0 ); return true ; }
449-
450448 const char * str = token.c_str ();
451449
450+ if ( (N == 0 ) || ((N == 1 ) && (str[0 ] == ' 0' )) ) { val = eT (0 ); return true ; }
451+
452452 if ( (N == 3 ) || (N == 4 ) )
453453 {
454454 const bool neg = (str[0 ] == ' -' );
@@ -1075,7 +1075,7 @@ diskio::save_coord_ascii(const Mat< std::complex<T> >& x, std::ostream& f)
10751075 for (uword col=0 ; col < x.n_cols ; ++col)
10761076 for (uword row=0 ; row < x.n_rows ; ++row)
10771077 {
1078- const eT val = x.at (row,col);
1078+ const eT& val = x.at (row,col);
10791079
10801080 if (val != eT_zero)
10811081 {
@@ -1182,11 +1182,6 @@ diskio::save_pgm_binary(const Mat<eT>& x, const std::string& final_name)
11821182
11831183
11841184
1185- //
1186- // TODO:
1187- // add functionality to save the image in a normalised format,
1188- // ie. scaled so that every value falls in the [0,255] range.
1189-
11901185// ! Save a matrix as a PGM greyscale image
11911186template <typename eT>
11921187inline
@@ -2221,10 +2216,7 @@ diskio::load_coord_ascii(Mat<eT>& x, std::istream& f, std::string& err_msg)
22212216
22222217 line_stream >> token;
22232218
2224- if (line_stream.fail () == false )
2225- {
2226- diskio::convert_token ( val, token );
2227- }
2219+ if (line_stream.fail () == false ) { diskio::convert_token ( val, token ); }
22282220
22292221 if (val != eT (0 )) { tmp (line_row,line_col) = val; }
22302222 }
@@ -2325,18 +2317,11 @@ diskio::load_coord_ascii(Mat< std::complex<T> >& x, std::istream& f, std::string
23252317
23262318 line_stream >> token_real;
23272319
2328- if (line_stream.fail () == false )
2329- {
2330- diskio::convert_token ( val_real, token_real );
2331- }
2332-
2320+ if (line_stream.fail () == false ) { diskio::convert_token ( val_real, token_real ); }
23332321
23342322 line_stream >> token_imag;
23352323
2336- if (line_stream.fail () == false )
2337- {
2338- diskio::convert_token ( val_imag, token_imag );
2339- }
2324+ if (line_stream.fail () == false ) { diskio::convert_token ( val_imag, token_imag ); }
23402325
23412326 if ( (val_real != T (0 )) || (val_imag != T (0 )) )
23422327 {
@@ -2919,13 +2904,22 @@ diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f, const char separator
29192904 uword x_n_rows = x.n_rows ;
29202905 uword x_n_cols = x.n_cols ;
29212906
2907+ const eT eT_zero = eT (0 );
2908+
29222909 for (uword row=0 ; row < x_n_rows; ++row)
29232910 {
29242911 for (uword col=0 ; col < x_n_cols; ++col)
29252912 {
29262913 const eT val = x.at (row,col);
29272914
2928- if (val != eT (0 )) { arma_ostream::raw_print_elem (f, val); }
2915+ if (val == eT_zero)
2916+ {
2917+ f.put (' 0' );
2918+ }
2919+ else
2920+ {
2921+ arma_ostream::raw_print_elem (f, val);
2922+ }
29292923
29302924 if ( col < (x_n_cols-1 ) ) { f.put (separator); }
29312925 }
@@ -3426,10 +3420,7 @@ diskio::load_coord_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg)
34263420
34273421 line_stream >> token;
34283422
3429- if (line_stream.fail () == false )
3430- {
3431- diskio::convert_token ( val, token );
3432- }
3423+ if (line_stream.fail () == false ) { diskio::convert_token ( val, token ); }
34333424
34343425 if (val != eT (0 )) { tmp (line_row,line_col) = val; }
34353426 }
@@ -3530,18 +3521,11 @@ diskio::load_coord_ascii(SpMat< std::complex<T> >& x, std::istream& f, std::stri
35303521
35313522 line_stream >> token_real;
35323523
3533- if (line_stream.fail () == false )
3534- {
3535- diskio::convert_token ( val_real, token_real );
3536- }
3537-
3524+ if (line_stream.fail () == false ) { diskio::convert_token ( val_real, token_real ); }
35383525
35393526 line_stream >> token_imag;
35403527
3541- if (line_stream.fail () == false )
3542- {
3543- diskio::convert_token ( val_imag, token_imag );
3544- }
3528+ if (line_stream.fail () == false ) { diskio::convert_token ( val_imag, token_imag ); }
35453529
35463530 if ( (val_real != T (0 )) || (val_imag != T (0 )) )
35473531 {
0 commit comments