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