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