max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup arma_ostream max@0: //! @{ max@0: max@0: max@0: max@0: inline max@0: arma_ostream_state::arma_ostream_state(const std::ostream& o) max@0: : orig_flags (o.flags()) max@0: , orig_precision(o.precision()) max@0: , orig_width (o.width()) max@0: , orig_fill (o.fill()) max@0: { max@0: } max@0: max@0: max@0: max@0: inline max@0: void max@0: arma_ostream_state::restore(std::ostream& o) const max@0: { max@0: o.flags (orig_flags); max@0: o.precision(orig_precision); max@0: o.width (orig_width); max@0: o.fill (orig_fill); max@0: } max@0: max@0: max@0: max@0: // max@0: // max@0: max@0: max@0: max@0: template max@0: inline max@0: std::streamsize max@0: arma_ostream::modify_stream(std::ostream& o, const eT* data, const uword n_elem) max@0: { max@0: o.unsetf(ios::showbase); max@0: o.unsetf(ios::uppercase); max@0: o.unsetf(ios::showpos); max@0: max@0: o.fill(' '); max@0: max@0: std::streamsize cell_width; max@0: max@0: bool use_layout_B = false; max@0: bool use_layout_C = false; max@0: max@0: for(uword i=0; i= eT(+100) || max@0: ( (is_signed::value == true) && (val <= eT(-100)) ) || max@0: ( (is_non_integral::value == true) && (val > eT(0)) && (val <= eT(+1e-4)) ) || max@0: ( (is_non_integral::value == true) && (is_signed::value == true) && (val < eT(0)) && (val >= eT(-1e-4)) ) max@0: ) max@0: { max@0: use_layout_C = true; max@0: break; max@0: } max@0: max@0: if( max@0: (val >= eT(+10)) || ( (is_signed::value == true) && (val <= eT(-10)) ) max@0: ) max@0: { max@0: use_layout_B = true; max@0: } max@0: } max@0: max@0: if(use_layout_C == true) max@0: { max@0: o.setf(ios::scientific); max@0: o.setf(ios::right); max@0: o.unsetf(ios::fixed); max@0: o.precision(4); max@0: cell_width = 13; max@0: } max@0: else max@0: if(use_layout_B == true) max@0: { max@0: o.unsetf(ios::scientific); max@0: o.setf(ios::right); max@0: o.setf(ios::fixed); max@0: o.precision(4); max@0: cell_width = 10; max@0: } max@0: else max@0: { max@0: o.unsetf(ios::scientific); max@0: o.setf(ios::right); max@0: o.setf(ios::fixed); max@0: o.precision(4); max@0: cell_width = 9; max@0: } max@0: max@0: return cell_width; max@0: } max@0: max@0: max@0: max@0: //! "better than nothing" settings for complex numbers max@0: template max@0: inline max@0: std::streamsize max@0: arma_ostream::modify_stream(std::ostream& o, const std::complex* data, const uword n_elem) max@0: { max@0: arma_ignore(data); max@0: arma_ignore(n_elem); max@0: max@0: o.unsetf(ios::showbase); max@0: o.unsetf(ios::uppercase); max@0: o.fill(' '); max@0: max@0: o.setf(ios::scientific); max@0: o.setf(ios::showpos); max@0: o.setf(ios::right); max@0: o.unsetf(ios::fixed); max@0: max@0: std::streamsize cell_width; max@0: max@0: o.precision(3); max@0: cell_width = 2 + 2*(1 + 3 + o.precision() + 5) + 1; max@0: max@0: return cell_width; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: arma_ostream::print_elem_zero(std::ostream& o) max@0: { max@0: const std::streamsize orig_precision = o.precision(); max@0: max@0: o.precision(0); max@0: max@0: o << eT(0); max@0: max@0: o.precision(orig_precision); max@0: } max@0: max@0: max@0: max@0: //! Print an element to the specified stream max@0: template max@0: arma_inline max@0: void max@0: arma_ostream::print_elem(std::ostream& o, const eT& x) max@0: { max@0: if(x != eT(0)) max@0: { max@0: o << x; max@0: } max@0: else max@0: { max@0: arma_ostream::print_elem_zero(o); max@0: } max@0: } max@0: max@0: max@0: max@0: //! Print a complex element to the specified stream max@0: template max@0: inline max@0: void max@0: arma_ostream::print_elem(std::ostream& o, const std::complex& x) max@0: { max@0: if( (x.real() != T(0)) || (x.imag() != T(0)) ) max@0: { max@0: std::ostringstream ss; max@0: ss.flags(o.flags()); max@0: //ss.imbue(o.getloc()); max@0: ss.precision(o.precision()); max@0: max@0: ss << '(' << x.real() << ',' << x.imag() << ')'; max@0: o << ss.str(); max@0: } max@0: else max@0: { max@0: o << "(0,0)"; max@0: } max@0: } max@0: max@0: max@0: max@0: //! Print a matrix to the specified stream max@0: template max@0: inline max@0: void max@0: arma_ostream::print(std::ostream& o, const Mat& m, const bool modify) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const arma_ostream_state stream_state(o); max@0: max@0: const std::streamsize cell_width = modify ? arma_ostream::modify_stream(o, m.memptr(), m.n_elem) : o.width(); max@0: max@0: const uword m_n_rows = m.n_rows; max@0: const uword m_n_cols = m.n_cols; max@0: max@0: if(m.is_empty() == false) max@0: { max@0: if(m_n_cols > 0) max@0: { max@0: if(cell_width > 0) max@0: { max@0: for(uword row=0; row < m_n_rows; ++row) max@0: { max@0: for(uword col=0; col < m_n_cols; ++col) max@0: { max@0: // the cell width appears to be reset after each element is printed, max@0: // hence we need to restore it max@0: o.width(cell_width); max@0: arma_ostream::print_elem(o, m.at(row,col)); max@0: } max@0: max@0: o << '\n'; max@0: } max@0: } max@0: else max@0: { max@0: for(uword row=0; row < m_n_rows; ++row) max@0: { max@0: for(uword col=0; col < m_n_cols-1; ++col) max@0: { max@0: arma_ostream::print_elem(o, m.at(row,col)); max@0: o << ' '; max@0: } max@0: max@0: arma_ostream::print_elem(o, m.at(row, m_n_cols-1)); max@0: o << '\n'; max@0: } max@0: } max@0: } max@0: } max@0: else max@0: { max@0: o << "[matrix size: " << m_n_rows << 'x' << m_n_cols << "]\n"; max@0: } max@0: max@0: o.flush(); max@0: stream_state.restore(o); max@0: } max@0: max@0: max@0: max@0: //! Print a cube to the specified stream max@0: template max@0: inline max@0: void max@0: arma_ostream::print(std::ostream& o, const Cube& x, const bool modify) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const arma_ostream_state stream_state(o); max@0: max@0: const std::streamsize cell_width = modify ? arma_ostream::modify_stream(o, x.memptr(), x.n_elem) : o.width(); max@0: max@0: if(x.is_empty() == false) max@0: { max@0: for(uword slice=0; slice < x.n_slices; ++slice) max@0: { max@0: o << "[cube slice " << slice << ']' << '\n'; max@0: o.width(cell_width); max@0: arma_ostream::print(o, x.slice(slice), false); max@0: o << '\n'; max@0: } max@0: } max@0: else max@0: { max@0: o << "[cube size: " << x.n_rows << 'x' << x.n_cols << 'x' << x.n_slices << "]\n"; max@0: } max@0: max@0: stream_state.restore(o); max@0: } max@0: max@0: max@0: max@0: max@0: //! Print a field to the specified stream max@0: //! Assumes type oT can be printed, i.e. oT has std::ostream& operator<< (std::ostream&, const oT&) max@0: template max@0: inline max@0: void max@0: arma_ostream::print(std::ostream& o, const field& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const arma_ostream_state stream_state(o); max@0: max@0: const std::streamsize cell_width = o.width(); max@0: max@0: const uword x_n_rows = x.n_rows; max@0: const uword x_n_cols = x.n_cols; max@0: max@0: if(x.is_empty() == false) max@0: { max@0: for(uword col=0; col max@0: inline max@0: void max@0: arma_ostream::print(std::ostream& o, const subview_field& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const arma_ostream_state stream_state(o); max@0: max@0: const std::streamsize cell_width = o.width(); max@0: max@0: const uword x_n_rows = x.n_rows; max@0: const uword x_n_cols = x.n_cols; max@0: max@0: for(uword col=0; col