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