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 |
453
|
36 #include "CMatrix.h" |
1
|
37 #include "Range.h" |
2926
|
38 #include "cmd-edit.h" |
1352
|
39 #include "dMatrix.h" |
2891
|
40 #include "lo-mappers.h" |
4051
|
41 #include "lo-sstream.h" |
2317
|
42 #include "mach-info.h" |
1651
|
43 #include "oct-cmplx.h" |
1755
|
44 #include "str-vec.h" |
1
|
45 |
3933
|
46 #include "Cell.h" |
1352
|
47 #include "defun.h" |
|
48 #include "error.h" |
2165
|
49 #include "gripes.h" |
1755
|
50 #include "oct-obj.h" |
3685
|
51 #include "oct-stream.h" |
1352
|
52 #include "pager.h" |
|
53 #include "pr-output.h" |
1282
|
54 #include "sysdep.h" |
1
|
55 #include "utils.h" |
1352
|
56 #include "variables.h" |
1
|
57 |
3105
|
58 // TRUE means use a scaled fixed point format for `format long' and |
|
59 // `format short'. |
|
60 static bool Vfixed_point_format; |
|
61 |
2165
|
62 // The maximum field width for a number printed by the default output |
|
63 // routines. |
|
64 static int Voutput_max_field_width; |
|
65 |
|
66 // The precision of the numbers printed by the default output |
|
67 // routines. |
|
68 static int Voutput_precision; |
|
69 |
|
70 // TRUE means that the dimensions of empty matrices should be printed |
|
71 // like this: x = [](2x0). |
|
72 static bool Vprint_empty_dimensions; |
|
73 |
|
74 // TRUE means that the rows of big matrices should be split into |
|
75 // smaller slices that fit on the screen. |
|
76 static bool Vsplit_long_rows; |
|
77 |
3018
|
78 // TRUE means don't do any fancy formatting. |
2387
|
79 static bool free_format = false; |
1
|
80 |
3018
|
81 // TRUE means print plus sign for nonzero, blank for zero. |
2387
|
82 static bool plus_format = false; |
1
|
83 |
3018
|
84 // TRUE means always print like dollars and cents. |
2387
|
85 static bool bank_format = false; |
1282
|
86 |
3018
|
87 // TRUE means print data in hexadecimal format. |
3608
|
88 static int hex_format = 0; |
1282
|
89 |
3018
|
90 // TRUE means print data in binary-bit-pattern format. |
1309
|
91 static int bit_format = 0; |
|
92 |
3018
|
93 // TRUE means don't put newlines around the column number headers. |
2387
|
94 static bool compact_format = false; |
1186
|
95 |
3018
|
96 // TRUE means use an e format. |
2387
|
97 static bool print_e = false; |
1
|
98 |
3018
|
99 // TRUE means print E instead of e for exponent field. |
2387
|
100 static bool print_big_e = false; |
1
|
101 |
3608
|
102 class pr_formatted_float; |
|
103 |
|
104 class |
|
105 float_format |
|
106 { |
|
107 public: |
|
108 |
3682
|
109 float_format (int w = -1, int p = -1, int f = 0) |
3608
|
110 : fw (w), prec (p), fmt (f), up (0), sp (0) { } |
|
111 |
|
112 float_format (const float_format& ff) |
|
113 : fw (ff.fw), prec (ff.prec), fmt (ff.fmt), up (ff.up), sp (ff.sp) { } |
|
114 |
|
115 float_format& operator = (const float_format& ff) |
|
116 { |
|
117 if (&ff != this) |
|
118 { |
|
119 fw = ff.fw; |
|
120 prec = ff.prec; |
|
121 fmt = ff.fmt; |
|
122 up = ff.up; |
|
123 sp = ff.sp; |
|
124 } |
|
125 |
|
126 return *this; |
|
127 } |
|
128 |
|
129 ~float_format (void) { } |
|
130 |
|
131 float_format& scientific (void) { fmt = std::ios::scientific; return *this; } |
|
132 float_format& fixed (void) { fmt = std::ios::fixed; return *this; } |
|
133 float_format& general (void) { fmt = 0; return *this; } |
|
134 |
|
135 float_format& uppercase (void) { up = std::ios::uppercase; return *this; } |
|
136 float_format& lowercase (void) { up = 0; return *this; } |
|
137 |
|
138 float_format& precision (int p) { prec = p; return *this; } |
|
139 |
|
140 float_format& width (int w) { fw = w; return *this; } |
|
141 |
|
142 float_format& trailing_zeros (bool tz = true) |
|
143 { sp = tz ? std::ios::showpoint : 0; return *this; } |
|
144 |
|
145 friend std::ostream& operator << (std::ostream& os, |
|
146 const pr_formatted_float& pff); |
|
147 |
|
148 private: |
|
149 |
|
150 // Field width. Zero means as wide as necessary. |
|
151 int fw; |
|
152 |
|
153 // Precision. |
|
154 int prec; |
|
155 |
|
156 // Format. |
|
157 int fmt; |
|
158 |
|
159 // E or e. |
|
160 int up; |
|
161 |
|
162 // Show trailing zeros. |
|
163 int sp; |
|
164 }; |
|
165 |
|
166 class |
|
167 pr_formatted_float |
|
168 { |
|
169 public: |
|
170 |
|
171 const float_format& f; |
|
172 |
|
173 double val; |
|
174 |
|
175 pr_formatted_float (const float_format& f_arg, double val_arg) |
|
176 : f (f_arg), val (val_arg) { } |
|
177 }; |
|
178 |
|
179 std::ostream& |
|
180 operator << (std::ostream& os, const pr_formatted_float& pff) |
|
181 { |
3682
|
182 if (pff.f.fw >= 0) |
3608
|
183 os << std::setw (pff.f.fw); |
|
184 |
3682
|
185 if (pff.f.prec >= 0) |
3608
|
186 os << std::setprecision (pff.f.prec); |
|
187 |
3775
|
188 std::ios::fmtflags oflags = |
|
189 os.flags (static_cast<std::ios::fmtflags> |
|
190 (pff.f.fmt | pff.f.up | pff.f.sp)); |
3608
|
191 |
|
192 os << pff.val; |
|
193 |
|
194 os.flags (oflags); |
|
195 |
|
196 return os; |
|
197 } |
|
198 |
|
199 // Current format for real numbers and the real part of complex |
|
200 // numbers. |
|
201 static float_format *curr_real_fmt = 0; |
|
202 |
|
203 // Current format for the imaginary part of complex numbers. |
|
204 static float_format *curr_imag_fmt = 0; |
1309
|
205 |
1
|
206 static double |
164
|
207 pr_max_internal (const Matrix& m) |
1
|
208 { |
|
209 int nr = m.rows (); |
|
210 int nc = m.columns (); |
|
211 |
3130
|
212 double result = -DBL_MAX; |
1
|
213 |
|
214 for (int j = 0; j < nc; j++) |
|
215 for (int i = 0; i < nr; i++) |
|
216 { |
3608
|
217 double val = m(i,j); |
4025
|
218 if (xisinf (val) || octave_is_NaN_or_NA (val)) |
1
|
219 continue; |
|
220 |
|
221 if (val > result) |
|
222 result = val; |
|
223 } |
3608
|
224 |
1
|
225 return result; |
|
226 } |
|
227 |
|
228 static double |
164
|
229 pr_min_internal (const Matrix& m) |
1
|
230 { |
|
231 int nr = m.rows (); |
|
232 int nc = m.columns (); |
|
233 |
|
234 double result = DBL_MAX; |
|
235 |
|
236 for (int j = 0; j < nc; j++) |
|
237 for (int i = 0; i < nr; i++) |
|
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 |
1658
|
250 // XXX FIXME XXX -- it would be nice to share more code among these |
|
251 // functions,.. |
|
252 |
1
|
253 static void |
3611
|
254 set_real_format (bool sign, int digits, bool inf_or_nan, bool int_only, |
1658
|
255 int &fw) |
1
|
256 { |
3608
|
257 static float_format fmt; |
1
|
258 |
2165
|
259 int prec = Voutput_precision; |
1
|
260 |
|
261 int ld, rd; |
|
262 |
|
263 if (bank_format) |
|
264 { |
|
265 fw = digits < 0 ? 4 : digits + 3; |
|
266 if (inf_or_nan && fw < 3) |
|
267 fw = 3; |
|
268 fw += sign; |
|
269 rd = 2; |
|
270 } |
1282
|
271 else if (hex_format) |
|
272 { |
|
273 fw = 2 * sizeof (double); |
|
274 rd = 0; |
|
275 } |
1309
|
276 else if (bit_format) |
|
277 { |
|
278 fw = 8 * sizeof (double); |
|
279 rd = 0; |
|
280 } |
3611
|
281 else if (inf_or_nan || int_only) |
1
|
282 { |
|
283 fw = digits; |
|
284 if (inf_or_nan && fw < 3) |
|
285 fw = 3; |
|
286 fw += sign; |
3682
|
287 rd = fw; |
1
|
288 } |
|
289 else |
|
290 { |
|
291 if (digits > 0) |
|
292 { |
|
293 ld = digits; |
1658
|
294 rd = prec > digits ? prec - digits : prec; |
1
|
295 digits++; |
|
296 } |
|
297 else |
|
298 { |
|
299 ld = 1; |
1658
|
300 rd = prec > digits ? prec - digits : prec; |
1
|
301 digits = -digits + 1; |
|
302 } |
|
303 |
|
304 fw = ld + 1 + rd; |
|
305 if (inf_or_nan && fw < 3) |
|
306 fw = 3; |
|
307 fw += sign; |
|
308 } |
|
309 |
1309
|
310 if (! (bank_format || hex_format || bit_format) |
2165
|
311 && (fw > Voutput_max_field_width || print_e)) |
1
|
312 { |
|
313 int exp_field = 4; |
|
314 if (digits > 100) |
|
315 exp_field++; |
|
316 |
|
317 fw = 2 + prec + exp_field; |
|
318 if (inf_or_nan && fw < 3) |
|
319 fw = 3; |
|
320 fw += sign; |
|
321 |
3608
|
322 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
323 |
1
|
324 if (print_big_e) |
3608
|
325 fmt.uppercase (); |
1
|
326 } |
3611
|
327 else if (inf_or_nan || int_only) |
|
328 fmt = float_format (fw, rd); |
1
|
329 else |
3608
|
330 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
331 |
3608
|
332 curr_real_fmt = &fmt; |
1
|
333 } |
|
334 |
1658
|
335 static void |
|
336 set_format (double d, int& fw) |
|
337 { |
|
338 curr_real_fmt = 0; |
|
339 curr_imag_fmt = 0; |
|
340 |
|
341 if (free_format) |
|
342 return; |
|
343 |
2387
|
344 bool sign = (d < 0.0); |
1658
|
345 |
2387
|
346 bool inf_or_nan = (xisinf (d) || xisnan (d)); |
1658
|
347 |
3611
|
348 bool int_only = (! inf_or_nan && D_NINT (d) == d); |
1658
|
349 |
|
350 double d_abs = d < 0.0 ? -d : d; |
|
351 |
2800
|
352 int digits = (inf_or_nan || d_abs == 0.0) |
|
353 ? 0 : static_cast<int> (floor (log10 (d_abs) + 1.0)); |
1658
|
354 |
3611
|
355 set_real_format (sign, digits, inf_or_nan, int_only, fw); |
1658
|
356 } |
|
357 |
1
|
358 static inline void |
|
359 set_format (double d) |
|
360 { |
|
361 int fw; |
|
362 set_format (d, fw); |
|
363 } |
|
364 |
|
365 static void |
2387
|
366 set_real_matrix_format (bool sign, int x_max, int x_min, |
|
367 bool inf_or_nan, int int_or_inf_or_nan, int& fw) |
1
|
368 { |
3608
|
369 static float_format fmt; |
1
|
370 |
2165
|
371 int prec = Voutput_precision; |
1
|
372 |
|
373 int ld, rd; |
|
374 |
|
375 if (bank_format) |
|
376 { |
|
377 int digits = x_max > x_min ? x_max : x_min; |
|
378 fw = digits <= 0 ? 4 : digits + 3; |
|
379 if (inf_or_nan && fw < 3) |
|
380 fw = 3; |
|
381 fw += sign; |
|
382 rd = 2; |
|
383 } |
1282
|
384 else if (hex_format) |
|
385 { |
|
386 fw = 2 * sizeof (double); |
|
387 rd = 0; |
|
388 } |
1309
|
389 else if (bit_format) |
|
390 { |
|
391 fw = 8 * sizeof (double); |
|
392 rd = 0; |
|
393 } |
3268
|
394 else if (Vfixed_point_format) |
|
395 { |
|
396 rd = prec; |
|
397 fw = rd + 2; |
|
398 if (inf_or_nan && fw < 3) |
|
399 fw = 3; |
|
400 fw += sign; |
|
401 } |
1715
|
402 else if (int_or_inf_or_nan) |
1
|
403 { |
|
404 int digits = x_max > x_min ? x_max : x_min; |
|
405 fw = digits <= 0 ? 1 : digits; |
|
406 if (inf_or_nan && fw < 3) |
|
407 fw = 3; |
|
408 fw += sign; |
3682
|
409 rd = fw; |
1
|
410 } |
|
411 else |
|
412 { |
|
413 int ld_max, rd_max; |
|
414 if (x_max > 0) |
|
415 { |
|
416 ld_max = x_max; |
1658
|
417 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
418 x_max++; |
|
419 } |
|
420 else |
|
421 { |
|
422 ld_max = 1; |
1658
|
423 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
424 x_max = -x_max + 1; |
|
425 } |
|
426 |
|
427 int ld_min, rd_min; |
|
428 if (x_min > 0) |
|
429 { |
|
430 ld_min = x_min; |
1658
|
431 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
432 x_min++; |
|
433 } |
|
434 else |
|
435 { |
|
436 ld_min = 1; |
1658
|
437 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
438 x_min = -x_min + 1; |
|
439 } |
|
440 |
|
441 ld = ld_max > ld_min ? ld_max : ld_min; |
|
442 rd = rd_max > rd_min ? rd_max : rd_min; |
|
443 |
|
444 fw = ld + 1 + rd; |
|
445 if (inf_or_nan && fw < 3) |
|
446 fw = 3; |
|
447 fw += sign; |
|
448 } |
|
449 |
1658
|
450 if (! (bank_format || hex_format || bit_format) |
3105
|
451 && (print_e |
|
452 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
453 { |
|
454 int exp_field = 4; |
|
455 if (x_max > 100 || x_min > 100) |
|
456 exp_field++; |
|
457 |
|
458 fw = 2 + prec + exp_field; |
|
459 if (inf_or_nan && fw < 3) |
|
460 fw = 3; |
|
461 fw += sign; |
|
462 |
3608
|
463 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
464 |
1
|
465 if (print_big_e) |
3608
|
466 fmt.uppercase (); |
1
|
467 } |
3611
|
468 else if (int_or_inf_or_nan) |
|
469 fmt = float_format (fw, rd); |
1
|
470 else |
3608
|
471 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
472 |
3608
|
473 curr_real_fmt = &fmt; |
1
|
474 } |
|
475 |
1658
|
476 static void |
3105
|
477 set_format (const Matrix& m, int& fw, double& scale) |
1658
|
478 { |
|
479 curr_real_fmt = 0; |
|
480 curr_imag_fmt = 0; |
|
481 |
|
482 if (free_format) |
|
483 return; |
|
484 |
2387
|
485 bool sign = m.any_element_is_negative (); |
1658
|
486 |
2387
|
487 bool inf_or_nan = m.any_element_is_inf_or_nan (); |
1658
|
488 |
2387
|
489 bool int_or_inf_or_nan = m.all_elements_are_int_or_inf_or_nan (); |
1658
|
490 |
2387
|
491 Matrix m_abs = m.abs (); |
1658
|
492 double max_abs = pr_max_internal (m_abs); |
|
493 double min_abs = pr_min_internal (m_abs); |
|
494 |
2800
|
495 int x_max = max_abs == 0.0 |
|
496 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
497 |
|
498 int x_min = min_abs == 0.0 |
|
499 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
500 |
3776
|
501 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
502 |
1658
|
503 set_real_matrix_format (sign, x_max, x_min, inf_or_nan, |
1715
|
504 int_or_inf_or_nan, fw); |
1658
|
505 } |
|
506 |
1
|
507 static inline void |
164
|
508 set_format (const Matrix& m) |
1
|
509 { |
|
510 int fw; |
3105
|
511 double scale; |
|
512 set_format (m, fw, scale); |
1
|
513 } |
|
514 |
|
515 static void |
2387
|
516 set_complex_format (bool sign, int x_max, int x_min, int r_x, |
|
517 bool inf_or_nan, int int_only, int& r_fw, int& i_fw) |
1
|
518 { |
3608
|
519 static float_format r_fmt; |
|
520 static float_format i_fmt; |
1
|
521 |
2165
|
522 int prec = Voutput_precision; |
1
|
523 |
|
524 int ld, rd; |
|
525 |
|
526 if (bank_format) |
|
527 { |
|
528 int digits = r_x; |
|
529 i_fw = 0; |
|
530 r_fw = digits <= 0 ? 4 : digits + 3; |
|
531 if (inf_or_nan && r_fw < 3) |
|
532 r_fw = 3; |
|
533 r_fw += sign; |
|
534 rd = 2; |
|
535 } |
1282
|
536 else if (hex_format) |
|
537 { |
|
538 r_fw = 2 * sizeof (double); |
|
539 i_fw = 2 * sizeof (double); |
|
540 rd = 0; |
|
541 } |
1309
|
542 else if (bit_format) |
|
543 { |
|
544 r_fw = 8 * sizeof (double); |
|
545 i_fw = 8 * sizeof (double); |
|
546 rd = 0; |
|
547 } |
1658
|
548 else if (inf_or_nan || int_only) |
1
|
549 { |
|
550 int digits = x_max > x_min ? x_max : x_min; |
|
551 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
552 if (inf_or_nan && i_fw < 3) |
|
553 i_fw = r_fw = 3; |
|
554 r_fw += sign; |
3682
|
555 rd = r_fw; |
1
|
556 } |
|
557 else |
|
558 { |
|
559 int ld_max, rd_max; |
|
560 if (x_max > 0) |
|
561 { |
|
562 ld_max = x_max; |
1658
|
563 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
564 x_max++; |
|
565 } |
|
566 else |
|
567 { |
|
568 ld_max = 1; |
1658
|
569 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
570 x_max = -x_max + 1; |
|
571 } |
|
572 |
|
573 int ld_min, rd_min; |
|
574 if (x_min > 0) |
|
575 { |
|
576 ld_min = x_min; |
1658
|
577 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
578 x_min++; |
|
579 } |
|
580 else |
|
581 { |
|
582 ld_min = 1; |
1658
|
583 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
584 x_min = -x_min + 1; |
|
585 } |
|
586 |
|
587 ld = ld_max > ld_min ? ld_max : ld_min; |
|
588 rd = rd_max > rd_min ? rd_max : rd_min; |
|
589 |
|
590 i_fw = r_fw = ld + 1 + rd; |
|
591 if (inf_or_nan && i_fw < 3) |
|
592 i_fw = r_fw = 3; |
|
593 r_fw += sign; |
|
594 } |
|
595 |
1309
|
596 if (! (bank_format || hex_format || bit_format) |
2165
|
597 && (r_fw > Voutput_max_field_width || print_e)) |
1
|
598 { |
|
599 int exp_field = 4; |
|
600 if (x_max > 100 || x_min > 100) |
|
601 exp_field++; |
|
602 |
|
603 i_fw = r_fw = 1 + prec + exp_field; |
|
604 if (inf_or_nan && i_fw < 3) |
|
605 i_fw = r_fw = 3; |
|
606 r_fw += sign; |
|
607 |
3608
|
608 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
609 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
610 |
1
|
611 if (print_big_e) |
|
612 { |
3608
|
613 r_fmt.uppercase (); |
|
614 i_fmt.uppercase (); |
1
|
615 } |
|
616 } |
3611
|
617 else if (inf_or_nan || int_only) |
|
618 { |
|
619 r_fmt = float_format (r_fw, rd); |
|
620 i_fmt = float_format (i_fw, rd); |
|
621 } |
1
|
622 else |
|
623 { |
3608
|
624 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
625 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
626 } |
|
627 |
3608
|
628 curr_real_fmt = &r_fmt; |
|
629 curr_imag_fmt = &i_fmt; |
1
|
630 } |
|
631 |
1658
|
632 static void |
|
633 set_format (const Complex& c, int& r_fw, int& i_fw) |
|
634 { |
|
635 curr_real_fmt = 0; |
|
636 curr_imag_fmt = 0; |
|
637 |
|
638 if (free_format) |
|
639 return; |
|
640 |
|
641 double rp = c.real (); |
|
642 double ip = c.imag (); |
|
643 |
2387
|
644 bool sign = (rp < 0.0); |
1658
|
645 |
2387
|
646 bool inf_or_nan = (xisinf (c) || xisnan (c)); |
1658
|
647 |
2387
|
648 bool int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip); |
1658
|
649 |
|
650 double r_abs = rp < 0.0 ? -rp : rp; |
|
651 double i_abs = ip < 0.0 ? -ip : ip; |
|
652 |
2800
|
653 int r_x = r_abs == 0.0 |
|
654 ? 0 : static_cast<int> (floor (log10 (r_abs) + 1.0)); |
|
655 |
|
656 int i_x = i_abs == 0.0 |
|
657 ? 0 : static_cast<int> (floor (log10 (i_abs) + 1.0)); |
1658
|
658 |
|
659 int x_max, x_min; |
|
660 |
|
661 if (r_x > i_x) |
|
662 { |
|
663 x_max = r_x; |
|
664 x_min = i_x; |
|
665 } |
|
666 else |
|
667 { |
|
668 x_max = i_x; |
|
669 x_min = r_x; |
|
670 } |
|
671 |
|
672 set_complex_format (sign, x_max, x_min, r_x, inf_or_nan, int_only, |
|
673 r_fw, i_fw); |
|
674 } |
|
675 |
1
|
676 static inline void |
164
|
677 set_format (const Complex& c) |
1
|
678 { |
|
679 int r_fw, i_fw; |
|
680 set_format (c, r_fw, i_fw); |
|
681 } |
|
682 |
|
683 static void |
2387
|
684 set_complex_matrix_format (bool sign, int x_max, int x_min, |
|
685 int r_x_max, int r_x_min, bool inf_or_nan, |
1715
|
686 int int_or_inf_or_nan, int& r_fw, int& i_fw) |
1
|
687 { |
3608
|
688 static float_format r_fmt; |
|
689 static float_format i_fmt; |
1
|
690 |
2165
|
691 int prec = Voutput_precision; |
1
|
692 |
|
693 int ld, rd; |
|
694 |
|
695 if (bank_format) |
|
696 { |
|
697 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
698 i_fw = 0; |
|
699 r_fw = digits <= 0 ? 4 : digits + 3; |
|
700 if (inf_or_nan && i_fw < 3) |
|
701 i_fw = r_fw = 3; |
|
702 r_fw += sign; |
|
703 rd = 2; |
|
704 } |
1282
|
705 else if (hex_format) |
|
706 { |
|
707 r_fw = 2 * sizeof (double); |
|
708 i_fw = 2 * sizeof (double); |
|
709 rd = 0; |
|
710 } |
1309
|
711 else if (bit_format) |
|
712 { |
|
713 r_fw = 8 * sizeof (double); |
|
714 i_fw = 8 * sizeof (double); |
|
715 rd = 0; |
|
716 } |
3268
|
717 else if (Vfixed_point_format) |
|
718 { |
|
719 rd = prec; |
|
720 i_fw = r_fw = rd + 2; |
|
721 if (inf_or_nan && i_fw < 3) |
|
722 i_fw = r_fw = 3; |
|
723 r_fw += sign; |
|
724 } |
1715
|
725 else if (int_or_inf_or_nan) |
1
|
726 { |
|
727 int digits = x_max > x_min ? x_max : x_min; |
|
728 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
729 if (inf_or_nan && i_fw < 3) |
|
730 i_fw = r_fw = 3; |
|
731 r_fw += sign; |
3682
|
732 rd = r_fw; |
1
|
733 } |
|
734 else |
|
735 { |
|
736 int ld_max, rd_max; |
|
737 if (x_max > 0) |
|
738 { |
|
739 ld_max = x_max; |
1658
|
740 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
741 x_max++; |
|
742 } |
|
743 else |
|
744 { |
|
745 ld_max = 1; |
1658
|
746 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
747 x_max = -x_max + 1; |
|
748 } |
|
749 |
|
750 int ld_min, rd_min; |
|
751 if (x_min > 0) |
|
752 { |
|
753 ld_min = x_min; |
1658
|
754 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
755 x_min++; |
|
756 } |
|
757 else |
|
758 { |
|
759 ld_min = 1; |
1658
|
760 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
761 x_min = -x_min + 1; |
|
762 } |
|
763 |
|
764 ld = ld_max > ld_min ? ld_max : ld_min; |
|
765 rd = rd_max > rd_min ? rd_max : rd_min; |
|
766 |
|
767 i_fw = r_fw = ld + 1 + rd; |
|
768 if (inf_or_nan && i_fw < 3) |
|
769 i_fw = r_fw = 3; |
|
770 r_fw += sign; |
|
771 } |
|
772 |
1309
|
773 if (! (bank_format || hex_format || bit_format) |
3105
|
774 && (print_e |
|
775 || (! Vfixed_point_format && r_fw > Voutput_max_field_width))) |
1
|
776 { |
|
777 int exp_field = 4; |
|
778 if (x_max > 100 || x_min > 100) |
|
779 exp_field++; |
|
780 |
|
781 i_fw = r_fw = 1 + prec + exp_field; |
|
782 if (inf_or_nan && i_fw < 3) |
|
783 i_fw = r_fw = 3; |
|
784 r_fw += sign; |
|
785 |
3608
|
786 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
787 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
788 |
1
|
789 if (print_big_e) |
|
790 { |
3608
|
791 r_fmt.uppercase (); |
|
792 i_fmt.uppercase (); |
1
|
793 } |
|
794 } |
3611
|
795 else if (int_or_inf_or_nan) |
|
796 { |
|
797 r_fmt = float_format (r_fw, rd); |
|
798 i_fmt = float_format (i_fw, rd); |
|
799 } |
1
|
800 else |
|
801 { |
3608
|
802 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
803 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
804 } |
|
805 |
3608
|
806 curr_real_fmt = &r_fmt; |
|
807 curr_imag_fmt = &i_fmt; |
1
|
808 } |
|
809 |
1658
|
810 static void |
3105
|
811 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw, double& scale) |
1658
|
812 { |
|
813 curr_real_fmt = 0; |
|
814 curr_imag_fmt = 0; |
|
815 |
|
816 if (free_format) |
|
817 return; |
|
818 |
|
819 Matrix rp = real (cm); |
|
820 Matrix ip = imag (cm); |
|
821 |
2387
|
822 bool sign = rp.any_element_is_negative (); |
1658
|
823 |
2387
|
824 bool inf_or_nan = cm.any_element_is_inf_or_nan (); |
1658
|
825 |
2387
|
826 bool int_or_inf_or_nan = (rp.all_elements_are_int_or_inf_or_nan () |
|
827 && ip.all_elements_are_int_or_inf_or_nan ()); |
1658
|
828 |
2387
|
829 Matrix r_m_abs = rp.abs (); |
1658
|
830 double r_max_abs = pr_max_internal (r_m_abs); |
|
831 double r_min_abs = pr_min_internal (r_m_abs); |
|
832 |
2387
|
833 Matrix i_m_abs = ip.abs (); |
1658
|
834 double i_max_abs = pr_max_internal (i_m_abs); |
|
835 double i_min_abs = pr_min_internal (i_m_abs); |
|
836 |
2800
|
837 int r_x_max = r_max_abs == 0.0 |
|
838 ? 0 : static_cast<int> (floor (log10 (r_max_abs) + 1.0)); |
|
839 |
|
840 int r_x_min = r_min_abs == 0.0 |
|
841 ? 0 : static_cast<int> (floor (log10 (r_min_abs) + 1.0)); |
1658
|
842 |
2800
|
843 int i_x_max = i_max_abs == 0.0 |
|
844 ? 0 : static_cast<int> (floor (log10 (i_max_abs) + 1.0)); |
|
845 |
|
846 int i_x_min = i_min_abs == 0.0 |
|
847 ? 0 : static_cast<int> (floor (log10 (i_min_abs) + 1.0)); |
1658
|
848 |
|
849 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
850 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
851 |
3776
|
852 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
853 |
1658
|
854 set_complex_matrix_format (sign, x_max, x_min, r_x_max, r_x_min, |
1715
|
855 inf_or_nan, int_or_inf_or_nan, r_fw, i_fw); |
1658
|
856 } |
|
857 |
1
|
858 static inline void |
164
|
859 set_format (const ComplexMatrix& cm) |
1
|
860 { |
|
861 int r_fw, i_fw; |
3105
|
862 double scale; |
|
863 set_format (cm, r_fw, i_fw, scale); |
1
|
864 } |
|
865 |
|
866 static void |
3608
|
867 set_range_format (bool sign, int x_max, int x_min, int all_ints, int& fw) |
1
|
868 { |
3608
|
869 static float_format fmt; |
1
|
870 |
2165
|
871 int prec = Voutput_precision; |
1
|
872 |
|
873 int ld, rd; |
|
874 |
|
875 if (bank_format) |
|
876 { |
|
877 int digits = x_max > x_min ? x_max : x_min; |
|
878 fw = sign + digits < 0 ? 4 : digits + 3; |
|
879 rd = 2; |
|
880 } |
1282
|
881 else if (hex_format) |
|
882 { |
|
883 fw = 2 * sizeof (double); |
|
884 rd = 0; |
|
885 } |
1309
|
886 else if (bit_format) |
|
887 { |
|
888 fw = 8 * sizeof (double); |
|
889 rd = 0; |
|
890 } |
1658
|
891 else if (all_ints) |
1
|
892 { |
|
893 int digits = x_max > x_min ? x_max : x_min; |
|
894 fw = sign + digits; |
3682
|
895 rd = fw; |
1
|
896 } |
3105
|
897 else if (Vfixed_point_format) |
|
898 { |
|
899 rd = prec; |
|
900 fw = rd + 2 + sign; |
|
901 } |
1
|
902 else |
|
903 { |
|
904 int ld_max, rd_max; |
|
905 if (x_max > 0) |
|
906 { |
|
907 ld_max = x_max; |
1658
|
908 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
909 x_max++; |
|
910 } |
|
911 else |
|
912 { |
|
913 ld_max = 1; |
1658
|
914 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
915 x_max = -x_max + 1; |
|
916 } |
|
917 |
|
918 int ld_min, rd_min; |
|
919 if (x_min > 0) |
|
920 { |
|
921 ld_min = x_min; |
1658
|
922 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
923 x_min++; |
|
924 } |
|
925 else |
|
926 { |
|
927 ld_min = 1; |
1658
|
928 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
929 x_min = -x_min + 1; |
|
930 } |
|
931 |
|
932 ld = ld_max > ld_min ? ld_max : ld_min; |
|
933 rd = rd_max > rd_min ? rd_max : rd_min; |
|
934 |
|
935 fw = sign + ld + 1 + rd; |
|
936 } |
|
937 |
1309
|
938 if (! (bank_format || hex_format || bit_format) |
3105
|
939 && (print_e |
|
940 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
941 { |
|
942 int exp_field = 4; |
|
943 if (x_max > 100 || x_min > 100) |
|
944 exp_field++; |
|
945 |
|
946 fw = sign + 2 + prec + exp_field; |
|
947 |
3608
|
948 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
949 |
1
|
950 if (print_big_e) |
3608
|
951 fmt.uppercase (); |
1
|
952 } |
3611
|
953 else if (all_ints) |
|
954 fmt = float_format (fw, rd); |
1
|
955 else |
3608
|
956 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
957 |
3608
|
958 curr_real_fmt = &fmt; |
1
|
959 } |
|
960 |
1658
|
961 static void |
3105
|
962 set_format (const Range& r, int& fw, double& scale) |
1658
|
963 { |
|
964 curr_real_fmt = 0; |
|
965 curr_imag_fmt = 0; |
|
966 |
|
967 if (free_format) |
|
968 return; |
|
969 |
|
970 double r_min = r.base (); |
|
971 double r_max = r.limit (); |
|
972 |
|
973 if (r_max < r_min) |
|
974 { |
|
975 double tmp = r_max; |
|
976 r_max = r_min; |
|
977 r_min = tmp; |
|
978 } |
|
979 |
2387
|
980 bool sign = (r_min < 0.0); |
1658
|
981 |
2387
|
982 bool all_ints = r.all_elements_are_ints (); |
1658
|
983 |
|
984 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
985 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
986 |
2800
|
987 int x_max = max_abs == 0.0 |
|
988 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
989 |
|
990 int x_min = min_abs == 0.0 |
|
991 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
992 |
3776
|
993 scale = (x_max == 0 || all_ints) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
994 |
1658
|
995 set_range_format (sign, x_max, x_min, all_ints, fw); |
|
996 } |
|
997 |
1
|
998 static inline void |
164
|
999 set_format (const Range& r) |
1
|
1000 { |
|
1001 int fw; |
3105
|
1002 double scale; |
|
1003 set_format (r, fw, scale); |
1
|
1004 } |
|
1005 |
1282
|
1006 union equiv |
|
1007 { |
|
1008 double d; |
|
1009 unsigned char i[sizeof (double)]; |
|
1010 }; |
|
1011 |
1309
|
1012 #define PRINT_CHAR_BITS(os, c) \ |
|
1013 do \ |
|
1014 { \ |
|
1015 unsigned char ctmp = c; \ |
|
1016 char stmp[9]; \ |
1488
|
1017 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \ |
|
1018 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1019 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1020 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1021 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1022 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1023 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1024 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \ |
1309
|
1025 stmp[8] = '\0'; \ |
3013
|
1026 os << stmp; \ |
1309
|
1027 } \ |
|
1028 while (0) |
|
1029 |
|
1030 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
1031 do \ |
|
1032 { \ |
|
1033 unsigned char ctmp = c; \ |
|
1034 char stmp[9]; \ |
1488
|
1035 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \ |
|
1036 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1037 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1038 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1039 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1040 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1041 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1042 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \ |
1309
|
1043 stmp[8] = '\0'; \ |
3013
|
1044 os << stmp; \ |
1309
|
1045 } \ |
|
1046 while (0) |
|
1047 |
2522
|
1048 static void |
3608
|
1049 pr_any_float (const float_format *fmt, std::ostream& os, double d, int fw = 0) |
1
|
1050 { |
529
|
1051 if (fmt) |
1
|
1052 { |
1282
|
1053 if (hex_format) |
|
1054 { |
|
1055 equiv tmp; |
|
1056 tmp.d = d; |
|
1057 |
|
1058 // Unless explicitly asked for, always print in big-endian |
|
1059 // format. |
|
1060 |
|
1061 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
1062 // formats and not for Cray? |
|
1063 |
3013
|
1064 // XXX FIXME XXX -- will bad things happen if we are |
|
1065 // interrupted before resetting the format flags and fill |
|
1066 // character? |
|
1067 |
2317
|
1068 oct_mach_info::float_format flt_fmt = |
|
1069 oct_mach_info::native_float_format (); |
|
1070 |
3013
|
1071 char ofill = os.fill ('0'); |
|
1072 |
3608
|
1073 std::ios::fmtflags oflags |
|
1074 = os.flags (std::ios::right | std::ios::hex); |
3013
|
1075 |
1282
|
1076 if (hex_format > 1 |
2317
|
1077 || flt_fmt == oct_mach_info::ieee_big_endian |
|
1078 || flt_fmt == oct_mach_info::cray |
|
1079 || flt_fmt == oct_mach_info::unknown) |
1282
|
1080 { |
1322
|
1081 for (size_t i = 0; i < sizeof (double); i++) |
3548
|
1082 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1083 } |
|
1084 else |
|
1085 { |
1328
|
1086 for (int i = sizeof (double) - 1; i >= 0; i--) |
3548
|
1087 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1088 } |
3013
|
1089 |
|
1090 os.fill (ofill); |
|
1091 os.setf (oflags); |
1282
|
1092 } |
1309
|
1093 else if (bit_format) |
|
1094 { |
|
1095 equiv tmp; |
|
1096 tmp.d = d; |
|
1097 |
|
1098 // Unless explicitly asked for, always print in big-endian |
|
1099 // format. |
|
1100 |
|
1101 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
1102 // formats and not for Cray? |
|
1103 |
2317
|
1104 oct_mach_info::float_format flt_fmt = |
|
1105 oct_mach_info::native_float_format (); |
|
1106 |
|
1107 if (flt_fmt == oct_mach_info::ieee_big_endian |
|
1108 || flt_fmt == oct_mach_info::cray |
|
1109 || flt_fmt == oct_mach_info::unknown) |
1309
|
1110 { |
1322
|
1111 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1112 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1113 } |
|
1114 else |
|
1115 { |
|
1116 if (bit_format > 1) |
|
1117 { |
1328
|
1118 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1119 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
1120 } |
|
1121 else |
|
1122 { |
|
1123 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
1124 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1125 } |
|
1126 } |
|
1127 } |
1282
|
1128 else if (xisinf (d)) |
1
|
1129 { |
2804
|
1130 const char *s; |
1
|
1131 if (d < 0.0) |
|
1132 s = "-Inf"; |
|
1133 else |
|
1134 s = "Inf"; |
|
1135 |
|
1136 if (fw > 0) |
3548
|
1137 os << std::setw (fw) << s; |
1
|
1138 else |
|
1139 os << s; |
|
1140 } |
4025
|
1141 else if (octave_is_NA (d)) |
|
1142 { |
|
1143 if (fw > 0) |
|
1144 os << std::setw (fw) << "NA"; |
|
1145 else |
|
1146 os << "NA"; |
|
1147 } |
1
|
1148 else if (xisnan (d)) |
|
1149 { |
|
1150 if (fw > 0) |
3548
|
1151 os << std::setw (fw) << "NaN"; |
1
|
1152 else |
|
1153 os << "NaN"; |
|
1154 } |
|
1155 else |
3608
|
1156 os << pr_formatted_float (*fmt, d); |
1
|
1157 } |
529
|
1158 else |
|
1159 os << d; |
1
|
1160 } |
|
1161 |
|
1162 static inline void |
3608
|
1163 pr_float (std::ostream& os, double d, int fw = 0, double scale = 1.0) |
1
|
1164 { |
3608
|
1165 if (Vfixed_point_format && scale != 1.0) |
|
1166 d /= scale; |
|
1167 |
1
|
1168 pr_any_float (curr_real_fmt, os, d, fw); |
|
1169 } |
|
1170 |
|
1171 static inline void |
3523
|
1172 pr_imag_float (std::ostream& os, double d, int fw = 0) |
1
|
1173 { |
|
1174 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1175 } |
|
1176 |
2522
|
1177 static void |
3608
|
1178 pr_complex (std::ostream& os, const Complex& c, int r_fw = 0, |
|
1179 int i_fw = 0, double scale = 1.0) |
1
|
1180 { |
3608
|
1181 Complex tmp = (Vfixed_point_format && scale != 1.0) ? c / scale : c; |
|
1182 |
|
1183 double r = tmp.real (); |
|
1184 |
1
|
1185 pr_float (os, r, r_fw); |
3608
|
1186 |
1
|
1187 if (! bank_format) |
|
1188 { |
3608
|
1189 double i = tmp.imag (); |
1309
|
1190 if (! (hex_format || bit_format) && i < 0) |
1
|
1191 { |
|
1192 os << " - "; |
|
1193 i = -i; |
|
1194 pr_imag_float (os, i, i_fw); |
|
1195 } |
|
1196 else |
|
1197 { |
1309
|
1198 if (hex_format || bit_format) |
1282
|
1199 os << " "; |
|
1200 else |
|
1201 os << " + "; |
|
1202 |
1
|
1203 pr_imag_float (os, i, i_fw); |
|
1204 } |
|
1205 os << "i"; |
|
1206 } |
|
1207 } |
|
1208 |
626
|
1209 static void |
3523
|
1210 print_empty_matrix (std::ostream& os, int nr, int nc, bool pr_as_read_syntax) |
626
|
1211 { |
|
1212 assert (nr == 0 || nc == 0); |
|
1213 |
|
1214 if (pr_as_read_syntax) |
|
1215 { |
|
1216 if (nr == 0 && nc == 0) |
|
1217 os << "[]"; |
|
1218 else |
|
1219 os << "zeros (" << nr << ", " << nc << ")"; |
|
1220 } |
|
1221 else |
|
1222 { |
|
1223 os << "[]"; |
2165
|
1224 if (Vprint_empty_dimensions) |
626
|
1225 os << "(" << nr << "x" << nc << ")"; |
|
1226 } |
|
1227 } |
|
1228 |
1186
|
1229 static void |
3523
|
1230 pr_scale_header (std::ostream& os, double scale) |
3105
|
1231 { |
|
1232 if (Vfixed_point_format && scale != 1.0) |
|
1233 { |
3568
|
1234 os << " " |
|
1235 << std::setw (8) << std::setprecision (1) |
|
1236 << std::setiosflags (std::ios::scientific|std::ios::left) |
|
1237 << scale |
|
1238 << std::resetiosflags (std::ios::scientific|std::ios::left) |
|
1239 << " *\n"; |
3105
|
1240 |
|
1241 if (! compact_format) |
|
1242 os << "\n"; |
|
1243 } |
|
1244 } |
|
1245 |
|
1246 static void |
3523
|
1247 pr_col_num_header (std::ostream& os, int total_width, int max_width, |
1972
|
1248 int lim, int col, int extra_indent) |
1186
|
1249 { |
2165
|
1250 if (total_width > max_width && Vsplit_long_rows) |
1186
|
1251 { |
|
1252 if (col != 0 && ! compact_format) |
2915
|
1253 os << "\n\n"; |
1186
|
1254 |
|
1255 int num_cols = lim - col; |
|
1256 |
3548
|
1257 os << std::setw (extra_indent) << ""; |
1972
|
1258 |
1186
|
1259 if (num_cols == 1) |
|
1260 os << " Column " << col + 1 << ":\n"; |
|
1261 else if (num_cols == 2) |
|
1262 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1263 else |
|
1264 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
2915
|
1265 |
|
1266 if (! compact_format) |
|
1267 os << "\n"; |
1186
|
1268 } |
|
1269 } |
|
1270 |
3248
|
1271 static inline void |
3608
|
1272 pr_plus_format (std::ostream& os, double d) |
3248
|
1273 { |
|
1274 if (d == 0.0) |
|
1275 os << " "; |
|
1276 else if (d < 0.0) |
|
1277 os << "-"; |
|
1278 else |
|
1279 os << "+"; |
|
1280 } |
|
1281 |
1
|
1282 void |
3523
|
1283 octave_print_internal (std::ostream& os, double d, bool pr_as_read_syntax) |
1
|
1284 { |
|
1285 if (plus_format) |
|
1286 { |
3608
|
1287 pr_plus_format (os, d); |
1
|
1288 } |
|
1289 else |
|
1290 { |
|
1291 set_format (d); |
|
1292 if (free_format) |
|
1293 os << d; |
|
1294 else |
|
1295 pr_float (os, d); |
|
1296 } |
|
1297 } |
|
1298 |
|
1299 void |
3608
|
1300 octave_print_internal (std::ostream& os, const Matrix& m, |
|
1301 bool pr_as_read_syntax, int extra_indent) |
1
|
1302 { |
|
1303 int nr = m.rows (); |
|
1304 int nc = m.columns (); |
|
1305 |
2408
|
1306 if (nr == 0 || nc == 0) |
626
|
1307 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1308 else if (plus_format && ! pr_as_read_syntax) |
1
|
1309 { |
|
1310 for (int i = 0; i < nr; i++) |
|
1311 { |
|
1312 for (int j = 0; j < nc; j++) |
|
1313 { |
|
1314 if (j == 0) |
|
1315 os << " "; |
|
1316 |
3608
|
1317 pr_plus_format (os, m(i,j)); |
1
|
1318 } |
2907
|
1319 |
|
1320 if (i < nr - 1) |
|
1321 os << "\n"; |
1
|
1322 } |
|
1323 } |
|
1324 else |
|
1325 { |
|
1326 int fw; |
3105
|
1327 double scale = 1.0; |
|
1328 set_format (m, fw, scale); |
1
|
1329 int column_width = fw + 2; |
|
1330 int total_width = nc * column_width; |
2926
|
1331 int max_width = command_editor::terminal_cols (); |
1
|
1332 |
626
|
1333 if (pr_as_read_syntax) |
|
1334 max_width -= 4; |
1972
|
1335 else |
|
1336 max_width -= extra_indent; |
|
1337 |
|
1338 if (max_width < 0) |
|
1339 max_width = 0; |
626
|
1340 |
1
|
1341 if (free_format) |
|
1342 { |
626
|
1343 if (pr_as_read_syntax) |
|
1344 os << "[\n"; |
|
1345 |
1
|
1346 os << m; |
626
|
1347 |
|
1348 if (pr_as_read_syntax) |
|
1349 os << "]"; |
|
1350 |
1
|
1351 return; |
|
1352 } |
|
1353 |
|
1354 int inc = nc; |
2165
|
1355 if (total_width > max_width && Vsplit_long_rows) |
1
|
1356 { |
|
1357 inc = max_width / column_width; |
|
1358 if (inc == 0) |
|
1359 inc++; |
|
1360 } |
|
1361 |
626
|
1362 if (pr_as_read_syntax) |
1
|
1363 { |
|
1364 for (int i = 0; i < nr; i++) |
|
1365 { |
626
|
1366 int col = 0; |
|
1367 while (col < nc) |
1
|
1368 { |
626
|
1369 int lim = col + inc < nc ? col + inc : nc; |
|
1370 |
|
1371 for (int j = col; j < lim; j++) |
|
1372 { |
|
1373 if (i == 0 && j == 0) |
|
1374 os << "[ "; |
|
1375 else |
|
1376 { |
|
1377 if (j > col && j < lim) |
|
1378 os << ", "; |
|
1379 else |
|
1380 os << " "; |
|
1381 } |
|
1382 |
3608
|
1383 pr_float (os, m(i,j)); |
626
|
1384 } |
|
1385 |
|
1386 col += inc; |
|
1387 |
|
1388 if (col >= nc) |
|
1389 { |
|
1390 if (i == nr - 1) |
|
1391 os << " ]"; |
|
1392 else |
|
1393 os << ";\n"; |
|
1394 } |
|
1395 else |
|
1396 os << " ...\n"; |
1
|
1397 } |
|
1398 } |
626
|
1399 } |
|
1400 else |
|
1401 { |
3105
|
1402 pr_scale_header (os, scale); |
|
1403 |
626
|
1404 for (int col = 0; col < nc; col += inc) |
|
1405 { |
|
1406 int lim = col + inc < nc ? col + inc : nc; |
|
1407 |
1972
|
1408 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1409 extra_indent); |
626
|
1410 |
|
1411 for (int i = 0; i < nr; i++) |
|
1412 { |
3548
|
1413 os << std::setw (extra_indent) << ""; |
1972
|
1414 |
626
|
1415 for (int j = col; j < lim; j++) |
|
1416 { |
|
1417 os << " "; |
|
1418 |
3608
|
1419 pr_float (os, m(i,j), fw, scale); |
626
|
1420 } |
|
1421 |
2907
|
1422 if (i < nr - 1) |
|
1423 os << "\n"; |
626
|
1424 } |
|
1425 } |
1
|
1426 } |
|
1427 } |
|
1428 } |
|
1429 |
3248
|
1430 static inline void |
3608
|
1431 pr_plus_format (std::ostream& os, const Complex& c) |
3248
|
1432 { |
|
1433 double rp = c.real (); |
|
1434 double ip = c.imag (); |
|
1435 |
|
1436 if (rp == 0.0) |
|
1437 { |
|
1438 if (ip == 0.0) |
|
1439 os << " "; |
|
1440 else |
|
1441 os << "i"; |
|
1442 } |
|
1443 else if (ip == 0.0) |
3608
|
1444 pr_plus_format (os, rp); |
3248
|
1445 else |
|
1446 os << "c"; |
|
1447 } |
|
1448 |
1
|
1449 void |
3523
|
1450 octave_print_internal (std::ostream& os, const Complex& c, |
1972
|
1451 bool pr_as_read_syntax) |
1
|
1452 { |
|
1453 if (plus_format) |
|
1454 { |
3608
|
1455 pr_plus_format (os, c); |
1
|
1456 } |
|
1457 else |
|
1458 { |
|
1459 set_format (c); |
|
1460 if (free_format) |
|
1461 os << c; |
|
1462 else |
|
1463 pr_complex (os, c); |
|
1464 } |
|
1465 } |
|
1466 |
|
1467 void |
3523
|
1468 octave_print_internal (std::ostream& os, const ComplexMatrix& cm, |
1972
|
1469 bool pr_as_read_syntax, int extra_indent) |
1
|
1470 { |
|
1471 int nr = cm.rows (); |
|
1472 int nc = cm.columns (); |
|
1473 |
2408
|
1474 if (nr == 0 || nc == 0) |
626
|
1475 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1476 else if (plus_format && ! pr_as_read_syntax) |
1
|
1477 { |
|
1478 for (int i = 0; i < nr; i++) |
|
1479 { |
|
1480 for (int j = 0; j < nc; j++) |
|
1481 { |
|
1482 if (j == 0) |
|
1483 os << " "; |
|
1484 |
3608
|
1485 pr_plus_format (os, cm(i,j)); |
1
|
1486 } |
2907
|
1487 |
|
1488 if (i < nr - 1) |
|
1489 os << "\n"; |
1
|
1490 } |
|
1491 } |
|
1492 else |
|
1493 { |
|
1494 int r_fw, i_fw; |
3105
|
1495 double scale = 1.0; |
|
1496 set_format (cm, r_fw, i_fw, scale); |
1
|
1497 int column_width = i_fw + r_fw; |
1309
|
1498 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
1
|
1499 int total_width = nc * column_width; |
2926
|
1500 int max_width = command_editor::terminal_cols (); |
1
|
1501 |
626
|
1502 if (pr_as_read_syntax) |
|
1503 max_width -= 4; |
1972
|
1504 else |
|
1505 max_width -= extra_indent; |
|
1506 |
|
1507 if (max_width < 0) |
|
1508 max_width = 0; |
626
|
1509 |
1
|
1510 if (free_format) |
|
1511 { |
626
|
1512 if (pr_as_read_syntax) |
|
1513 os << "[\n"; |
|
1514 |
1
|
1515 os << cm; |
626
|
1516 |
|
1517 if (pr_as_read_syntax) |
|
1518 os << "]"; |
|
1519 |
1
|
1520 return; |
|
1521 } |
|
1522 |
|
1523 int inc = nc; |
2165
|
1524 if (total_width > max_width && Vsplit_long_rows) |
1
|
1525 { |
|
1526 inc = max_width / column_width; |
|
1527 if (inc == 0) |
|
1528 inc++; |
|
1529 } |
|
1530 |
626
|
1531 if (pr_as_read_syntax) |
1
|
1532 { |
|
1533 for (int i = 0; i < nr; i++) |
|
1534 { |
626
|
1535 int col = 0; |
|
1536 while (col < nc) |
1
|
1537 { |
626
|
1538 int lim = col + inc < nc ? col + inc : nc; |
|
1539 |
|
1540 for (int j = col; j < lim; j++) |
|
1541 { |
|
1542 if (i == 0 && j == 0) |
|
1543 os << "[ "; |
|
1544 else |
|
1545 { |
|
1546 if (j > col && j < lim) |
|
1547 os << ", "; |
|
1548 else |
|
1549 os << " "; |
|
1550 } |
|
1551 |
3608
|
1552 pr_complex (os, cm(i,j)); |
626
|
1553 } |
|
1554 |
|
1555 col += inc; |
|
1556 |
|
1557 if (col >= nc) |
|
1558 { |
|
1559 if (i == nr - 1) |
|
1560 os << " ]"; |
|
1561 else |
|
1562 os << ";\n"; |
|
1563 } |
1
|
1564 else |
626
|
1565 os << " ...\n"; |
1
|
1566 } |
|
1567 } |
626
|
1568 } |
|
1569 else |
|
1570 { |
3105
|
1571 pr_scale_header (os, scale); |
|
1572 |
626
|
1573 for (int col = 0; col < nc; col += inc) |
|
1574 { |
|
1575 int lim = col + inc < nc ? col + inc : nc; |
|
1576 |
1972
|
1577 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1578 extra_indent); |
626
|
1579 |
|
1580 for (int i = 0; i < nr; i++) |
|
1581 { |
3548
|
1582 os << std::setw (extra_indent) << ""; |
1972
|
1583 |
626
|
1584 for (int j = col; j < lim; j++) |
|
1585 { |
|
1586 os << " "; |
|
1587 |
3608
|
1588 pr_complex (os, cm(i,j), r_fw, i_fw, scale); |
626
|
1589 } |
2907
|
1590 |
|
1591 if (i < nr - 1) |
|
1592 os << "\n"; |
626
|
1593 } |
|
1594 } |
1
|
1595 } |
|
1596 } |
|
1597 } |
|
1598 |
|
1599 void |
3523
|
1600 octave_print_internal (std::ostream& os, const Range& r, |
1972
|
1601 bool pr_as_read_syntax, int extra_indent) |
1
|
1602 { |
626
|
1603 double base = r.base (); |
1
|
1604 double increment = r.inc (); |
626
|
1605 double limit = r.limit (); |
1
|
1606 int num_elem = r.nelem (); |
|
1607 |
626
|
1608 if (plus_format && ! pr_as_read_syntax) |
1
|
1609 { |
|
1610 os << " "; |
|
1611 for (int i = 0; i < num_elem; i++) |
|
1612 { |
626
|
1613 double val = base + i * increment; |
1
|
1614 if (val == 0.0) |
|
1615 os << " "; |
|
1616 else |
|
1617 os << "+"; |
|
1618 } |
|
1619 } |
|
1620 else |
|
1621 { |
|
1622 int fw; |
3105
|
1623 double scale = 1.0; |
|
1624 set_format (r, fw, scale); |
1
|
1625 |
626
|
1626 if (pr_as_read_syntax) |
1
|
1627 { |
626
|
1628 if (free_format) |
|
1629 { |
|
1630 os << base << " : "; |
|
1631 if (increment != 1.0) |
|
1632 os << increment << " : "; |
|
1633 os << limit; |
|
1634 } |
|
1635 else |
|
1636 { |
|
1637 pr_float (os, base, fw); |
|
1638 os << " : "; |
|
1639 if (increment != 1.0) |
|
1640 { |
|
1641 pr_float (os, increment, fw); |
|
1642 os << " : "; |
|
1643 } |
|
1644 pr_float (os, limit, fw); |
|
1645 } |
1
|
1646 } |
626
|
1647 else |
|
1648 { |
|
1649 int column_width = fw + 2; |
|
1650 int total_width = num_elem * column_width; |
2926
|
1651 int max_width = command_editor::terminal_cols (); |
1
|
1652 |
626
|
1653 if (free_format) |
|
1654 { |
|
1655 os << r; |
|
1656 return; |
|
1657 } |
1
|
1658 |
626
|
1659 int inc = num_elem; |
2165
|
1660 if (total_width > max_width && Vsplit_long_rows) |
1
|
1661 { |
626
|
1662 inc = max_width / column_width; |
|
1663 if (inc == 0) |
|
1664 inc++; |
1
|
1665 } |
|
1666 |
1972
|
1667 max_width -= extra_indent; |
|
1668 |
|
1669 if (max_width < 0) |
|
1670 max_width = 0; |
|
1671 |
3105
|
1672 pr_scale_header (os, scale); |
|
1673 |
626
|
1674 int col = 0; |
|
1675 while (col < num_elem) |
1
|
1676 { |
626
|
1677 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1678 |
1972
|
1679 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1680 extra_indent); |
|
1681 |
3548
|
1682 os << std::setw (extra_indent) << ""; |
626
|
1683 |
|
1684 for (int i = col; i < lim; i++) |
|
1685 { |
|
1686 double val = base + i * increment; |
3105
|
1687 |
626
|
1688 os << " "; |
3105
|
1689 |
3608
|
1690 pr_float (os, val, fw, scale); |
626
|
1691 } |
|
1692 |
2907
|
1693 col += inc; |
626
|
1694 |
2907
|
1695 if (col < num_elem) |
|
1696 os << "\n"; |
1
|
1697 } |
|
1698 } |
|
1699 } |
|
1700 } |
|
1701 |
1343
|
1702 void |
3523
|
1703 octave_print_internal (std::ostream& os, const boolMatrix& bm, |
3215
|
1704 bool pr_as_read_syntax, |
|
1705 int extra_indent) |
|
1706 { |
|
1707 Matrix tmp (bm); |
|
1708 octave_print_internal (os, tmp, pr_as_read_syntax, extra_indent); |
|
1709 } |
|
1710 |
|
1711 void |
3523
|
1712 octave_print_internal (std::ostream& os, const charMatrix& chm, |
3215
|
1713 bool pr_as_read_syntax, |
|
1714 int /* extra_indent XXX FIXME XXX */, |
|
1715 bool pr_as_string) |
1343
|
1716 { |
1572
|
1717 if (pr_as_string) |
|
1718 { |
|
1719 int nstr = chm.rows (); |
1343
|
1720 |
1572
|
1721 if (pr_as_read_syntax && nstr > 1) |
|
1722 os << "[ "; |
1343
|
1723 |
2907
|
1724 if (nstr != 0) |
1343
|
1725 { |
2664
|
1726 for (int i = 0; i < nstr; i++) |
1572
|
1727 { |
3523
|
1728 std::string row = chm.row_as_string (i); |
2664
|
1729 |
|
1730 if (pr_as_read_syntax) |
|
1731 { |
|
1732 os << "\"" << undo_string_escapes (row) << "\""; |
1343
|
1733 |
2664
|
1734 if (i < nstr - 1) |
|
1735 os << "; "; |
|
1736 } |
|
1737 else |
2907
|
1738 { |
|
1739 os << row; |
|
1740 |
|
1741 if (i < nstr - 1) |
|
1742 os << "\n"; |
|
1743 } |
1572
|
1744 } |
1343
|
1745 } |
1572
|
1746 |
|
1747 if (pr_as_read_syntax && nstr > 1) |
|
1748 os << " ]"; |
1343
|
1749 } |
1572
|
1750 else |
|
1751 { |
|
1752 os << "sorry, printing char matrices not implemented yet\n"; |
|
1753 } |
1343
|
1754 } |
|
1755 |
3933
|
1756 extern void |
|
1757 octave_print_internal (std::ostream&, const Cell&, bool, int, bool) |
3928
|
1758 { |
3933
|
1759 panic_impossible (); |
3928
|
1760 } |
|
1761 |
3685
|
1762 DEFUN (disp, args, nargout, |
|
1763 "-*- texinfo -*-\n\ |
|
1764 @deftypefn {Built-in Function} {} disp (@var{x})\n\ |
|
1765 Display the value of @var{x}. For example,\n\ |
|
1766 \n\ |
|
1767 @example\n\ |
|
1768 disp (\"The value of pi is:\"), disp (pi)\n\ |
|
1769 \n\ |
|
1770 @print{} the value of pi is:\n\ |
|
1771 @print{} 3.1416\n\ |
|
1772 @end example\n\ |
|
1773 \n\ |
|
1774 @noindent\n\ |
|
1775 Note that the output from @code{disp} always ends with a newline.\n\ |
|
1776 \n\ |
|
1777 If an output value is requested, @code{disp} prints nothing and\n\ |
|
1778 returns the formatted output in a string.\n\ |
|
1779 @end deftypefn\n\ |
|
1780 @seealso{fdisp}") |
|
1781 { |
|
1782 octave_value retval; |
|
1783 |
|
1784 int nargin = args.length (); |
|
1785 |
|
1786 if (nargin == 1 && nargout < 2) |
|
1787 { |
|
1788 if (nargout == 0) |
|
1789 args(0).print (octave_stdout); |
|
1790 else |
|
1791 { |
4051
|
1792 OSSTREAM buf; |
3685
|
1793 args(0).print (buf); |
4051
|
1794 buf << OSSTREAM_ENDS; |
|
1795 retval = OSSTREAM_STR (buf); |
|
1796 OSSTREAM_FREEZE (buf); |
3685
|
1797 } |
|
1798 } |
|
1799 else |
|
1800 print_usage ("disp"); |
|
1801 |
|
1802 return retval; |
|
1803 } |
|
1804 |
|
1805 DEFUN (fdisp, args, , |
|
1806 "-*- texinfo -*-\n\ |
|
1807 @deftypefn {Built-in Function} {} fdisp (@var{fid}, @var{x})\n\ |
|
1808 Display the value of @var{x} on the stream @var{fid}. For example,\n\ |
|
1809 \n\ |
|
1810 @example\n\ |
|
1811 disp (stdout, \"The value of pi is:\"), disp (stdout, pi)\n\ |
|
1812 \n\ |
|
1813 @print{} the value of pi is:\n\ |
|
1814 @print{} 3.1416\n\ |
|
1815 @end example\n\ |
|
1816 \n\ |
|
1817 @noindent\n\ |
|
1818 Note that the output from @code{disp} always ends with a newline.\n\ |
|
1819 \n\ |
|
1820 If an output value is requested, @code{disp} prints nothing and\n\ |
|
1821 returns the formatted output in a string.\n\ |
|
1822 @end deftypefn\n\ |
|
1823 @seealso{disp}") |
|
1824 { |
|
1825 octave_value retval; |
|
1826 |
|
1827 int nargin = args.length (); |
|
1828 |
|
1829 if (nargin == 2) |
|
1830 { |
|
1831 int fid = octave_stream_list::get_file_number (args (0)); |
|
1832 |
|
1833 octave_stream os = octave_stream_list::lookup (fid, "fdisp"); |
|
1834 |
|
1835 if (! error_state) |
|
1836 { |
3769
|
1837 std::ostream *osp = os.output_stream (); |
3685
|
1838 |
|
1839 if (osp) |
|
1840 args(1).print (*osp); |
|
1841 else |
|
1842 error ("fdisp: stream not open for writing"); |
|
1843 } |
|
1844 } |
|
1845 else |
|
1846 print_usage ("fdisp"); |
|
1847 |
|
1848 return retval; |
|
1849 } |
|
1850 |
1
|
1851 static void |
|
1852 init_format_state (void) |
|
1853 { |
2387
|
1854 free_format = false; |
|
1855 plus_format = false; |
|
1856 bank_format = false; |
3608
|
1857 hex_format = 0; |
1309
|
1858 bit_format = 0; |
2387
|
1859 print_e = false; |
|
1860 print_big_e = false; |
1
|
1861 } |
|
1862 |
|
1863 static void |
|
1864 set_output_prec_and_fw (int prec, int fw) |
|
1865 { |
2800
|
1866 bind_builtin_variable ("output_precision", static_cast<double> (prec)); |
|
1867 bind_builtin_variable ("output_max_field_width", static_cast<double> (fw)); |
1
|
1868 } |
|
1869 |
1755
|
1870 static void |
|
1871 set_format_style (int argc, const string_vector& argv) |
1
|
1872 { |
1755
|
1873 int idx = 1; |
|
1874 |
1899
|
1875 if (--argc > 0) |
1
|
1876 { |
3523
|
1877 std::string arg = argv[idx++]; |
2584
|
1878 |
1755
|
1879 if (arg == "short") |
1
|
1880 { |
1755
|
1881 if (--argc > 0) |
1
|
1882 { |
1755
|
1883 arg = argv[idx++]; |
|
1884 |
|
1885 if (arg == "e") |
1
|
1886 { |
1755
|
1887 init_format_state (); |
2387
|
1888 print_e = true; |
1755
|
1889 } |
|
1890 else if (arg == "E") |
|
1891 { |
|
1892 init_format_state (); |
2387
|
1893 print_e = true; |
|
1894 print_big_e = true; |
1
|
1895 } |
|
1896 else |
|
1897 { |
1755
|
1898 error ("format: unrecognized option `short %s'", |
|
1899 arg.c_str ()); |
|
1900 return; |
|
1901 } |
|
1902 } |
|
1903 else |
|
1904 init_format_state (); |
|
1905 |
|
1906 set_output_prec_and_fw (3, 8); |
|
1907 } |
|
1908 else if (arg == "long") |
|
1909 { |
|
1910 if (--argc > 0) |
|
1911 { |
|
1912 arg = argv[idx++]; |
|
1913 |
|
1914 if (arg == "e") |
|
1915 { |
|
1916 init_format_state (); |
2387
|
1917 print_e = true; |
1755
|
1918 } |
|
1919 else if (arg == "E") |
|
1920 { |
|
1921 init_format_state (); |
2387
|
1922 print_e = true; |
|
1923 print_big_e = true; |
1
|
1924 } |
|
1925 else |
1755
|
1926 { |
|
1927 error ("format: unrecognized option `long %s'", |
|
1928 arg.c_str ()); |
|
1929 return; |
|
1930 } |
1186
|
1931 } |
1
|
1932 else |
1755
|
1933 init_format_state (); |
|
1934 |
|
1935 set_output_prec_and_fw (15, 24); |
|
1936 } |
|
1937 else if (arg == "hex") |
|
1938 { |
|
1939 init_format_state (); |
3608
|
1940 hex_format = 1; |
1755
|
1941 } |
|
1942 else if (arg == "native-hex") |
|
1943 { |
|
1944 init_format_state (); |
|
1945 hex_format = 2; |
|
1946 } |
|
1947 else if (arg == "bit") |
|
1948 { |
|
1949 init_format_state (); |
|
1950 bit_format = 1; |
|
1951 } |
|
1952 else if (arg == "native-bit") |
|
1953 { |
|
1954 init_format_state (); |
|
1955 bit_format = 2; |
|
1956 } |
|
1957 else if (arg == "+" || arg == "plus") |
|
1958 { |
|
1959 init_format_state (); |
2387
|
1960 plus_format = true; |
1755
|
1961 } |
|
1962 else if (arg == "bank") |
|
1963 { |
|
1964 init_format_state (); |
2387
|
1965 bank_format = true; |
1755
|
1966 } |
|
1967 else if (arg == "free") |
|
1968 { |
|
1969 init_format_state (); |
2387
|
1970 free_format = true; |
1755
|
1971 } |
|
1972 else if (arg == "none") |
|
1973 { |
|
1974 init_format_state (); |
2387
|
1975 free_format = true; |
1755
|
1976 } |
|
1977 else if (arg == "compact") |
|
1978 { |
2387
|
1979 compact_format = true; |
1755
|
1980 } |
|
1981 else if (arg == "loose") |
|
1982 { |
2387
|
1983 compact_format = false; |
1
|
1984 } |
|
1985 else |
1755
|
1986 error ("format: unrecognized format state `%s'", arg.c_str ()); |
1
|
1987 } |
|
1988 else |
|
1989 { |
|
1990 init_format_state (); |
|
1991 set_output_prec_and_fw (5, 10); |
|
1992 } |
|
1993 } |
|
1994 |
1957
|
1995 DEFUN_TEXT (format, args, , |
3372
|
1996 "-*- texinfo -*-\n\ |
|
1997 @deffn {Command} format options\n\ |
|
1998 Control the format of the output produced by @code{disp} and Octave's\n\ |
|
1999 normal echoing mechanism. Valid options are listed in the following\n\ |
|
2000 table.\n\ |
|
2001 \n\ |
|
2002 @table @code\n\ |
|
2003 @item short\n\ |
|
2004 Octave will try to print numbers with at\n\ |
|
2005 least 3 significant figures within a field that is a maximum of 8\n\ |
|
2006 characters wide.\n\ |
|
2007 \n\ |
|
2008 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
2009 decimal point and all the numbers fit within the maximum field width,\n\ |
|
2010 it switches to an @samp{e} format.\n\ |
|
2011 \n\ |
|
2012 @item long\n\ |
|
2013 Octave will try to print numbers with at least 15 significant figures\n\ |
|
2014 within a field that is a maximum of 24 characters wide.\n\ |
|
2015 \n\ |
|
2016 As will the @samp{short} format, Octave will switch to an @samp{e}\n\ |
|
2017 format if it is unable to format a matrix so that columns line up on the\n\ |
|
2018 decimal point and all the numbers fit within the maximum field width.\n\ |
|
2019 \n\ |
|
2020 @item long e\n\ |
|
2021 @itemx short e\n\ |
|
2022 The same as @samp{format long} or @samp{format short} but always display\n\ |
|
2023 output with an @samp{e} format. For example, with the @samp{short e}\n\ |
|
2024 format, pi is displayed as @code{3.14e+00}.\n\ |
|
2025 \n\ |
|
2026 @item long E\n\ |
|
2027 @itemx short E\n\ |
|
2028 The same as @samp{format long e} or @samp{format short e} but always\n\ |
|
2029 display output with an uppercase @samp{E} format. For example, with\n\ |
|
2030 the @samp{long E} format, pi is displayed as\n\ |
|
2031 @code{3.14159265358979E+00}.\n\ |
|
2032 \n\ |
|
2033 @item free\n\ |
|
2034 @itemx none\n\ |
|
2035 Print output in free format, without trying to line up columns of\n\ |
|
2036 matrices on the decimal point. This also causes complex numbers to be\n\ |
|
2037 formatted like this @samp{(0.604194, 0.607088)} instead of like this\n\ |
|
2038 @samp{0.60419 + 0.60709i}.\n\ |
529
|
2039 \n\ |
3372
|
2040 @item bank\n\ |
|
2041 Print in a fixed format with two places to the right of the decimal\n\ |
|
2042 point.\n\ |
|
2043 \n\ |
|
2044 @item +\n\ |
|
2045 Print a @samp{+} symbol for nonzero matrix elements and a space for zero\n\ |
|
2046 matrix elements. This format can be very useful for examining the\n\ |
|
2047 structure of a large matrix.\n\ |
|
2048 \n\ |
|
2049 @item hex\n\ |
|
2050 Print the hexadecimal representation numbers as they are stored in\n\ |
|
2051 memory. For example, on a workstation which stores 8 byte real values\n\ |
|
2052 in IEEE format with the least significant byte first, the value of\n\ |
|
2053 @code{pi} when printed in @code{hex} format is @code{400921fb54442d18}.\n\ |
|
2054 This format only works for numeric values.\n\ |
|
2055 \n\ |
|
2056 @item bit\n\ |
|
2057 Print the bit representation of numbers as stored in memory.\n\ |
|
2058 For example, the value of @code{pi} is\n\ |
|
2059 \n\ |
|
2060 @example\n\ |
|
2061 @group\n\ |
|
2062 01000000000010010010000111111011\n\ |
|
2063 01010100010001000010110100011000\n\ |
|
2064 @end group\n\ |
|
2065 @end example\n\ |
|
2066 \n\ |
|
2067 (shown here in two 32 bit sections for typesetting purposes) when\n\ |
|
2068 printed in bit format on a workstation which stores 8 byte real values\n\ |
|
2069 in IEEE format with the least significant byte first. This format only\n\ |
|
2070 works for numeric types.\n\ |
|
2071 @end table\n\ |
|
2072 \n\ |
|
2073 By default, Octave will try to print numbers with at least 5 significant\n\ |
|
2074 figures within a field that is a maximum of 10 characters wide.\n\ |
|
2075 \n\ |
|
2076 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
2077 decimal point and all the numbers fit within the maximum field width,\n\ |
|
2078 it switches to an @samp{e} format.\n\ |
|
2079 \n\ |
|
2080 If @code{format} is invoked without any options, the default format\n\ |
|
2081 state is restored.\n\ |
|
2082 @end deffn") |
529
|
2083 { |
2086
|
2084 octave_value_list retval; |
529
|
2085 |
1755
|
2086 int argc = args.length () + 1; |
|
2087 |
1968
|
2088 string_vector argv = args.make_argv ("format"); |
1755
|
2089 |
|
2090 if (error_state) |
|
2091 return retval; |
529
|
2092 |
|
2093 set_format_style (argc, argv); |
|
2094 |
|
2095 return retval; |
|
2096 } |
|
2097 |
2165
|
2098 static int |
3105
|
2099 fixed_point_format (void) |
|
2100 { |
|
2101 Vfixed_point_format = check_preference ("fixed_point_format"); |
|
2102 |
|
2103 return 0; |
|
2104 } |
|
2105 |
|
2106 static int |
2165
|
2107 output_max_field_width (void) |
|
2108 { |
|
2109 double val; |
|
2110 if (builtin_real_scalar_variable ("output_max_field_width", val) |
4025
|
2111 && ! octave_is_NaN_or_NA (val)) |
2165
|
2112 { |
|
2113 int ival = NINT (val); |
2800
|
2114 if (ival > 0 && ival == val) |
2165
|
2115 { |
|
2116 Voutput_max_field_width = ival; |
|
2117 return 0; |
|
2118 } |
|
2119 } |
|
2120 gripe_invalid_value_specified ("output_max_field_width"); |
|
2121 return -1; |
|
2122 } |
|
2123 |
|
2124 static int |
|
2125 output_precision (void) |
|
2126 { |
|
2127 double val; |
|
2128 if (builtin_real_scalar_variable ("output_precision", val) |
4025
|
2129 && ! octave_is_NaN_or_NA (val)) |
2165
|
2130 { |
|
2131 int ival = NINT (val); |
2800
|
2132 if (ival >= 0 && ival == val) |
2165
|
2133 { |
|
2134 Voutput_precision = ival; |
|
2135 return 0; |
|
2136 } |
|
2137 } |
|
2138 gripe_invalid_value_specified ("output_precision"); |
|
2139 return -1; |
|
2140 } |
|
2141 |
|
2142 static int |
|
2143 print_empty_dimensions (void) |
|
2144 { |
|
2145 Vprint_empty_dimensions = check_preference ("print_empty_dimensions"); |
|
2146 |
|
2147 return 0; |
|
2148 } |
|
2149 |
|
2150 static int |
|
2151 split_long_rows (void) |
|
2152 { |
|
2153 Vsplit_long_rows = check_preference ("split_long_rows"); |
|
2154 |
|
2155 return 0; |
|
2156 } |
|
2157 |
|
2158 void |
|
2159 symbols_of_pr_output (void) |
|
2160 { |
3258
|
2161 DEFVAR (fixed_point_format, 0.0, fixed_point_format, |
3321
|
2162 "-*- texinfo -*-\n\ |
|
2163 @defvr {Built-in Variable} fixed_point_format\n\ |
|
2164 If the value of this variable is nonzero, Octave will scale all values\n\ |
|
2165 in a matrix so that the largest may be written with one leading digit.\n\ |
|
2166 The scaling factor is printed on the first line of output. For example,\n\ |
|
2167 \n\ |
|
2168 @example\n\ |
|
2169 @group\n\ |
|
2170 octave:1> logspace (1, 7, 5)'\n\ |
|
2171 ans =\n\ |
|
2172 \n\ |
|
2173 1.0e+07 *\n\ |
|
2174 \n\ |
|
2175 0.00000\n\ |
|
2176 0.00003\n\ |
|
2177 0.00100\n\ |
|
2178 0.03162\n\ |
|
2179 1.00000\n\ |
|
2180 @end group\n\ |
|
2181 @end example\n\ |
|
2182 \n\ |
|
2183 @noindent\n\ |
|
2184 Notice that first value appears to be zero when it is actually 1. For\n\ |
|
2185 this reason, you should be careful when setting\n\ |
|
2186 @code{fixed_point_format} to a nonzero value.\n\ |
|
2187 \n\ |
|
2188 The default value of @code{fixed_point_format} is 0.\n\ |
3333
|
2189 @end defvr"); |
3105
|
2190 |
3258
|
2191 DEFVAR (output_max_field_width, 10.0, output_max_field_width, |
3321
|
2192 "-*- texinfo -*-\n\ |
|
2193 @defvr {Built-in Variable} output_max_field_width\n\ |
|
2194 This variable specifies the maximum width of a numeric output field.\n\ |
|
2195 The default value is 10.\n\ |
3333
|
2196 @end defvr"); |
2165
|
2197 |
3258
|
2198 DEFVAR (output_precision, 5.0, output_precision, |
3321
|
2199 "-*- texinfo -*-\n\ |
|
2200 @defvr {Built-in Variable} output_precision\n\ |
|
2201 This variable specifies the minimum number of significant figures to\n\ |
|
2202 display for numeric output. The default value is 5.\n\ |
3333
|
2203 @end defvr"); |
2165
|
2204 |
3258
|
2205 DEFVAR (print_empty_dimensions, 1.0, print_empty_dimensions, |
3321
|
2206 "-*- texinfo -*-\n\ |
|
2207 @defvr {Built-in Variable} print_empty_dimensions\n\ |
|
2208 If the value of @code{print_empty_dimensions} is nonzero, the\n\ |
|
2209 dimensions of empty matrices are printed along with the empty matrix\n\ |
|
2210 symbol, @samp{[]}. For example, the expression\n\ |
|
2211 \n\ |
|
2212 @example\n\ |
|
2213 zeros (3, 0)\n\ |
|
2214 @end example\n\ |
|
2215 \n\ |
|
2216 @noindent\n\ |
|
2217 will print\n\ |
|
2218 \n\ |
|
2219 @example\n\ |
|
2220 ans = [](3x0)\n\ |
|
2221 @end example\n\ |
3333
|
2222 @end defvr"); |
2165
|
2223 |
3258
|
2224 DEFVAR (split_long_rows, 1.0, split_long_rows, |
3321
|
2225 "-*- texinfo -*-\n\ |
|
2226 @defvr {Built-in Variable} split_long_rows\n\ |
|
2227 For large matrices, Octave may not be able to display all the columns of\n\ |
|
2228 a given row on one line of your screen. This can result in missing\n\ |
|
2229 information or output that is nearly impossible to decipher, depending\n\ |
|
2230 on whether your terminal truncates or wraps long lines.\n\ |
|
2231 \n\ |
|
2232 If the value of @code{split_long_rows} is nonzero, Octave will display\n\ |
|
2233 the matrix in a series of smaller pieces, each of which can fit within\n\ |
|
2234 the limits of your terminal width. Each set of rows is labeled so that\n\ |
|
2235 you can easily see which columns are currently being displayed.\n\ |
|
2236 For example:\n\ |
|
2237 \n\ |
|
2238 @smallexample\n\ |
|
2239 @group\n\ |
|
2240 octave:13> rand (2,10)\n\ |
|
2241 ans =\n\ |
|
2242 \n\ |
|
2243 Columns 1 through 6:\n\ |
|
2244 \n\ |
|
2245 0.75883 0.93290 0.40064 0.43818 0.94958 0.16467\n\ |
|
2246 0.75697 0.51942 0.40031 0.61784 0.92309 0.40201\n\ |
|
2247 \n\ |
|
2248 Columns 7 through 10:\n\ |
|
2249 \n\ |
|
2250 0.90174 0.11854 0.72313 0.73326\n\ |
|
2251 0.44672 0.94303 0.56564 0.82150\n\ |
|
2252 @end group\n\ |
|
2253 @end smallexample\n\ |
|
2254 \n\ |
|
2255 @noindent\n\ |
|
2256 The default value of @code{split_long_rows} is nonzero.\n\ |
3333
|
2257 @end defvr"); |
2165
|
2258 } |
|
2259 |
1
|
2260 /* |
|
2261 ;;; Local Variables: *** |
|
2262 ;;; mode: C++ *** |
|
2263 ;;; End: *** |
|
2264 */ |