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