annotate armadillo-2.4.4/include/armadillo_bits/arma_ostream_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
rev   line source
max@0 1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
max@0 2 // Copyright (C) 2008-2011 Conrad Sanderson
max@0 3 //
max@0 4 // This file is part of the Armadillo C++ library.
max@0 5 // It is provided without any warranty of fitness
max@0 6 // for any purpose. You can redistribute this file
max@0 7 // and/or modify it under the terms of the GNU
max@0 8 // Lesser General Public License (LGPL) as published
max@0 9 // by the Free Software Foundation, either version 3
max@0 10 // of the License or (at your option) any later version.
max@0 11 // (see http://www.opensource.org/licenses for more info)
max@0 12
max@0 13
max@0 14 //! \addtogroup arma_ostream
max@0 15 //! @{
max@0 16
max@0 17
max@0 18
max@0 19 inline
max@0 20 arma_ostream_state::arma_ostream_state(const std::ostream& o)
max@0 21 : orig_flags (o.flags())
max@0 22 , orig_precision(o.precision())
max@0 23 , orig_width (o.width())
max@0 24 , orig_fill (o.fill())
max@0 25 {
max@0 26 }
max@0 27
max@0 28
max@0 29
max@0 30 inline
max@0 31 void
max@0 32 arma_ostream_state::restore(std::ostream& o) const
max@0 33 {
max@0 34 o.flags (orig_flags);
max@0 35 o.precision(orig_precision);
max@0 36 o.width (orig_width);
max@0 37 o.fill (orig_fill);
max@0 38 }
max@0 39
max@0 40
max@0 41
max@0 42 //
max@0 43 //
max@0 44
max@0 45
max@0 46
max@0 47 template<typename eT>
max@0 48 inline
max@0 49 std::streamsize
max@0 50 arma_ostream::modify_stream(std::ostream& o, const eT* data, const uword n_elem)
max@0 51 {
max@0 52 o.unsetf(ios::showbase);
max@0 53 o.unsetf(ios::uppercase);
max@0 54 o.unsetf(ios::showpos);
max@0 55
max@0 56 o.fill(' ');
max@0 57
max@0 58 std::streamsize cell_width;
max@0 59
max@0 60 bool use_layout_B = false;
max@0 61 bool use_layout_C = false;
max@0 62
max@0 63 for(uword i=0; i<n_elem; ++i)
max@0 64 {
max@0 65 const eT val = data[i];
max@0 66
max@0 67 if(
max@0 68 val >= eT(+100) ||
max@0 69 ( (is_signed<eT>::value == true) && (val <= eT(-100)) ) ||
max@0 70 ( (is_non_integral<eT>::value == true) && (val > eT(0)) && (val <= eT(+1e-4)) ) ||
max@0 71 ( (is_non_integral<eT>::value == true) && (is_signed<eT>::value == true) && (val < eT(0)) && (val >= eT(-1e-4)) )
max@0 72 )
max@0 73 {
max@0 74 use_layout_C = true;
max@0 75 break;
max@0 76 }
max@0 77
max@0 78 if(
max@0 79 (val >= eT(+10)) || ( (is_signed<eT>::value == true) && (val <= eT(-10)) )
max@0 80 )
max@0 81 {
max@0 82 use_layout_B = true;
max@0 83 }
max@0 84 }
max@0 85
max@0 86 if(use_layout_C == true)
max@0 87 {
max@0 88 o.setf(ios::scientific);
max@0 89 o.setf(ios::right);
max@0 90 o.unsetf(ios::fixed);
max@0 91 o.precision(4);
max@0 92 cell_width = 13;
max@0 93 }
max@0 94 else
max@0 95 if(use_layout_B == true)
max@0 96 {
max@0 97 o.unsetf(ios::scientific);
max@0 98 o.setf(ios::right);
max@0 99 o.setf(ios::fixed);
max@0 100 o.precision(4);
max@0 101 cell_width = 10;
max@0 102 }
max@0 103 else
max@0 104 {
max@0 105 o.unsetf(ios::scientific);
max@0 106 o.setf(ios::right);
max@0 107 o.setf(ios::fixed);
max@0 108 o.precision(4);
max@0 109 cell_width = 9;
max@0 110 }
max@0 111
max@0 112 return cell_width;
max@0 113 }
max@0 114
max@0 115
max@0 116
max@0 117 //! "better than nothing" settings for complex numbers
max@0 118 template<typename T>
max@0 119 inline
max@0 120 std::streamsize
max@0 121 arma_ostream::modify_stream(std::ostream& o, const std::complex<T>* data, const uword n_elem)
max@0 122 {
max@0 123 arma_ignore(data);
max@0 124 arma_ignore(n_elem);
max@0 125
max@0 126 o.unsetf(ios::showbase);
max@0 127 o.unsetf(ios::uppercase);
max@0 128 o.fill(' ');
max@0 129
max@0 130 o.setf(ios::scientific);
max@0 131 o.setf(ios::showpos);
max@0 132 o.setf(ios::right);
max@0 133 o.unsetf(ios::fixed);
max@0 134
max@0 135 std::streamsize cell_width;
max@0 136
max@0 137 o.precision(3);
max@0 138 cell_width = 2 + 2*(1 + 3 + o.precision() + 5) + 1;
max@0 139
max@0 140 return cell_width;
max@0 141 }
max@0 142
max@0 143
max@0 144
max@0 145 template<typename eT>
max@0 146 inline
max@0 147 void
max@0 148 arma_ostream::print_elem_zero(std::ostream& o)
max@0 149 {
max@0 150 const std::streamsize orig_precision = o.precision();
max@0 151
max@0 152 o.precision(0);
max@0 153
max@0 154 o << eT(0);
max@0 155
max@0 156 o.precision(orig_precision);
max@0 157 }
max@0 158
max@0 159
max@0 160
max@0 161 //! Print an element to the specified stream
max@0 162 template<typename eT>
max@0 163 arma_inline
max@0 164 void
max@0 165 arma_ostream::print_elem(std::ostream& o, const eT& x)
max@0 166 {
max@0 167 if(x != eT(0))
max@0 168 {
max@0 169 o << x;
max@0 170 }
max@0 171 else
max@0 172 {
max@0 173 arma_ostream::print_elem_zero<eT>(o);
max@0 174 }
max@0 175 }
max@0 176
max@0 177
max@0 178
max@0 179 //! Print a complex element to the specified stream
max@0 180 template<typename T>
max@0 181 inline
max@0 182 void
max@0 183 arma_ostream::print_elem(std::ostream& o, const std::complex<T>& x)
max@0 184 {
max@0 185 if( (x.real() != T(0)) || (x.imag() != T(0)) )
max@0 186 {
max@0 187 std::ostringstream ss;
max@0 188 ss.flags(o.flags());
max@0 189 //ss.imbue(o.getloc());
max@0 190 ss.precision(o.precision());
max@0 191
max@0 192 ss << '(' << x.real() << ',' << x.imag() << ')';
max@0 193 o << ss.str();
max@0 194 }
max@0 195 else
max@0 196 {
max@0 197 o << "(0,0)";
max@0 198 }
max@0 199 }
max@0 200
max@0 201
max@0 202
max@0 203 //! Print a matrix to the specified stream
max@0 204 template<typename eT>
max@0 205 inline
max@0 206 void
max@0 207 arma_ostream::print(std::ostream& o, const Mat<eT>& m, const bool modify)
max@0 208 {
max@0 209 arma_extra_debug_sigprint();
max@0 210
max@0 211 const arma_ostream_state stream_state(o);
max@0 212
max@0 213 const std::streamsize cell_width = modify ? arma_ostream::modify_stream(o, m.memptr(), m.n_elem) : o.width();
max@0 214
max@0 215 const uword m_n_rows = m.n_rows;
max@0 216 const uword m_n_cols = m.n_cols;
max@0 217
max@0 218 if(m.is_empty() == false)
max@0 219 {
max@0 220 if(m_n_cols > 0)
max@0 221 {
max@0 222 if(cell_width > 0)
max@0 223 {
max@0 224 for(uword row=0; row < m_n_rows; ++row)
max@0 225 {
max@0 226 for(uword col=0; col < m_n_cols; ++col)
max@0 227 {
max@0 228 // the cell width appears to be reset after each element is printed,
max@0 229 // hence we need to restore it
max@0 230 o.width(cell_width);
max@0 231 arma_ostream::print_elem(o, m.at(row,col));
max@0 232 }
max@0 233
max@0 234 o << '\n';
max@0 235 }
max@0 236 }
max@0 237 else
max@0 238 {
max@0 239 for(uword row=0; row < m_n_rows; ++row)
max@0 240 {
max@0 241 for(uword col=0; col < m_n_cols-1; ++col)
max@0 242 {
max@0 243 arma_ostream::print_elem(o, m.at(row,col));
max@0 244 o << ' ';
max@0 245 }
max@0 246
max@0 247 arma_ostream::print_elem(o, m.at(row, m_n_cols-1));
max@0 248 o << '\n';
max@0 249 }
max@0 250 }
max@0 251 }
max@0 252 }
max@0 253 else
max@0 254 {
max@0 255 o << "[matrix size: " << m_n_rows << 'x' << m_n_cols << "]\n";
max@0 256 }
max@0 257
max@0 258 o.flush();
max@0 259 stream_state.restore(o);
max@0 260 }
max@0 261
max@0 262
max@0 263
max@0 264 //! Print a cube to the specified stream
max@0 265 template<typename eT>
max@0 266 inline
max@0 267 void
max@0 268 arma_ostream::print(std::ostream& o, const Cube<eT>& x, const bool modify)
max@0 269 {
max@0 270 arma_extra_debug_sigprint();
max@0 271
max@0 272 const arma_ostream_state stream_state(o);
max@0 273
max@0 274 const std::streamsize cell_width = modify ? arma_ostream::modify_stream(o, x.memptr(), x.n_elem) : o.width();
max@0 275
max@0 276 if(x.is_empty() == false)
max@0 277 {
max@0 278 for(uword slice=0; slice < x.n_slices; ++slice)
max@0 279 {
max@0 280 o << "[cube slice " << slice << ']' << '\n';
max@0 281 o.width(cell_width);
max@0 282 arma_ostream::print(o, x.slice(slice), false);
max@0 283 o << '\n';
max@0 284 }
max@0 285 }
max@0 286 else
max@0 287 {
max@0 288 o << "[cube size: " << x.n_rows << 'x' << x.n_cols << 'x' << x.n_slices << "]\n";
max@0 289 }
max@0 290
max@0 291 stream_state.restore(o);
max@0 292 }
max@0 293
max@0 294
max@0 295
max@0 296
max@0 297 //! Print a field to the specified stream
max@0 298 //! Assumes type oT can be printed, i.e. oT has std::ostream& operator<< (std::ostream&, const oT&)
max@0 299 template<typename oT>
max@0 300 inline
max@0 301 void
max@0 302 arma_ostream::print(std::ostream& o, const field<oT>& x)
max@0 303 {
max@0 304 arma_extra_debug_sigprint();
max@0 305
max@0 306 const arma_ostream_state stream_state(o);
max@0 307
max@0 308 const std::streamsize cell_width = o.width();
max@0 309
max@0 310 const uword x_n_rows = x.n_rows;
max@0 311 const uword x_n_cols = x.n_cols;
max@0 312
max@0 313 if(x.is_empty() == false)
max@0 314 {
max@0 315 for(uword col=0; col<x_n_cols; ++col)
max@0 316 {
max@0 317 o << "[field column " << col << ']' << '\n';
max@0 318
max@0 319 for(uword row=0; row<x_n_rows; ++row)
max@0 320 {
max@0 321 o.width(cell_width);
max@0 322 o << x.at(row,col) << '\n';
max@0 323 }
max@0 324
max@0 325 o << '\n';
max@0 326 }
max@0 327 }
max@0 328 else
max@0 329 {
max@0 330 o << "[field size: " << x_n_rows << 'x' << x_n_cols << "]\n";
max@0 331 }
max@0 332
max@0 333 o.flush();
max@0 334 stream_state.restore(o);
max@0 335 }
max@0 336
max@0 337
max@0 338
max@0 339 //! Print a subfield to the specified stream
max@0 340 //! Assumes type oT can be printed, i.e. oT has std::ostream& operator<< (std::ostream&, const oT&)
max@0 341 template<typename oT>
max@0 342 inline
max@0 343 void
max@0 344 arma_ostream::print(std::ostream& o, const subview_field<oT>& x)
max@0 345 {
max@0 346 arma_extra_debug_sigprint();
max@0 347
max@0 348 const arma_ostream_state stream_state(o);
max@0 349
max@0 350 const std::streamsize cell_width = o.width();
max@0 351
max@0 352 const uword x_n_rows = x.n_rows;
max@0 353 const uword x_n_cols = x.n_cols;
max@0 354
max@0 355 for(uword col=0; col<x_n_cols; ++col)
max@0 356 {
max@0 357 o << "[field column " << col << ']' << '\n';
max@0 358 for(uword row=0; row<x_n_rows; ++row)
max@0 359 {
max@0 360 o.width(cell_width);
max@0 361 o << x.at(row,col) << '\n';
max@0 362 }
max@0 363
max@0 364 o << '\n';
max@0 365 }
max@0 366
max@0 367 o.flush();
max@0 368 stream_state.restore(o);
max@0 369 }
max@0 370
max@0 371
max@0 372
max@0 373 //! @}
max@0 374