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