@@ -915,11 +915,18 @@ diskio::save_csv_ascii(const Mat<eT>& x, std::ostream& f, const char separator)
915915 uword x_n_rows = x.n_rows ;
916916 uword x_n_cols = x.n_cols ;
917917
918+ const eT eT_int_lowest = eT (std::numeric_limits<int >::lowest ());
919+ const eT eT_int_max = eT (std::numeric_limits<int >::max ());
920+
918921 for (uword row=0 ; row < x_n_rows; ++row)
919922 {
920923 for (uword col=0 ; col < x_n_cols; ++col)
921924 {
922- arma_ostream::raw_print_elem (f, x.at (row,col));
925+ const eT val = x.at (row,col);
926+
927+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest) && (val < eT_int_max) && (eT (int (val)) == val);
928+
929+ (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
923930
924931 if ( col < (x_n_cols-1 ) ) { f.put (separator); }
925932 }
@@ -950,6 +957,9 @@ diskio::save_csv_ascii(const Mat< std::complex<T> >& x, std::ostream& f, const c
950957
951958 diskio::prepare_stream<eT>(f);
952959
960+ const T T_int_lowest = T (std::numeric_limits<int >::lowest ());
961+ const T T_int_max = T (std::numeric_limits<int >::max ());
962+
953963 uword x_n_rows = x.n_rows ;
954964 uword x_n_cols = x.n_cols ;
955965
@@ -959,14 +969,20 @@ diskio::save_csv_ascii(const Mat< std::complex<T> >& x, std::ostream& f, const c
959969 {
960970 const eT& val = x.at (row,col);
961971
962- const T tmp_r = std::real (val);
963- const T tmp_i = std::imag (val);
964- const T tmp_i_abs = (tmp_i < T (0 )) ? T (-tmp_i) : T (tmp_i);
965- const char tmp_sign = (tmp_i < T (0 )) ? char (' -' ) : char (' +' );
972+ const T val_r = std::real (val);
973+ const T val_i = std::imag (val);
974+ const T abs_i = (val_i < T (0 )) ? T (-val_i) : T (val_i);
975+ const char sgn_i = (val_i < T (0 )) ? char (' -' ) : char (' +' );
976+
977+ const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lowest) && (val_r < T_int_max) && (T (int (val_r)) == val_r);
978+ const bool abs_i_is_real_int = (is_real<T>::yes) && arma_isfinite (abs_i) && (abs_i < T_int_max) && (T (int (abs_i)) == abs_i);
979+
980+ (val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
981+
982+ f.put (sgn_i);
983+
984+ (abs_i_is_real_int) ? arma_ostream::raw_print_elem (f, int (abs_i)) : arma_ostream::raw_print_elem (f, abs_i);
966985
967- arma_ostream::raw_print_elem (f, tmp_r );
968- f.put (tmp_sign);
969- arma_ostream::raw_print_elem (f, tmp_i_abs);
970986 f.put (' i' );
971987
972988 if ( col < (x_n_cols-1 ) ) { f.put (separator); }
@@ -1025,15 +1041,25 @@ diskio::save_coord_ascii(const Mat<eT>& x, std::ostream& f)
10251041
10261042 diskio::prepare_stream<eT>(f);
10271043
1044+ const eT eT_zero = eT (0 );
1045+ const eT eT_int_lowest = eT (std::numeric_limits<int >::lowest ());
1046+ const eT eT_int_max = eT (std::numeric_limits<int >::max ());
1047+
10281048 for (uword col=0 ; col < x.n_cols ; ++col)
10291049 for (uword row=0 ; row < x.n_rows ; ++row)
10301050 {
10311051 const eT val = x.at (row,col);
10321052
1033- if (val != eT (0 ))
1034- {
1035- f << row << ' ' << col << ' ' << val << ' \n ' ;
1036- }
1053+ if (val == eT_zero) { continue ; }
1054+
1055+ f << row; f.put (' ' );
1056+ f << col; f.put (' ' );
1057+
1058+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest) && (val < eT_int_max) && (eT (int (val)) == val);
1059+
1060+ (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
1061+
1062+ f.put (' \n ' );
10371063 }
10381064
10391065 // make sure it's possible to figure out the matrix size later
@@ -1070,17 +1096,33 @@ diskio::save_coord_ascii(const Mat< std::complex<T> >& x, std::ostream& f)
10701096
10711097 diskio::prepare_stream<eT>(f);
10721098
1073- const eT eT_zero = eT (0 );
1099+ const eT eT_zero = eT (0 );
1100+ const T T_int_lowest = T (std::numeric_limits<int >::lowest ());
1101+ const T T_int_max = T (std::numeric_limits<int >::max ());
10741102
10751103 for (uword col=0 ; col < x.n_cols ; ++col)
10761104 for (uword row=0 ; row < x.n_rows ; ++row)
10771105 {
10781106 const eT& val = x.at (row,col);
10791107
1080- if (val != eT_zero)
1081- {
1082- f << row << ' ' << col << ' ' << val.real () << ' ' << val.imag () << ' \n ' ;
1083- }
1108+ if (val == eT_zero) { continue ; }
1109+
1110+ f << row; f.put (' ' );
1111+ f << col; f.put (' ' );
1112+
1113+ const T val_r = std::real (val);
1114+ const T val_i = std::imag (val);
1115+
1116+ const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lowest) && (val_r < T_int_max) && (T (int (val_r)) == val_r);
1117+ const bool val_i_is_real_int = (is_real<T>::yes) && arma_isfinite (val_i) && (val_i > T_int_lowest) && (val_i < T_int_max) && (T (int (val_i)) == val_i);
1118+
1119+ (val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
1120+
1121+ f.put (' ' );
1122+
1123+ (val_i_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_i)) : arma_ostream::raw_print_elem (f, val_i);
1124+
1125+ f.put (' \n ' );
10841126 }
10851127
10861128 // make sure it's possible to figure out the matrix size later
@@ -2904,7 +2946,9 @@ diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f, const char separator
29042946 uword x_n_rows = x.n_rows ;
29052947 uword x_n_cols = x.n_cols ;
29062948
2907- const eT eT_zero = eT (0 );
2949+ const eT eT_zero = eT (0 );
2950+ const eT eT_int_lowest = eT (std::numeric_limits<int >::lowest ());
2951+ const eT eT_int_max = eT (std::numeric_limits<int >::max ());
29082952
29092953 for (uword row=0 ; row < x_n_rows; ++row)
29102954 {
@@ -2918,7 +2962,9 @@ diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f, const char separator
29182962 }
29192963 else
29202964 {
2921- arma_ostream::raw_print_elem (f, val);
2965+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest) && (val < eT_int_max) && (eT (int (val)) == val);
2966+
2967+ (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
29222968 }
29232969
29242970 if ( col < (x_n_cols-1 ) ) { f.put (separator); }
@@ -2998,14 +3044,24 @@ diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f)
29983044
29993045 diskio::prepare_stream<eT>(f);
30003046
3047+ const eT eT_int_lowest = eT (std::numeric_limits<int >::lowest ());
3048+ const eT eT_int_max = eT (std::numeric_limits<int >::max ());
3049+
30013050 typename SpMat<eT>::const_iterator iter = x.begin ();
30023051 typename SpMat<eT>::const_iterator iter_end = x.end ();
30033052
30043053 for (; iter != iter_end; ++iter)
30053054 {
3055+ f << iter.row (); f.put (' ' );
3056+ f << iter.col (); f.put (' ' );
3057+
30063058 const eT val = (*iter);
30073059
3008- f << iter.row () << ' ' << iter.col () << ' ' << val << ' \n ' ;
3060+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest) && (val < eT_int_max) && (eT (int (val)) == val);
3061+
3062+ (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
3063+
3064+ f.put (' \n ' );
30093065 }
30103066
30113067
@@ -3044,14 +3100,32 @@ diskio::save_coord_ascii(const SpMat< std::complex<T> >& x, std::ostream& f)
30443100
30453101 diskio::prepare_stream<eT>(f);
30463102
3103+ const T T_int_lowest = T (std::numeric_limits<int >::lowest ());
3104+ const T T_int_max = T (std::numeric_limits<int >::max ());
3105+
30473106 typename SpMat<eT>::const_iterator iter = x.begin ();
30483107 typename SpMat<eT>::const_iterator iter_end = x.end ();
30493108
30503109 for (; iter != iter_end; ++iter)
30513110 {
3111+ f << iter.row (); f.put (' ' );
3112+ f << iter.col (); f.put (' ' );
3113+
30523114 const eT val = (*iter);
30533115
3054- f << iter.row () << ' ' << iter.col () << ' ' << val.real () << ' ' << val.imag () << ' \n ' ;
3116+ const T val_r = std::real (val);
3117+ const T val_i = std::imag (val);
3118+
3119+ const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lowest) && (val_r < T_int_max) && (T (int (val_r)) == val_r);
3120+ const bool val_i_is_real_int = (is_real<T>::yes) && arma_isfinite (val_i) && (val_i > T_int_lowest) && (val_i < T_int_max) && (T (int (val_i)) == val_i);
3121+
3122+ (val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
3123+
3124+ f.put (' ' );
3125+
3126+ (val_i_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_i)) : arma_ostream::raw_print_elem (f, val_i);
3127+
3128+ f.put (' \n ' );
30553129 }
30563130
30573131 // make sure it's possible to figure out the matrix size later
0 commit comments