arma_ostream_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // 
00007 // This file is part of the Armadillo C++ library.
00008 // It is provided without any warranty of fitness
00009 // for any purpose. You can redistribute this file
00010 // and/or modify it under the terms of the GNU
00011 // Lesser General Public License (LGPL) as published
00012 // by the Free Software Foundation, either version 3
00013 // of the License or (at your option) any later version.
00014 // (see http://www.opensource.org/licenses for more info)
00015 
00016 
00017 //! \addtogroup arma_ostream
00018 //! @{
00019 
00020 
00021 
00022 inline
00023 arma_ostream_state::arma_ostream_state(const std::ostream& o)
00024   : orig_flags    (o.flags())
00025   , orig_precision(o.precision())
00026   , orig_width    (o.width())
00027   , orig_fill     (o.fill())
00028   {
00029   }
00030 
00031 
00032 
00033 inline
00034 void
00035 arma_ostream_state::restore(std::ostream& o) const
00036   {
00037   o.flags    (orig_flags);
00038   o.precision(orig_precision);
00039   o.width    (orig_width);
00040   o.fill     (orig_fill);
00041   }
00042 
00043 
00044 
00045 //
00046 //
00047 
00048 
00049 
00050 template<typename eT>
00051 inline
00052 u32
00053 arma_ostream::modify_stream(std::ostream& o, const eT* data, const u32 n_elem)
00054   {
00055   o.unsetf(ios::showbase);
00056   o.unsetf(ios::uppercase);
00057   o.unsetf(ios::showpos);
00058   
00059   o.fill(' ');
00060   
00061   u32 cell_width;
00062   
00063   bool use_layout_B = false;
00064   bool use_layout_C = false;
00065   
00066   for(u32 i=0; i<n_elem; ++i)
00067     {
00068     const eT val = data[i];
00069     
00070     if(
00071       val >= eT(+100) ||
00072       ( (is_signed<eT>::value == true) && (val <= eT(-100)) ) ||
00073       ( (is_non_integral<eT>::value == true) && (val > eT(0)) && (val <= eT(+1e-4)) ) ||
00074       ( (is_non_integral<eT>::value == true) && (is_signed<eT>::value == true) && (val < eT(0)) && (val >= eT(-1e-4)) ) 
00075       )
00076       {
00077       use_layout_C = true;
00078       break;
00079       }
00080       
00081     if(
00082       (val >= eT(+10)) || ( (is_signed<eT>::value == true) && (val <= eT(-10)) )
00083       )
00084       {
00085       use_layout_B = true;
00086       }
00087     }
00088   
00089   if(use_layout_C == true)
00090     {
00091     o.setf(ios::scientific);
00092     o.setf(ios::right);
00093     o.unsetf(ios::fixed);
00094     o.precision(4);
00095     cell_width = 13;
00096     }
00097   else
00098   if(use_layout_B == true)
00099     {
00100     o.unsetf(ios::scientific);
00101     o.setf(ios::right);
00102     o.setf(ios::fixed);
00103     o.precision(4);
00104     cell_width = 10;
00105     }
00106   else
00107     {
00108     o.unsetf(ios::scientific);
00109     o.setf(ios::right);
00110     o.setf(ios::fixed);
00111     o.precision(4);
00112     cell_width = 9;
00113     }
00114   
00115   return cell_width;
00116   }
00117 
00118 
00119 
00120 //! "better than nothing" settings for complex numbers
00121 template<typename T>
00122 inline
00123 u32
00124 arma_ostream::modify_stream(std::ostream& o, const std::complex<T>* data, const u32 n_elem)
00125   {
00126   o.unsetf(ios::showbase);
00127   o.unsetf(ios::uppercase);
00128   o.fill(' ');
00129   
00130   o.setf(ios::scientific);
00131   o.setf(ios::showpos);
00132   o.setf(ios::right);
00133   o.unsetf(ios::fixed);
00134   
00135   u32 cell_width;
00136   
00137   o.precision(3);
00138   cell_width = 2 + 2*(1 + 3 + o.precision() + 5) + 1;
00139 
00140   return cell_width;
00141   }
00142 
00143 
00144 
00145 //! Print an element to the specified stream
00146 template<typename eT>
00147 arma_inline
00148 void
00149 arma_ostream::print_elem(std::ostream& o, const eT& x)
00150   {
00151   if(x != eT(0))
00152     {
00153     o << x;
00154     }
00155   else
00156     {
00157     const std::streamsize orig_precision = o.precision();
00158     
00159     o.precision(0);
00160     
00161     o << eT(0);
00162     
00163     o.precision(orig_precision);
00164     }
00165   }
00166 
00167 
00168 
00169 //! Print a complex element to the specified stream
00170 //! EXPERIMENTAL !
00171 template<typename T>
00172 arma_inline
00173 void
00174 arma_ostream::print_elem(std::ostream& o, const std::complex<T>& x)
00175   {
00176   if( (x.real() != T(0)) || (x.imag() != T(0)) )
00177     {
00178     std::ostringstream ss;
00179     ss.flags(o.flags());
00180     //ss.imbue(o.getloc());
00181     ss.precision(o.precision());
00182   
00183     ss << '(' << x.real() << ',' << x.imag() << ')';
00184     o << ss.str();
00185     }
00186   else
00187     {
00188     o << "(0,0)";
00189     }
00190   }
00191 
00192 
00193 
00194 //! Print a matrix to the specified stream
00195 template<typename eT>
00196 inline
00197 void
00198 arma_ostream::print(std::ostream& o, const Mat<eT>& m, const bool modify)
00199   {
00200   arma_extra_debug_sigprint();
00201   
00202   const arma_ostream_state stream_state(o);
00203 
00204   u32 cell_width;
00205   
00206   if(modify == true)
00207     {
00208     cell_width = arma_ostream::modify_stream(o, m.memptr(), m.n_elem);
00209     }
00210   else
00211     {
00212     cell_width = o.width();  // copy the user's cell width
00213     }
00214   
00215   if(cell_width > 0)
00216     {
00217     for(u32 row=0; row < m.n_rows; ++row)
00218       {
00219       for(u32 col=0; col < m.n_cols; ++col)
00220         {
00221         // the cell width appears to be reset after each element is printed,
00222         // hence we need to restore it
00223         o.width(cell_width);
00224         arma_ostream::print_elem(o, m.at(row,col));
00225         }
00226       
00227       o << '\n';
00228       }
00229     }
00230   else
00231     {
00232     for(u32 row=0; row < m.n_rows; ++row)
00233       {
00234       for(u32 col=0; col < m.n_cols-1; ++col)
00235         {
00236         arma_ostream::print_elem(o, m.at(row,col));
00237         o << ' ';
00238         }
00239       
00240       arma_ostream::print_elem(o, m.at(row, m.n_cols-1));
00241       o << '\n';
00242       }
00243     }
00244   
00245   o.flush();
00246   stream_state.restore(o);
00247   }
00248 
00249 
00250 
00251 //! Print a cube to the specified stream
00252 template<typename eT>
00253 inline
00254 void
00255 arma_ostream::print(std::ostream& o, const Cube<eT>& x, const bool modify)
00256   {
00257   arma_extra_debug_sigprint();
00258   
00259   const arma_ostream_state stream_state(o);
00260 
00261   u32 cell_width;
00262   
00263   if(modify == true)
00264     {
00265     cell_width = arma_ostream::modify_stream(o, x.memptr(), x.n_elem);
00266     }
00267   else
00268     {
00269     cell_width = o.width();
00270     }
00271   
00272   for(u32 slice=0; slice < x.n_slices; ++slice)
00273     {
00274     o << "[cube slice " << slice << ']' << '\n';
00275     o.width(cell_width);
00276     arma_ostream::print(o, x.slice(slice), false);
00277     o << '\n';
00278     }
00279 
00280   stream_state.restore(o);
00281   }
00282 
00283 
00284 
00285 
00286 //! Print a field to the specified stream
00287 //! Assumes type oT can be printed, i.e. oT has std::ostream& operator<< (std::ostream&, const oT&) 
00288 template<typename oT>
00289 inline
00290 void
00291 arma_ostream::print(std::ostream& o, const field<oT>& x)
00292   {
00293   arma_extra_debug_sigprint();
00294   
00295   const arma_ostream_state stream_state(o);
00296 
00297   const std::streamsize cell_width = o.width();
00298 
00299   for(u32 col=0; col<x.n_cols; ++col)
00300     {
00301     o << "[field column " << col << ']' << '\n'; 
00302     for(u32 row=0; row<x.n_rows; ++row)
00303       {
00304       o.width(cell_width);
00305       o << x.at(row,col) << '\n';
00306       }
00307     
00308     o << '\n';
00309     }
00310   
00311   o.flush();
00312   stream_state.restore(o);
00313   }
00314 
00315 
00316 
00317 //! Print a subfield to the specified stream
00318 //! Assumes type oT can be printed, i.e. oT has std::ostream& operator<< (std::ostream&, const oT&) 
00319 template<typename oT>
00320 inline
00321 void
00322 arma_ostream::print(std::ostream& o, const subview_field<oT>& x)
00323   {
00324   arma_extra_debug_sigprint();
00325   
00326   const arma_ostream_state stream_state(o);
00327   
00328   const std::streamsize cell_width = o.width();
00329   
00330   for(u32 col=0; col<x.n_cols; ++col)
00331     {
00332     o << "[subfield column " << col << ']' << '\n'; 
00333     for(u32 row=0; row<x.n_rows; ++row)
00334       {
00335       o.width(cell_width);
00336       o << x.at(row,col) << '\n';
00337       }
00338     
00339     o << '\n';
00340     }
00341   
00342   o.flush();
00343   stream_state.restore(o);
00344   }
00345 
00346 
00347 
00348 //! @}