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