1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1346
|
28 #include <cfloat> |
3124
|
29 #include <cmath> |
2825
|
30 #include <cstdio> |
1346
|
31 #include <cstring> |
1343
|
32 |
3503
|
33 #include <iomanip> |
|
34 #include <iostream> |
5765
|
35 #include <sstream> |
1728
|
36 #include <string> |
|
37 |
4655
|
38 #include "Array-util.h" |
453
|
39 #include "CMatrix.h" |
1
|
40 #include "Range.h" |
2926
|
41 #include "cmd-edit.h" |
1352
|
42 #include "dMatrix.h" |
2891
|
43 #include "lo-mappers.h" |
2317
|
44 #include "mach-info.h" |
1651
|
45 #include "oct-cmplx.h" |
4153
|
46 #include "quit.h" |
1755
|
47 #include "str-vec.h" |
1
|
48 |
3933
|
49 #include "Cell.h" |
1352
|
50 #include "defun.h" |
|
51 #include "error.h" |
2165
|
52 #include "gripes.h" |
1755
|
53 #include "oct-obj.h" |
3685
|
54 #include "oct-stream.h" |
1352
|
55 #include "pager.h" |
|
56 #include "pr-output.h" |
1282
|
57 #include "sysdep.h" |
1
|
58 #include "utils.h" |
1352
|
59 #include "variables.h" |
1
|
60 |
3105
|
61 // TRUE means use a scaled fixed point format for `format long' and |
|
62 // `format short'. |
|
63 static bool Vfixed_point_format; |
|
64 |
2165
|
65 // The maximum field width for a number printed by the default output |
|
66 // routines. |
|
67 static int Voutput_max_field_width; |
|
68 |
|
69 // The precision of the numbers printed by the default output |
|
70 // routines. |
|
71 static int Voutput_precision; |
|
72 |
5360
|
73 // TRUE means that the dimensions of empty objects should be printed |
2165
|
74 // like this: x = [](2x0). |
5360
|
75 bool Vprint_empty_dimensions; |
2165
|
76 |
|
77 // TRUE means that the rows of big matrices should be split into |
|
78 // smaller slices that fit on the screen. |
|
79 static bool Vsplit_long_rows; |
|
80 |
5759
|
81 // How many levels of structure elements should we print? |
|
82 int Vstruct_levels_to_print; |
|
83 |
3018
|
84 // TRUE means don't do any fancy formatting. |
2387
|
85 static bool free_format = false; |
1
|
86 |
3018
|
87 // TRUE means print plus sign for nonzero, blank for zero. |
2387
|
88 static bool plus_format = false; |
1
|
89 |
4632
|
90 // First char for > 0, second for < 0, third for == 0. |
|
91 static std::string plus_format_chars = "+ "; |
|
92 |
3018
|
93 // TRUE means always print like dollars and cents. |
2387
|
94 static bool bank_format = false; |
1282
|
95 |
3018
|
96 // TRUE means print data in hexadecimal format. |
3608
|
97 static int hex_format = 0; |
1282
|
98 |
3018
|
99 // TRUE means print data in binary-bit-pattern format. |
1309
|
100 static int bit_format = 0; |
|
101 |
3018
|
102 // TRUE means don't put newlines around the column number headers. |
2387
|
103 static bool compact_format = false; |
1186
|
104 |
3018
|
105 // TRUE means use an e format. |
2387
|
106 static bool print_e = false; |
1
|
107 |
4509
|
108 // TRUE means use a g format. |
|
109 static bool print_g = false; |
|
110 |
3018
|
111 // TRUE means print E instead of e for exponent field. |
2387
|
112 static bool print_big_e = false; |
1
|
113 |
3608
|
114 class pr_formatted_float; |
|
115 |
4509
|
116 static int |
|
117 current_output_max_field_width (void) |
|
118 { |
|
119 return Voutput_max_field_width; |
|
120 } |
|
121 |
|
122 static int |
|
123 current_output_precision (void) |
|
124 { |
|
125 return Voutput_precision; |
|
126 } |
|
127 |
3608
|
128 class |
|
129 float_format |
|
130 { |
|
131 public: |
|
132 |
4509
|
133 float_format (int w = current_output_max_field_width (), |
|
134 int p = current_output_precision (), int f = 0) |
3608
|
135 : fw (w), prec (p), fmt (f), up (0), sp (0) { } |
|
136 |
|
137 float_format (const float_format& ff) |
|
138 : fw (ff.fw), prec (ff.prec), fmt (ff.fmt), up (ff.up), sp (ff.sp) { } |
|
139 |
|
140 float_format& operator = (const float_format& ff) |
|
141 { |
|
142 if (&ff != this) |
|
143 { |
|
144 fw = ff.fw; |
|
145 prec = ff.prec; |
|
146 fmt = ff.fmt; |
|
147 up = ff.up; |
|
148 sp = ff.sp; |
|
149 } |
|
150 |
|
151 return *this; |
|
152 } |
|
153 |
|
154 ~float_format (void) { } |
|
155 |
|
156 float_format& scientific (void) { fmt = std::ios::scientific; return *this; } |
|
157 float_format& fixed (void) { fmt = std::ios::fixed; return *this; } |
|
158 float_format& general (void) { fmt = 0; return *this; } |
|
159 |
|
160 float_format& uppercase (void) { up = std::ios::uppercase; return *this; } |
|
161 float_format& lowercase (void) { up = 0; return *this; } |
|
162 |
|
163 float_format& precision (int p) { prec = p; return *this; } |
|
164 |
|
165 float_format& width (int w) { fw = w; return *this; } |
|
166 |
|
167 float_format& trailing_zeros (bool tz = true) |
|
168 { sp = tz ? std::ios::showpoint : 0; return *this; } |
|
169 |
|
170 friend std::ostream& operator << (std::ostream& os, |
|
171 const pr_formatted_float& pff); |
|
172 |
|
173 private: |
|
174 |
|
175 // Field width. Zero means as wide as necessary. |
|
176 int fw; |
|
177 |
|
178 // Precision. |
|
179 int prec; |
|
180 |
|
181 // Format. |
|
182 int fmt; |
|
183 |
|
184 // E or e. |
|
185 int up; |
|
186 |
|
187 // Show trailing zeros. |
|
188 int sp; |
|
189 }; |
|
190 |
|
191 class |
|
192 pr_formatted_float |
|
193 { |
|
194 public: |
|
195 |
|
196 const float_format& f; |
|
197 |
|
198 double val; |
|
199 |
|
200 pr_formatted_float (const float_format& f_arg, double val_arg) |
|
201 : f (f_arg), val (val_arg) { } |
|
202 }; |
|
203 |
|
204 std::ostream& |
|
205 operator << (std::ostream& os, const pr_formatted_float& pff) |
|
206 { |
3682
|
207 if (pff.f.fw >= 0) |
3608
|
208 os << std::setw (pff.f.fw); |
|
209 |
3682
|
210 if (pff.f.prec >= 0) |
3608
|
211 os << std::setprecision (pff.f.prec); |
|
212 |
3775
|
213 std::ios::fmtflags oflags = |
|
214 os.flags (static_cast<std::ios::fmtflags> |
|
215 (pff.f.fmt | pff.f.up | pff.f.sp)); |
3608
|
216 |
|
217 os << pff.val; |
|
218 |
|
219 os.flags (oflags); |
|
220 |
|
221 return os; |
|
222 } |
|
223 |
|
224 // Current format for real numbers and the real part of complex |
|
225 // numbers. |
|
226 static float_format *curr_real_fmt = 0; |
|
227 |
|
228 // Current format for the imaginary part of complex numbers. |
|
229 static float_format *curr_imag_fmt = 0; |
1309
|
230 |
1
|
231 static double |
164
|
232 pr_max_internal (const Matrix& m) |
1
|
233 { |
5275
|
234 octave_idx_type nr = m.rows (); |
|
235 octave_idx_type nc = m.columns (); |
1
|
236 |
3130
|
237 double result = -DBL_MAX; |
1
|
238 |
5748
|
239 bool all_inf_or_nan = true; |
|
240 |
5275
|
241 for (octave_idx_type j = 0; j < nc; j++) |
|
242 for (octave_idx_type i = 0; i < nr; i++) |
1
|
243 { |
3608
|
244 double val = m(i,j); |
5388
|
245 if (xisinf (val) || xisnan (val)) |
1
|
246 continue; |
|
247 |
5748
|
248 all_inf_or_nan = false; |
|
249 |
1
|
250 if (val > result) |
|
251 result = val; |
|
252 } |
3608
|
253 |
5748
|
254 if (all_inf_or_nan) |
|
255 result = 0.0; |
|
256 |
1
|
257 return result; |
|
258 } |
|
259 |
|
260 static double |
164
|
261 pr_min_internal (const Matrix& m) |
1
|
262 { |
5275
|
263 octave_idx_type nr = m.rows (); |
|
264 octave_idx_type nc = m.columns (); |
1
|
265 |
|
266 double result = DBL_MAX; |
|
267 |
5748
|
268 bool all_inf_or_nan = true; |
|
269 |
5275
|
270 for (octave_idx_type j = 0; j < nc; j++) |
|
271 for (octave_idx_type i = 0; i < nr; i++) |
1
|
272 { |
3608
|
273 double val = m(i,j); |
5389
|
274 if (xisinf (val) || xisnan (val)) |
1
|
275 continue; |
|
276 |
5748
|
277 all_inf_or_nan = false; |
|
278 |
1
|
279 if (val < result) |
|
280 result = val; |
|
281 } |
3608
|
282 |
5748
|
283 if (all_inf_or_nan) |
|
284 result = 0.0; |
|
285 |
1
|
286 return result; |
|
287 } |
|
288 |
1658
|
289 // XXX FIXME XXX -- it would be nice to share more code among these |
|
290 // functions,.. |
|
291 |
1
|
292 static void |
3611
|
293 set_real_format (bool sign, int digits, bool inf_or_nan, bool int_only, |
1658
|
294 int &fw) |
1
|
295 { |
3608
|
296 static float_format fmt; |
1
|
297 |
2165
|
298 int prec = Voutput_precision; |
1
|
299 |
|
300 int ld, rd; |
|
301 |
|
302 if (bank_format) |
|
303 { |
|
304 fw = digits < 0 ? 4 : digits + 3; |
5748
|
305 if (inf_or_nan && fw < 4) |
|
306 fw = 4; |
1
|
307 rd = 2; |
|
308 } |
1282
|
309 else if (hex_format) |
|
310 { |
|
311 fw = 2 * sizeof (double); |
|
312 rd = 0; |
|
313 } |
1309
|
314 else if (bit_format) |
|
315 { |
|
316 fw = 8 * sizeof (double); |
|
317 rd = 0; |
|
318 } |
3611
|
319 else if (inf_or_nan || int_only) |
1
|
320 { |
|
321 fw = digits; |
5748
|
322 if (inf_or_nan && fw < 4) |
|
323 fw = 4; |
3682
|
324 rd = fw; |
1
|
325 } |
|
326 else |
|
327 { |
|
328 if (digits > 0) |
|
329 { |
|
330 ld = digits; |
1658
|
331 rd = prec > digits ? prec - digits : prec; |
1
|
332 digits++; |
|
333 } |
|
334 else |
|
335 { |
|
336 ld = 1; |
1658
|
337 rd = prec > digits ? prec - digits : prec; |
1
|
338 digits = -digits + 1; |
|
339 } |
|
340 |
|
341 fw = ld + 1 + rd; |
5748
|
342 if (inf_or_nan && fw < 4) |
|
343 fw = 4; |
1
|
344 } |
|
345 |
1309
|
346 if (! (bank_format || hex_format || bit_format) |
4509
|
347 && (fw > Voutput_max_field_width || print_e || print_g)) |
1
|
348 { |
4509
|
349 if (print_g) |
|
350 fmt = float_format (); |
|
351 else |
|
352 { |
|
353 int exp_field = 4; |
|
354 if (digits > 100) |
|
355 exp_field++; |
1
|
356 |
4509
|
357 fw = 2 + prec + exp_field; |
5748
|
358 if (inf_or_nan && fw < 4) |
|
359 fw = 4; |
1
|
360 |
4509
|
361 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
362 } |
3608
|
363 |
1
|
364 if (print_big_e) |
3608
|
365 fmt.uppercase (); |
1
|
366 } |
5086
|
367 else if (! bank_format && (inf_or_nan || int_only)) |
3611
|
368 fmt = float_format (fw, rd); |
1
|
369 else |
3608
|
370 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
371 |
3608
|
372 curr_real_fmt = &fmt; |
1
|
373 } |
|
374 |
1658
|
375 static void |
|
376 set_format (double d, int& fw) |
|
377 { |
|
378 curr_real_fmt = 0; |
|
379 curr_imag_fmt = 0; |
|
380 |
|
381 if (free_format) |
|
382 return; |
|
383 |
2387
|
384 bool sign = (d < 0.0); |
1658
|
385 |
5389
|
386 bool inf_or_nan = (xisinf (d) || xisnan (d)); |
1658
|
387 |
3611
|
388 bool int_only = (! inf_or_nan && D_NINT (d) == d); |
1658
|
389 |
|
390 double d_abs = d < 0.0 ? -d : d; |
|
391 |
2800
|
392 int digits = (inf_or_nan || d_abs == 0.0) |
|
393 ? 0 : static_cast<int> (floor (log10 (d_abs) + 1.0)); |
1658
|
394 |
3611
|
395 set_real_format (sign, digits, inf_or_nan, int_only, fw); |
1658
|
396 } |
|
397 |
1
|
398 static inline void |
|
399 set_format (double d) |
|
400 { |
|
401 int fw; |
|
402 set_format (d, fw); |
|
403 } |
|
404 |
|
405 static void |
2387
|
406 set_real_matrix_format (bool sign, int x_max, int x_min, |
|
407 bool inf_or_nan, int int_or_inf_or_nan, int& fw) |
1
|
408 { |
3608
|
409 static float_format fmt; |
1
|
410 |
2165
|
411 int prec = Voutput_precision; |
1
|
412 |
|
413 int ld, rd; |
|
414 |
|
415 if (bank_format) |
|
416 { |
|
417 int digits = x_max > x_min ? x_max : x_min; |
|
418 fw = digits <= 0 ? 4 : digits + 3; |
5748
|
419 if (inf_or_nan && fw < 4) |
|
420 fw = 4; |
1
|
421 rd = 2; |
|
422 } |
1282
|
423 else if (hex_format) |
|
424 { |
|
425 fw = 2 * sizeof (double); |
|
426 rd = 0; |
|
427 } |
1309
|
428 else if (bit_format) |
|
429 { |
|
430 fw = 8 * sizeof (double); |
|
431 rd = 0; |
|
432 } |
4509
|
433 else if (Vfixed_point_format && ! print_g) |
3268
|
434 { |
|
435 rd = prec; |
|
436 fw = rd + 2; |
5748
|
437 if (inf_or_nan && fw < 4) |
|
438 fw = 4; |
3268
|
439 } |
1715
|
440 else if (int_or_inf_or_nan) |
1
|
441 { |
|
442 int digits = x_max > x_min ? x_max : x_min; |
|
443 fw = digits <= 0 ? 1 : digits; |
5748
|
444 if (inf_or_nan && fw < 4) |
|
445 fw = 4; |
3682
|
446 rd = fw; |
1
|
447 } |
|
448 else |
|
449 { |
|
450 int ld_max, rd_max; |
|
451 if (x_max > 0) |
|
452 { |
|
453 ld_max = x_max; |
1658
|
454 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
455 x_max++; |
|
456 } |
|
457 else |
|
458 { |
|
459 ld_max = 1; |
1658
|
460 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
461 x_max = -x_max + 1; |
|
462 } |
|
463 |
|
464 int ld_min, rd_min; |
|
465 if (x_min > 0) |
|
466 { |
|
467 ld_min = x_min; |
1658
|
468 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
469 x_min++; |
|
470 } |
|
471 else |
|
472 { |
|
473 ld_min = 1; |
1658
|
474 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
475 x_min = -x_min + 1; |
|
476 } |
|
477 |
|
478 ld = ld_max > ld_min ? ld_max : ld_min; |
|
479 rd = rd_max > rd_min ? rd_max : rd_min; |
|
480 |
|
481 fw = ld + 1 + rd; |
5748
|
482 if (inf_or_nan && fw < 4) |
|
483 fw = 4; |
1
|
484 } |
|
485 |
1658
|
486 if (! (bank_format || hex_format || bit_format) |
3105
|
487 && (print_e |
4509
|
488 || print_g |
3105
|
489 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
490 { |
4509
|
491 if (print_g) |
|
492 fmt = float_format (); |
|
493 else |
|
494 { |
|
495 int exp_field = 4; |
|
496 if (x_max > 100 || x_min > 100) |
|
497 exp_field++; |
1
|
498 |
4509
|
499 fw = 2 + prec + exp_field; |
5748
|
500 if (inf_or_nan && fw < 4) |
|
501 fw = 4; |
1
|
502 |
4509
|
503 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
504 } |
3608
|
505 |
1
|
506 if (print_big_e) |
3608
|
507 fmt.uppercase (); |
1
|
508 } |
5086
|
509 else if (! bank_format && int_or_inf_or_nan) |
3611
|
510 fmt = float_format (fw, rd); |
1
|
511 else |
3608
|
512 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
513 |
3608
|
514 curr_real_fmt = &fmt; |
1
|
515 } |
|
516 |
1658
|
517 static void |
3105
|
518 set_format (const Matrix& m, int& fw, double& scale) |
1658
|
519 { |
|
520 curr_real_fmt = 0; |
|
521 curr_imag_fmt = 0; |
|
522 |
|
523 if (free_format) |
|
524 return; |
|
525 |
4431
|
526 bool sign = m.any_element_is_negative (true); |
1658
|
527 |
2387
|
528 bool inf_or_nan = m.any_element_is_inf_or_nan (); |
1658
|
529 |
2387
|
530 bool int_or_inf_or_nan = m.all_elements_are_int_or_inf_or_nan (); |
1658
|
531 |
2387
|
532 Matrix m_abs = m.abs (); |
1658
|
533 double max_abs = pr_max_internal (m_abs); |
|
534 double min_abs = pr_min_internal (m_abs); |
|
535 |
2800
|
536 int x_max = max_abs == 0.0 |
|
537 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
538 |
|
539 int x_min = min_abs == 0.0 |
|
540 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
541 |
4270
|
542 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
543 |
1658
|
544 set_real_matrix_format (sign, x_max, x_min, inf_or_nan, |
1715
|
545 int_or_inf_or_nan, fw); |
1658
|
546 } |
|
547 |
1
|
548 static inline void |
164
|
549 set_format (const Matrix& m) |
1
|
550 { |
|
551 int fw; |
3105
|
552 double scale; |
|
553 set_format (m, fw, scale); |
1
|
554 } |
|
555 |
|
556 static void |
2387
|
557 set_complex_format (bool sign, int x_max, int x_min, int r_x, |
|
558 bool inf_or_nan, int int_only, int& r_fw, int& i_fw) |
1
|
559 { |
3608
|
560 static float_format r_fmt; |
|
561 static float_format i_fmt; |
1
|
562 |
2165
|
563 int prec = Voutput_precision; |
1
|
564 |
|
565 int ld, rd; |
|
566 |
|
567 if (bank_format) |
|
568 { |
|
569 int digits = r_x; |
|
570 i_fw = 0; |
|
571 r_fw = digits <= 0 ? 4 : digits + 3; |
5748
|
572 if (inf_or_nan && r_fw < 4) |
|
573 r_fw = 4; |
1
|
574 rd = 2; |
|
575 } |
1282
|
576 else if (hex_format) |
|
577 { |
|
578 r_fw = 2 * sizeof (double); |
|
579 i_fw = 2 * sizeof (double); |
|
580 rd = 0; |
|
581 } |
1309
|
582 else if (bit_format) |
|
583 { |
|
584 r_fw = 8 * sizeof (double); |
|
585 i_fw = 8 * sizeof (double); |
|
586 rd = 0; |
|
587 } |
1658
|
588 else if (inf_or_nan || int_only) |
1
|
589 { |
|
590 int digits = x_max > x_min ? x_max : x_min; |
|
591 i_fw = r_fw = digits <= 0 ? 1 : digits; |
5748
|
592 if (inf_or_nan && i_fw < 4) |
|
593 i_fw = r_fw = 4; |
3682
|
594 rd = r_fw; |
1
|
595 } |
|
596 else |
|
597 { |
|
598 int ld_max, rd_max; |
|
599 if (x_max > 0) |
|
600 { |
|
601 ld_max = x_max; |
1658
|
602 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
603 x_max++; |
|
604 } |
|
605 else |
|
606 { |
|
607 ld_max = 1; |
1658
|
608 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
609 x_max = -x_max + 1; |
|
610 } |
|
611 |
|
612 int ld_min, rd_min; |
|
613 if (x_min > 0) |
|
614 { |
|
615 ld_min = x_min; |
1658
|
616 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
617 x_min++; |
|
618 } |
|
619 else |
|
620 { |
|
621 ld_min = 1; |
1658
|
622 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
623 x_min = -x_min + 1; |
|
624 } |
|
625 |
|
626 ld = ld_max > ld_min ? ld_max : ld_min; |
|
627 rd = rd_max > rd_min ? rd_max : rd_min; |
|
628 |
|
629 i_fw = r_fw = ld + 1 + rd; |
5748
|
630 if (inf_or_nan && i_fw < 4) |
|
631 i_fw = r_fw = 4; |
1
|
632 } |
|
633 |
1309
|
634 if (! (bank_format || hex_format || bit_format) |
4509
|
635 && (r_fw > Voutput_max_field_width || print_e || print_g)) |
1
|
636 { |
4509
|
637 if (print_g) |
|
638 { |
|
639 r_fmt = float_format (); |
|
640 i_fmt = float_format (); |
|
641 } |
|
642 else |
|
643 { |
|
644 int exp_field = 4; |
|
645 if (x_max > 100 || x_min > 100) |
|
646 exp_field++; |
1
|
647 |
4509
|
648 i_fw = r_fw = 1 + prec + exp_field; |
5748
|
649 if (inf_or_nan && i_fw < 4) |
|
650 i_fw = r_fw = 4; |
1
|
651 |
4509
|
652 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
653 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
654 } |
3608
|
655 |
1
|
656 if (print_big_e) |
|
657 { |
3608
|
658 r_fmt.uppercase (); |
|
659 i_fmt.uppercase (); |
1
|
660 } |
|
661 } |
5086
|
662 else if (! bank_format && (inf_or_nan || int_only)) |
3611
|
663 { |
|
664 r_fmt = float_format (r_fw, rd); |
|
665 i_fmt = float_format (i_fw, rd); |
|
666 } |
1
|
667 else |
|
668 { |
3608
|
669 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
670 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
671 } |
|
672 |
3608
|
673 curr_real_fmt = &r_fmt; |
|
674 curr_imag_fmt = &i_fmt; |
1
|
675 } |
|
676 |
1658
|
677 static void |
|
678 set_format (const Complex& c, int& r_fw, int& i_fw) |
|
679 { |
|
680 curr_real_fmt = 0; |
|
681 curr_imag_fmt = 0; |
|
682 |
|
683 if (free_format) |
|
684 return; |
|
685 |
|
686 double rp = c.real (); |
|
687 double ip = c.imag (); |
|
688 |
2387
|
689 bool sign = (rp < 0.0); |
1658
|
690 |
2387
|
691 bool inf_or_nan = (xisinf (c) || xisnan (c)); |
1658
|
692 |
2387
|
693 bool int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip); |
1658
|
694 |
|
695 double r_abs = rp < 0.0 ? -rp : rp; |
|
696 double i_abs = ip < 0.0 ? -ip : ip; |
|
697 |
2800
|
698 int r_x = r_abs == 0.0 |
|
699 ? 0 : static_cast<int> (floor (log10 (r_abs) + 1.0)); |
|
700 |
|
701 int i_x = i_abs == 0.0 |
|
702 ? 0 : static_cast<int> (floor (log10 (i_abs) + 1.0)); |
1658
|
703 |
|
704 int x_max, x_min; |
|
705 |
|
706 if (r_x > i_x) |
|
707 { |
|
708 x_max = r_x; |
|
709 x_min = i_x; |
|
710 } |
|
711 else |
|
712 { |
|
713 x_max = i_x; |
|
714 x_min = r_x; |
|
715 } |
|
716 |
|
717 set_complex_format (sign, x_max, x_min, r_x, inf_or_nan, int_only, |
|
718 r_fw, i_fw); |
|
719 } |
|
720 |
1
|
721 static inline void |
164
|
722 set_format (const Complex& c) |
1
|
723 { |
|
724 int r_fw, i_fw; |
|
725 set_format (c, r_fw, i_fw); |
|
726 } |
|
727 |
|
728 static void |
2387
|
729 set_complex_matrix_format (bool sign, int x_max, int x_min, |
|
730 int r_x_max, int r_x_min, bool inf_or_nan, |
1715
|
731 int int_or_inf_or_nan, int& r_fw, int& i_fw) |
1
|
732 { |
3608
|
733 static float_format r_fmt; |
|
734 static float_format i_fmt; |
1
|
735 |
2165
|
736 int prec = Voutput_precision; |
1
|
737 |
|
738 int ld, rd; |
|
739 |
|
740 if (bank_format) |
|
741 { |
|
742 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
743 i_fw = 0; |
|
744 r_fw = digits <= 0 ? 4 : digits + 3; |
5748
|
745 if (inf_or_nan && i_fw < 4) |
|
746 i_fw = r_fw = 4; |
1
|
747 rd = 2; |
|
748 } |
1282
|
749 else if (hex_format) |
|
750 { |
|
751 r_fw = 2 * sizeof (double); |
|
752 i_fw = 2 * sizeof (double); |
|
753 rd = 0; |
|
754 } |
1309
|
755 else if (bit_format) |
|
756 { |
|
757 r_fw = 8 * sizeof (double); |
|
758 i_fw = 8 * sizeof (double); |
|
759 rd = 0; |
|
760 } |
4509
|
761 else if (Vfixed_point_format && ! print_g) |
3268
|
762 { |
|
763 rd = prec; |
|
764 i_fw = r_fw = rd + 2; |
5748
|
765 if (inf_or_nan && i_fw < 4) |
|
766 i_fw = r_fw = 4; |
3268
|
767 } |
1715
|
768 else if (int_or_inf_or_nan) |
1
|
769 { |
|
770 int digits = x_max > x_min ? x_max : x_min; |
|
771 i_fw = r_fw = digits <= 0 ? 1 : digits; |
5748
|
772 if (inf_or_nan && i_fw < 4) |
|
773 i_fw = r_fw = 4; |
3682
|
774 rd = r_fw; |
1
|
775 } |
|
776 else |
|
777 { |
|
778 int ld_max, rd_max; |
|
779 if (x_max > 0) |
|
780 { |
|
781 ld_max = x_max; |
1658
|
782 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
783 x_max++; |
|
784 } |
|
785 else |
|
786 { |
|
787 ld_max = 1; |
1658
|
788 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
789 x_max = -x_max + 1; |
|
790 } |
|
791 |
|
792 int ld_min, rd_min; |
|
793 if (x_min > 0) |
|
794 { |
|
795 ld_min = x_min; |
1658
|
796 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
797 x_min++; |
|
798 } |
|
799 else |
|
800 { |
|
801 ld_min = 1; |
1658
|
802 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
803 x_min = -x_min + 1; |
|
804 } |
|
805 |
|
806 ld = ld_max > ld_min ? ld_max : ld_min; |
|
807 rd = rd_max > rd_min ? rd_max : rd_min; |
|
808 |
|
809 i_fw = r_fw = ld + 1 + rd; |
5748
|
810 if (inf_or_nan && i_fw < 4) |
|
811 i_fw = r_fw = 4; |
1
|
812 } |
|
813 |
1309
|
814 if (! (bank_format || hex_format || bit_format) |
3105
|
815 && (print_e |
4509
|
816 || print_g |
3105
|
817 || (! Vfixed_point_format && r_fw > Voutput_max_field_width))) |
1
|
818 { |
4509
|
819 if (print_g) |
|
820 { |
|
821 r_fmt = float_format (); |
|
822 i_fmt = float_format (); |
|
823 } |
|
824 else |
|
825 { |
|
826 int exp_field = 4; |
|
827 if (x_max > 100 || x_min > 100) |
|
828 exp_field++; |
1
|
829 |
4509
|
830 i_fw = r_fw = 1 + prec + exp_field; |
5748
|
831 if (inf_or_nan && i_fw < 4) |
|
832 i_fw = r_fw = 4; |
1
|
833 |
4509
|
834 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
835 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
836 } |
3608
|
837 |
1
|
838 if (print_big_e) |
|
839 { |
3608
|
840 r_fmt.uppercase (); |
|
841 i_fmt.uppercase (); |
1
|
842 } |
|
843 } |
5086
|
844 else if (! bank_format && int_or_inf_or_nan) |
3611
|
845 { |
|
846 r_fmt = float_format (r_fw, rd); |
|
847 i_fmt = float_format (i_fw, rd); |
|
848 } |
1
|
849 else |
|
850 { |
3608
|
851 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
852 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
853 } |
|
854 |
3608
|
855 curr_real_fmt = &r_fmt; |
|
856 curr_imag_fmt = &i_fmt; |
1
|
857 } |
|
858 |
1658
|
859 static void |
3105
|
860 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw, double& scale) |
1658
|
861 { |
|
862 curr_real_fmt = 0; |
|
863 curr_imag_fmt = 0; |
|
864 |
|
865 if (free_format) |
|
866 return; |
|
867 |
|
868 Matrix rp = real (cm); |
|
869 Matrix ip = imag (cm); |
|
870 |
4431
|
871 bool sign = rp.any_element_is_negative (true); |
1658
|
872 |
2387
|
873 bool inf_or_nan = cm.any_element_is_inf_or_nan (); |
1658
|
874 |
2387
|
875 bool int_or_inf_or_nan = (rp.all_elements_are_int_or_inf_or_nan () |
|
876 && ip.all_elements_are_int_or_inf_or_nan ()); |
1658
|
877 |
2387
|
878 Matrix r_m_abs = rp.abs (); |
1658
|
879 double r_max_abs = pr_max_internal (r_m_abs); |
|
880 double r_min_abs = pr_min_internal (r_m_abs); |
|
881 |
2387
|
882 Matrix i_m_abs = ip.abs (); |
1658
|
883 double i_max_abs = pr_max_internal (i_m_abs); |
|
884 double i_min_abs = pr_min_internal (i_m_abs); |
|
885 |
2800
|
886 int r_x_max = r_max_abs == 0.0 |
|
887 ? 0 : static_cast<int> (floor (log10 (r_max_abs) + 1.0)); |
|
888 |
|
889 int r_x_min = r_min_abs == 0.0 |
|
890 ? 0 : static_cast<int> (floor (log10 (r_min_abs) + 1.0)); |
1658
|
891 |
2800
|
892 int i_x_max = i_max_abs == 0.0 |
|
893 ? 0 : static_cast<int> (floor (log10 (i_max_abs) + 1.0)); |
|
894 |
|
895 int i_x_min = i_min_abs == 0.0 |
|
896 ? 0 : static_cast<int> (floor (log10 (i_min_abs) + 1.0)); |
1658
|
897 |
|
898 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
899 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
900 |
4270
|
901 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
902 |
1658
|
903 set_complex_matrix_format (sign, x_max, x_min, r_x_max, r_x_min, |
1715
|
904 inf_or_nan, int_or_inf_or_nan, r_fw, i_fw); |
1658
|
905 } |
|
906 |
1
|
907 static inline void |
164
|
908 set_format (const ComplexMatrix& cm) |
1
|
909 { |
|
910 int r_fw, i_fw; |
3105
|
911 double scale; |
|
912 set_format (cm, r_fw, i_fw, scale); |
1
|
913 } |
|
914 |
|
915 static void |
3608
|
916 set_range_format (bool sign, int x_max, int x_min, int all_ints, int& fw) |
1
|
917 { |
3608
|
918 static float_format fmt; |
1
|
919 |
2165
|
920 int prec = Voutput_precision; |
1
|
921 |
|
922 int ld, rd; |
|
923 |
|
924 if (bank_format) |
|
925 { |
|
926 int digits = x_max > x_min ? x_max : x_min; |
|
927 fw = sign + digits < 0 ? 4 : digits + 3; |
|
928 rd = 2; |
|
929 } |
1282
|
930 else if (hex_format) |
|
931 { |
|
932 fw = 2 * sizeof (double); |
|
933 rd = 0; |
|
934 } |
1309
|
935 else if (bit_format) |
|
936 { |
|
937 fw = 8 * sizeof (double); |
|
938 rd = 0; |
|
939 } |
1658
|
940 else if (all_ints) |
1
|
941 { |
|
942 int digits = x_max > x_min ? x_max : x_min; |
|
943 fw = sign + digits; |
3682
|
944 rd = fw; |
1
|
945 } |
4509
|
946 else if (Vfixed_point_format && ! print_g) |
3105
|
947 { |
|
948 rd = prec; |
|
949 fw = rd + 2 + sign; |
|
950 } |
1
|
951 else |
|
952 { |
|
953 int ld_max, rd_max; |
|
954 if (x_max > 0) |
|
955 { |
|
956 ld_max = x_max; |
1658
|
957 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
958 x_max++; |
|
959 } |
|
960 else |
|
961 { |
|
962 ld_max = 1; |
1658
|
963 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
964 x_max = -x_max + 1; |
|
965 } |
|
966 |
|
967 int ld_min, rd_min; |
|
968 if (x_min > 0) |
|
969 { |
|
970 ld_min = x_min; |
1658
|
971 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
972 x_min++; |
|
973 } |
|
974 else |
|
975 { |
|
976 ld_min = 1; |
1658
|
977 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
978 x_min = -x_min + 1; |
|
979 } |
|
980 |
|
981 ld = ld_max > ld_min ? ld_max : ld_min; |
|
982 rd = rd_max > rd_min ? rd_max : rd_min; |
|
983 |
|
984 fw = sign + ld + 1 + rd; |
|
985 } |
|
986 |
1309
|
987 if (! (bank_format || hex_format || bit_format) |
3105
|
988 && (print_e |
4509
|
989 || print_g |
3105
|
990 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
991 { |
4509
|
992 if (print_g) |
|
993 fmt = float_format (); |
|
994 else |
|
995 { |
|
996 int exp_field = 4; |
|
997 if (x_max > 100 || x_min > 100) |
|
998 exp_field++; |
1
|
999 |
4509
|
1000 fw = sign + 2 + prec + exp_field; |
1
|
1001 |
4509
|
1002 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
1003 } |
3608
|
1004 |
1
|
1005 if (print_big_e) |
3608
|
1006 fmt.uppercase (); |
1
|
1007 } |
5086
|
1008 else if (! bank_format && all_ints) |
3611
|
1009 fmt = float_format (fw, rd); |
1
|
1010 else |
3608
|
1011 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
1012 |
3608
|
1013 curr_real_fmt = &fmt; |
1
|
1014 } |
|
1015 |
1658
|
1016 static void |
3105
|
1017 set_format (const Range& r, int& fw, double& scale) |
1658
|
1018 { |
|
1019 curr_real_fmt = 0; |
|
1020 curr_imag_fmt = 0; |
|
1021 |
|
1022 if (free_format) |
|
1023 return; |
|
1024 |
|
1025 double r_min = r.base (); |
|
1026 double r_max = r.limit (); |
|
1027 |
|
1028 if (r_max < r_min) |
|
1029 { |
|
1030 double tmp = r_max; |
|
1031 r_max = r_min; |
|
1032 r_min = tmp; |
|
1033 } |
|
1034 |
2387
|
1035 bool sign = (r_min < 0.0); |
1658
|
1036 |
2387
|
1037 bool all_ints = r.all_elements_are_ints (); |
1658
|
1038 |
|
1039 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
1040 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
1041 |
2800
|
1042 int x_max = max_abs == 0.0 |
|
1043 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
1044 |
|
1045 int x_min = min_abs == 0.0 |
|
1046 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
1047 |
4270
|
1048 scale = (x_max == 0 || all_ints) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
1049 |
1658
|
1050 set_range_format (sign, x_max, x_min, all_ints, fw); |
|
1051 } |
|
1052 |
1
|
1053 static inline void |
164
|
1054 set_format (const Range& r) |
1
|
1055 { |
|
1056 int fw; |
3105
|
1057 double scale; |
|
1058 set_format (r, fw, scale); |
1
|
1059 } |
|
1060 |
1282
|
1061 union equiv |
|
1062 { |
|
1063 double d; |
|
1064 unsigned char i[sizeof (double)]; |
|
1065 }; |
|
1066 |
1309
|
1067 #define PRINT_CHAR_BITS(os, c) \ |
|
1068 do \ |
|
1069 { \ |
|
1070 unsigned char ctmp = c; \ |
|
1071 char stmp[9]; \ |
1488
|
1072 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \ |
|
1073 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1074 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1075 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1076 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1077 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1078 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1079 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \ |
1309
|
1080 stmp[8] = '\0'; \ |
3013
|
1081 os << stmp; \ |
1309
|
1082 } \ |
|
1083 while (0) |
|
1084 |
|
1085 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
1086 do \ |
|
1087 { \ |
|
1088 unsigned char ctmp = c; \ |
|
1089 char stmp[9]; \ |
1488
|
1090 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \ |
|
1091 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1092 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1093 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1094 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1095 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1096 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1097 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \ |
1309
|
1098 stmp[8] = '\0'; \ |
3013
|
1099 os << stmp; \ |
1309
|
1100 } \ |
|
1101 while (0) |
|
1102 |
2522
|
1103 static void |
3608
|
1104 pr_any_float (const float_format *fmt, std::ostream& os, double d, int fw = 0) |
1
|
1105 { |
529
|
1106 if (fmt) |
1
|
1107 { |
5544
|
1108 // Unless explicitly asked for, always print in big-endian |
|
1109 // format for hex and bit formats. |
|
1110 // |
|
1111 // {bit,hex}_format == 1: print big-endian |
|
1112 // {bit,hex}_format == 2: print native |
|
1113 |
1282
|
1114 if (hex_format) |
|
1115 { |
|
1116 equiv tmp; |
|
1117 tmp.d = d; |
|
1118 |
|
1119 // Unless explicitly asked for, always print in big-endian |
|
1120 // format. |
|
1121 |
|
1122 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
1123 // formats and not for Cray? |
|
1124 |
3013
|
1125 // XXX FIXME XXX -- will bad things happen if we are |
|
1126 // interrupted before resetting the format flags and fill |
|
1127 // character? |
|
1128 |
2317
|
1129 oct_mach_info::float_format flt_fmt = |
|
1130 oct_mach_info::native_float_format (); |
|
1131 |
3013
|
1132 char ofill = os.fill ('0'); |
|
1133 |
3608
|
1134 std::ios::fmtflags oflags |
|
1135 = os.flags (std::ios::right | std::ios::hex); |
3013
|
1136 |
1282
|
1137 if (hex_format > 1 |
4574
|
1138 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian |
|
1139 || flt_fmt == oct_mach_info::flt_fmt_cray |
|
1140 || flt_fmt == oct_mach_info::flt_fmt_unknown) |
1282
|
1141 { |
1322
|
1142 for (size_t i = 0; i < sizeof (double); i++) |
3548
|
1143 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1144 } |
|
1145 else |
|
1146 { |
1328
|
1147 for (int i = sizeof (double) - 1; i >= 0; i--) |
3548
|
1148 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1149 } |
3013
|
1150 |
|
1151 os.fill (ofill); |
|
1152 os.setf (oflags); |
1282
|
1153 } |
1309
|
1154 else if (bit_format) |
|
1155 { |
|
1156 equiv tmp; |
|
1157 tmp.d = d; |
|
1158 |
|
1159 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
1160 // formats and not for Cray? |
|
1161 |
2317
|
1162 oct_mach_info::float_format flt_fmt = |
|
1163 oct_mach_info::native_float_format (); |
|
1164 |
4574
|
1165 if (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian |
|
1166 || flt_fmt == oct_mach_info::flt_fmt_cray |
|
1167 || flt_fmt == oct_mach_info::flt_fmt_unknown) |
1309
|
1168 { |
1322
|
1169 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1170 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1171 } |
|
1172 else |
|
1173 { |
|
1174 if (bit_format > 1) |
|
1175 { |
1328
|
1176 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1177 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
1178 } |
|
1179 else |
|
1180 { |
|
1181 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
1182 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1183 } |
|
1184 } |
|
1185 } |
1282
|
1186 else if (xisinf (d)) |
1
|
1187 { |
2804
|
1188 const char *s; |
1
|
1189 if (d < 0.0) |
|
1190 s = "-Inf"; |
|
1191 else |
|
1192 s = "Inf"; |
|
1193 |
|
1194 if (fw > 0) |
3548
|
1195 os << std::setw (fw) << s; |
1
|
1196 else |
|
1197 os << s; |
|
1198 } |
4025
|
1199 else if (octave_is_NA (d)) |
|
1200 { |
|
1201 if (fw > 0) |
|
1202 os << std::setw (fw) << "NA"; |
|
1203 else |
|
1204 os << "NA"; |
|
1205 } |
1
|
1206 else if (xisnan (d)) |
|
1207 { |
|
1208 if (fw > 0) |
3548
|
1209 os << std::setw (fw) << "NaN"; |
1
|
1210 else |
|
1211 os << "NaN"; |
|
1212 } |
|
1213 else |
3608
|
1214 os << pr_formatted_float (*fmt, d); |
1
|
1215 } |
529
|
1216 else |
|
1217 os << d; |
1
|
1218 } |
|
1219 |
|
1220 static inline void |
3608
|
1221 pr_float (std::ostream& os, double d, int fw = 0, double scale = 1.0) |
1
|
1222 { |
4509
|
1223 if (Vfixed_point_format && ! print_g && scale != 1.0) |
3608
|
1224 d /= scale; |
|
1225 |
1
|
1226 pr_any_float (curr_real_fmt, os, d, fw); |
|
1227 } |
|
1228 |
|
1229 static inline void |
3523
|
1230 pr_imag_float (std::ostream& os, double d, int fw = 0) |
1
|
1231 { |
|
1232 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1233 } |
|
1234 |
2522
|
1235 static void |
3608
|
1236 pr_complex (std::ostream& os, const Complex& c, int r_fw = 0, |
|
1237 int i_fw = 0, double scale = 1.0) |
1
|
1238 { |
4509
|
1239 Complex tmp |
|
1240 = (Vfixed_point_format && ! print_g && scale != 1.0) ? c / scale : c; |
3608
|
1241 |
|
1242 double r = tmp.real (); |
|
1243 |
1
|
1244 pr_float (os, r, r_fw); |
3608
|
1245 |
1
|
1246 if (! bank_format) |
|
1247 { |
3608
|
1248 double i = tmp.imag (); |
4349
|
1249 if (! (hex_format || bit_format) && lo_ieee_signbit (i)) |
1
|
1250 { |
|
1251 os << " - "; |
|
1252 i = -i; |
|
1253 pr_imag_float (os, i, i_fw); |
|
1254 } |
|
1255 else |
|
1256 { |
1309
|
1257 if (hex_format || bit_format) |
1282
|
1258 os << " "; |
|
1259 else |
|
1260 os << " + "; |
|
1261 |
1
|
1262 pr_imag_float (os, i, i_fw); |
|
1263 } |
|
1264 os << "i"; |
|
1265 } |
|
1266 } |
|
1267 |
626
|
1268 static void |
5275
|
1269 print_empty_matrix (std::ostream& os, octave_idx_type nr, octave_idx_type nc, bool pr_as_read_syntax) |
626
|
1270 { |
|
1271 assert (nr == 0 || nc == 0); |
|
1272 |
|
1273 if (pr_as_read_syntax) |
|
1274 { |
|
1275 if (nr == 0 && nc == 0) |
|
1276 os << "[]"; |
|
1277 else |
|
1278 os << "zeros (" << nr << ", " << nc << ")"; |
|
1279 } |
|
1280 else |
|
1281 { |
|
1282 os << "[]"; |
4559
|
1283 |
2165
|
1284 if (Vprint_empty_dimensions) |
626
|
1285 os << "(" << nr << "x" << nc << ")"; |
|
1286 } |
|
1287 } |
|
1288 |
1186
|
1289 static void |
4559
|
1290 print_empty_nd_array (std::ostream& os, const dim_vector& dims, |
|
1291 bool pr_as_read_syntax) |
|
1292 { |
|
1293 assert (dims.any_zero ()); |
|
1294 |
|
1295 if (pr_as_read_syntax) |
|
1296 os << "zeros (" << dims.str (',') << ")"; |
|
1297 else |
|
1298 { |
|
1299 os << "[]"; |
|
1300 |
|
1301 if (Vprint_empty_dimensions) |
|
1302 os << "(" << dims.str () << ")"; |
|
1303 } |
|
1304 } |
|
1305 |
|
1306 static void |
3523
|
1307 pr_scale_header (std::ostream& os, double scale) |
3105
|
1308 { |
4509
|
1309 if (Vfixed_point_format && ! print_g && scale != 1.0) |
3105
|
1310 { |
3568
|
1311 os << " " |
|
1312 << std::setw (8) << std::setprecision (1) |
|
1313 << std::setiosflags (std::ios::scientific|std::ios::left) |
|
1314 << scale |
|
1315 << std::resetiosflags (std::ios::scientific|std::ios::left) |
|
1316 << " *\n"; |
3105
|
1317 |
|
1318 if (! compact_format) |
|
1319 os << "\n"; |
|
1320 } |
|
1321 } |
|
1322 |
|
1323 static void |
5275
|
1324 pr_col_num_header (std::ostream& os, octave_idx_type total_width, int max_width, |
|
1325 octave_idx_type lim, octave_idx_type col, int extra_indent) |
1186
|
1326 { |
2165
|
1327 if (total_width > max_width && Vsplit_long_rows) |
1186
|
1328 { |
4833
|
1329 if (col != 0) |
|
1330 { |
|
1331 if (compact_format) |
|
1332 os << "\n"; |
|
1333 else |
|
1334 os << "\n\n"; |
|
1335 } |
1186
|
1336 |
5275
|
1337 octave_idx_type num_cols = lim - col; |
1186
|
1338 |
3548
|
1339 os << std::setw (extra_indent) << ""; |
1972
|
1340 |
1186
|
1341 if (num_cols == 1) |
|
1342 os << " Column " << col + 1 << ":\n"; |
|
1343 else if (num_cols == 2) |
|
1344 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1345 else |
|
1346 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
2915
|
1347 |
|
1348 if (! compact_format) |
|
1349 os << "\n"; |
1186
|
1350 } |
|
1351 } |
|
1352 |
5030
|
1353 template <class T> |
5404
|
1354 inline void |
5030
|
1355 pr_plus_format (std::ostream& os, const T& val) |
3248
|
1356 { |
5030
|
1357 if (val > T (0)) |
4632
|
1358 os << plus_format_chars[0]; |
5030
|
1359 else if (val < T (0)) |
4632
|
1360 os << plus_format_chars[1]; |
3248
|
1361 else |
4632
|
1362 os << plus_format_chars[2]; |
3248
|
1363 } |
|
1364 |
1
|
1365 void |
4661
|
1366 octave_print_internal (std::ostream& os, double d, |
|
1367 bool /* pr_as_read_syntax */) |
1
|
1368 { |
|
1369 if (plus_format) |
|
1370 { |
3608
|
1371 pr_plus_format (os, d); |
1
|
1372 } |
|
1373 else |
|
1374 { |
|
1375 set_format (d); |
|
1376 if (free_format) |
|
1377 os << d; |
|
1378 else |
|
1379 pr_float (os, d); |
|
1380 } |
|
1381 } |
|
1382 |
|
1383 void |
3608
|
1384 octave_print_internal (std::ostream& os, const Matrix& m, |
|
1385 bool pr_as_read_syntax, int extra_indent) |
1
|
1386 { |
5275
|
1387 octave_idx_type nr = m.rows (); |
|
1388 octave_idx_type nc = m.columns (); |
1
|
1389 |
2408
|
1390 if (nr == 0 || nc == 0) |
626
|
1391 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1392 else if (plus_format && ! pr_as_read_syntax) |
1
|
1393 { |
5275
|
1394 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1395 { |
5275
|
1396 for (octave_idx_type j = 0; j < nc; j++) |
1
|
1397 { |
4153
|
1398 OCTAVE_QUIT; |
|
1399 |
3608
|
1400 pr_plus_format (os, m(i,j)); |
1
|
1401 } |
2907
|
1402 |
|
1403 if (i < nr - 1) |
|
1404 os << "\n"; |
1
|
1405 } |
|
1406 } |
|
1407 else |
|
1408 { |
|
1409 int fw; |
3105
|
1410 double scale = 1.0; |
|
1411 set_format (m, fw, scale); |
1
|
1412 int column_width = fw + 2; |
5275
|
1413 octave_idx_type total_width = nc * column_width; |
|
1414 octave_idx_type max_width = command_editor::terminal_cols (); |
1
|
1415 |
626
|
1416 if (pr_as_read_syntax) |
|
1417 max_width -= 4; |
1972
|
1418 else |
|
1419 max_width -= extra_indent; |
|
1420 |
|
1421 if (max_width < 0) |
|
1422 max_width = 0; |
626
|
1423 |
1
|
1424 if (free_format) |
|
1425 { |
626
|
1426 if (pr_as_read_syntax) |
|
1427 os << "[\n"; |
|
1428 |
1
|
1429 os << m; |
626
|
1430 |
|
1431 if (pr_as_read_syntax) |
|
1432 os << "]"; |
|
1433 |
1
|
1434 return; |
|
1435 } |
|
1436 |
5275
|
1437 octave_idx_type inc = nc; |
2165
|
1438 if (total_width > max_width && Vsplit_long_rows) |
1
|
1439 { |
|
1440 inc = max_width / column_width; |
|
1441 if (inc == 0) |
|
1442 inc++; |
|
1443 } |
|
1444 |
626
|
1445 if (pr_as_read_syntax) |
1
|
1446 { |
5275
|
1447 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1448 { |
5275
|
1449 octave_idx_type col = 0; |
626
|
1450 while (col < nc) |
1
|
1451 { |
5275
|
1452 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
|
1453 |
|
1454 for (octave_idx_type j = col; j < lim; j++) |
626
|
1455 { |
4153
|
1456 OCTAVE_QUIT; |
|
1457 |
626
|
1458 if (i == 0 && j == 0) |
|
1459 os << "[ "; |
|
1460 else |
|
1461 { |
|
1462 if (j > col && j < lim) |
|
1463 os << ", "; |
|
1464 else |
|
1465 os << " "; |
|
1466 } |
|
1467 |
3608
|
1468 pr_float (os, m(i,j)); |
626
|
1469 } |
|
1470 |
|
1471 col += inc; |
|
1472 |
|
1473 if (col >= nc) |
|
1474 { |
|
1475 if (i == nr - 1) |
|
1476 os << " ]"; |
|
1477 else |
|
1478 os << ";\n"; |
|
1479 } |
|
1480 else |
|
1481 os << " ...\n"; |
1
|
1482 } |
|
1483 } |
626
|
1484 } |
|
1485 else |
|
1486 { |
3105
|
1487 pr_scale_header (os, scale); |
|
1488 |
5275
|
1489 for (octave_idx_type col = 0; col < nc; col += inc) |
626
|
1490 { |
5275
|
1491 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
626
|
1492 |
1972
|
1493 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1494 extra_indent); |
626
|
1495 |
5275
|
1496 for (octave_idx_type i = 0; i < nr; i++) |
626
|
1497 { |
3548
|
1498 os << std::setw (extra_indent) << ""; |
1972
|
1499 |
5275
|
1500 for (octave_idx_type j = col; j < lim; j++) |
626
|
1501 { |
4153
|
1502 OCTAVE_QUIT; |
|
1503 |
626
|
1504 os << " "; |
|
1505 |
3608
|
1506 pr_float (os, m(i,j), fw, scale); |
626
|
1507 } |
|
1508 |
2907
|
1509 if (i < nr - 1) |
|
1510 os << "\n"; |
626
|
1511 } |
|
1512 } |
1
|
1513 } |
|
1514 } |
|
1515 } |
|
1516 |
4532
|
1517 #define PRINT_ND_ARRAY(os, nda, NDA_T, ELT_T, MAT_T) \ |
|
1518 do \ |
|
1519 { \ |
4559
|
1520 if (nda.is_empty ()) \ |
|
1521 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); \ |
|
1522 else \ |
|
1523 { \ |
4532
|
1524 \ |
4559
|
1525 int ndims = nda.ndims (); \ |
|
1526 \ |
|
1527 dim_vector dims = nda.dims (); \ |
4532
|
1528 \ |
5275
|
1529 Array<octave_idx_type> ra_idx (ndims, 0); \ |
4532
|
1530 \ |
5275
|
1531 octave_idx_type m = 1; \ |
4532
|
1532 \ |
4559
|
1533 for (int i = 2; i < ndims; i++) \ |
|
1534 m *= dims(i); \ |
|
1535 \ |
5275
|
1536 octave_idx_type nr = dims(0); \ |
|
1537 octave_idx_type nc = dims(1); \ |
4532
|
1538 \ |
5275
|
1539 for (octave_idx_type i = 0; i < m; i++) \ |
4559
|
1540 { \ |
4655
|
1541 OCTAVE_QUIT; \ |
|
1542 \ |
4559
|
1543 std::string nm = "ans"; \ |
4532
|
1544 \ |
4559
|
1545 if (m > 1) \ |
|
1546 { \ |
|
1547 nm += "(:,:,"; \ |
4532
|
1548 \ |
5765
|
1549 std::ostringstream buf; \ |
4532
|
1550 \ |
4559
|
1551 for (int k = 2; k < ndims; k++) \ |
|
1552 { \ |
|
1553 buf << ra_idx(k) + 1; \ |
4532
|
1554 \ |
4559
|
1555 if (k < ndims - 1) \ |
|
1556 buf << ","; \ |
|
1557 else \ |
|
1558 buf << ")"; \ |
|
1559 } \ |
4532
|
1560 \ |
5765
|
1561 nm += buf.str (); \ |
4559
|
1562 } \ |
4532
|
1563 \ |
4559
|
1564 Array<idx_vector> idx (ndims); \ |
4532
|
1565 \ |
4559
|
1566 idx(0) = idx_vector (':'); \ |
|
1567 idx(1) = idx_vector (':'); \ |
|
1568 \ |
|
1569 for (int k = 2; k < ndims; k++) \ |
|
1570 idx(k) = idx_vector (ra_idx(k) + 1); \ |
4532
|
1571 \ |
4559
|
1572 octave_value page \ |
|
1573 = MAT_T (Array2<ELT_T> (nda.index (idx), nr, nc)); \ |
|
1574 \ |
|
1575 page.print_with_name (os, nm); \ |
4532
|
1576 \ |
4559
|
1577 if (i < m) \ |
|
1578 NDA_T::increment_index (ra_idx, dims, 2); \ |
|
1579 } \ |
|
1580 } \ |
4532
|
1581 } \ |
|
1582 while (0) |
|
1583 |
4513
|
1584 void |
|
1585 octave_print_internal (std::ostream& os, const NDArray& nda, |
|
1586 bool pr_as_read_syntax, int extra_indent) |
|
1587 { |
|
1588 switch (nda.ndims ()) |
|
1589 { |
|
1590 case 1: |
|
1591 case 2: |
|
1592 octave_print_internal (os, nda.matrix_value (), |
|
1593 pr_as_read_syntax, extra_indent); |
|
1594 break; |
|
1595 |
|
1596 default: |
4532
|
1597 PRINT_ND_ARRAY (os, nda, NDArray, double, Matrix); |
4513
|
1598 break; |
|
1599 } |
|
1600 } |
|
1601 |
5030
|
1602 template <> |
3248
|
1603 static inline void |
3608
|
1604 pr_plus_format (std::ostream& os, const Complex& c) |
3248
|
1605 { |
|
1606 double rp = c.real (); |
|
1607 double ip = c.imag (); |
|
1608 |
|
1609 if (rp == 0.0) |
|
1610 { |
|
1611 if (ip == 0.0) |
|
1612 os << " "; |
|
1613 else |
|
1614 os << "i"; |
|
1615 } |
|
1616 else if (ip == 0.0) |
3608
|
1617 pr_plus_format (os, rp); |
3248
|
1618 else |
|
1619 os << "c"; |
|
1620 } |
|
1621 |
1
|
1622 void |
3523
|
1623 octave_print_internal (std::ostream& os, const Complex& c, |
4661
|
1624 bool /* pr_as_read_syntax */) |
1
|
1625 { |
|
1626 if (plus_format) |
|
1627 { |
3608
|
1628 pr_plus_format (os, c); |
1
|
1629 } |
|
1630 else |
|
1631 { |
|
1632 set_format (c); |
|
1633 if (free_format) |
|
1634 os << c; |
|
1635 else |
|
1636 pr_complex (os, c); |
|
1637 } |
|
1638 } |
|
1639 |
|
1640 void |
3523
|
1641 octave_print_internal (std::ostream& os, const ComplexMatrix& cm, |
1972
|
1642 bool pr_as_read_syntax, int extra_indent) |
1
|
1643 { |
5275
|
1644 octave_idx_type nr = cm.rows (); |
|
1645 octave_idx_type nc = cm.columns (); |
1
|
1646 |
2408
|
1647 if (nr == 0 || nc == 0) |
626
|
1648 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1649 else if (plus_format && ! pr_as_read_syntax) |
1
|
1650 { |
5275
|
1651 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1652 { |
5275
|
1653 for (octave_idx_type j = 0; j < nc; j++) |
1
|
1654 { |
4153
|
1655 OCTAVE_QUIT; |
|
1656 |
3608
|
1657 pr_plus_format (os, cm(i,j)); |
1
|
1658 } |
2907
|
1659 |
|
1660 if (i < nr - 1) |
|
1661 os << "\n"; |
1
|
1662 } |
|
1663 } |
|
1664 else |
|
1665 { |
|
1666 int r_fw, i_fw; |
3105
|
1667 double scale = 1.0; |
|
1668 set_format (cm, r_fw, i_fw, scale); |
1
|
1669 int column_width = i_fw + r_fw; |
1309
|
1670 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
5275
|
1671 octave_idx_type total_width = nc * column_width; |
|
1672 octave_idx_type max_width = command_editor::terminal_cols (); |
1
|
1673 |
626
|
1674 if (pr_as_read_syntax) |
|
1675 max_width -= 4; |
1972
|
1676 else |
|
1677 max_width -= extra_indent; |
|
1678 |
|
1679 if (max_width < 0) |
|
1680 max_width = 0; |
626
|
1681 |
1
|
1682 if (free_format) |
|
1683 { |
626
|
1684 if (pr_as_read_syntax) |
|
1685 os << "[\n"; |
|
1686 |
1
|
1687 os << cm; |
626
|
1688 |
|
1689 if (pr_as_read_syntax) |
|
1690 os << "]"; |
|
1691 |
1
|
1692 return; |
|
1693 } |
|
1694 |
5275
|
1695 octave_idx_type inc = nc; |
2165
|
1696 if (total_width > max_width && Vsplit_long_rows) |
1
|
1697 { |
|
1698 inc = max_width / column_width; |
|
1699 if (inc == 0) |
|
1700 inc++; |
|
1701 } |
|
1702 |
626
|
1703 if (pr_as_read_syntax) |
1
|
1704 { |
5275
|
1705 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1706 { |
5275
|
1707 octave_idx_type col = 0; |
626
|
1708 while (col < nc) |
1
|
1709 { |
5275
|
1710 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
|
1711 |
|
1712 for (octave_idx_type j = col; j < lim; j++) |
626
|
1713 { |
4153
|
1714 OCTAVE_QUIT; |
|
1715 |
626
|
1716 if (i == 0 && j == 0) |
|
1717 os << "[ "; |
|
1718 else |
|
1719 { |
|
1720 if (j > col && j < lim) |
|
1721 os << ", "; |
|
1722 else |
|
1723 os << " "; |
|
1724 } |
|
1725 |
3608
|
1726 pr_complex (os, cm(i,j)); |
626
|
1727 } |
|
1728 |
|
1729 col += inc; |
|
1730 |
|
1731 if (col >= nc) |
|
1732 { |
|
1733 if (i == nr - 1) |
|
1734 os << " ]"; |
|
1735 else |
|
1736 os << ";\n"; |
|
1737 } |
1
|
1738 else |
626
|
1739 os << " ...\n"; |
1
|
1740 } |
|
1741 } |
626
|
1742 } |
|
1743 else |
|
1744 { |
3105
|
1745 pr_scale_header (os, scale); |
|
1746 |
5275
|
1747 for (octave_idx_type col = 0; col < nc; col += inc) |
626
|
1748 { |
5275
|
1749 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
626
|
1750 |
1972
|
1751 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1752 extra_indent); |
626
|
1753 |
5275
|
1754 for (octave_idx_type i = 0; i < nr; i++) |
626
|
1755 { |
3548
|
1756 os << std::setw (extra_indent) << ""; |
1972
|
1757 |
5275
|
1758 for (octave_idx_type j = col; j < lim; j++) |
626
|
1759 { |
4153
|
1760 OCTAVE_QUIT; |
|
1761 |
626
|
1762 os << " "; |
|
1763 |
3608
|
1764 pr_complex (os, cm(i,j), r_fw, i_fw, scale); |
626
|
1765 } |
2907
|
1766 |
|
1767 if (i < nr - 1) |
|
1768 os << "\n"; |
626
|
1769 } |
|
1770 } |
1
|
1771 } |
|
1772 } |
|
1773 } |
|
1774 |
|
1775 void |
4513
|
1776 octave_print_internal (std::ostream& os, const ComplexNDArray& nda, |
|
1777 bool pr_as_read_syntax, int extra_indent) |
|
1778 { |
|
1779 switch (nda.ndims ()) |
|
1780 { |
|
1781 case 1: |
|
1782 case 2: |
|
1783 octave_print_internal (os, nda.matrix_value (), |
|
1784 pr_as_read_syntax, extra_indent); |
|
1785 break; |
|
1786 |
|
1787 default: |
4532
|
1788 PRINT_ND_ARRAY (os, nda, ComplexNDArray, Complex, ComplexMatrix); |
4513
|
1789 break; |
|
1790 } |
|
1791 } |
|
1792 |
|
1793 void |
3523
|
1794 octave_print_internal (std::ostream& os, const Range& r, |
1972
|
1795 bool pr_as_read_syntax, int extra_indent) |
1
|
1796 { |
626
|
1797 double base = r.base (); |
1
|
1798 double increment = r.inc (); |
626
|
1799 double limit = r.limit (); |
5275
|
1800 octave_idx_type num_elem = r.nelem (); |
1
|
1801 |
626
|
1802 if (plus_format && ! pr_as_read_syntax) |
1
|
1803 { |
5275
|
1804 for (octave_idx_type i = 0; i < num_elem; i++) |
1
|
1805 { |
4153
|
1806 OCTAVE_QUIT; |
|
1807 |
626
|
1808 double val = base + i * increment; |
4632
|
1809 |
|
1810 pr_plus_format (os, val); |
1
|
1811 } |
|
1812 } |
|
1813 else |
|
1814 { |
|
1815 int fw; |
3105
|
1816 double scale = 1.0; |
|
1817 set_format (r, fw, scale); |
1
|
1818 |
626
|
1819 if (pr_as_read_syntax) |
1
|
1820 { |
626
|
1821 if (free_format) |
|
1822 { |
|
1823 os << base << " : "; |
|
1824 if (increment != 1.0) |
|
1825 os << increment << " : "; |
|
1826 os << limit; |
|
1827 } |
|
1828 else |
|
1829 { |
|
1830 pr_float (os, base, fw); |
|
1831 os << " : "; |
|
1832 if (increment != 1.0) |
|
1833 { |
|
1834 pr_float (os, increment, fw); |
|
1835 os << " : "; |
|
1836 } |
|
1837 pr_float (os, limit, fw); |
|
1838 } |
1
|
1839 } |
626
|
1840 else |
|
1841 { |
|
1842 int column_width = fw + 2; |
5275
|
1843 octave_idx_type total_width = num_elem * column_width; |
|
1844 octave_idx_type max_width = command_editor::terminal_cols (); |
1
|
1845 |
626
|
1846 if (free_format) |
|
1847 { |
|
1848 os << r; |
|
1849 return; |
|
1850 } |
1
|
1851 |
5275
|
1852 octave_idx_type inc = num_elem; |
2165
|
1853 if (total_width > max_width && Vsplit_long_rows) |
1
|
1854 { |
626
|
1855 inc = max_width / column_width; |
|
1856 if (inc == 0) |
|
1857 inc++; |
1
|
1858 } |
|
1859 |
1972
|
1860 max_width -= extra_indent; |
|
1861 |
|
1862 if (max_width < 0) |
|
1863 max_width = 0; |
|
1864 |
3105
|
1865 pr_scale_header (os, scale); |
|
1866 |
5275
|
1867 octave_idx_type col = 0; |
626
|
1868 while (col < num_elem) |
1
|
1869 { |
5275
|
1870 octave_idx_type lim = col + inc < num_elem ? col + inc : num_elem; |
626
|
1871 |
1972
|
1872 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1873 extra_indent); |
|
1874 |
3548
|
1875 os << std::setw (extra_indent) << ""; |
626
|
1876 |
5275
|
1877 for (octave_idx_type i = col; i < lim; i++) |
626
|
1878 { |
4153
|
1879 OCTAVE_QUIT; |
|
1880 |
626
|
1881 double val = base + i * increment; |
3105
|
1882 |
4791
|
1883 if (i == num_elem - 1) |
|
1884 { |
|
1885 // See the comments in Range::matrix_value. |
|
1886 |
|
1887 if ((increment > 0 && val > limit) |
|
1888 || (increment < 0 && val < limit)) |
|
1889 val = limit; |
|
1890 } |
|
1891 |
626
|
1892 os << " "; |
3105
|
1893 |
3608
|
1894 pr_float (os, val, fw, scale); |
626
|
1895 } |
|
1896 |
2907
|
1897 col += inc; |
1
|
1898 } |
|
1899 } |
|
1900 } |
|
1901 } |
|
1902 |
1343
|
1903 void |
3523
|
1904 octave_print_internal (std::ostream& os, const boolMatrix& bm, |
3215
|
1905 bool pr_as_read_syntax, |
|
1906 int extra_indent) |
|
1907 { |
|
1908 Matrix tmp (bm); |
|
1909 octave_print_internal (os, tmp, pr_as_read_syntax, extra_indent); |
|
1910 } |
|
1911 |
|
1912 void |
4513
|
1913 octave_print_internal (std::ostream& os, const boolNDArray& nda, |
|
1914 bool pr_as_read_syntax, |
|
1915 int extra_indent) |
|
1916 { |
|
1917 switch (nda.ndims ()) |
|
1918 { |
|
1919 case 1: |
|
1920 case 2: |
|
1921 octave_print_internal (os, nda.matrix_value (), |
|
1922 pr_as_read_syntax, extra_indent); |
|
1923 break; |
|
1924 |
|
1925 default: |
4532
|
1926 PRINT_ND_ARRAY (os, nda, boolNDArray, bool, boolMatrix); |
4513
|
1927 break; |
|
1928 } |
|
1929 } |
|
1930 |
|
1931 void |
3523
|
1932 octave_print_internal (std::ostream& os, const charMatrix& chm, |
3215
|
1933 bool pr_as_read_syntax, |
|
1934 int /* extra_indent XXX FIXME XXX */, |
|
1935 bool pr_as_string) |
1343
|
1936 { |
1572
|
1937 if (pr_as_string) |
|
1938 { |
5275
|
1939 octave_idx_type nstr = chm.rows (); |
1343
|
1940 |
1572
|
1941 if (pr_as_read_syntax && nstr > 1) |
|
1942 os << "[ "; |
1343
|
1943 |
2907
|
1944 if (nstr != 0) |
1343
|
1945 { |
5275
|
1946 for (octave_idx_type i = 0; i < nstr; i++) |
1572
|
1947 { |
4153
|
1948 OCTAVE_QUIT; |
|
1949 |
3523
|
1950 std::string row = chm.row_as_string (i); |
2664
|
1951 |
|
1952 if (pr_as_read_syntax) |
|
1953 { |
|
1954 os << "\"" << undo_string_escapes (row) << "\""; |
1343
|
1955 |
2664
|
1956 if (i < nstr - 1) |
|
1957 os << "; "; |
|
1958 } |
|
1959 else |
2907
|
1960 { |
|
1961 os << row; |
|
1962 |
|
1963 if (i < nstr - 1) |
|
1964 os << "\n"; |
|
1965 } |
1572
|
1966 } |
1343
|
1967 } |
1572
|
1968 |
|
1969 if (pr_as_read_syntax && nstr > 1) |
|
1970 os << " ]"; |
1343
|
1971 } |
1572
|
1972 else |
|
1973 { |
|
1974 os << "sorry, printing char matrices not implemented yet\n"; |
|
1975 } |
1343
|
1976 } |
|
1977 |
4513
|
1978 void |
|
1979 octave_print_internal (std::ostream& os, const charNDArray& nda, |
4663
|
1980 bool pr_as_read_syntax, int extra_indent, |
4513
|
1981 bool pr_as_string) |
|
1982 { |
|
1983 switch (nda.ndims ()) |
|
1984 { |
|
1985 case 1: |
|
1986 case 2: |
|
1987 octave_print_internal (os, nda.matrix_value (), |
|
1988 pr_as_read_syntax, extra_indent, pr_as_string); |
|
1989 break; |
|
1990 |
|
1991 default: |
4532
|
1992 PRINT_ND_ARRAY (os, nda, charNDArray, char, charMatrix); |
4513
|
1993 break; |
|
1994 } |
|
1995 } |
|
1996 |
4655
|
1997 void |
4925
|
1998 octave_print_internal (std::ostream& os, const std::string& s, |
|
1999 bool pr_as_read_syntax, int extra_indent) |
|
2000 { |
|
2001 ArrayN<std::string> nda (dim_vector (1, 1), s); |
|
2002 |
|
2003 octave_print_internal (os, nda, pr_as_read_syntax, extra_indent); |
|
2004 } |
|
2005 |
|
2006 void |
4655
|
2007 octave_print_internal (std::ostream& os, const ArrayN<std::string>& nda, |
4663
|
2008 bool pr_as_read_syntax, int /* extra_indent */) |
4655
|
2009 { |
|
2010 // XXX FIXME XXX -- this mostly duplicates the code in the |
|
2011 // PRINT_ND_ARRAY macro. |
|
2012 |
|
2013 if (nda.is_empty ()) |
|
2014 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); |
|
2015 else if (nda.length () == 1) |
|
2016 { |
|
2017 os << nda(0); |
|
2018 } |
|
2019 else |
|
2020 { |
|
2021 int ndims = nda.ndims (); |
|
2022 |
|
2023 dim_vector dims = nda.dims (); |
|
2024 |
5275
|
2025 Array<octave_idx_type> ra_idx (ndims, 0); |
|
2026 |
|
2027 octave_idx_type m = 1; |
4655
|
2028 |
|
2029 for (int i = 2; i < ndims; i++) |
|
2030 m *= dims(i); |
|
2031 |
5275
|
2032 octave_idx_type nr = dims(0); |
|
2033 octave_idx_type nc = dims(1); |
|
2034 |
|
2035 for (octave_idx_type i = 0; i < m; i++) |
4655
|
2036 { |
|
2037 std::string nm = "ans"; |
|
2038 |
|
2039 if (m > 1) |
|
2040 { |
|
2041 nm += "(:,:,"; |
|
2042 |
5765
|
2043 std::ostringstream buf; |
4655
|
2044 |
|
2045 for (int k = 2; k < ndims; k++) |
|
2046 { |
|
2047 buf << ra_idx(k) + 1; |
|
2048 |
|
2049 if (k < ndims - 1) |
|
2050 buf << ","; |
|
2051 else |
|
2052 buf << ")"; |
|
2053 } |
|
2054 |
5765
|
2055 nm += buf.str (); |
4655
|
2056 } |
|
2057 |
|
2058 Array<idx_vector> idx (ndims); |
|
2059 |
|
2060 idx(0) = idx_vector (':'); |
|
2061 idx(1) = idx_vector (':'); |
|
2062 |
|
2063 for (int k = 2; k < ndims; k++) |
|
2064 idx(k) = idx_vector (ra_idx(k) + 1); |
|
2065 |
|
2066 Array2<std::string> page (nda.index (idx), nr, nc); |
|
2067 |
|
2068 // XXX FIXME XXX -- need to do some more work to put these |
|
2069 // in neatly aligned columns... |
|
2070 |
5275
|
2071 octave_idx_type n_rows = page.rows (); |
|
2072 octave_idx_type n_cols = page.cols (); |
4655
|
2073 |
|
2074 os << nm << " =\n\n"; |
|
2075 |
5275
|
2076 for (octave_idx_type ii = 0; ii < n_rows; ii++) |
4655
|
2077 { |
5275
|
2078 for (octave_idx_type jj = 0; jj < n_cols; jj++) |
4655
|
2079 os << " " << page(ii,jj); |
|
2080 |
|
2081 os << "\n"; |
|
2082 } |
|
2083 |
|
2084 if (i < m - 1) |
|
2085 os << "\n"; |
|
2086 |
|
2087 if (i < m) |
|
2088 increment_index (ra_idx, dims, 2); |
|
2089 } |
|
2090 } |
|
2091 } |
|
2092 |
4901
|
2093 template <class T> |
|
2094 class |
|
2095 octave_print_conv |
|
2096 { |
|
2097 public: |
|
2098 typedef T print_conv_type; |
|
2099 }; |
|
2100 |
|
2101 #define PRINT_CONV(T1, T2) \ |
|
2102 template <> \ |
|
2103 class \ |
|
2104 octave_print_conv<T1> \ |
|
2105 { \ |
|
2106 public: \ |
|
2107 typedef T2 print_conv_type; \ |
|
2108 } |
|
2109 |
|
2110 PRINT_CONV (octave_int8, octave_int16); |
|
2111 PRINT_CONV (octave_uint8, octave_uint16); |
|
2112 |
|
2113 #undef PRINT_CONV |
|
2114 |
|
2115 template <class T> |
4949
|
2116 static inline void |
|
2117 pr_int (std::ostream& os, const T& d, int fw = 0) |
|
2118 { |
|
2119 size_t sz = d.byte_size(); |
|
2120 const unsigned char * tmpi = d.iptr(); |
|
2121 |
5544
|
2122 // Unless explicitly asked for, always print in big-endian |
|
2123 // format for hex and bit formats. |
|
2124 // |
|
2125 // {bit,hex}_format == 1: print big-endian |
|
2126 // {bit,hex}_format == 2: print native |
|
2127 |
4949
|
2128 if (hex_format) |
|
2129 { |
|
2130 char ofill = os.fill ('0'); |
|
2131 |
|
2132 std::ios::fmtflags oflags |
|
2133 = os.flags (std::ios::right | std::ios::hex); |
|
2134 |
|
2135 if (hex_format > 1 || oct_mach_info::words_big_endian ()) |
|
2136 { |
|
2137 for (size_t i = 0; i < sz; i++) |
|
2138 os << std::setw (2) << static_cast<int> (tmpi[i]); |
|
2139 } |
|
2140 else |
|
2141 { |
|
2142 for (int i = sz - 1; i >= 0; i--) |
|
2143 os << std::setw (2) << static_cast<int> (tmpi[i]); |
|
2144 } |
|
2145 |
|
2146 os.fill (ofill); |
|
2147 os.setf (oflags); |
|
2148 } |
|
2149 else if (bit_format) |
|
2150 { |
5544
|
2151 if (oct_mach_info::words_big_endian ()) |
4949
|
2152 { |
|
2153 for (size_t i = 0; i < sz; i++) |
5544
|
2154 PRINT_CHAR_BITS (os, tmpi[i]); |
4949
|
2155 } |
|
2156 else |
|
2157 { |
5544
|
2158 if (bit_format > 1) |
|
2159 { |
|
2160 for (size_t i = 0; i < sz; i++) |
|
2161 PRINT_CHAR_BITS_SWAPPED (os, tmpi[i]); |
|
2162 } |
|
2163 else |
|
2164 { |
|
2165 for (int i = sz - 1; i >= 0; i--) |
|
2166 PRINT_CHAR_BITS (os, tmpi[i]); |
|
2167 } |
4949
|
2168 } |
|
2169 } |
|
2170 else |
|
2171 { |
|
2172 os << std::setw (fw) |
|
2173 << typename octave_print_conv<T>::print_conv_type (d); |
|
2174 |
|
2175 if (bank_format) |
|
2176 os << ".00"; |
|
2177 } |
|
2178 } |
|
2179 |
|
2180 template <class T> |
4901
|
2181 void |
|
2182 octave_print_internal (std::ostream& os, const intNDArray<T>& nda, |
4949
|
2183 bool pr_as_read_syntax, int extra_indent) |
4901
|
2184 { |
|
2185 // XXX FIXME XXX -- this mostly duplicates the code in the |
|
2186 // PRINT_ND_ARRAY macro. |
|
2187 |
|
2188 if (nda.is_empty ()) |
|
2189 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); |
|
2190 else if (nda.length () == 1) |
4949
|
2191 octave_print_internal (os, nda (0), pr_as_read_syntax); |
|
2192 else if (plus_format && ! pr_as_read_syntax) |
4901
|
2193 { |
|
2194 int ndims = nda.ndims (); |
|
2195 |
5275
|
2196 Array<octave_idx_type> ra_idx (ndims, 0); |
4949
|
2197 |
4901
|
2198 dim_vector dims = nda.dims (); |
|
2199 |
5275
|
2200 octave_idx_type m = 1; |
4901
|
2201 |
|
2202 for (int i = 2; i < ndims; i++) |
|
2203 m *= dims(i); |
|
2204 |
5275
|
2205 octave_idx_type nr = dims(0); |
|
2206 octave_idx_type nc = dims(1); |
|
2207 |
|
2208 for (octave_idx_type i = 0; i < m; i++) |
4901
|
2209 { |
|
2210 if (m > 1) |
|
2211 { |
4949
|
2212 std::string nm = "ans(:,:,"; |
4901
|
2213 |
5765
|
2214 std::ostringstream buf; |
4901
|
2215 |
|
2216 for (int k = 2; k < ndims; k++) |
|
2217 { |
|
2218 buf << ra_idx(k) + 1; |
|
2219 |
|
2220 if (k < ndims - 1) |
|
2221 buf << ","; |
|
2222 else |
|
2223 buf << ")"; |
|
2224 } |
|
2225 |
5765
|
2226 nm += buf.str (); |
4949
|
2227 |
|
2228 os << nm << " =\n\n"; |
4901
|
2229 } |
|
2230 |
|
2231 Array<idx_vector> idx (ndims); |
|
2232 |
|
2233 idx(0) = idx_vector (':'); |
|
2234 idx(1) = idx_vector (':'); |
|
2235 |
|
2236 for (int k = 2; k < ndims; k++) |
|
2237 idx(k) = idx_vector (ra_idx(k) + 1); |
|
2238 |
|
2239 Array2<T> page (nda.index (idx), nr, nc); |
|
2240 |
5275
|
2241 for (octave_idx_type ii = 0; ii < nr; ii++) |
4901
|
2242 { |
5275
|
2243 for (octave_idx_type jj = 0; jj < nc; jj++) |
4949
|
2244 { |
|
2245 OCTAVE_QUIT; |
|
2246 |
|
2247 pr_plus_format (os, page(ii,jj)); |
|
2248 } |
|
2249 |
|
2250 if ((ii < nr - 1) || (i < m -1)) |
|
2251 os << "\n"; |
4901
|
2252 } |
|
2253 |
|
2254 if (i < m - 1) |
4949
|
2255 { |
|
2256 os << "\n"; |
|
2257 increment_index (ra_idx, dims, 2); |
|
2258 } |
|
2259 } |
|
2260 } |
|
2261 else |
|
2262 { |
|
2263 int ndims = nda.ndims (); |
|
2264 |
|
2265 dim_vector dims = nda.dims (); |
|
2266 |
5275
|
2267 Array<octave_idx_type> ra_idx (ndims, 0); |
|
2268 |
|
2269 octave_idx_type m = 1; |
4949
|
2270 |
|
2271 for (int i = 2; i < ndims; i++) |
|
2272 m *= dims(i); |
|
2273 |
5275
|
2274 octave_idx_type nr = dims(0); |
|
2275 octave_idx_type nc = dims(1); |
4949
|
2276 |
|
2277 int fw = 0; |
|
2278 if (hex_format) |
|
2279 fw = 2 * nda(0).byte_size (); |
|
2280 else if (bit_format) |
|
2281 fw = nda(0).nbits (); |
|
2282 else |
|
2283 { |
|
2284 bool isneg = false; |
|
2285 int digits = 0; |
|
2286 |
5275
|
2287 for (octave_idx_type i = 0; i < dims.numel (); i++) |
4949
|
2288 { |
|
2289 int new_digits = static_cast<int> |
|
2290 (floor (log10 (double (abs (nda(i).value ()))) + 1.0)); |
|
2291 |
|
2292 if (new_digits > digits) |
|
2293 digits = new_digits; |
|
2294 |
|
2295 if (! isneg) |
|
2296 isneg = (abs (nda(i).value ()) != nda(i).value ()); |
|
2297 } |
|
2298 |
|
2299 fw = digits + isneg; |
|
2300 } |
|
2301 |
|
2302 int column_width = fw + (bank_format ? 5 : 2); |
5275
|
2303 octave_idx_type total_width = nc * column_width; |
4949
|
2304 int max_width = command_editor::terminal_cols () - extra_indent; |
5275
|
2305 octave_idx_type inc = nc; |
4949
|
2306 if (total_width > max_width && Vsplit_long_rows) |
|
2307 { |
|
2308 inc = max_width / column_width; |
|
2309 if (inc == 0) |
|
2310 inc++; |
|
2311 } |
|
2312 |
5275
|
2313 for (octave_idx_type i = 0; i < m; i++) |
4949
|
2314 { |
|
2315 if (m > 1) |
|
2316 { |
|
2317 std::string nm = "ans(:,:,"; |
|
2318 |
5765
|
2319 std::ostringstream buf; |
4949
|
2320 |
|
2321 for (int k = 2; k < ndims; k++) |
|
2322 { |
|
2323 buf << ra_idx(k) + 1; |
|
2324 |
|
2325 if (k < ndims - 1) |
|
2326 buf << ","; |
|
2327 else |
|
2328 buf << ")"; |
|
2329 } |
|
2330 |
5765
|
2331 nm += buf.str (); |
4949
|
2332 |
|
2333 os << nm << " =\n\n"; |
|
2334 } |
|
2335 |
|
2336 Array<idx_vector> idx (ndims); |
|
2337 |
|
2338 idx(0) = idx_vector (':'); |
|
2339 idx(1) = idx_vector (':'); |
|
2340 |
|
2341 for (int k = 2; k < ndims; k++) |
|
2342 idx(k) = idx_vector (ra_idx(k) + 1); |
|
2343 |
|
2344 Array2<T> page (nda.index (idx), nr, nc); |
|
2345 |
|
2346 if (free_format) |
|
2347 { |
|
2348 if (pr_as_read_syntax) |
|
2349 os << "[\n"; |
|
2350 |
5275
|
2351 for (octave_idx_type ii = 0; ii < nr; ii++) |
4949
|
2352 { |
5275
|
2353 for (octave_idx_type jj = 0; jj < nc; jj++) |
4949
|
2354 { |
|
2355 OCTAVE_QUIT; |
|
2356 os << " "; |
|
2357 os << typename octave_print_conv<T>::print_conv_type (page(ii,jj)); |
|
2358 } |
|
2359 os << "\n"; |
|
2360 } |
|
2361 |
|
2362 if (pr_as_read_syntax) |
|
2363 os << "]"; |
|
2364 } |
|
2365 else |
|
2366 { |
5275
|
2367 octave_idx_type n_rows = page.rows (); |
|
2368 octave_idx_type n_cols = page.cols (); |
|
2369 |
|
2370 for (octave_idx_type col = 0; col < n_cols; col += inc) |
4949
|
2371 { |
5275
|
2372 octave_idx_type lim = col + inc < n_cols ? col + inc : n_cols; |
4949
|
2373 |
|
2374 pr_col_num_header (os, total_width, max_width, lim, col, |
|
2375 extra_indent); |
|
2376 |
5275
|
2377 for (octave_idx_type ii = 0; ii < n_rows; ii++) |
4949
|
2378 { |
|
2379 os << std::setw (extra_indent) << ""; |
|
2380 |
5275
|
2381 for (octave_idx_type jj = col; jj < lim; jj++) |
4949
|
2382 { |
|
2383 OCTAVE_QUIT; |
|
2384 os << " "; |
|
2385 pr_int (os, page(ii,jj), fw); |
|
2386 } |
|
2387 if ((ii < n_rows - 1) || (i < m -1)) |
|
2388 os << "\n"; |
|
2389 } |
|
2390 } |
|
2391 } |
|
2392 |
|
2393 if (i < m - 1) |
|
2394 { |
|
2395 os << "\n"; |
|
2396 increment_index (ra_idx, dims, 2); |
|
2397 } |
4901
|
2398 } |
|
2399 } |
|
2400 } |
|
2401 |
|
2402 // XXX FIXME XXX -- this is not the right spot for this... |
|
2403 |
|
2404 template void |
4949
|
2405 pr_int (std::ostream&, const octave_int8&, int); |
|
2406 |
|
2407 template void |
|
2408 pr_int (std::ostream&, const octave_int16&, int); |
|
2409 |
|
2410 template void |
|
2411 pr_int (std::ostream&, const octave_int32&, int); |
|
2412 |
|
2413 template void |
|
2414 pr_int (std::ostream&, const octave_int64&, int); |
|
2415 |
|
2416 template void |
|
2417 pr_int (std::ostream&, const octave_uint8&, int); |
|
2418 |
|
2419 template void |
|
2420 pr_int (std::ostream&, const octave_uint16&, int); |
|
2421 |
|
2422 template void |
|
2423 pr_int (std::ostream&, const octave_uint32&, int); |
|
2424 |
|
2425 template void |
|
2426 pr_int (std::ostream&, const octave_uint64&, int); |
|
2427 |
|
2428 template void |
4901
|
2429 octave_print_internal (std::ostream&, const intNDArray<octave_int8>&, |
|
2430 bool, int); |
|
2431 |
|
2432 template void |
|
2433 octave_print_internal (std::ostream&, const intNDArray<octave_int16>&, |
|
2434 bool, int); |
|
2435 |
|
2436 template void |
|
2437 octave_print_internal (std::ostream&, const intNDArray<octave_int32>&, |
|
2438 bool, int); |
|
2439 |
|
2440 template void |
|
2441 octave_print_internal (std::ostream&, const intNDArray<octave_int64>&, |
|
2442 bool, int); |
|
2443 |
|
2444 template void |
|
2445 octave_print_internal (std::ostream&, const intNDArray<octave_uint8>&, |
|
2446 bool, int); |
|
2447 |
|
2448 template void |
|
2449 octave_print_internal (std::ostream&, const intNDArray<octave_uint16>&, |
|
2450 bool, int); |
|
2451 |
|
2452 template void |
|
2453 octave_print_internal (std::ostream&, const intNDArray<octave_uint32>&, |
|
2454 bool, int); |
|
2455 |
|
2456 template void |
|
2457 octave_print_internal (std::ostream&, const intNDArray<octave_uint64>&, |
|
2458 bool, int); |
|
2459 |
|
2460 template <class T> |
|
2461 void |
4933
|
2462 octave_print_internal (std::ostream& os, const octave_int<T>& val, bool) |
4901
|
2463 { |
4949
|
2464 if (plus_format) |
|
2465 { |
|
2466 pr_plus_format (os, val); |
|
2467 } |
|
2468 else |
|
2469 { |
|
2470 if (free_format) |
5030
|
2471 os << typename octave_print_conv<octave_int<T> >::print_conv_type (val); |
4949
|
2472 else |
|
2473 pr_int (os, val); |
|
2474 } |
4901
|
2475 } |
|
2476 |
|
2477 // XXX FIXME XXX -- this is not the right spot for this... |
|
2478 |
|
2479 template void |
|
2480 octave_print_internal (std::ostream&, const octave_int8&, bool); |
|
2481 |
|
2482 template void |
|
2483 octave_print_internal (std::ostream&, const octave_int16&, bool); |
|
2484 |
|
2485 template void |
|
2486 octave_print_internal (std::ostream&, const octave_int32&, bool); |
|
2487 |
|
2488 template void |
|
2489 octave_print_internal (std::ostream&, const octave_int64&, bool); |
|
2490 |
|
2491 template void |
|
2492 octave_print_internal (std::ostream&, const octave_uint8&, bool); |
|
2493 |
|
2494 template void |
|
2495 octave_print_internal (std::ostream&, const octave_uint16&, bool); |
|
2496 |
|
2497 template void |
|
2498 octave_print_internal (std::ostream&, const octave_uint32&, bool); |
|
2499 |
|
2500 template void |
|
2501 octave_print_internal (std::ostream&, const octave_uint64&, bool); |
|
2502 |
3933
|
2503 extern void |
|
2504 octave_print_internal (std::ostream&, const Cell&, bool, int, bool) |
3928
|
2505 { |
3933
|
2506 panic_impossible (); |
3928
|
2507 } |
|
2508 |
3685
|
2509 DEFUN (disp, args, nargout, |
|
2510 "-*- texinfo -*-\n\ |
|
2511 @deftypefn {Built-in Function} {} disp (@var{x})\n\ |
|
2512 Display the value of @var{x}. For example,\n\ |
|
2513 \n\ |
|
2514 @example\n\ |
|
2515 disp (\"The value of pi is:\"), disp (pi)\n\ |
|
2516 \n\ |
|
2517 @print{} the value of pi is:\n\ |
|
2518 @print{} 3.1416\n\ |
|
2519 @end example\n\ |
|
2520 \n\ |
|
2521 @noindent\n\ |
|
2522 Note that the output from @code{disp} always ends with a newline.\n\ |
|
2523 \n\ |
|
2524 If an output value is requested, @code{disp} prints nothing and\n\ |
|
2525 returns the formatted output in a string.\n\ |
5642
|
2526 @seealso{fdisp}\n\ |
|
2527 @end deftypefn") |
3685
|
2528 { |
|
2529 octave_value retval; |
|
2530 |
|
2531 int nargin = args.length (); |
|
2532 |
|
2533 if (nargin == 1 && nargout < 2) |
|
2534 { |
|
2535 if (nargout == 0) |
|
2536 args(0).print (octave_stdout); |
|
2537 else |
|
2538 { |
5765
|
2539 std::ostringstream buf; |
3685
|
2540 args(0).print (buf); |
5765
|
2541 retval = buf.str (); |
3685
|
2542 } |
|
2543 } |
|
2544 else |
|
2545 print_usage ("disp"); |
|
2546 |
|
2547 return retval; |
|
2548 } |
|
2549 |
|
2550 DEFUN (fdisp, args, , |
|
2551 "-*- texinfo -*-\n\ |
|
2552 @deftypefn {Built-in Function} {} fdisp (@var{fid}, @var{x})\n\ |
|
2553 Display the value of @var{x} on the stream @var{fid}. For example,\n\ |
|
2554 \n\ |
|
2555 @example\n\ |
4869
|
2556 fdisp (stdout, \"The value of pi is:\"), fdisp (stdout, pi)\n\ |
3685
|
2557 \n\ |
|
2558 @print{} the value of pi is:\n\ |
|
2559 @print{} 3.1416\n\ |
|
2560 @end example\n\ |
|
2561 \n\ |
|
2562 @noindent\n\ |
4869
|
2563 Note that the output from @code{fdisp} always ends with a newline.\n\ |
5642
|
2564 @seealso{disp}\n\ |
|
2565 @end deftypefn") |
3685
|
2566 { |
|
2567 octave_value retval; |
|
2568 |
|
2569 int nargin = args.length (); |
|
2570 |
|
2571 if (nargin == 2) |
|
2572 { |
|
2573 int fid = octave_stream_list::get_file_number (args (0)); |
|
2574 |
|
2575 octave_stream os = octave_stream_list::lookup (fid, "fdisp"); |
|
2576 |
|
2577 if (! error_state) |
|
2578 { |
3769
|
2579 std::ostream *osp = os.output_stream (); |
3685
|
2580 |
|
2581 if (osp) |
|
2582 args(1).print (*osp); |
|
2583 else |
|
2584 error ("fdisp: stream not open for writing"); |
|
2585 } |
|
2586 } |
|
2587 else |
|
2588 print_usage ("fdisp"); |
|
2589 |
|
2590 return retval; |
|
2591 } |
|
2592 |
1
|
2593 static void |
|
2594 init_format_state (void) |
|
2595 { |
2387
|
2596 free_format = false; |
|
2597 plus_format = false; |
|
2598 bank_format = false; |
3608
|
2599 hex_format = 0; |
1309
|
2600 bit_format = 0; |
4833
|
2601 compact_format = false; |
2387
|
2602 print_e = false; |
|
2603 print_big_e = false; |
4509
|
2604 print_g = false; |
1
|
2605 } |
|
2606 |
|
2607 static void |
|
2608 set_output_prec_and_fw (int prec, int fw) |
|
2609 { |
4233
|
2610 bind_builtin_variable ("output_precision", prec); |
|
2611 bind_builtin_variable ("output_max_field_width", fw); |
1
|
2612 } |
|
2613 |
1755
|
2614 static void |
|
2615 set_format_style (int argc, const string_vector& argv) |
1
|
2616 { |
1755
|
2617 int idx = 1; |
|
2618 |
1899
|
2619 if (--argc > 0) |
1
|
2620 { |
3523
|
2621 std::string arg = argv[idx++]; |
2584
|
2622 |
1755
|
2623 if (arg == "short") |
1
|
2624 { |
1755
|
2625 if (--argc > 0) |
1
|
2626 { |
1755
|
2627 arg = argv[idx++]; |
|
2628 |
|
2629 if (arg == "e") |
1
|
2630 { |
1755
|
2631 init_format_state (); |
2387
|
2632 print_e = true; |
1755
|
2633 } |
|
2634 else if (arg == "E") |
|
2635 { |
|
2636 init_format_state (); |
2387
|
2637 print_e = true; |
|
2638 print_big_e = true; |
1
|
2639 } |
4509
|
2640 else if (arg == "g") |
|
2641 { |
|
2642 init_format_state (); |
|
2643 print_g = true; |
|
2644 } |
|
2645 else if (arg == "G") |
|
2646 { |
|
2647 init_format_state (); |
|
2648 print_g = true; |
|
2649 print_big_e = true; |
|
2650 } |
1
|
2651 else |
|
2652 { |
1755
|
2653 error ("format: unrecognized option `short %s'", |
|
2654 arg.c_str ()); |
|
2655 return; |
|
2656 } |
|
2657 } |
|
2658 else |
|
2659 init_format_state (); |
|
2660 |
4509
|
2661 set_output_prec_and_fw (5, 10); |
1755
|
2662 } |
|
2663 else if (arg == "long") |
|
2664 { |
|
2665 if (--argc > 0) |
|
2666 { |
|
2667 arg = argv[idx++]; |
|
2668 |
|
2669 if (arg == "e") |
|
2670 { |
|
2671 init_format_state (); |
2387
|
2672 print_e = true; |
1755
|
2673 } |
|
2674 else if (arg == "E") |
|
2675 { |
|
2676 init_format_state (); |
2387
|
2677 print_e = true; |
|
2678 print_big_e = true; |
1
|
2679 } |
4509
|
2680 else if (arg == "g") |
|
2681 { |
|
2682 init_format_state (); |
|
2683 print_g = true; |
|
2684 } |
|
2685 else if (arg == "G") |
|
2686 { |
|
2687 init_format_state (); |
|
2688 print_g = true; |
|
2689 print_big_e = true; |
|
2690 } |
1
|
2691 else |
1755
|
2692 { |
|
2693 error ("format: unrecognized option `long %s'", |
|
2694 arg.c_str ()); |
|
2695 return; |
|
2696 } |
1186
|
2697 } |
1
|
2698 else |
1755
|
2699 init_format_state (); |
|
2700 |
4509
|
2701 set_output_prec_and_fw (15, 20); |
1755
|
2702 } |
|
2703 else if (arg == "hex") |
|
2704 { |
|
2705 init_format_state (); |
3608
|
2706 hex_format = 1; |
1755
|
2707 } |
|
2708 else if (arg == "native-hex") |
|
2709 { |
|
2710 init_format_state (); |
|
2711 hex_format = 2; |
|
2712 } |
|
2713 else if (arg == "bit") |
|
2714 { |
|
2715 init_format_state (); |
|
2716 bit_format = 1; |
|
2717 } |
|
2718 else if (arg == "native-bit") |
|
2719 { |
|
2720 init_format_state (); |
|
2721 bit_format = 2; |
|
2722 } |
|
2723 else if (arg == "+" || arg == "plus") |
|
2724 { |
4632
|
2725 if (--argc > 0) |
|
2726 { |
|
2727 arg = argv[idx++]; |
|
2728 |
|
2729 if (arg.length () == 3) |
|
2730 plus_format_chars = arg; |
|
2731 else |
|
2732 { |
|
2733 error ("format: invalid option for plus format"); |
|
2734 return; |
|
2735 } |
|
2736 } |
|
2737 else |
|
2738 plus_format_chars = "+ "; |
|
2739 |
1755
|
2740 init_format_state (); |
2387
|
2741 plus_format = true; |
1755
|
2742 } |
|
2743 else if (arg == "bank") |
|
2744 { |
|
2745 init_format_state (); |
2387
|
2746 bank_format = true; |
1755
|
2747 } |
|
2748 else if (arg == "free") |
|
2749 { |
|
2750 init_format_state (); |
2387
|
2751 free_format = true; |
1755
|
2752 } |
|
2753 else if (arg == "none") |
|
2754 { |
|
2755 init_format_state (); |
2387
|
2756 free_format = true; |
1755
|
2757 } |
|
2758 else if (arg == "compact") |
|
2759 { |
2387
|
2760 compact_format = true; |
1755
|
2761 } |
|
2762 else if (arg == "loose") |
|
2763 { |
2387
|
2764 compact_format = false; |
1
|
2765 } |
|
2766 else |
1755
|
2767 error ("format: unrecognized format state `%s'", arg.c_str ()); |
1
|
2768 } |
|
2769 else |
|
2770 { |
|
2771 init_format_state (); |
|
2772 set_output_prec_and_fw (5, 10); |
|
2773 } |
|
2774 } |
|
2775 |
4208
|
2776 DEFCMD (format, args, , |
3372
|
2777 "-*- texinfo -*-\n\ |
|
2778 @deffn {Command} format options\n\ |
|
2779 Control the format of the output produced by @code{disp} and Octave's\n\ |
|
2780 normal echoing mechanism. Valid options are listed in the following\n\ |
|
2781 table.\n\ |
|
2782 \n\ |
|
2783 @table @code\n\ |
|
2784 @item short\n\ |
|
2785 Octave will try to print numbers with at\n\ |
4509
|
2786 least 5 significant figures within a field that is a maximum of 10\n\ |
|
2787 characters wide (not counting additional spacing that is added between\n\ |
|
2788 columns of a matrix).\n\ |
3372
|
2789 \n\ |
|
2790 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
2791 decimal point and all the numbers fit within the maximum field width,\n\ |
|
2792 it switches to an @samp{e} format.\n\ |
|
2793 \n\ |
|
2794 @item long\n\ |
|
2795 Octave will try to print numbers with at least 15 significant figures\n\ |
4509
|
2796 within a field that is a maximum of 20 characters wide (not counting\n\ |
|
2797 additional spacing that is added between columns of a matrix).\n\ |
3372
|
2798 \n\ |
|
2799 As will the @samp{short} format, Octave will switch to an @samp{e}\n\ |
|
2800 format if it is unable to format a matrix so that columns line up on the\n\ |
|
2801 decimal point and all the numbers fit within the maximum field width.\n\ |
|
2802 \n\ |
|
2803 @item long e\n\ |
|
2804 @itemx short e\n\ |
|
2805 The same as @samp{format long} or @samp{format short} but always display\n\ |
|
2806 output with an @samp{e} format. For example, with the @samp{short e}\n\ |
4509
|
2807 format, @code{pi} is displayed as @code{3.14e+00}.\n\ |
3372
|
2808 \n\ |
|
2809 @item long E\n\ |
|
2810 @itemx short E\n\ |
|
2811 The same as @samp{format long e} or @samp{format short e} but always\n\ |
|
2812 display output with an uppercase @samp{E} format. For example, with\n\ |
4509
|
2813 the @samp{long E} format, @code{pi} is displayed as\n\ |
3372
|
2814 @code{3.14159265358979E+00}.\n\ |
4509
|
2815 @item long g\n\ |
|
2816 @itemx short g\n\ |
|
2817 Choose between normal @samp{long} (or @samp{short}) and and\n\ |
|
2818 @samp{long e} (or @samp{short e}) formats based on the magnitude\n\ |
|
2819 of the number. For example, with the @samp{short g} format,\n\ |
|
2820 @code{pi .^ [2; 4; 8; 16; 32]} is displayed as\n\ |
|
2821 \n\ |
|
2822 @example\n\ |
|
2823 @group\n\ |
|
2824 ans =\n\ |
|
2825 \n\ |
|
2826 3.1416\n\ |
|
2827 9.8696\n\ |
|
2828 97.409\n\ |
|
2829 9488.5\n\ |
|
2830 9.0032e+07\n\ |
|
2831 8.1058e+15\n\ |
|
2832 @end group\n\ |
|
2833 @end example\n\ |
|
2834 \n\ |
|
2835 @item long G\n\ |
|
2836 @itemx short G\n\ |
|
2837 The same as @samp{format long g} or @samp{format short g} but use an\n\ |
|
2838 uppercase @samp{E} format. For example, with the @samp{short G} format,\n\ |
|
2839 @code{pi .^ [2; 4; 8; 16; 32]} is displayed as\n\ |
|
2840 \n\ |
|
2841 @example\n\ |
|
2842 @group\n\ |
|
2843 ans =\n\ |
|
2844 \n\ |
|
2845 3.1416\n\ |
|
2846 9.8696\n\ |
|
2847 97.409\n\ |
|
2848 9488.5\n\ |
|
2849 9.0032E+07\n\ |
|
2850 8.1058E+15\n\ |
|
2851 @end group\n\ |
|
2852 @end example\n\ |
3372
|
2853 \n\ |
|
2854 @item free\n\ |
|
2855 @itemx none\n\ |
|
2856 Print output in free format, without trying to line up columns of\n\ |
|
2857 matrices on the decimal point. This also causes complex numbers to be\n\ |
|
2858 formatted like this @samp{(0.604194, 0.607088)} instead of like this\n\ |
|
2859 @samp{0.60419 + 0.60709i}.\n\ |
529
|
2860 \n\ |
3372
|
2861 @item bank\n\ |
|
2862 Print in a fixed format with two places to the right of the decimal\n\ |
|
2863 point.\n\ |
|
2864 \n\ |
|
2865 @item +\n\ |
4632
|
2866 @itemx + @var{chars}\n\ |
|
2867 @itemx plus\n\ |
|
2868 @itemx plus @var{chars}\n\ |
3372
|
2869 Print a @samp{+} symbol for nonzero matrix elements and a space for zero\n\ |
|
2870 matrix elements. This format can be very useful for examining the\n\ |
|
2871 structure of a large matrix.\n\ |
|
2872 \n\ |
4632
|
2873 The optional argument @var{chars} specifies a list of 3 characters to use\n\ |
|
2874 for printing values greater than zero, less than zero and equal to zero.\n\ |
|
2875 For example, with the @samp{+ \"+-.\"} format, @code{[1, 0, -1; -1, 0, 1]}\n\ |
|
2876 is displayed as\n\ |
|
2877 \n\ |
|
2878 @example\n\ |
|
2879 @group\n\ |
|
2880 ans =\n\ |
|
2881 \n\ |
|
2882 +.-\n\ |
|
2883 -.+\n\ |
|
2884 @end group\n\ |
|
2885 @end example\n\ |
|
2886 \n\ |
4833
|
2887 @itemx native-hex\n\ |
3372
|
2888 Print the hexadecimal representation numbers as they are stored in\n\ |
|
2889 memory. For example, on a workstation which stores 8 byte real values\n\ |
|
2890 in IEEE format with the least significant byte first, the value of\n\ |
|
2891 @code{pi} when printed in @code{hex} format is @code{400921fb54442d18}.\n\ |
|
2892 This format only works for numeric values.\n\ |
|
2893 \n\ |
4833
|
2894 @item hex\n\ |
|
2895 The same as @code{native-hex}, but always print the most significant\n\ |
|
2896 byte first.\n\ |
|
2897 @item native-bit\n\ |
3372
|
2898 Print the bit representation of numbers as stored in memory.\n\ |
|
2899 For example, the value of @code{pi} is\n\ |
|
2900 \n\ |
|
2901 @example\n\ |
|
2902 @group\n\ |
|
2903 01000000000010010010000111111011\n\ |
|
2904 01010100010001000010110100011000\n\ |
|
2905 @end group\n\ |
|
2906 @end example\n\ |
|
2907 \n\ |
|
2908 (shown here in two 32 bit sections for typesetting purposes) when\n\ |
|
2909 printed in bit format on a workstation which stores 8 byte real values\n\ |
|
2910 in IEEE format with the least significant byte first. This format only\n\ |
|
2911 works for numeric types.\n\ |
4833
|
2912 @item bit\n\ |
|
2913 The same as @code{native-bit}, but always print the most significant\n\ |
|
2914 bits first.\n\ |
|
2915 @item compact\n\ |
|
2916 Remove extra blank space around column number labels.\n\ |
|
2917 @item loose\n\ |
|
2918 Insert blank lines above and below column number labels (this is the\n\ |
|
2919 default).\n\ |
3372
|
2920 @end table\n\ |
|
2921 \n\ |
|
2922 By default, Octave will try to print numbers with at least 5 significant\n\ |
|
2923 figures within a field that is a maximum of 10 characters wide.\n\ |
|
2924 \n\ |
|
2925 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
2926 decimal point and all the numbers fit within the maximum field width,\n\ |
|
2927 it switches to an @samp{e} format.\n\ |
|
2928 \n\ |
|
2929 If @code{format} is invoked without any options, the default format\n\ |
|
2930 state is restored.\n\ |
|
2931 @end deffn") |
529
|
2932 { |
2086
|
2933 octave_value_list retval; |
529
|
2934 |
1755
|
2935 int argc = args.length () + 1; |
|
2936 |
1968
|
2937 string_vector argv = args.make_argv ("format"); |
1755
|
2938 |
|
2939 if (error_state) |
|
2940 return retval; |
529
|
2941 |
|
2942 set_format_style (argc, argv); |
|
2943 |
|
2944 return retval; |
|
2945 } |
|
2946 |
2165
|
2947 static int |
3105
|
2948 fixed_point_format (void) |
|
2949 { |
|
2950 Vfixed_point_format = check_preference ("fixed_point_format"); |
|
2951 |
|
2952 return 0; |
|
2953 } |
|
2954 |
|
2955 static int |
2165
|
2956 output_max_field_width (void) |
|
2957 { |
|
2958 double val; |
|
2959 if (builtin_real_scalar_variable ("output_max_field_width", val) |
5389
|
2960 && ! xisnan (val)) |
2165
|
2961 { |
|
2962 int ival = NINT (val); |
2800
|
2963 if (ival > 0 && ival == val) |
2165
|
2964 { |
|
2965 Voutput_max_field_width = ival; |
|
2966 return 0; |
|
2967 } |
|
2968 } |
|
2969 gripe_invalid_value_specified ("output_max_field_width"); |
|
2970 return -1; |
|
2971 } |
|
2972 |
|
2973 static int |
|
2974 output_precision (void) |
|
2975 { |
|
2976 double val; |
|
2977 if (builtin_real_scalar_variable ("output_precision", val) |
5389
|
2978 && ! xisnan (val)) |
2165
|
2979 { |
|
2980 int ival = NINT (val); |
2800
|
2981 if (ival >= 0 && ival == val) |
2165
|
2982 { |
|
2983 Voutput_precision = ival; |
|
2984 return 0; |
|
2985 } |
|
2986 } |
|
2987 gripe_invalid_value_specified ("output_precision"); |
|
2988 return -1; |
|
2989 } |
|
2990 |
|
2991 static int |
|
2992 print_empty_dimensions (void) |
|
2993 { |
|
2994 Vprint_empty_dimensions = check_preference ("print_empty_dimensions"); |
|
2995 |
|
2996 return 0; |
|
2997 } |
|
2998 |
|
2999 static int |
|
3000 split_long_rows (void) |
|
3001 { |
|
3002 Vsplit_long_rows = check_preference ("split_long_rows"); |
|
3003 |
|
3004 return 0; |
|
3005 } |
|
3006 |
5759
|
3007 static int |
|
3008 struct_levels_to_print (void) |
|
3009 { |
|
3010 double val; |
|
3011 if (builtin_real_scalar_variable ("struct_levels_to_print", val) |
|
3012 && ! xisnan (val)) |
|
3013 { |
|
3014 int ival = NINT (val); |
|
3015 if (ival == val) |
|
3016 { |
|
3017 Vstruct_levels_to_print = ival; |
|
3018 return 0; |
|
3019 } |
|
3020 } |
|
3021 gripe_invalid_value_specified ("struct_levels_to_print"); |
|
3022 return -1; |
|
3023 } |
|
3024 |
2165
|
3025 void |
|
3026 symbols_of_pr_output (void) |
|
3027 { |
4233
|
3028 DEFVAR (fixed_point_format, false, fixed_point_format, |
3321
|
3029 "-*- texinfo -*-\n\ |
|
3030 @defvr {Built-in Variable} fixed_point_format\n\ |
|
3031 If the value of this variable is nonzero, Octave will scale all values\n\ |
|
3032 in a matrix so that the largest may be written with one leading digit.\n\ |
|
3033 The scaling factor is printed on the first line of output. For example,\n\ |
|
3034 \n\ |
|
3035 @example\n\ |
|
3036 @group\n\ |
|
3037 octave:1> logspace (1, 7, 5)'\n\ |
|
3038 ans =\n\ |
|
3039 \n\ |
|
3040 1.0e+07 *\n\ |
|
3041 \n\ |
|
3042 0.00000\n\ |
|
3043 0.00003\n\ |
|
3044 0.00100\n\ |
|
3045 0.03162\n\ |
|
3046 1.00000\n\ |
|
3047 @end group\n\ |
|
3048 @end example\n\ |
|
3049 \n\ |
|
3050 @noindent\n\ |
|
3051 Notice that first value appears to be zero when it is actually 1. For\n\ |
|
3052 this reason, you should be careful when setting\n\ |
|
3053 @code{fixed_point_format} to a nonzero value.\n\ |
|
3054 \n\ |
|
3055 The default value of @code{fixed_point_format} is 0.\n\ |
3333
|
3056 @end defvr"); |
3105
|
3057 |
3258
|
3058 DEFVAR (output_max_field_width, 10.0, output_max_field_width, |
3321
|
3059 "-*- texinfo -*-\n\ |
|
3060 @defvr {Built-in Variable} output_max_field_width\n\ |
|
3061 This variable specifies the maximum width of a numeric output field.\n\ |
|
3062 The default value is 10.\n\ |
3333
|
3063 @end defvr"); |
2165
|
3064 |
3258
|
3065 DEFVAR (output_precision, 5.0, output_precision, |
3321
|
3066 "-*- texinfo -*-\n\ |
|
3067 @defvr {Built-in Variable} output_precision\n\ |
|
3068 This variable specifies the minimum number of significant figures to\n\ |
|
3069 display for numeric output. The default value is 5.\n\ |
3333
|
3070 @end defvr"); |
2165
|
3071 |
4233
|
3072 DEFVAR (print_empty_dimensions, true, print_empty_dimensions, |
3321
|
3073 "-*- texinfo -*-\n\ |
|
3074 @defvr {Built-in Variable} print_empty_dimensions\n\ |
|
3075 If the value of @code{print_empty_dimensions} is nonzero, the\n\ |
|
3076 dimensions of empty matrices are printed along with the empty matrix\n\ |
|
3077 symbol, @samp{[]}. For example, the expression\n\ |
|
3078 \n\ |
|
3079 @example\n\ |
|
3080 zeros (3, 0)\n\ |
|
3081 @end example\n\ |
|
3082 \n\ |
|
3083 @noindent\n\ |
|
3084 will print\n\ |
|
3085 \n\ |
|
3086 @example\n\ |
|
3087 ans = [](3x0)\n\ |
|
3088 @end example\n\ |
3333
|
3089 @end defvr"); |
2165
|
3090 |
4233
|
3091 DEFVAR (split_long_rows, true, split_long_rows, |
3321
|
3092 "-*- texinfo -*-\n\ |
|
3093 @defvr {Built-in Variable} split_long_rows\n\ |
|
3094 For large matrices, Octave may not be able to display all the columns of\n\ |
|
3095 a given row on one line of your screen. This can result in missing\n\ |
|
3096 information or output that is nearly impossible to decipher, depending\n\ |
|
3097 on whether your terminal truncates or wraps long lines.\n\ |
|
3098 \n\ |
|
3099 If the value of @code{split_long_rows} is nonzero, Octave will display\n\ |
|
3100 the matrix in a series of smaller pieces, each of which can fit within\n\ |
|
3101 the limits of your terminal width. Each set of rows is labeled so that\n\ |
|
3102 you can easily see which columns are currently being displayed.\n\ |
|
3103 For example:\n\ |
|
3104 \n\ |
|
3105 @smallexample\n\ |
|
3106 @group\n\ |
|
3107 octave:13> rand (2,10)\n\ |
|
3108 ans =\n\ |
|
3109 \n\ |
|
3110 Columns 1 through 6:\n\ |
|
3111 \n\ |
|
3112 0.75883 0.93290 0.40064 0.43818 0.94958 0.16467\n\ |
|
3113 0.75697 0.51942 0.40031 0.61784 0.92309 0.40201\n\ |
|
3114 \n\ |
|
3115 Columns 7 through 10:\n\ |
|
3116 \n\ |
|
3117 0.90174 0.11854 0.72313 0.73326\n\ |
|
3118 0.44672 0.94303 0.56564 0.82150\n\ |
|
3119 @end group\n\ |
|
3120 @end smallexample\n\ |
|
3121 \n\ |
|
3122 @noindent\n\ |
|
3123 The default value of @code{split_long_rows} is nonzero.\n\ |
3333
|
3124 @end defvr"); |
5759
|
3125 |
|
3126 DEFVAR (struct_levels_to_print, 2.0, struct_levels_to_print, |
|
3127 "-*- texinfo -*-\n\ |
|
3128 @defvr {Built-in Variable} struct_levels_to_print\n\ |
|
3129 You can tell Octave how many structure levels to display by setting the\n\ |
|
3130 built-in variable @code{struct_levels_to_print}. The default value is 2.\n\ |
|
3131 @end defvr"); |
2165
|
3132 } |
|
3133 |
1
|
3134 /* |
|
3135 ;;; Local Variables: *** |
|
3136 ;;; mode: C++ *** |
|
3137 ;;; End: *** |
|
3138 */ |