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