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