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