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