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


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:56