Mercurial > hg > segmenter-vamp-plugin
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8b6102e2a9b0 |
---|---|
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 | |
14 //! \addtogroup arma_ostream | |
15 //! @{ | |
16 | |
17 | |
18 | |
19 inline | |
20 arma_ostream_state::arma_ostream_state(const std::ostream& o) | |
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 | |
117 //! "better than nothing" settings for complex numbers | |
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 | |
148 arma_ostream::print_elem_zero(std::ostream& o) | |
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 | |
161 //! Print an element to the specified stream | |
162 template<typename eT> | |
163 arma_inline | |
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 | |
179 //! Print a complex element to the specified stream | |
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 | |
203 //! Print a matrix to the specified stream | |
204 template<typename eT> | |
205 inline | |
206 void | |
207 arma_ostream::print(std::ostream& o, const Mat<eT>& m, const bool modify) | |
208 { | |
209 arma_extra_debug_sigprint(); | |
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 | |
264 //! Print a cube to the specified stream | |
265 template<typename eT> | |
266 inline | |
267 void | |
268 arma_ostream::print(std::ostream& o, const Cube<eT>& x, const bool modify) | |
269 { | |
270 arma_extra_debug_sigprint(); | |
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 | |
297 //! Print a field to the specified stream | |
298 //! Assumes type oT can be printed, i.e. oT has std::ostream& operator<< (std::ostream&, const oT&) | |
299 template<typename oT> | |
300 inline | |
301 void | |
302 arma_ostream::print(std::ostream& o, const field<oT>& x) | |
303 { | |
304 arma_extra_debug_sigprint(); | |
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 | |
339 //! Print a subfield to the specified stream | |
340 //! Assumes type oT can be printed, i.e. oT has std::ostream& operator<< (std::ostream&, const oT&) | |
341 template<typename oT> | |
342 inline | |
343 void | |
344 arma_ostream::print(std::ostream& o, const subview_field<oT>& x) | |
345 { | |
346 arma_extra_debug_sigprint(); | |
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 | |
373 //! @} | |
374 |