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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
1
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
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> |
5765
|
34 #include <sstream> |
1728
|
35 #include <string> |
|
36 |
4655
|
37 #include "Array-util.h" |
453
|
38 #include "CMatrix.h" |
1
|
39 #include "Range.h" |
2926
|
40 #include "cmd-edit.h" |
1352
|
41 #include "dMatrix.h" |
2891
|
42 #include "lo-mappers.h" |
2317
|
43 #include "mach-info.h" |
1651
|
44 #include "oct-cmplx.h" |
4153
|
45 #include "quit.h" |
1755
|
46 #include "str-vec.h" |
1
|
47 |
3933
|
48 #include "Cell.h" |
1352
|
49 #include "defun.h" |
|
50 #include "error.h" |
2165
|
51 #include "gripes.h" |
1755
|
52 #include "oct-obj.h" |
3685
|
53 #include "oct-stream.h" |
1352
|
54 #include "pager.h" |
|
55 #include "pr-output.h" |
1282
|
56 #include "sysdep.h" |
6803
|
57 #include "unwind-prot.h" |
1
|
58 #include "utils.h" |
1352
|
59 #include "variables.h" |
1
|
60 |
3105
|
61 // TRUE means use a scaled fixed point format for `format long' and |
|
62 // `format short'. |
5794
|
63 static bool Vfixed_point_format = false; |
3105
|
64 |
2165
|
65 // The maximum field width for a number printed by the default output |
|
66 // routines. |
5794
|
67 static int Voutput_max_field_width = 10; |
2165
|
68 |
|
69 // The precision of the numbers printed by the default output |
|
70 // routines. |
5794
|
71 static int Voutput_precision = 5; |
2165
|
72 |
5360
|
73 // TRUE means that the dimensions of empty objects should be printed |
2165
|
74 // like this: x = [](2x0). |
5794
|
75 bool Vprint_empty_dimensions = true; |
2165
|
76 |
|
77 // TRUE means that the rows of big matrices should be split into |
|
78 // smaller slices that fit on the screen. |
5794
|
79 static bool Vsplit_long_rows = true; |
2165
|
80 |
5759
|
81 // How many levels of structure elements should we print? |
5794
|
82 int Vstruct_levels_to_print = 2; |
5759
|
83 |
3018
|
84 // TRUE means don't do any fancy formatting. |
2387
|
85 static bool free_format = false; |
1
|
86 |
3018
|
87 // TRUE means print plus sign for nonzero, blank for zero. |
2387
|
88 static bool plus_format = false; |
1
|
89 |
4632
|
90 // First char for > 0, second for < 0, third for == 0. |
|
91 static std::string plus_format_chars = "+ "; |
|
92 |
6788
|
93 // TRUE means always print in a rational approximation |
|
94 static bool rat_format = false; |
|
95 |
|
96 // Used to force the length of the rational approximation string for Frats |
|
97 static int rat_string_len = -1; |
|
98 |
3018
|
99 // TRUE means always print like dollars and cents. |
2387
|
100 static bool bank_format = false; |
1282
|
101 |
3018
|
102 // TRUE means print data in hexadecimal format. |
3608
|
103 static int hex_format = 0; |
1282
|
104 |
3018
|
105 // TRUE means print data in binary-bit-pattern format. |
1309
|
106 static int bit_format = 0; |
|
107 |
3018
|
108 // TRUE means don't put newlines around the column number headers. |
2387
|
109 static bool compact_format = false; |
1186
|
110 |
3018
|
111 // TRUE means use an e format. |
2387
|
112 static bool print_e = false; |
1
|
113 |
4509
|
114 // TRUE means use a g format. |
|
115 static bool print_g = false; |
|
116 |
3018
|
117 // TRUE means print E instead of e for exponent field. |
2387
|
118 static bool print_big_e = false; |
1
|
119 |
3608
|
120 class pr_formatted_float; |
6788
|
121 class pr_rational_float; |
3608
|
122 |
4509
|
123 static int |
|
124 current_output_max_field_width (void) |
|
125 { |
|
126 return Voutput_max_field_width; |
|
127 } |
|
128 |
|
129 static int |
|
130 current_output_precision (void) |
|
131 { |
|
132 return Voutput_precision; |
|
133 } |
|
134 |
3608
|
135 class |
|
136 float_format |
|
137 { |
|
138 public: |
|
139 |
4509
|
140 float_format (int w = current_output_max_field_width (), |
|
141 int p = current_output_precision (), int f = 0) |
3608
|
142 : fw (w), prec (p), fmt (f), up (0), sp (0) { } |
|
143 |
|
144 float_format (const float_format& ff) |
|
145 : fw (ff.fw), prec (ff.prec), fmt (ff.fmt), up (ff.up), sp (ff.sp) { } |
|
146 |
|
147 float_format& operator = (const float_format& ff) |
|
148 { |
|
149 if (&ff != this) |
|
150 { |
|
151 fw = ff.fw; |
|
152 prec = ff.prec; |
|
153 fmt = ff.fmt; |
|
154 up = ff.up; |
|
155 sp = ff.sp; |
|
156 } |
|
157 |
|
158 return *this; |
|
159 } |
|
160 |
|
161 ~float_format (void) { } |
|
162 |
|
163 float_format& scientific (void) { fmt = std::ios::scientific; return *this; } |
|
164 float_format& fixed (void) { fmt = std::ios::fixed; return *this; } |
|
165 float_format& general (void) { fmt = 0; return *this; } |
|
166 |
|
167 float_format& uppercase (void) { up = std::ios::uppercase; return *this; } |
|
168 float_format& lowercase (void) { up = 0; return *this; } |
|
169 |
|
170 float_format& precision (int p) { prec = p; return *this; } |
|
171 |
|
172 float_format& width (int w) { fw = w; return *this; } |
|
173 |
|
174 float_format& trailing_zeros (bool tz = true) |
|
175 { sp = tz ? std::ios::showpoint : 0; return *this; } |
|
176 |
|
177 friend std::ostream& operator << (std::ostream& os, |
|
178 const pr_formatted_float& pff); |
|
179 |
6788
|
180 friend std::ostream& operator << (std::ostream& os, |
|
181 const pr_rational_float& pff); |
|
182 |
3608
|
183 private: |
|
184 |
|
185 // Field width. Zero means as wide as necessary. |
|
186 int fw; |
|
187 |
|
188 // Precision. |
|
189 int prec; |
|
190 |
|
191 // Format. |
|
192 int fmt; |
|
193 |
|
194 // E or e. |
|
195 int up; |
|
196 |
|
197 // Show trailing zeros. |
|
198 int sp; |
|
199 }; |
|
200 |
|
201 class |
|
202 pr_formatted_float |
|
203 { |
|
204 public: |
|
205 |
|
206 const float_format& f; |
|
207 |
|
208 double val; |
|
209 |
|
210 pr_formatted_float (const float_format& f_arg, double val_arg) |
|
211 : f (f_arg), val (val_arg) { } |
|
212 }; |
|
213 |
|
214 std::ostream& |
|
215 operator << (std::ostream& os, const pr_formatted_float& pff) |
|
216 { |
3682
|
217 if (pff.f.fw >= 0) |
3608
|
218 os << std::setw (pff.f.fw); |
|
219 |
3682
|
220 if (pff.f.prec >= 0) |
3608
|
221 os << std::setprecision (pff.f.prec); |
|
222 |
3775
|
223 std::ios::fmtflags oflags = |
|
224 os.flags (static_cast<std::ios::fmtflags> |
|
225 (pff.f.fmt | pff.f.up | pff.f.sp)); |
3608
|
226 |
|
227 os << pff.val; |
|
228 |
|
229 os.flags (oflags); |
|
230 |
|
231 return os; |
|
232 } |
|
233 |
6788
|
234 static inline std::string |
|
235 rational_approx (double val, int len) |
|
236 { |
|
237 std::string s; |
|
238 |
|
239 if (len <= 0) |
|
240 len = 10; |
|
241 |
|
242 if (xisinf (val)) |
|
243 s = "1/0"; |
|
244 else if (xisnan (val)) |
|
245 s = "0/0"; |
|
246 else if (val < INT_MIN || val > INT_MAX || D_NINT (val) == val) |
|
247 { |
|
248 std::ostringstream buf; |
|
249 buf.flags (std::ios::fixed); |
|
250 buf << std::setprecision (0) << xround(val); |
|
251 s = buf.str (); |
|
252 } |
|
253 else |
|
254 { |
|
255 double lastn = 1.; |
|
256 double lastd = 0.; |
|
257 double n = xround (val); |
|
258 double d = 1.; |
|
259 double frac = val - n; |
|
260 int m = 0; |
|
261 |
|
262 std::ostringstream buf2; |
|
263 buf2.flags (std::ios::fixed); |
|
264 buf2 << std::setprecision (0) << static_cast<int>(n); |
|
265 s = buf2.str(); |
|
266 |
|
267 while (1) |
|
268 { |
|
269 double flip = 1. / frac; |
|
270 double step = xround (flip); |
|
271 double nextn = n; |
|
272 double nextd = d; |
|
273 frac = flip - step; |
|
274 n = n * step + lastn; |
|
275 d = d * step + lastd; |
|
276 lastn = nextn; |
|
277 lastd = nextd; |
|
278 |
|
279 std::ostringstream buf; |
|
280 buf.flags (std::ios::fixed); |
|
281 buf << std::setprecision (0) << static_cast<int>(n) |
|
282 << "/" << static_cast<int>(d); |
|
283 m++; |
|
284 |
|
285 if (n < 0 && d < 0) |
|
286 { |
|
287 // Double negative, string can be two characters longer.. |
|
288 if (buf.str().length() > static_cast<unsigned int>(len + 2) && |
|
289 m > 1) |
|
290 break; |
|
291 } |
|
292 else if (buf.str().length() > static_cast<unsigned int>(len) && |
|
293 m > 1) |
|
294 break; |
|
295 |
|
296 s = buf.str(); |
|
297 |
|
298 // Have we converged to 1/intmax ? |
|
299 if (m > 100 || fabs (frac) < 1 / static_cast<double>(INT_MAX)) |
|
300 { |
|
301 lastn = n; |
|
302 lastd = d; |
|
303 break; |
|
304 } |
|
305 } |
|
306 |
|
307 if (lastd < 0.) |
|
308 { |
|
309 // Move sign to the top |
|
310 lastd = - lastd; |
|
311 lastn = - lastn; |
|
312 std::ostringstream buf; |
|
313 buf.flags (std::ios::fixed); |
|
314 buf << std::setprecision (0) << static_cast<int>(lastn) |
|
315 << "/" << static_cast<int>(lastd); |
|
316 s = buf.str(); |
|
317 } |
|
318 } |
|
319 |
|
320 return s; |
|
321 } |
|
322 |
|
323 class |
|
324 pr_rational_float |
|
325 { |
|
326 public: |
|
327 |
|
328 const float_format& f; |
|
329 |
|
330 double val; |
|
331 |
|
332 pr_rational_float (const float_format& f_arg, double val_arg) |
|
333 : f (f_arg), val (val_arg) { } |
|
334 }; |
|
335 |
|
336 std::ostream& |
|
337 operator << (std::ostream& os, const pr_rational_float& prf) |
|
338 { |
|
339 int fw = (rat_string_len > 0 ? rat_string_len : prf.f.fw); |
|
340 std::string s = rational_approx (prf.val, fw); |
|
341 |
|
342 if (fw >= 0) |
|
343 os << std::setw (fw); |
|
344 |
|
345 std::ios::fmtflags oflags = |
|
346 os.flags (static_cast<std::ios::fmtflags> |
|
347 (prf.f.fmt | prf.f.up | prf.f.sp)); |
|
348 |
|
349 if (fw > 0 && s.length() > static_cast<unsigned int>(fw)) |
|
350 os << "*"; |
|
351 else |
|
352 os << s; |
|
353 |
|
354 os.flags (oflags); |
|
355 |
|
356 return os; |
|
357 } |
|
358 |
3608
|
359 // Current format for real numbers and the real part of complex |
|
360 // numbers. |
|
361 static float_format *curr_real_fmt = 0; |
|
362 |
|
363 // Current format for the imaginary part of complex numbers. |
|
364 static float_format *curr_imag_fmt = 0; |
1309
|
365 |
1
|
366 static double |
164
|
367 pr_max_internal (const Matrix& m) |
1
|
368 { |
5275
|
369 octave_idx_type nr = m.rows (); |
|
370 octave_idx_type nc = m.columns (); |
1
|
371 |
3130
|
372 double result = -DBL_MAX; |
1
|
373 |
5748
|
374 bool all_inf_or_nan = true; |
|
375 |
5275
|
376 for (octave_idx_type j = 0; j < nc; j++) |
|
377 for (octave_idx_type i = 0; i < nr; i++) |
1
|
378 { |
3608
|
379 double val = m(i,j); |
5388
|
380 if (xisinf (val) || xisnan (val)) |
1
|
381 continue; |
|
382 |
5748
|
383 all_inf_or_nan = false; |
|
384 |
1
|
385 if (val > result) |
|
386 result = val; |
|
387 } |
3608
|
388 |
5748
|
389 if (all_inf_or_nan) |
|
390 result = 0.0; |
|
391 |
1
|
392 return result; |
|
393 } |
|
394 |
|
395 static double |
164
|
396 pr_min_internal (const Matrix& m) |
1
|
397 { |
5275
|
398 octave_idx_type nr = m.rows (); |
|
399 octave_idx_type nc = m.columns (); |
1
|
400 |
|
401 double result = DBL_MAX; |
|
402 |
5748
|
403 bool all_inf_or_nan = true; |
|
404 |
5275
|
405 for (octave_idx_type j = 0; j < nc; j++) |
|
406 for (octave_idx_type i = 0; i < nr; i++) |
1
|
407 { |
3608
|
408 double val = m(i,j); |
5389
|
409 if (xisinf (val) || xisnan (val)) |
1
|
410 continue; |
|
411 |
5748
|
412 all_inf_or_nan = false; |
|
413 |
1
|
414 if (val < result) |
|
415 result = val; |
|
416 } |
3608
|
417 |
5748
|
418 if (all_inf_or_nan) |
|
419 result = 0.0; |
|
420 |
1
|
421 return result; |
|
422 } |
|
423 |
5775
|
424 // FIXME -- it would be nice to share more code among these |
1658
|
425 // functions,.. |
|
426 |
1
|
427 static void |
6959
|
428 set_real_format (int digits, bool inf_or_nan, bool int_only, int &fw) |
1
|
429 { |
3608
|
430 static float_format fmt; |
1
|
431 |
2165
|
432 int prec = Voutput_precision; |
1
|
433 |
|
434 int ld, rd; |
|
435 |
6788
|
436 if (rat_format) |
|
437 { |
|
438 fw = 0; |
|
439 rd = 0; |
|
440 } |
|
441 else if (bank_format) |
1
|
442 { |
|
443 fw = digits < 0 ? 4 : digits + 3; |
5748
|
444 if (inf_or_nan && fw < 4) |
|
445 fw = 4; |
1
|
446 rd = 2; |
|
447 } |
1282
|
448 else if (hex_format) |
|
449 { |
|
450 fw = 2 * sizeof (double); |
|
451 rd = 0; |
|
452 } |
1309
|
453 else if (bit_format) |
|
454 { |
|
455 fw = 8 * sizeof (double); |
|
456 rd = 0; |
|
457 } |
3611
|
458 else if (inf_or_nan || int_only) |
1
|
459 { |
5832
|
460 fw = 1 + digits; |
5748
|
461 if (inf_or_nan && fw < 4) |
|
462 fw = 4; |
3682
|
463 rd = fw; |
1
|
464 } |
|
465 else |
|
466 { |
|
467 if (digits > 0) |
|
468 { |
|
469 ld = digits; |
1658
|
470 rd = prec > digits ? prec - digits : prec; |
1
|
471 digits++; |
|
472 } |
|
473 else |
|
474 { |
|
475 ld = 1; |
1658
|
476 rd = prec > digits ? prec - digits : prec; |
1
|
477 digits = -digits + 1; |
|
478 } |
|
479 |
5832
|
480 fw = 1 + ld + 1 + rd; |
5748
|
481 if (inf_or_nan && fw < 4) |
|
482 fw = 4; |
1
|
483 } |
|
484 |
6788
|
485 if (! (rat_format || bank_format || hex_format || bit_format) |
4509
|
486 && (fw > Voutput_max_field_width || print_e || print_g)) |
1
|
487 { |
4509
|
488 if (print_g) |
|
489 fmt = float_format (); |
|
490 else |
|
491 { |
|
492 int exp_field = 4; |
|
493 if (digits > 100) |
|
494 exp_field++; |
1
|
495 |
4509
|
496 fw = 2 + prec + exp_field; |
5748
|
497 if (inf_or_nan && fw < 4) |
|
498 fw = 4; |
1
|
499 |
4509
|
500 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
501 } |
3608
|
502 |
1
|
503 if (print_big_e) |
3608
|
504 fmt.uppercase (); |
1
|
505 } |
5086
|
506 else if (! bank_format && (inf_or_nan || int_only)) |
3611
|
507 fmt = float_format (fw, rd); |
1
|
508 else |
3608
|
509 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
510 |
3608
|
511 curr_real_fmt = &fmt; |
1
|
512 } |
|
513 |
1658
|
514 static void |
|
515 set_format (double d, int& fw) |
|
516 { |
|
517 curr_real_fmt = 0; |
|
518 curr_imag_fmt = 0; |
|
519 |
|
520 if (free_format) |
|
521 return; |
|
522 |
5389
|
523 bool inf_or_nan = (xisinf (d) || xisnan (d)); |
1658
|
524 |
3611
|
525 bool int_only = (! inf_or_nan && D_NINT (d) == d); |
1658
|
526 |
|
527 double d_abs = d < 0.0 ? -d : d; |
|
528 |
2800
|
529 int digits = (inf_or_nan || d_abs == 0.0) |
|
530 ? 0 : static_cast<int> (floor (log10 (d_abs) + 1.0)); |
1658
|
531 |
6959
|
532 set_real_format (digits, inf_or_nan, int_only, fw); |
1658
|
533 } |
|
534 |
1
|
535 static inline void |
|
536 set_format (double d) |
|
537 { |
|
538 int fw; |
|
539 set_format (d, fw); |
|
540 } |
|
541 |
|
542 static void |
6959
|
543 set_real_matrix_format (int x_max, int x_min, bool inf_or_nan, |
|
544 int int_or_inf_or_nan, int& fw) |
1
|
545 { |
3608
|
546 static float_format fmt; |
1
|
547 |
2165
|
548 int prec = Voutput_precision; |
1
|
549 |
|
550 int ld, rd; |
|
551 |
6788
|
552 if (rat_format) |
|
553 { |
|
554 fw = 9; |
|
555 rd = 0; |
|
556 } |
|
557 else if (bank_format) |
1
|
558 { |
|
559 int digits = x_max > x_min ? x_max : x_min; |
|
560 fw = digits <= 0 ? 4 : digits + 3; |
5748
|
561 if (inf_or_nan && fw < 4) |
|
562 fw = 4; |
1
|
563 rd = 2; |
|
564 } |
1282
|
565 else if (hex_format) |
|
566 { |
|
567 fw = 2 * sizeof (double); |
|
568 rd = 0; |
|
569 } |
1309
|
570 else if (bit_format) |
|
571 { |
|
572 fw = 8 * sizeof (double); |
|
573 rd = 0; |
|
574 } |
4509
|
575 else if (Vfixed_point_format && ! print_g) |
3268
|
576 { |
|
577 rd = prec; |
|
578 fw = rd + 2; |
5748
|
579 if (inf_or_nan && fw < 4) |
|
580 fw = 4; |
3268
|
581 } |
1715
|
582 else if (int_or_inf_or_nan) |
1
|
583 { |
|
584 int digits = x_max > x_min ? x_max : x_min; |
5945
|
585 fw = digits <= 0 ? 2 : digits + 1; |
5748
|
586 if (inf_or_nan && fw < 4) |
|
587 fw = 4; |
3682
|
588 rd = fw; |
1
|
589 } |
|
590 else |
|
591 { |
|
592 int ld_max, rd_max; |
|
593 if (x_max > 0) |
|
594 { |
|
595 ld_max = x_max; |
1658
|
596 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
597 x_max++; |
|
598 } |
|
599 else |
|
600 { |
|
601 ld_max = 1; |
1658
|
602 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
603 x_max = -x_max + 1; |
|
604 } |
|
605 |
|
606 int ld_min, rd_min; |
|
607 if (x_min > 0) |
|
608 { |
|
609 ld_min = x_min; |
1658
|
610 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
611 x_min++; |
|
612 } |
|
613 else |
|
614 { |
|
615 ld_min = 1; |
1658
|
616 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
617 x_min = -x_min + 1; |
|
618 } |
|
619 |
|
620 ld = ld_max > ld_min ? ld_max : ld_min; |
|
621 rd = rd_max > rd_min ? rd_max : rd_min; |
|
622 |
5832
|
623 fw = 1 + ld + 1 + rd; |
5748
|
624 if (inf_or_nan && fw < 4) |
|
625 fw = 4; |
1
|
626 } |
|
627 |
6788
|
628 if (! (rat_format || bank_format || hex_format || bit_format) |
3105
|
629 && (print_e |
4509
|
630 || print_g |
3105
|
631 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
632 { |
4509
|
633 if (print_g) |
|
634 fmt = float_format (); |
|
635 else |
|
636 { |
|
637 int exp_field = 4; |
|
638 if (x_max > 100 || x_min > 100) |
|
639 exp_field++; |
1
|
640 |
4509
|
641 fw = 2 + prec + exp_field; |
5748
|
642 if (inf_or_nan && fw < 4) |
|
643 fw = 4; |
1
|
644 |
4509
|
645 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
646 } |
3608
|
647 |
1
|
648 if (print_big_e) |
3608
|
649 fmt.uppercase (); |
1
|
650 } |
5086
|
651 else if (! bank_format && int_or_inf_or_nan) |
3611
|
652 fmt = float_format (fw, rd); |
1
|
653 else |
3608
|
654 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
655 |
3608
|
656 curr_real_fmt = &fmt; |
1
|
657 } |
|
658 |
1658
|
659 static void |
3105
|
660 set_format (const Matrix& m, int& fw, double& scale) |
1658
|
661 { |
|
662 curr_real_fmt = 0; |
|
663 curr_imag_fmt = 0; |
|
664 |
|
665 if (free_format) |
|
666 return; |
|
667 |
2387
|
668 bool inf_or_nan = m.any_element_is_inf_or_nan (); |
1658
|
669 |
2387
|
670 bool int_or_inf_or_nan = m.all_elements_are_int_or_inf_or_nan (); |
1658
|
671 |
2387
|
672 Matrix m_abs = m.abs (); |
1658
|
673 double max_abs = pr_max_internal (m_abs); |
|
674 double min_abs = pr_min_internal (m_abs); |
|
675 |
2800
|
676 int x_max = max_abs == 0.0 |
|
677 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
678 |
|
679 int x_min = min_abs == 0.0 |
|
680 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
681 |
4270
|
682 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
683 |
6959
|
684 set_real_matrix_format (x_max, x_min, inf_or_nan, int_or_inf_or_nan, fw); |
1658
|
685 } |
|
686 |
1
|
687 static inline void |
164
|
688 set_format (const Matrix& m) |
1
|
689 { |
|
690 int fw; |
3105
|
691 double scale; |
|
692 set_format (m, fw, scale); |
1
|
693 } |
|
694 |
|
695 static void |
6959
|
696 set_complex_format (int x_max, int x_min, int r_x, bool inf_or_nan, |
|
697 int int_only, int& r_fw, int& i_fw) |
1
|
698 { |
3608
|
699 static float_format r_fmt; |
|
700 static float_format i_fmt; |
1
|
701 |
2165
|
702 int prec = Voutput_precision; |
1
|
703 |
|
704 int ld, rd; |
|
705 |
6788
|
706 if (rat_format) |
|
707 { |
|
708 i_fw = 0; |
|
709 r_fw = 0; |
|
710 rd = 0; |
|
711 } |
|
712 else if (bank_format) |
1
|
713 { |
|
714 int digits = r_x; |
|
715 i_fw = 0; |
|
716 r_fw = digits <= 0 ? 4 : digits + 3; |
5748
|
717 if (inf_or_nan && r_fw < 4) |
|
718 r_fw = 4; |
1
|
719 rd = 2; |
|
720 } |
1282
|
721 else if (hex_format) |
|
722 { |
|
723 r_fw = 2 * sizeof (double); |
|
724 i_fw = 2 * sizeof (double); |
|
725 rd = 0; |
|
726 } |
1309
|
727 else if (bit_format) |
|
728 { |
|
729 r_fw = 8 * sizeof (double); |
|
730 i_fw = 8 * sizeof (double); |
|
731 rd = 0; |
|
732 } |
1658
|
733 else if (inf_or_nan || int_only) |
1
|
734 { |
|
735 int digits = x_max > x_min ? x_max : x_min; |
5945
|
736 i_fw = digits <= 0 ? 1 : digits; |
|
737 r_fw = i_fw + 1; |
|
738 if (inf_or_nan && i_fw < 3) |
|
739 { |
|
740 i_fw = 3; |
|
741 r_fw = 4; |
|
742 } |
3682
|
743 rd = r_fw; |
1
|
744 } |
|
745 else |
|
746 { |
|
747 int ld_max, rd_max; |
|
748 if (x_max > 0) |
|
749 { |
|
750 ld_max = x_max; |
1658
|
751 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
752 x_max++; |
|
753 } |
|
754 else |
|
755 { |
|
756 ld_max = 1; |
1658
|
757 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
758 x_max = -x_max + 1; |
|
759 } |
|
760 |
|
761 int ld_min, rd_min; |
|
762 if (x_min > 0) |
|
763 { |
|
764 ld_min = x_min; |
1658
|
765 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
766 x_min++; |
|
767 } |
|
768 else |
|
769 { |
|
770 ld_min = 1; |
1658
|
771 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
772 x_min = -x_min + 1; |
|
773 } |
|
774 |
|
775 ld = ld_max > ld_min ? ld_max : ld_min; |
|
776 rd = rd_max > rd_min ? rd_max : rd_min; |
|
777 |
5945
|
778 i_fw = ld + 1 + rd; |
|
779 r_fw = i_fw + 1; |
|
780 if (inf_or_nan && i_fw < 3) |
|
781 { |
|
782 i_fw = 3; |
|
783 r_fw = 4; |
|
784 } |
1
|
785 } |
|
786 |
6788
|
787 if (! (rat_format || bank_format || hex_format || bit_format) |
4509
|
788 && (r_fw > Voutput_max_field_width || print_e || print_g)) |
1
|
789 { |
4509
|
790 if (print_g) |
|
791 { |
|
792 r_fmt = float_format (); |
|
793 i_fmt = float_format (); |
|
794 } |
|
795 else |
|
796 { |
|
797 int exp_field = 4; |
|
798 if (x_max > 100 || x_min > 100) |
|
799 exp_field++; |
1
|
800 |
5945
|
801 i_fw = prec + exp_field; |
|
802 r_fw = i_fw + 1; |
|
803 if (inf_or_nan && i_fw < 3) |
|
804 { |
|
805 i_fw = 3; |
|
806 r_fw = 4; |
|
807 } |
1
|
808 |
4509
|
809 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
810 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
811 } |
3608
|
812 |
1
|
813 if (print_big_e) |
|
814 { |
3608
|
815 r_fmt.uppercase (); |
|
816 i_fmt.uppercase (); |
1
|
817 } |
|
818 } |
5086
|
819 else if (! bank_format && (inf_or_nan || int_only)) |
3611
|
820 { |
|
821 r_fmt = float_format (r_fw, rd); |
|
822 i_fmt = float_format (i_fw, rd); |
|
823 } |
1
|
824 else |
|
825 { |
3608
|
826 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
827 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
828 } |
|
829 |
3608
|
830 curr_real_fmt = &r_fmt; |
|
831 curr_imag_fmt = &i_fmt; |
1
|
832 } |
|
833 |
1658
|
834 static void |
|
835 set_format (const Complex& c, int& r_fw, int& i_fw) |
|
836 { |
|
837 curr_real_fmt = 0; |
|
838 curr_imag_fmt = 0; |
|
839 |
|
840 if (free_format) |
|
841 return; |
|
842 |
|
843 double rp = c.real (); |
|
844 double ip = c.imag (); |
|
845 |
2387
|
846 bool inf_or_nan = (xisinf (c) || xisnan (c)); |
1658
|
847 |
2387
|
848 bool int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip); |
1658
|
849 |
|
850 double r_abs = rp < 0.0 ? -rp : rp; |
|
851 double i_abs = ip < 0.0 ? -ip : ip; |
|
852 |
2800
|
853 int r_x = r_abs == 0.0 |
|
854 ? 0 : static_cast<int> (floor (log10 (r_abs) + 1.0)); |
|
855 |
|
856 int i_x = i_abs == 0.0 |
|
857 ? 0 : static_cast<int> (floor (log10 (i_abs) + 1.0)); |
1658
|
858 |
|
859 int x_max, x_min; |
|
860 |
|
861 if (r_x > i_x) |
|
862 { |
|
863 x_max = r_x; |
|
864 x_min = i_x; |
|
865 } |
|
866 else |
|
867 { |
|
868 x_max = i_x; |
|
869 x_min = r_x; |
|
870 } |
|
871 |
6959
|
872 set_complex_format (x_max, x_min, r_x, inf_or_nan, int_only, r_fw, i_fw); |
1658
|
873 } |
|
874 |
1
|
875 static inline void |
164
|
876 set_format (const Complex& c) |
1
|
877 { |
|
878 int r_fw, i_fw; |
|
879 set_format (c, r_fw, i_fw); |
|
880 } |
|
881 |
|
882 static void |
6959
|
883 set_complex_matrix_format (int x_max, int x_min, int r_x_max, |
|
884 int r_x_min, bool inf_or_nan, |
1715
|
885 int int_or_inf_or_nan, int& r_fw, int& i_fw) |
1
|
886 { |
3608
|
887 static float_format r_fmt; |
|
888 static float_format i_fmt; |
1
|
889 |
2165
|
890 int prec = Voutput_precision; |
1
|
891 |
|
892 int ld, rd; |
|
893 |
6788
|
894 if (rat_format) |
|
895 { |
|
896 i_fw = 9; |
|
897 r_fw = 9; |
|
898 rd = 0; |
|
899 } |
|
900 else if (bank_format) |
1
|
901 { |
|
902 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
903 i_fw = 0; |
|
904 r_fw = digits <= 0 ? 4 : digits + 3; |
5945
|
905 if (inf_or_nan && r_fw < 4) |
|
906 r_fw = 4; |
1
|
907 rd = 2; |
|
908 } |
1282
|
909 else if (hex_format) |
|
910 { |
|
911 r_fw = 2 * sizeof (double); |
|
912 i_fw = 2 * sizeof (double); |
|
913 rd = 0; |
|
914 } |
1309
|
915 else if (bit_format) |
|
916 { |
|
917 r_fw = 8 * sizeof (double); |
|
918 i_fw = 8 * sizeof (double); |
|
919 rd = 0; |
|
920 } |
4509
|
921 else if (Vfixed_point_format && ! print_g) |
3268
|
922 { |
|
923 rd = prec; |
5945
|
924 i_fw = rd + 1; |
|
925 r_fw = i_fw + 1; |
|
926 if (inf_or_nan && i_fw < 3) |
|
927 { |
|
928 i_fw = 3; |
|
929 r_fw = 4; |
|
930 } |
3268
|
931 } |
1715
|
932 else if (int_or_inf_or_nan) |
1
|
933 { |
|
934 int digits = x_max > x_min ? x_max : x_min; |
5945
|
935 i_fw = digits <= 0 ? 1 : digits; |
|
936 r_fw = i_fw + 1; |
|
937 if (inf_or_nan && i_fw < 3) |
|
938 { |
|
939 i_fw = 3; |
|
940 r_fw = 4; |
|
941 } |
3682
|
942 rd = r_fw; |
1
|
943 } |
|
944 else |
|
945 { |
|
946 int ld_max, rd_max; |
|
947 if (x_max > 0) |
|
948 { |
|
949 ld_max = x_max; |
1658
|
950 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
951 x_max++; |
|
952 } |
|
953 else |
|
954 { |
|
955 ld_max = 1; |
1658
|
956 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
957 x_max = -x_max + 1; |
|
958 } |
|
959 |
|
960 int ld_min, rd_min; |
|
961 if (x_min > 0) |
|
962 { |
|
963 ld_min = x_min; |
1658
|
964 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
965 x_min++; |
|
966 } |
|
967 else |
|
968 { |
|
969 ld_min = 1; |
1658
|
970 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
971 x_min = -x_min + 1; |
|
972 } |
|
973 |
|
974 ld = ld_max > ld_min ? ld_max : ld_min; |
|
975 rd = rd_max > rd_min ? rd_max : rd_min; |
|
976 |
5945
|
977 i_fw = ld + 1 + rd; |
|
978 r_fw = i_fw + 1; |
|
979 if (inf_or_nan && i_fw < 3) |
|
980 { |
|
981 i_fw = 3; |
|
982 r_fw = 4; |
|
983 } |
1
|
984 } |
|
985 |
6788
|
986 if (! (rat_format || bank_format || hex_format || bit_format) |
3105
|
987 && (print_e |
4509
|
988 || print_g |
3105
|
989 || (! Vfixed_point_format && r_fw > Voutput_max_field_width))) |
1
|
990 { |
4509
|
991 if (print_g) |
|
992 { |
|
993 r_fmt = float_format (); |
|
994 i_fmt = float_format (); |
|
995 } |
|
996 else |
|
997 { |
|
998 int exp_field = 4; |
|
999 if (x_max > 100 || x_min > 100) |
|
1000 exp_field++; |
1
|
1001 |
5945
|
1002 i_fw = prec + exp_field; |
|
1003 r_fw = i_fw + 1; |
|
1004 if (inf_or_nan && i_fw < 3) |
|
1005 { |
|
1006 i_fw = 3; |
|
1007 r_fw = 4; |
|
1008 } |
1
|
1009 |
4509
|
1010 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
1011 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
1012 } |
3608
|
1013 |
1
|
1014 if (print_big_e) |
|
1015 { |
3608
|
1016 r_fmt.uppercase (); |
|
1017 i_fmt.uppercase (); |
1
|
1018 } |
|
1019 } |
5086
|
1020 else if (! bank_format && int_or_inf_or_nan) |
3611
|
1021 { |
|
1022 r_fmt = float_format (r_fw, rd); |
|
1023 i_fmt = float_format (i_fw, rd); |
|
1024 } |
1
|
1025 else |
|
1026 { |
3608
|
1027 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
1028 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
1029 } |
|
1030 |
3608
|
1031 curr_real_fmt = &r_fmt; |
|
1032 curr_imag_fmt = &i_fmt; |
1
|
1033 } |
|
1034 |
1658
|
1035 static void |
3105
|
1036 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw, double& scale) |
1658
|
1037 { |
|
1038 curr_real_fmt = 0; |
|
1039 curr_imag_fmt = 0; |
|
1040 |
|
1041 if (free_format) |
|
1042 return; |
|
1043 |
|
1044 Matrix rp = real (cm); |
|
1045 Matrix ip = imag (cm); |
|
1046 |
2387
|
1047 bool inf_or_nan = cm.any_element_is_inf_or_nan (); |
1658
|
1048 |
2387
|
1049 bool int_or_inf_or_nan = (rp.all_elements_are_int_or_inf_or_nan () |
|
1050 && ip.all_elements_are_int_or_inf_or_nan ()); |
1658
|
1051 |
2387
|
1052 Matrix r_m_abs = rp.abs (); |
1658
|
1053 double r_max_abs = pr_max_internal (r_m_abs); |
|
1054 double r_min_abs = pr_min_internal (r_m_abs); |
|
1055 |
2387
|
1056 Matrix i_m_abs = ip.abs (); |
1658
|
1057 double i_max_abs = pr_max_internal (i_m_abs); |
|
1058 double i_min_abs = pr_min_internal (i_m_abs); |
|
1059 |
2800
|
1060 int r_x_max = r_max_abs == 0.0 |
|
1061 ? 0 : static_cast<int> (floor (log10 (r_max_abs) + 1.0)); |
|
1062 |
|
1063 int r_x_min = r_min_abs == 0.0 |
|
1064 ? 0 : static_cast<int> (floor (log10 (r_min_abs) + 1.0)); |
1658
|
1065 |
2800
|
1066 int i_x_max = i_max_abs == 0.0 |
|
1067 ? 0 : static_cast<int> (floor (log10 (i_max_abs) + 1.0)); |
|
1068 |
|
1069 int i_x_min = i_min_abs == 0.0 |
|
1070 ? 0 : static_cast<int> (floor (log10 (i_min_abs) + 1.0)); |
1658
|
1071 |
|
1072 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
1073 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
1074 |
4270
|
1075 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
1076 |
6959
|
1077 set_complex_matrix_format (x_max, x_min, r_x_max, r_x_min, inf_or_nan, |
|
1078 int_or_inf_or_nan, r_fw, i_fw); |
1658
|
1079 } |
|
1080 |
1
|
1081 static inline void |
164
|
1082 set_format (const ComplexMatrix& cm) |
1
|
1083 { |
|
1084 int r_fw, i_fw; |
3105
|
1085 double scale; |
|
1086 set_format (cm, r_fw, i_fw, scale); |
1
|
1087 } |
|
1088 |
|
1089 static void |
3608
|
1090 set_range_format (bool sign, int x_max, int x_min, int all_ints, int& fw) |
1
|
1091 { |
3608
|
1092 static float_format fmt; |
1
|
1093 |
2165
|
1094 int prec = Voutput_precision; |
1
|
1095 |
|
1096 int ld, rd; |
|
1097 |
6788
|
1098 if (rat_format) |
|
1099 { |
|
1100 fw = 9; |
|
1101 rd = 0; |
|
1102 } |
|
1103 else if (bank_format) |
1
|
1104 { |
|
1105 int digits = x_max > x_min ? x_max : x_min; |
|
1106 fw = sign + digits < 0 ? 4 : digits + 3; |
|
1107 rd = 2; |
|
1108 } |
1282
|
1109 else if (hex_format) |
|
1110 { |
|
1111 fw = 2 * sizeof (double); |
|
1112 rd = 0; |
|
1113 } |
1309
|
1114 else if (bit_format) |
|
1115 { |
|
1116 fw = 8 * sizeof (double); |
|
1117 rd = 0; |
|
1118 } |
1658
|
1119 else if (all_ints) |
1
|
1120 { |
|
1121 int digits = x_max > x_min ? x_max : x_min; |
|
1122 fw = sign + digits; |
3682
|
1123 rd = fw; |
1
|
1124 } |
4509
|
1125 else if (Vfixed_point_format && ! print_g) |
3105
|
1126 { |
|
1127 rd = prec; |
|
1128 fw = rd + 2 + sign; |
|
1129 } |
1
|
1130 else |
|
1131 { |
|
1132 int ld_max, rd_max; |
|
1133 if (x_max > 0) |
|
1134 { |
|
1135 ld_max = x_max; |
1658
|
1136 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
1137 x_max++; |
|
1138 } |
|
1139 else |
|
1140 { |
|
1141 ld_max = 1; |
1658
|
1142 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
1143 x_max = -x_max + 1; |
|
1144 } |
|
1145 |
|
1146 int ld_min, rd_min; |
|
1147 if (x_min > 0) |
|
1148 { |
|
1149 ld_min = x_min; |
1658
|
1150 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
1151 x_min++; |
|
1152 } |
|
1153 else |
|
1154 { |
|
1155 ld_min = 1; |
1658
|
1156 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
1157 x_min = -x_min + 1; |
|
1158 } |
|
1159 |
|
1160 ld = ld_max > ld_min ? ld_max : ld_min; |
|
1161 rd = rd_max > rd_min ? rd_max : rd_min; |
|
1162 |
5832
|
1163 fw = sign + 1 + ld + 1 + rd; |
1
|
1164 } |
|
1165 |
6788
|
1166 if (! (rat_format || bank_format || hex_format || bit_format) |
3105
|
1167 && (print_e |
4509
|
1168 || print_g |
3105
|
1169 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
1170 { |
4509
|
1171 if (print_g) |
|
1172 fmt = float_format (); |
|
1173 else |
|
1174 { |
|
1175 int exp_field = 4; |
|
1176 if (x_max > 100 || x_min > 100) |
|
1177 exp_field++; |
1
|
1178 |
4509
|
1179 fw = sign + 2 + prec + exp_field; |
1
|
1180 |
4509
|
1181 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
1182 } |
3608
|
1183 |
1
|
1184 if (print_big_e) |
3608
|
1185 fmt.uppercase (); |
1
|
1186 } |
5086
|
1187 else if (! bank_format && all_ints) |
3611
|
1188 fmt = float_format (fw, rd); |
1
|
1189 else |
3608
|
1190 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
1191 |
3608
|
1192 curr_real_fmt = &fmt; |
1
|
1193 } |
|
1194 |
1658
|
1195 static void |
3105
|
1196 set_format (const Range& r, int& fw, double& scale) |
1658
|
1197 { |
|
1198 curr_real_fmt = 0; |
|
1199 curr_imag_fmt = 0; |
|
1200 |
|
1201 if (free_format) |
|
1202 return; |
|
1203 |
|
1204 double r_min = r.base (); |
|
1205 double r_max = r.limit (); |
|
1206 |
|
1207 if (r_max < r_min) |
|
1208 { |
|
1209 double tmp = r_max; |
|
1210 r_max = r_min; |
|
1211 r_min = tmp; |
|
1212 } |
|
1213 |
2387
|
1214 bool sign = (r_min < 0.0); |
1658
|
1215 |
2387
|
1216 bool all_ints = r.all_elements_are_ints (); |
1658
|
1217 |
|
1218 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
1219 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
1220 |
2800
|
1221 int x_max = max_abs == 0.0 |
|
1222 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
1223 |
|
1224 int x_min = min_abs == 0.0 |
|
1225 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
1226 |
4270
|
1227 scale = (x_max == 0 || all_ints) ? 1.0 : std::pow (10.0, x_max - 1); |
3105
|
1228 |
1658
|
1229 set_range_format (sign, x_max, x_min, all_ints, fw); |
|
1230 } |
|
1231 |
1
|
1232 static inline void |
164
|
1233 set_format (const Range& r) |
1
|
1234 { |
|
1235 int fw; |
3105
|
1236 double scale; |
|
1237 set_format (r, fw, scale); |
1
|
1238 } |
|
1239 |
1282
|
1240 union equiv |
|
1241 { |
|
1242 double d; |
|
1243 unsigned char i[sizeof (double)]; |
|
1244 }; |
|
1245 |
1309
|
1246 #define PRINT_CHAR_BITS(os, c) \ |
|
1247 do \ |
|
1248 { \ |
|
1249 unsigned char ctmp = c; \ |
|
1250 char stmp[9]; \ |
1488
|
1251 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \ |
|
1252 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1253 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1254 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1255 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1256 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1257 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1258 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \ |
1309
|
1259 stmp[8] = '\0'; \ |
3013
|
1260 os << stmp; \ |
1309
|
1261 } \ |
|
1262 while (0) |
|
1263 |
|
1264 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
1265 do \ |
|
1266 { \ |
|
1267 unsigned char ctmp = c; \ |
|
1268 char stmp[9]; \ |
1488
|
1269 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \ |
|
1270 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1271 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1272 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1273 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1274 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1275 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1276 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \ |
1309
|
1277 stmp[8] = '\0'; \ |
3013
|
1278 os << stmp; \ |
1309
|
1279 } \ |
|
1280 while (0) |
|
1281 |
2522
|
1282 static void |
3608
|
1283 pr_any_float (const float_format *fmt, std::ostream& os, double d, int fw = 0) |
1
|
1284 { |
529
|
1285 if (fmt) |
1
|
1286 { |
5544
|
1287 // Unless explicitly asked for, always print in big-endian |
|
1288 // format for hex and bit formats. |
|
1289 // |
|
1290 // {bit,hex}_format == 1: print big-endian |
|
1291 // {bit,hex}_format == 2: print native |
|
1292 |
1282
|
1293 if (hex_format) |
|
1294 { |
|
1295 equiv tmp; |
|
1296 tmp.d = d; |
|
1297 |
|
1298 // Unless explicitly asked for, always print in big-endian |
|
1299 // format. |
|
1300 |
5775
|
1301 // FIXME -- is it correct to swap bytes for VAX |
1282
|
1302 // formats and not for Cray? |
|
1303 |
5775
|
1304 // FIXME -- will bad things happen if we are |
3013
|
1305 // interrupted before resetting the format flags and fill |
|
1306 // character? |
|
1307 |
2317
|
1308 oct_mach_info::float_format flt_fmt = |
|
1309 oct_mach_info::native_float_format (); |
|
1310 |
3013
|
1311 char ofill = os.fill ('0'); |
|
1312 |
3608
|
1313 std::ios::fmtflags oflags |
|
1314 = os.flags (std::ios::right | std::ios::hex); |
3013
|
1315 |
1282
|
1316 if (hex_format > 1 |
4574
|
1317 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian |
|
1318 || flt_fmt == oct_mach_info::flt_fmt_cray |
|
1319 || flt_fmt == oct_mach_info::flt_fmt_unknown) |
1282
|
1320 { |
1322
|
1321 for (size_t i = 0; i < sizeof (double); i++) |
3548
|
1322 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1323 } |
|
1324 else |
|
1325 { |
1328
|
1326 for (int i = sizeof (double) - 1; i >= 0; i--) |
3548
|
1327 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1328 } |
3013
|
1329 |
|
1330 os.fill (ofill); |
|
1331 os.setf (oflags); |
1282
|
1332 } |
1309
|
1333 else if (bit_format) |
|
1334 { |
|
1335 equiv tmp; |
|
1336 tmp.d = d; |
|
1337 |
5775
|
1338 // FIXME -- is it correct to swap bytes for VAX |
1309
|
1339 // formats and not for Cray? |
|
1340 |
2317
|
1341 oct_mach_info::float_format flt_fmt = |
|
1342 oct_mach_info::native_float_format (); |
|
1343 |
4574
|
1344 if (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian |
|
1345 || flt_fmt == oct_mach_info::flt_fmt_cray |
|
1346 || flt_fmt == oct_mach_info::flt_fmt_unknown) |
1309
|
1347 { |
1322
|
1348 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1349 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1350 } |
|
1351 else |
|
1352 { |
|
1353 if (bit_format > 1) |
|
1354 { |
1328
|
1355 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1356 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
1357 } |
|
1358 else |
|
1359 { |
|
1360 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
1361 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1362 } |
|
1363 } |
|
1364 } |
6788
|
1365 else if (octave_is_NA (d)) |
|
1366 { |
|
1367 if (fw > 0) |
|
1368 os << std::setw (fw) << "NA"; |
|
1369 else |
|
1370 os << "NA"; |
|
1371 } |
|
1372 else if (rat_format) |
|
1373 os << pr_rational_float (*fmt, d); |
1282
|
1374 else if (xisinf (d)) |
1
|
1375 { |
2804
|
1376 const char *s; |
1
|
1377 if (d < 0.0) |
|
1378 s = "-Inf"; |
|
1379 else |
|
1380 s = "Inf"; |
|
1381 |
|
1382 if (fw > 0) |
3548
|
1383 os << std::setw (fw) << s; |
1
|
1384 else |
|
1385 os << s; |
|
1386 } |
|
1387 else if (xisnan (d)) |
|
1388 { |
|
1389 if (fw > 0) |
3548
|
1390 os << std::setw (fw) << "NaN"; |
1
|
1391 else |
|
1392 os << "NaN"; |
|
1393 } |
|
1394 else |
3608
|
1395 os << pr_formatted_float (*fmt, d); |
1
|
1396 } |
529
|
1397 else |
|
1398 os << d; |
1
|
1399 } |
|
1400 |
|
1401 static inline void |
3608
|
1402 pr_float (std::ostream& os, double d, int fw = 0, double scale = 1.0) |
1
|
1403 { |
4509
|
1404 if (Vfixed_point_format && ! print_g && scale != 1.0) |
3608
|
1405 d /= scale; |
|
1406 |
1
|
1407 pr_any_float (curr_real_fmt, os, d, fw); |
|
1408 } |
|
1409 |
|
1410 static inline void |
3523
|
1411 pr_imag_float (std::ostream& os, double d, int fw = 0) |
1
|
1412 { |
|
1413 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1414 } |
|
1415 |
2522
|
1416 static void |
3608
|
1417 pr_complex (std::ostream& os, const Complex& c, int r_fw = 0, |
|
1418 int i_fw = 0, double scale = 1.0) |
1
|
1419 { |
4509
|
1420 Complex tmp |
|
1421 = (Vfixed_point_format && ! print_g && scale != 1.0) ? c / scale : c; |
3608
|
1422 |
|
1423 double r = tmp.real (); |
|
1424 |
1
|
1425 pr_float (os, r, r_fw); |
3608
|
1426 |
1
|
1427 if (! bank_format) |
|
1428 { |
3608
|
1429 double i = tmp.imag (); |
4349
|
1430 if (! (hex_format || bit_format) && lo_ieee_signbit (i)) |
1
|
1431 { |
|
1432 os << " - "; |
|
1433 i = -i; |
|
1434 pr_imag_float (os, i, i_fw); |
|
1435 } |
|
1436 else |
|
1437 { |
1309
|
1438 if (hex_format || bit_format) |
1282
|
1439 os << " "; |
|
1440 else |
|
1441 os << " + "; |
|
1442 |
1
|
1443 pr_imag_float (os, i, i_fw); |
|
1444 } |
|
1445 os << "i"; |
|
1446 } |
|
1447 } |
|
1448 |
626
|
1449 static void |
5275
|
1450 print_empty_matrix (std::ostream& os, octave_idx_type nr, octave_idx_type nc, bool pr_as_read_syntax) |
626
|
1451 { |
|
1452 assert (nr == 0 || nc == 0); |
|
1453 |
|
1454 if (pr_as_read_syntax) |
|
1455 { |
|
1456 if (nr == 0 && nc == 0) |
|
1457 os << "[]"; |
|
1458 else |
|
1459 os << "zeros (" << nr << ", " << nc << ")"; |
|
1460 } |
|
1461 else |
|
1462 { |
|
1463 os << "[]"; |
4559
|
1464 |
2165
|
1465 if (Vprint_empty_dimensions) |
626
|
1466 os << "(" << nr << "x" << nc << ")"; |
|
1467 } |
|
1468 } |
|
1469 |
1186
|
1470 static void |
4559
|
1471 print_empty_nd_array (std::ostream& os, const dim_vector& dims, |
|
1472 bool pr_as_read_syntax) |
|
1473 { |
|
1474 assert (dims.any_zero ()); |
|
1475 |
|
1476 if (pr_as_read_syntax) |
|
1477 os << "zeros (" << dims.str (',') << ")"; |
|
1478 else |
|
1479 { |
|
1480 os << "[]"; |
|
1481 |
|
1482 if (Vprint_empty_dimensions) |
|
1483 os << "(" << dims.str () << ")"; |
|
1484 } |
|
1485 } |
|
1486 |
|
1487 static void |
3523
|
1488 pr_scale_header (std::ostream& os, double scale) |
3105
|
1489 { |
4509
|
1490 if (Vfixed_point_format && ! print_g && scale != 1.0) |
3105
|
1491 { |
3568
|
1492 os << " " |
|
1493 << std::setw (8) << std::setprecision (1) |
|
1494 << std::setiosflags (std::ios::scientific|std::ios::left) |
|
1495 << scale |
|
1496 << std::resetiosflags (std::ios::scientific|std::ios::left) |
|
1497 << " *\n"; |
3105
|
1498 |
|
1499 if (! compact_format) |
|
1500 os << "\n"; |
|
1501 } |
|
1502 } |
|
1503 |
|
1504 static void |
5275
|
1505 pr_col_num_header (std::ostream& os, octave_idx_type total_width, int max_width, |
|
1506 octave_idx_type lim, octave_idx_type col, int extra_indent) |
1186
|
1507 { |
2165
|
1508 if (total_width > max_width && Vsplit_long_rows) |
1186
|
1509 { |
4833
|
1510 if (col != 0) |
|
1511 { |
|
1512 if (compact_format) |
|
1513 os << "\n"; |
|
1514 else |
|
1515 os << "\n\n"; |
|
1516 } |
1186
|
1517 |
5275
|
1518 octave_idx_type num_cols = lim - col; |
1186
|
1519 |
3548
|
1520 os << std::setw (extra_indent) << ""; |
1972
|
1521 |
1186
|
1522 if (num_cols == 1) |
|
1523 os << " Column " << col + 1 << ":\n"; |
|
1524 else if (num_cols == 2) |
|
1525 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1526 else |
|
1527 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
2915
|
1528 |
|
1529 if (! compact_format) |
|
1530 os << "\n"; |
1186
|
1531 } |
|
1532 } |
|
1533 |
5030
|
1534 template <class T> |
6018
|
1535 /* static */ inline void |
5030
|
1536 pr_plus_format (std::ostream& os, const T& val) |
3248
|
1537 { |
5030
|
1538 if (val > T (0)) |
4632
|
1539 os << plus_format_chars[0]; |
5030
|
1540 else if (val < T (0)) |
4632
|
1541 os << plus_format_chars[1]; |
3248
|
1542 else |
4632
|
1543 os << plus_format_chars[2]; |
3248
|
1544 } |
|
1545 |
1
|
1546 void |
4661
|
1547 octave_print_internal (std::ostream& os, double d, |
|
1548 bool /* pr_as_read_syntax */) |
1
|
1549 { |
|
1550 if (plus_format) |
|
1551 { |
3608
|
1552 pr_plus_format (os, d); |
1
|
1553 } |
|
1554 else |
|
1555 { |
|
1556 set_format (d); |
|
1557 if (free_format) |
|
1558 os << d; |
|
1559 else |
|
1560 pr_float (os, d); |
|
1561 } |
|
1562 } |
|
1563 |
|
1564 void |
3608
|
1565 octave_print_internal (std::ostream& os, const Matrix& m, |
|
1566 bool pr_as_read_syntax, int extra_indent) |
1
|
1567 { |
5275
|
1568 octave_idx_type nr = m.rows (); |
|
1569 octave_idx_type nc = m.columns (); |
1
|
1570 |
2408
|
1571 if (nr == 0 || nc == 0) |
626
|
1572 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1573 else if (plus_format && ! pr_as_read_syntax) |
1
|
1574 { |
5275
|
1575 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1576 { |
5275
|
1577 for (octave_idx_type j = 0; j < nc; j++) |
1
|
1578 { |
4153
|
1579 OCTAVE_QUIT; |
|
1580 |
3608
|
1581 pr_plus_format (os, m(i,j)); |
1
|
1582 } |
2907
|
1583 |
|
1584 if (i < nr - 1) |
|
1585 os << "\n"; |
1
|
1586 } |
|
1587 } |
|
1588 else |
|
1589 { |
|
1590 int fw; |
3105
|
1591 double scale = 1.0; |
|
1592 set_format (m, fw, scale); |
1
|
1593 int column_width = fw + 2; |
5275
|
1594 octave_idx_type total_width = nc * column_width; |
|
1595 octave_idx_type max_width = command_editor::terminal_cols (); |
1
|
1596 |
626
|
1597 if (pr_as_read_syntax) |
|
1598 max_width -= 4; |
1972
|
1599 else |
|
1600 max_width -= extra_indent; |
|
1601 |
|
1602 if (max_width < 0) |
|
1603 max_width = 0; |
626
|
1604 |
1
|
1605 if (free_format) |
|
1606 { |
626
|
1607 if (pr_as_read_syntax) |
|
1608 os << "[\n"; |
|
1609 |
1
|
1610 os << m; |
626
|
1611 |
|
1612 if (pr_as_read_syntax) |
|
1613 os << "]"; |
|
1614 |
1
|
1615 return; |
|
1616 } |
|
1617 |
5275
|
1618 octave_idx_type inc = nc; |
2165
|
1619 if (total_width > max_width && Vsplit_long_rows) |
1
|
1620 { |
|
1621 inc = max_width / column_width; |
|
1622 if (inc == 0) |
|
1623 inc++; |
|
1624 } |
|
1625 |
626
|
1626 if (pr_as_read_syntax) |
1
|
1627 { |
5275
|
1628 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1629 { |
5275
|
1630 octave_idx_type col = 0; |
626
|
1631 while (col < nc) |
1
|
1632 { |
5275
|
1633 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
|
1634 |
|
1635 for (octave_idx_type j = col; j < lim; j++) |
626
|
1636 { |
4153
|
1637 OCTAVE_QUIT; |
|
1638 |
626
|
1639 if (i == 0 && j == 0) |
|
1640 os << "[ "; |
|
1641 else |
|
1642 { |
|
1643 if (j > col && j < lim) |
|
1644 os << ", "; |
|
1645 else |
|
1646 os << " "; |
|
1647 } |
|
1648 |
3608
|
1649 pr_float (os, m(i,j)); |
626
|
1650 } |
|
1651 |
|
1652 col += inc; |
|
1653 |
|
1654 if (col >= nc) |
|
1655 { |
|
1656 if (i == nr - 1) |
|
1657 os << " ]"; |
|
1658 else |
|
1659 os << ";\n"; |
|
1660 } |
|
1661 else |
|
1662 os << " ...\n"; |
1
|
1663 } |
|
1664 } |
626
|
1665 } |
|
1666 else |
|
1667 { |
3105
|
1668 pr_scale_header (os, scale); |
|
1669 |
5275
|
1670 for (octave_idx_type col = 0; col < nc; col += inc) |
626
|
1671 { |
5275
|
1672 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
626
|
1673 |
1972
|
1674 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1675 extra_indent); |
626
|
1676 |
5275
|
1677 for (octave_idx_type i = 0; i < nr; i++) |
626
|
1678 { |
3548
|
1679 os << std::setw (extra_indent) << ""; |
1972
|
1680 |
5275
|
1681 for (octave_idx_type j = col; j < lim; j++) |
626
|
1682 { |
4153
|
1683 OCTAVE_QUIT; |
|
1684 |
626
|
1685 os << " "; |
|
1686 |
3608
|
1687 pr_float (os, m(i,j), fw, scale); |
626
|
1688 } |
|
1689 |
2907
|
1690 if (i < nr - 1) |
|
1691 os << "\n"; |
626
|
1692 } |
|
1693 } |
1
|
1694 } |
|
1695 } |
|
1696 } |
|
1697 |
4532
|
1698 #define PRINT_ND_ARRAY(os, nda, NDA_T, ELT_T, MAT_T) \ |
|
1699 do \ |
|
1700 { \ |
4559
|
1701 if (nda.is_empty ()) \ |
|
1702 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); \ |
|
1703 else \ |
|
1704 { \ |
4532
|
1705 \ |
4559
|
1706 int ndims = nda.ndims (); \ |
|
1707 \ |
|
1708 dim_vector dims = nda.dims (); \ |
4532
|
1709 \ |
5275
|
1710 Array<octave_idx_type> ra_idx (ndims, 0); \ |
4532
|
1711 \ |
5275
|
1712 octave_idx_type m = 1; \ |
4532
|
1713 \ |
4559
|
1714 for (int i = 2; i < ndims; i++) \ |
|
1715 m *= dims(i); \ |
|
1716 \ |
5275
|
1717 octave_idx_type nr = dims(0); \ |
|
1718 octave_idx_type nc = dims(1); \ |
4532
|
1719 \ |
5275
|
1720 for (octave_idx_type i = 0; i < m; i++) \ |
4559
|
1721 { \ |
4655
|
1722 OCTAVE_QUIT; \ |
|
1723 \ |
4559
|
1724 std::string nm = "ans"; \ |
4532
|
1725 \ |
4559
|
1726 if (m > 1) \ |
|
1727 { \ |
|
1728 nm += "(:,:,"; \ |
4532
|
1729 \ |
5765
|
1730 std::ostringstream buf; \ |
4532
|
1731 \ |
4559
|
1732 for (int k = 2; k < ndims; k++) \ |
|
1733 { \ |
|
1734 buf << ra_idx(k) + 1; \ |
4532
|
1735 \ |
4559
|
1736 if (k < ndims - 1) \ |
|
1737 buf << ","; \ |
|
1738 else \ |
|
1739 buf << ")"; \ |
|
1740 } \ |
4532
|
1741 \ |
5765
|
1742 nm += buf.str (); \ |
4559
|
1743 } \ |
4532
|
1744 \ |
4559
|
1745 Array<idx_vector> idx (ndims); \ |
4532
|
1746 \ |
4559
|
1747 idx(0) = idx_vector (':'); \ |
|
1748 idx(1) = idx_vector (':'); \ |
|
1749 \ |
|
1750 for (int k = 2; k < ndims; k++) \ |
|
1751 idx(k) = idx_vector (ra_idx(k) + 1); \ |
4532
|
1752 \ |
4559
|
1753 octave_value page \ |
|
1754 = MAT_T (Array2<ELT_T> (nda.index (idx), nr, nc)); \ |
|
1755 \ |
|
1756 page.print_with_name (os, nm); \ |
4532
|
1757 \ |
4559
|
1758 if (i < m) \ |
|
1759 NDA_T::increment_index (ra_idx, dims, 2); \ |
|
1760 } \ |
|
1761 } \ |
4532
|
1762 } \ |
|
1763 while (0) |
|
1764 |
4513
|
1765 void |
|
1766 octave_print_internal (std::ostream& os, const NDArray& nda, |
|
1767 bool pr_as_read_syntax, int extra_indent) |
|
1768 { |
|
1769 switch (nda.ndims ()) |
|
1770 { |
|
1771 case 1: |
|
1772 case 2: |
|
1773 octave_print_internal (os, nda.matrix_value (), |
|
1774 pr_as_read_syntax, extra_indent); |
|
1775 break; |
|
1776 |
|
1777 default: |
4532
|
1778 PRINT_ND_ARRAY (os, nda, NDArray, double, Matrix); |
4513
|
1779 break; |
|
1780 } |
|
1781 } |
|
1782 |
5030
|
1783 template <> |
6018
|
1784 /* static */ inline void |
6015
|
1785 pr_plus_format<> (std::ostream& os, const Complex& c) |
3248
|
1786 { |
|
1787 double rp = c.real (); |
|
1788 double ip = c.imag (); |
|
1789 |
|
1790 if (rp == 0.0) |
|
1791 { |
|
1792 if (ip == 0.0) |
|
1793 os << " "; |
|
1794 else |
|
1795 os << "i"; |
|
1796 } |
|
1797 else if (ip == 0.0) |
3608
|
1798 pr_plus_format (os, rp); |
3248
|
1799 else |
|
1800 os << "c"; |
|
1801 } |
|
1802 |
1
|
1803 void |
3523
|
1804 octave_print_internal (std::ostream& os, const Complex& c, |
4661
|
1805 bool /* pr_as_read_syntax */) |
1
|
1806 { |
|
1807 if (plus_format) |
|
1808 { |
3608
|
1809 pr_plus_format (os, c); |
1
|
1810 } |
|
1811 else |
|
1812 { |
|
1813 set_format (c); |
|
1814 if (free_format) |
|
1815 os << c; |
|
1816 else |
|
1817 pr_complex (os, c); |
|
1818 } |
|
1819 } |
|
1820 |
|
1821 void |
3523
|
1822 octave_print_internal (std::ostream& os, const ComplexMatrix& cm, |
1972
|
1823 bool pr_as_read_syntax, int extra_indent) |
1
|
1824 { |
5275
|
1825 octave_idx_type nr = cm.rows (); |
|
1826 octave_idx_type nc = cm.columns (); |
1
|
1827 |
2408
|
1828 if (nr == 0 || nc == 0) |
626
|
1829 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1830 else if (plus_format && ! pr_as_read_syntax) |
1
|
1831 { |
5275
|
1832 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1833 { |
5275
|
1834 for (octave_idx_type j = 0; j < nc; j++) |
1
|
1835 { |
4153
|
1836 OCTAVE_QUIT; |
|
1837 |
3608
|
1838 pr_plus_format (os, cm(i,j)); |
1
|
1839 } |
2907
|
1840 |
|
1841 if (i < nr - 1) |
|
1842 os << "\n"; |
1
|
1843 } |
|
1844 } |
|
1845 else |
|
1846 { |
|
1847 int r_fw, i_fw; |
3105
|
1848 double scale = 1.0; |
|
1849 set_format (cm, r_fw, i_fw, scale); |
1
|
1850 int column_width = i_fw + r_fw; |
6788
|
1851 column_width += (rat_format || bank_format || hex_format |
|
1852 || bit_format) ? 2 : 7; |
5275
|
1853 octave_idx_type total_width = nc * column_width; |
|
1854 octave_idx_type max_width = command_editor::terminal_cols (); |
1
|
1855 |
626
|
1856 if (pr_as_read_syntax) |
|
1857 max_width -= 4; |
1972
|
1858 else |
|
1859 max_width -= extra_indent; |
|
1860 |
|
1861 if (max_width < 0) |
|
1862 max_width = 0; |
626
|
1863 |
1
|
1864 if (free_format) |
|
1865 { |
626
|
1866 if (pr_as_read_syntax) |
|
1867 os << "[\n"; |
|
1868 |
1
|
1869 os << cm; |
626
|
1870 |
|
1871 if (pr_as_read_syntax) |
|
1872 os << "]"; |
|
1873 |
1
|
1874 return; |
|
1875 } |
|
1876 |
5275
|
1877 octave_idx_type inc = nc; |
2165
|
1878 if (total_width > max_width && Vsplit_long_rows) |
1
|
1879 { |
|
1880 inc = max_width / column_width; |
|
1881 if (inc == 0) |
|
1882 inc++; |
|
1883 } |
|
1884 |
626
|
1885 if (pr_as_read_syntax) |
1
|
1886 { |
5275
|
1887 for (octave_idx_type i = 0; i < nr; i++) |
1
|
1888 { |
5275
|
1889 octave_idx_type col = 0; |
626
|
1890 while (col < nc) |
1
|
1891 { |
5275
|
1892 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
|
1893 |
|
1894 for (octave_idx_type j = col; j < lim; j++) |
626
|
1895 { |
4153
|
1896 OCTAVE_QUIT; |
|
1897 |
626
|
1898 if (i == 0 && j == 0) |
|
1899 os << "[ "; |
|
1900 else |
|
1901 { |
|
1902 if (j > col && j < lim) |
|
1903 os << ", "; |
|
1904 else |
|
1905 os << " "; |
|
1906 } |
|
1907 |
3608
|
1908 pr_complex (os, cm(i,j)); |
626
|
1909 } |
|
1910 |
|
1911 col += inc; |
|
1912 |
|
1913 if (col >= nc) |
|
1914 { |
|
1915 if (i == nr - 1) |
|
1916 os << " ]"; |
|
1917 else |
|
1918 os << ";\n"; |
|
1919 } |
1
|
1920 else |
626
|
1921 os << " ...\n"; |
1
|
1922 } |
|
1923 } |
626
|
1924 } |
|
1925 else |
|
1926 { |
3105
|
1927 pr_scale_header (os, scale); |
|
1928 |
5275
|
1929 for (octave_idx_type col = 0; col < nc; col += inc) |
626
|
1930 { |
5275
|
1931 octave_idx_type lim = col + inc < nc ? col + inc : nc; |
626
|
1932 |
1972
|
1933 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1934 extra_indent); |
626
|
1935 |
5275
|
1936 for (octave_idx_type i = 0; i < nr; i++) |
626
|
1937 { |
3548
|
1938 os << std::setw (extra_indent) << ""; |
1972
|
1939 |
5275
|
1940 for (octave_idx_type j = col; j < lim; j++) |
626
|
1941 { |
4153
|
1942 OCTAVE_QUIT; |
|
1943 |
626
|
1944 os << " "; |
|
1945 |
3608
|
1946 pr_complex (os, cm(i,j), r_fw, i_fw, scale); |
626
|
1947 } |
2907
|
1948 |
|
1949 if (i < nr - 1) |
|
1950 os << "\n"; |
626
|
1951 } |
|
1952 } |
1
|
1953 } |
|
1954 } |
|
1955 } |
|
1956 |
|
1957 void |
4513
|
1958 octave_print_internal (std::ostream& os, const ComplexNDArray& nda, |
|
1959 bool pr_as_read_syntax, int extra_indent) |
|
1960 { |
|
1961 switch (nda.ndims ()) |
|
1962 { |
|
1963 case 1: |
|
1964 case 2: |
|
1965 octave_print_internal (os, nda.matrix_value (), |
|
1966 pr_as_read_syntax, extra_indent); |
|
1967 break; |
|
1968 |
|
1969 default: |
4532
|
1970 PRINT_ND_ARRAY (os, nda, ComplexNDArray, Complex, ComplexMatrix); |
4513
|
1971 break; |
|
1972 } |
|
1973 } |
|
1974 |
|
1975 void |
3523
|
1976 octave_print_internal (std::ostream& os, const Range& r, |
1972
|
1977 bool pr_as_read_syntax, int extra_indent) |
1
|
1978 { |
626
|
1979 double base = r.base (); |
1
|
1980 double increment = r.inc (); |
626
|
1981 double limit = r.limit (); |
5275
|
1982 octave_idx_type num_elem = r.nelem (); |
1
|
1983 |
626
|
1984 if (plus_format && ! pr_as_read_syntax) |
1
|
1985 { |
5275
|
1986 for (octave_idx_type i = 0; i < num_elem; i++) |
1
|
1987 { |
4153
|
1988 OCTAVE_QUIT; |
|
1989 |
626
|
1990 double val = base + i * increment; |
4632
|
1991 |
|
1992 pr_plus_format (os, val); |
1
|
1993 } |
|
1994 } |
|
1995 else |
|
1996 { |
|
1997 int fw; |
3105
|
1998 double scale = 1.0; |
|
1999 set_format (r, fw, scale); |
1
|
2000 |
626
|
2001 if (pr_as_read_syntax) |
1
|
2002 { |
626
|
2003 if (free_format) |
|
2004 { |
|
2005 os << base << " : "; |
|
2006 if (increment != 1.0) |
|
2007 os << increment << " : "; |
|
2008 os << limit; |
|
2009 } |
|
2010 else |
|
2011 { |
|
2012 pr_float (os, base, fw); |
|
2013 os << " : "; |
|
2014 if (increment != 1.0) |
|
2015 { |
|
2016 pr_float (os, increment, fw); |
|
2017 os << " : "; |
|
2018 } |
|
2019 pr_float (os, limit, fw); |
|
2020 } |
1
|
2021 } |
626
|
2022 else |
|
2023 { |
|
2024 int column_width = fw + 2; |
5275
|
2025 octave_idx_type total_width = num_elem * column_width; |
|
2026 octave_idx_type max_width = command_editor::terminal_cols (); |
1
|
2027 |
626
|
2028 if (free_format) |
|
2029 { |
|
2030 os << r; |
|
2031 return; |
|
2032 } |
1
|
2033 |
5275
|
2034 octave_idx_type inc = num_elem; |
2165
|
2035 if (total_width > max_width && Vsplit_long_rows) |
1
|
2036 { |
626
|
2037 inc = max_width / column_width; |
|
2038 if (inc == 0) |
|
2039 inc++; |
1
|
2040 } |
|
2041 |
1972
|
2042 max_width -= extra_indent; |
|
2043 |
|
2044 if (max_width < 0) |
|
2045 max_width = 0; |
|
2046 |
3105
|
2047 pr_scale_header (os, scale); |
|
2048 |
5275
|
2049 octave_idx_type col = 0; |
626
|
2050 while (col < num_elem) |
1
|
2051 { |
5275
|
2052 octave_idx_type lim = col + inc < num_elem ? col + inc : num_elem; |
626
|
2053 |
1972
|
2054 pr_col_num_header (os, total_width, max_width, lim, col, |
|
2055 extra_indent); |
|
2056 |
3548
|
2057 os << std::setw (extra_indent) << ""; |
626
|
2058 |
5275
|
2059 for (octave_idx_type i = col; i < lim; i++) |
626
|
2060 { |
4153
|
2061 OCTAVE_QUIT; |
|
2062 |
626
|
2063 double val = base + i * increment; |
3105
|
2064 |
4791
|
2065 if (i == num_elem - 1) |
|
2066 { |
|
2067 // See the comments in Range::matrix_value. |
|
2068 |
|
2069 if ((increment > 0 && val > limit) |
|
2070 || (increment < 0 && val < limit)) |
|
2071 val = limit; |
|
2072 } |
|
2073 |
626
|
2074 os << " "; |
3105
|
2075 |
3608
|
2076 pr_float (os, val, fw, scale); |
626
|
2077 } |
|
2078 |
2907
|
2079 col += inc; |
1
|
2080 } |
|
2081 } |
|
2082 } |
|
2083 } |
|
2084 |
1343
|
2085 void |
3523
|
2086 octave_print_internal (std::ostream& os, const boolMatrix& bm, |
3215
|
2087 bool pr_as_read_syntax, |
|
2088 int extra_indent) |
|
2089 { |
|
2090 Matrix tmp (bm); |
|
2091 octave_print_internal (os, tmp, pr_as_read_syntax, extra_indent); |
|
2092 } |
|
2093 |
|
2094 void |
4513
|
2095 octave_print_internal (std::ostream& os, const boolNDArray& nda, |
|
2096 bool pr_as_read_syntax, |
|
2097 int extra_indent) |
|
2098 { |
|
2099 switch (nda.ndims ()) |
|
2100 { |
|
2101 case 1: |
|
2102 case 2: |
|
2103 octave_print_internal (os, nda.matrix_value (), |
|
2104 pr_as_read_syntax, extra_indent); |
|
2105 break; |
|
2106 |
|
2107 default: |
4532
|
2108 PRINT_ND_ARRAY (os, nda, boolNDArray, bool, boolMatrix); |
4513
|
2109 break; |
|
2110 } |
|
2111 } |
|
2112 |
|
2113 void |
3523
|
2114 octave_print_internal (std::ostream& os, const charMatrix& chm, |
3215
|
2115 bool pr_as_read_syntax, |
5775
|
2116 int /* extra_indent FIXME */, |
3215
|
2117 bool pr_as_string) |
1343
|
2118 { |
1572
|
2119 if (pr_as_string) |
|
2120 { |
5275
|
2121 octave_idx_type nstr = chm.rows (); |
1343
|
2122 |
1572
|
2123 if (pr_as_read_syntax && nstr > 1) |
|
2124 os << "[ "; |
1343
|
2125 |
2907
|
2126 if (nstr != 0) |
1343
|
2127 { |
5275
|
2128 for (octave_idx_type i = 0; i < nstr; i++) |
1572
|
2129 { |
4153
|
2130 OCTAVE_QUIT; |
|
2131 |
3523
|
2132 std::string row = chm.row_as_string (i); |
2664
|
2133 |
|
2134 if (pr_as_read_syntax) |
|
2135 { |
|
2136 os << "\"" << undo_string_escapes (row) << "\""; |
1343
|
2137 |
2664
|
2138 if (i < nstr - 1) |
|
2139 os << "; "; |
|
2140 } |
|
2141 else |
2907
|
2142 { |
|
2143 os << row; |
|
2144 |
|
2145 if (i < nstr - 1) |
|
2146 os << "\n"; |
|
2147 } |
1572
|
2148 } |
1343
|
2149 } |
1572
|
2150 |
|
2151 if (pr_as_read_syntax && nstr > 1) |
|
2152 os << " ]"; |
1343
|
2153 } |
1572
|
2154 else |
|
2155 { |
|
2156 os << "sorry, printing char matrices not implemented yet\n"; |
|
2157 } |
1343
|
2158 } |
|
2159 |
4513
|
2160 void |
|
2161 octave_print_internal (std::ostream& os, const charNDArray& nda, |
4663
|
2162 bool pr_as_read_syntax, int extra_indent, |
4513
|
2163 bool pr_as_string) |
|
2164 { |
|
2165 switch (nda.ndims ()) |
|
2166 { |
|
2167 case 1: |
|
2168 case 2: |
|
2169 octave_print_internal (os, nda.matrix_value (), |
|
2170 pr_as_read_syntax, extra_indent, pr_as_string); |
|
2171 break; |
|
2172 |
|
2173 default: |
4532
|
2174 PRINT_ND_ARRAY (os, nda, charNDArray, char, charMatrix); |
4513
|
2175 break; |
|
2176 } |
|
2177 } |
|
2178 |
4655
|
2179 void |
4925
|
2180 octave_print_internal (std::ostream& os, const std::string& s, |
|
2181 bool pr_as_read_syntax, int extra_indent) |
|
2182 { |
|
2183 ArrayN<std::string> nda (dim_vector (1, 1), s); |
|
2184 |
|
2185 octave_print_internal (os, nda, pr_as_read_syntax, extra_indent); |
|
2186 } |
|
2187 |
|
2188 void |
4655
|
2189 octave_print_internal (std::ostream& os, const ArrayN<std::string>& nda, |
4663
|
2190 bool pr_as_read_syntax, int /* extra_indent */) |
4655
|
2191 { |
5775
|
2192 // FIXME -- this mostly duplicates the code in the |
4655
|
2193 // PRINT_ND_ARRAY macro. |
|
2194 |
|
2195 if (nda.is_empty ()) |
|
2196 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); |
|
2197 else if (nda.length () == 1) |
|
2198 { |
|
2199 os << nda(0); |
|
2200 } |
|
2201 else |
|
2202 { |
|
2203 int ndims = nda.ndims (); |
|
2204 |
|
2205 dim_vector dims = nda.dims (); |
|
2206 |
5275
|
2207 Array<octave_idx_type> ra_idx (ndims, 0); |
|
2208 |
|
2209 octave_idx_type m = 1; |
4655
|
2210 |
|
2211 for (int i = 2; i < ndims; i++) |
|
2212 m *= dims(i); |
|
2213 |
5275
|
2214 octave_idx_type nr = dims(0); |
|
2215 octave_idx_type nc = dims(1); |
|
2216 |
|
2217 for (octave_idx_type i = 0; i < m; i++) |
4655
|
2218 { |
|
2219 std::string nm = "ans"; |
|
2220 |
|
2221 if (m > 1) |
|
2222 { |
|
2223 nm += "(:,:,"; |
|
2224 |
5765
|
2225 std::ostringstream buf; |
4655
|
2226 |
|
2227 for (int k = 2; k < ndims; k++) |
|
2228 { |
|
2229 buf << ra_idx(k) + 1; |
|
2230 |
|
2231 if (k < ndims - 1) |
|
2232 buf << ","; |
|
2233 else |
|
2234 buf << ")"; |
|
2235 } |
|
2236 |
5765
|
2237 nm += buf.str (); |
4655
|
2238 } |
|
2239 |
|
2240 Array<idx_vector> idx (ndims); |
|
2241 |
|
2242 idx(0) = idx_vector (':'); |
|
2243 idx(1) = idx_vector (':'); |
|
2244 |
|
2245 for (int k = 2; k < ndims; k++) |
|
2246 idx(k) = idx_vector (ra_idx(k) + 1); |
|
2247 |
|
2248 Array2<std::string> page (nda.index (idx), nr, nc); |
|
2249 |
5775
|
2250 // FIXME -- need to do some more work to put these |
4655
|
2251 // in neatly aligned columns... |
|
2252 |
5275
|
2253 octave_idx_type n_rows = page.rows (); |
|
2254 octave_idx_type n_cols = page.cols (); |
4655
|
2255 |
|
2256 os << nm << " =\n\n"; |
|
2257 |
5275
|
2258 for (octave_idx_type ii = 0; ii < n_rows; ii++) |
4655
|
2259 { |
5275
|
2260 for (octave_idx_type jj = 0; jj < n_cols; jj++) |
4655
|
2261 os << " " << page(ii,jj); |
|
2262 |
|
2263 os << "\n"; |
|
2264 } |
|
2265 |
|
2266 if (i < m - 1) |
|
2267 os << "\n"; |
|
2268 |
|
2269 if (i < m) |
|
2270 increment_index (ra_idx, dims, 2); |
|
2271 } |
|
2272 } |
|
2273 } |
|
2274 |
4901
|
2275 template <class T> |
|
2276 class |
|
2277 octave_print_conv |
|
2278 { |
|
2279 public: |
|
2280 typedef T print_conv_type; |
|
2281 }; |
|
2282 |
|
2283 #define PRINT_CONV(T1, T2) \ |
|
2284 template <> \ |
|
2285 class \ |
|
2286 octave_print_conv<T1> \ |
|
2287 { \ |
|
2288 public: \ |
|
2289 typedef T2 print_conv_type; \ |
|
2290 } |
|
2291 |
|
2292 PRINT_CONV (octave_int8, octave_int16); |
|
2293 PRINT_CONV (octave_uint8, octave_uint16); |
|
2294 |
|
2295 #undef PRINT_CONV |
|
2296 |
|
2297 template <class T> |
6018
|
2298 /* static */ inline void |
4949
|
2299 pr_int (std::ostream& os, const T& d, int fw = 0) |
|
2300 { |
|
2301 size_t sz = d.byte_size(); |
|
2302 const unsigned char * tmpi = d.iptr(); |
|
2303 |
5544
|
2304 // Unless explicitly asked for, always print in big-endian |
|
2305 // format for hex and bit formats. |
|
2306 // |
|
2307 // {bit,hex}_format == 1: print big-endian |
|
2308 // {bit,hex}_format == 2: print native |
|
2309 |
4949
|
2310 if (hex_format) |
|
2311 { |
|
2312 char ofill = os.fill ('0'); |
|
2313 |
|
2314 std::ios::fmtflags oflags |
|
2315 = os.flags (std::ios::right | std::ios::hex); |
|
2316 |
|
2317 if (hex_format > 1 || oct_mach_info::words_big_endian ()) |
|
2318 { |
|
2319 for (size_t i = 0; i < sz; i++) |
|
2320 os << std::setw (2) << static_cast<int> (tmpi[i]); |
|
2321 } |
|
2322 else |
|
2323 { |
|
2324 for (int i = sz - 1; i >= 0; i--) |
|
2325 os << std::setw (2) << static_cast<int> (tmpi[i]); |
|
2326 } |
|
2327 |
|
2328 os.fill (ofill); |
|
2329 os.setf (oflags); |
|
2330 } |
|
2331 else if (bit_format) |
|
2332 { |
5544
|
2333 if (oct_mach_info::words_big_endian ()) |
4949
|
2334 { |
|
2335 for (size_t i = 0; i < sz; i++) |
5544
|
2336 PRINT_CHAR_BITS (os, tmpi[i]); |
4949
|
2337 } |
|
2338 else |
|
2339 { |
5544
|
2340 if (bit_format > 1) |
|
2341 { |
|
2342 for (size_t i = 0; i < sz; i++) |
|
2343 PRINT_CHAR_BITS_SWAPPED (os, tmpi[i]); |
|
2344 } |
|
2345 else |
|
2346 { |
|
2347 for (int i = sz - 1; i >= 0; i--) |
|
2348 PRINT_CHAR_BITS (os, tmpi[i]); |
|
2349 } |
4949
|
2350 } |
|
2351 } |
|
2352 else |
|
2353 { |
|
2354 os << std::setw (fw) |
|
2355 << typename octave_print_conv<T>::print_conv_type (d); |
|
2356 |
|
2357 if (bank_format) |
|
2358 os << ".00"; |
|
2359 } |
|
2360 } |
|
2361 |
6120
|
2362 // FIXME -- all this mess with abs is an attempt to avoid seeing |
|
2363 // |
|
2364 // warning: comparison of unsigned expression < 0 is always false |
|
2365 // |
|
2366 // from GCC. Isn't there a better way |
|
2367 |
4949
|
2368 template <class T> |
6018
|
2369 /* static */ inline T |
6008
|
2370 abs (T x) |
|
2371 { |
6120
|
2372 return x < 0 ? -x : x; |
6008
|
2373 } |
|
2374 |
6120
|
2375 #define INSTANTIATE_ABS(T) \ |
|
2376 template /* static */ inline T abs (T) |
|
2377 |
|
2378 INSTANTIATE_ABS(signed char); |
|
2379 INSTANTIATE_ABS(short); |
|
2380 INSTANTIATE_ABS(int); |
|
2381 INSTANTIATE_ABS(long); |
|
2382 INSTANTIATE_ABS(long long); |
|
2383 |
|
2384 #define SPECIALIZE_UABS(T) \ |
|
2385 template <> \ |
|
2386 /* static */ inline unsigned T \ |
|
2387 abs (unsigned T x) \ |
|
2388 { \ |
|
2389 return x; \ |
|
2390 } |
|
2391 |
|
2392 SPECIALIZE_UABS(char) |
|
2393 SPECIALIZE_UABS(short) |
|
2394 SPECIALIZE_UABS(int) |
|
2395 SPECIALIZE_UABS(long) |
|
2396 SPECIALIZE_UABS(long long) |
6008
|
2397 |
|
2398 template <class T> |
4901
|
2399 void |
|
2400 octave_print_internal (std::ostream& os, const intNDArray<T>& nda, |
4949
|
2401 bool pr_as_read_syntax, int extra_indent) |
4901
|
2402 { |
5775
|
2403 // FIXME -- this mostly duplicates the code in the |
4901
|
2404 // PRINT_ND_ARRAY macro. |
|
2405 |
|
2406 if (nda.is_empty ()) |
|
2407 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); |
|
2408 else if (nda.length () == 1) |
4949
|
2409 octave_print_internal (os, nda (0), pr_as_read_syntax); |
|
2410 else if (plus_format && ! pr_as_read_syntax) |
4901
|
2411 { |
|
2412 int ndims = nda.ndims (); |
|
2413 |
5275
|
2414 Array<octave_idx_type> ra_idx (ndims, 0); |
4949
|
2415 |
4901
|
2416 dim_vector dims = nda.dims (); |
|
2417 |
5275
|
2418 octave_idx_type m = 1; |
4901
|
2419 |
|
2420 for (int i = 2; i < ndims; i++) |
|
2421 m *= dims(i); |
|
2422 |
5275
|
2423 octave_idx_type nr = dims(0); |
|
2424 octave_idx_type nc = dims(1); |
|
2425 |
|
2426 for (octave_idx_type i = 0; i < m; i++) |
4901
|
2427 { |
|
2428 if (m > 1) |
|
2429 { |
4949
|
2430 std::string nm = "ans(:,:,"; |
4901
|
2431 |
5765
|
2432 std::ostringstream buf; |
4901
|
2433 |
|
2434 for (int k = 2; k < ndims; k++) |
|
2435 { |
|
2436 buf << ra_idx(k) + 1; |
|
2437 |
|
2438 if (k < ndims - 1) |
|
2439 buf << ","; |
|
2440 else |
|
2441 buf << ")"; |
|
2442 } |
|
2443 |
5765
|
2444 nm += buf.str (); |
4949
|
2445 |
|
2446 os << nm << " =\n\n"; |
4901
|
2447 } |
|
2448 |
|
2449 Array<idx_vector> idx (ndims); |
|
2450 |
|
2451 idx(0) = idx_vector (':'); |
|
2452 idx(1) = idx_vector (':'); |
|
2453 |
|
2454 for (int k = 2; k < ndims; k++) |
|
2455 idx(k) = idx_vector (ra_idx(k) + 1); |
|
2456 |
|
2457 Array2<T> page (nda.index (idx), nr, nc); |
|
2458 |
5275
|
2459 for (octave_idx_type ii = 0; ii < nr; ii++) |
4901
|
2460 { |
5275
|
2461 for (octave_idx_type jj = 0; jj < nc; jj++) |
4949
|
2462 { |
|
2463 OCTAVE_QUIT; |
|
2464 |
|
2465 pr_plus_format (os, page(ii,jj)); |
|
2466 } |
|
2467 |
|
2468 if ((ii < nr - 1) || (i < m -1)) |
|
2469 os << "\n"; |
4901
|
2470 } |
|
2471 |
|
2472 if (i < m - 1) |
4949
|
2473 { |
|
2474 os << "\n"; |
|
2475 increment_index (ra_idx, dims, 2); |
|
2476 } |
|
2477 } |
|
2478 } |
|
2479 else |
|
2480 { |
|
2481 int ndims = nda.ndims (); |
|
2482 |
|
2483 dim_vector dims = nda.dims (); |
|
2484 |
5275
|
2485 Array<octave_idx_type> ra_idx (ndims, 0); |
|
2486 |
|
2487 octave_idx_type m = 1; |
4949
|
2488 |
|
2489 for (int i = 2; i < ndims; i++) |
|
2490 m *= dims(i); |
|
2491 |
5275
|
2492 octave_idx_type nr = dims(0); |
|
2493 octave_idx_type nc = dims(1); |
4949
|
2494 |
|
2495 int fw = 0; |
|
2496 if (hex_format) |
|
2497 fw = 2 * nda(0).byte_size (); |
|
2498 else if (bit_format) |
|
2499 fw = nda(0).nbits (); |
|
2500 else |
|
2501 { |
|
2502 bool isneg = false; |
|
2503 int digits = 0; |
|
2504 |
5275
|
2505 for (octave_idx_type i = 0; i < dims.numel (); i++) |
4949
|
2506 { |
|
2507 int new_digits = static_cast<int> |
|
2508 (floor (log10 (double (abs (nda(i).value ()))) + 1.0)); |
|
2509 |
|
2510 if (new_digits > digits) |
|
2511 digits = new_digits; |
|
2512 |
|
2513 if (! isneg) |
|
2514 isneg = (abs (nda(i).value ()) != nda(i).value ()); |
|
2515 } |
|
2516 |
|
2517 fw = digits + isneg; |
|
2518 } |
|
2519 |
6788
|
2520 int column_width = fw + (rat_format ? 0 : (bank_format ? 5 : 2)); |
5275
|
2521 octave_idx_type total_width = nc * column_width; |
4949
|
2522 int max_width = command_editor::terminal_cols () - extra_indent; |
5275
|
2523 octave_idx_type inc = nc; |
4949
|
2524 if (total_width > max_width && Vsplit_long_rows) |
|
2525 { |
|
2526 inc = max_width / column_width; |
|
2527 if (inc == 0) |
|
2528 inc++; |
|
2529 } |
|
2530 |
5275
|
2531 for (octave_idx_type i = 0; i < m; i++) |
4949
|
2532 { |
|
2533 if (m > 1) |
|
2534 { |
|
2535 std::string nm = "ans(:,:,"; |
|
2536 |
5765
|
2537 std::ostringstream buf; |
4949
|
2538 |
|
2539 for (int k = 2; k < ndims; k++) |
|
2540 { |
|
2541 buf << ra_idx(k) + 1; |
|
2542 |
|
2543 if (k < ndims - 1) |
|
2544 buf << ","; |
|
2545 else |
|
2546 buf << ")"; |
|
2547 } |
|
2548 |
5765
|
2549 nm += buf.str (); |
4949
|
2550 |
|
2551 os << nm << " =\n\n"; |
|
2552 } |
|
2553 |
|
2554 Array<idx_vector> idx (ndims); |
|
2555 |
|
2556 idx(0) = idx_vector (':'); |
|
2557 idx(1) = idx_vector (':'); |
|
2558 |
|
2559 for (int k = 2; k < ndims; k++) |
|
2560 idx(k) = idx_vector (ra_idx(k) + 1); |
|
2561 |
|
2562 Array2<T> page (nda.index (idx), nr, nc); |
|
2563 |
|
2564 if (free_format) |
|
2565 { |
|
2566 if (pr_as_read_syntax) |
|
2567 os << "[\n"; |
|
2568 |
5275
|
2569 for (octave_idx_type ii = 0; ii < nr; ii++) |
4949
|
2570 { |
5275
|
2571 for (octave_idx_type jj = 0; jj < nc; jj++) |
4949
|
2572 { |
|
2573 OCTAVE_QUIT; |
|
2574 os << " "; |
|
2575 os << typename octave_print_conv<T>::print_conv_type (page(ii,jj)); |
|
2576 } |
|
2577 os << "\n"; |
|
2578 } |
|
2579 |
|
2580 if (pr_as_read_syntax) |
|
2581 os << "]"; |
|
2582 } |
|
2583 else |
|
2584 { |
5275
|
2585 octave_idx_type n_rows = page.rows (); |
|
2586 octave_idx_type n_cols = page.cols (); |
|
2587 |
|
2588 for (octave_idx_type col = 0; col < n_cols; col += inc) |
4949
|
2589 { |
5275
|
2590 octave_idx_type lim = col + inc < n_cols ? col + inc : n_cols; |
4949
|
2591 |
|
2592 pr_col_num_header (os, total_width, max_width, lim, col, |
|
2593 extra_indent); |
|
2594 |
5275
|
2595 for (octave_idx_type ii = 0; ii < n_rows; ii++) |
4949
|
2596 { |
|
2597 os << std::setw (extra_indent) << ""; |
|
2598 |
5275
|
2599 for (octave_idx_type jj = col; jj < lim; jj++) |
4949
|
2600 { |
|
2601 OCTAVE_QUIT; |
|
2602 os << " "; |
|
2603 pr_int (os, page(ii,jj), fw); |
|
2604 } |
|
2605 if ((ii < n_rows - 1) || (i < m -1)) |
|
2606 os << "\n"; |
|
2607 } |
|
2608 } |
|
2609 } |
|
2610 |
|
2611 if (i < m - 1) |
|
2612 { |
|
2613 os << "\n"; |
|
2614 increment_index (ra_idx, dims, 2); |
|
2615 } |
4901
|
2616 } |
|
2617 } |
|
2618 } |
|
2619 |
5775
|
2620 // FIXME -- this is not the right spot for this... |
4901
|
2621 |
|
2622 template void |
4949
|
2623 pr_int (std::ostream&, const octave_int8&, int); |
|
2624 |
|
2625 template void |
|
2626 pr_int (std::ostream&, const octave_int16&, int); |
|
2627 |
|
2628 template void |
|
2629 pr_int (std::ostream&, const octave_int32&, int); |
|
2630 |
|
2631 template void |
|
2632 pr_int (std::ostream&, const octave_int64&, int); |
|
2633 |
|
2634 template void |
|
2635 pr_int (std::ostream&, const octave_uint8&, int); |
|
2636 |
|
2637 template void |
|
2638 pr_int (std::ostream&, const octave_uint16&, int); |
|
2639 |
|
2640 template void |
|
2641 pr_int (std::ostream&, const octave_uint32&, int); |
|
2642 |
|
2643 template void |
|
2644 pr_int (std::ostream&, const octave_uint64&, int); |
|
2645 |
6109
|
2646 template OCTINTERP_API void |
4901
|
2647 octave_print_internal (std::ostream&, const intNDArray<octave_int8>&, |
|
2648 bool, int); |
|
2649 |
6109
|
2650 template OCTINTERP_API void |
4901
|
2651 octave_print_internal (std::ostream&, const intNDArray<octave_int16>&, |
|
2652 bool, int); |
|
2653 |
6109
|
2654 template OCTINTERP_API void |
4901
|
2655 octave_print_internal (std::ostream&, const intNDArray<octave_int32>&, |
|
2656 bool, int); |
|
2657 |
6109
|
2658 template OCTINTERP_API void |
4901
|
2659 octave_print_internal (std::ostream&, const intNDArray<octave_int64>&, |
|
2660 bool, int); |
|
2661 |
6109
|
2662 template OCTINTERP_API void |
4901
|
2663 octave_print_internal (std::ostream&, const intNDArray<octave_uint8>&, |
|
2664 bool, int); |
|
2665 |
6109
|
2666 template OCTINTERP_API void |
4901
|
2667 octave_print_internal (std::ostream&, const intNDArray<octave_uint16>&, |
|
2668 bool, int); |
|
2669 |
6109
|
2670 template OCTINTERP_API void |
4901
|
2671 octave_print_internal (std::ostream&, const intNDArray<octave_uint32>&, |
|
2672 bool, int); |
|
2673 |
6109
|
2674 template OCTINTERP_API void |
4901
|
2675 octave_print_internal (std::ostream&, const intNDArray<octave_uint64>&, |
|
2676 bool, int); |
|
2677 |
|
2678 template <class T> |
|
2679 void |
4933
|
2680 octave_print_internal (std::ostream& os, const octave_int<T>& val, bool) |
4901
|
2681 { |
4949
|
2682 if (plus_format) |
|
2683 { |
|
2684 pr_plus_format (os, val); |
|
2685 } |
|
2686 else |
|
2687 { |
|
2688 if (free_format) |
5030
|
2689 os << typename octave_print_conv<octave_int<T> >::print_conv_type (val); |
4949
|
2690 else |
|
2691 pr_int (os, val); |
|
2692 } |
4901
|
2693 } |
|
2694 |
5775
|
2695 // FIXME -- this is not the right spot for this... |
4901
|
2696 |
6109
|
2697 template OCTINTERP_API void |
4901
|
2698 octave_print_internal (std::ostream&, const octave_int8&, bool); |
|
2699 |
6109
|
2700 template OCTINTERP_API void |
4901
|
2701 octave_print_internal (std::ostream&, const octave_int16&, bool); |
|
2702 |
6109
|
2703 template OCTINTERP_API void |
4901
|
2704 octave_print_internal (std::ostream&, const octave_int32&, bool); |
|
2705 |
6109
|
2706 template OCTINTERP_API void |
4901
|
2707 octave_print_internal (std::ostream&, const octave_int64&, bool); |
|
2708 |
6109
|
2709 template OCTINTERP_API void |
4901
|
2710 octave_print_internal (std::ostream&, const octave_uint8&, bool); |
|
2711 |
6109
|
2712 template OCTINTERP_API void |
4901
|
2713 octave_print_internal (std::ostream&, const octave_uint16&, bool); |
|
2714 |
6109
|
2715 template OCTINTERP_API void |
4901
|
2716 octave_print_internal (std::ostream&, const octave_uint32&, bool); |
|
2717 |
6109
|
2718 template OCTINTERP_API void |
4901
|
2719 octave_print_internal (std::ostream&, const octave_uint64&, bool); |
|
2720 |
3933
|
2721 extern void |
|
2722 octave_print_internal (std::ostream&, const Cell&, bool, int, bool) |
3928
|
2723 { |
3933
|
2724 panic_impossible (); |
3928
|
2725 } |
|
2726 |
6788
|
2727 DEFUN (rats, args, nargout, |
|
2728 "-*- texinfo -*-\n\ |
|
2729 @deftypefn {Built-in Function} {} rats (@var{x}, @var{len})\n\ |
|
2730 Convert @var{x} into a rational approximation represented as a string.\n\ |
|
2731 You can convert the string back into a matrix as follows:\n\ |
|
2732 \n\ |
|
2733 @example\n\ |
|
2734 eval(['[',rats(hilb(4)),'];'])\n\ |
|
2735 @end example\n\ |
|
2736 \n\ |
|
2737 The optional second argument defines the maximum length of the string\n\ |
|
2738 representing the elements of @var{x}. By default @var{len} is 9.\n\ |
|
2739 @seealso{format, rat}\n\ |
|
2740 @end deftypefn") |
|
2741 { |
|
2742 octave_value retval; |
6803
|
2743 |
6788
|
2744 int nargin = args.length (); |
|
2745 |
6803
|
2746 unwind_protect::begin_frame ("Frats"); |
|
2747 |
|
2748 unwind_protect_int (rat_string_len); |
|
2749 |
6788
|
2750 rat_string_len = 9; |
6803
|
2751 |
6788
|
2752 if (nargin == 2) |
|
2753 rat_string_len = args(1).nint_value (); |
|
2754 |
6803
|
2755 if (! error_state) |
6788
|
2756 { |
|
2757 if (nargin < 3 && nargout < 2) |
|
2758 { |
6803
|
2759 octave_value arg = args(0); |
|
2760 |
|
2761 if (arg.is_numeric_type ()) |
|
2762 { |
|
2763 unwind_protect_bool (rat_format); |
|
2764 |
|
2765 rat_format = true; |
|
2766 |
|
2767 std::ostringstream buf; |
|
2768 args(0).print (buf); |
|
2769 std::string s = buf.str (); |
|
2770 |
|
2771 std::list<std::string> lst; |
|
2772 |
|
2773 size_t n = 0; |
|
2774 size_t s_len = s.length (); |
|
2775 |
|
2776 while (n < s_len) |
|
2777 { |
|
2778 size_t m = s.find ('\n', n); |
|
2779 |
|
2780 if (m == NPOS) |
|
2781 { |
|
2782 lst.push_back (s.substr (n)); |
|
2783 break; |
|
2784 } |
|
2785 else |
|
2786 { |
|
2787 lst.push_back (s.substr (n, m - n)); |
|
2788 n = m + 1; |
|
2789 } |
|
2790 } |
|
2791 |
|
2792 retval = string_vector (lst); |
|
2793 } |
|
2794 else |
|
2795 error ("rats: expecting numeric input"); |
6788
|
2796 } |
|
2797 else |
|
2798 print_usage (); |
|
2799 } |
|
2800 |
6803
|
2801 unwind_protect::run_frame ("Frats"); |
|
2802 |
6788
|
2803 return retval; |
|
2804 } |
|
2805 |
3685
|
2806 DEFUN (disp, args, nargout, |
|
2807 "-*- texinfo -*-\n\ |
|
2808 @deftypefn {Built-in Function} {} disp (@var{x})\n\ |
|
2809 Display the value of @var{x}. For example,\n\ |
|
2810 \n\ |
|
2811 @example\n\ |
|
2812 disp (\"The value of pi is:\"), disp (pi)\n\ |
|
2813 \n\ |
|
2814 @print{} the value of pi is:\n\ |
|
2815 @print{} 3.1416\n\ |
|
2816 @end example\n\ |
|
2817 \n\ |
|
2818 @noindent\n\ |
|
2819 Note that the output from @code{disp} always ends with a newline.\n\ |
|
2820 \n\ |
|
2821 If an output value is requested, @code{disp} prints nothing and\n\ |
|
2822 returns the formatted output in a string.\n\ |
5642
|
2823 @seealso{fdisp}\n\ |
|
2824 @end deftypefn") |
3685
|
2825 { |
|
2826 octave_value retval; |
|
2827 |
|
2828 int nargin = args.length (); |
|
2829 |
|
2830 if (nargin == 1 && nargout < 2) |
|
2831 { |
|
2832 if (nargout == 0) |
|
2833 args(0).print (octave_stdout); |
|
2834 else |
|
2835 { |
5765
|
2836 std::ostringstream buf; |
3685
|
2837 args(0).print (buf); |
5765
|
2838 retval = buf.str (); |
3685
|
2839 } |
|
2840 } |
|
2841 else |
5823
|
2842 print_usage (); |
3685
|
2843 |
|
2844 return retval; |
|
2845 } |
|
2846 |
|
2847 DEFUN (fdisp, args, , |
|
2848 "-*- texinfo -*-\n\ |
|
2849 @deftypefn {Built-in Function} {} fdisp (@var{fid}, @var{x})\n\ |
|
2850 Display the value of @var{x} on the stream @var{fid}. For example,\n\ |
|
2851 \n\ |
|
2852 @example\n\ |
4869
|
2853 fdisp (stdout, \"The value of pi is:\"), fdisp (stdout, pi)\n\ |
3685
|
2854 \n\ |
|
2855 @print{} the value of pi is:\n\ |
|
2856 @print{} 3.1416\n\ |
|
2857 @end example\n\ |
|
2858 \n\ |
|
2859 @noindent\n\ |
4869
|
2860 Note that the output from @code{fdisp} always ends with a newline.\n\ |
5642
|
2861 @seealso{disp}\n\ |
|
2862 @end deftypefn") |
3685
|
2863 { |
|
2864 octave_value retval; |
|
2865 |
|
2866 int nargin = args.length (); |
|
2867 |
|
2868 if (nargin == 2) |
|
2869 { |
|
2870 int fid = octave_stream_list::get_file_number (args (0)); |
|
2871 |
|
2872 octave_stream os = octave_stream_list::lookup (fid, "fdisp"); |
|
2873 |
|
2874 if (! error_state) |
|
2875 { |
3769
|
2876 std::ostream *osp = os.output_stream (); |
3685
|
2877 |
|
2878 if (osp) |
|
2879 args(1).print (*osp); |
|
2880 else |
|
2881 error ("fdisp: stream not open for writing"); |
|
2882 } |
|
2883 } |
|
2884 else |
5823
|
2885 print_usage (); |
3685
|
2886 |
|
2887 return retval; |
|
2888 } |
|
2889 |
1
|
2890 static void |
|
2891 init_format_state (void) |
|
2892 { |
2387
|
2893 free_format = false; |
|
2894 plus_format = false; |
6788
|
2895 rat_format = false; |
2387
|
2896 bank_format = false; |
3608
|
2897 hex_format = 0; |
1309
|
2898 bit_format = 0; |
4833
|
2899 compact_format = false; |
2387
|
2900 print_e = false; |
|
2901 print_big_e = false; |
4509
|
2902 print_g = false; |
1
|
2903 } |
|
2904 |
|
2905 static void |
|
2906 set_output_prec_and_fw (int prec, int fw) |
|
2907 { |
5794
|
2908 Voutput_precision = prec; |
|
2909 Voutput_max_field_width = fw; |
1
|
2910 } |
|
2911 |
1755
|
2912 static void |
|
2913 set_format_style (int argc, const string_vector& argv) |
1
|
2914 { |
1755
|
2915 int idx = 1; |
|
2916 |
1899
|
2917 if (--argc > 0) |
1
|
2918 { |
3523
|
2919 std::string arg = argv[idx++]; |
2584
|
2920 |
1755
|
2921 if (arg == "short") |
1
|
2922 { |
1755
|
2923 if (--argc > 0) |
1
|
2924 { |
1755
|
2925 arg = argv[idx++]; |
|
2926 |
|
2927 if (arg == "e") |
1
|
2928 { |
1755
|
2929 init_format_state (); |
2387
|
2930 print_e = true; |
1755
|
2931 } |
|
2932 else if (arg == "E") |
|
2933 { |
|
2934 init_format_state (); |
2387
|
2935 print_e = true; |
|
2936 print_big_e = true; |
1
|
2937 } |
4509
|
2938 else if (arg == "g") |
|
2939 { |
|
2940 init_format_state (); |
|
2941 print_g = true; |
|
2942 } |
|
2943 else if (arg == "G") |
|
2944 { |
|
2945 init_format_state (); |
|
2946 print_g = true; |
|
2947 print_big_e = true; |
|
2948 } |
1
|
2949 else |
|
2950 { |
1755
|
2951 error ("format: unrecognized option `short %s'", |
|
2952 arg.c_str ()); |
|
2953 return; |
|
2954 } |
|
2955 } |
|
2956 else |
|
2957 init_format_state (); |
|
2958 |
4509
|
2959 set_output_prec_and_fw (5, 10); |
1755
|
2960 } |
|
2961 else if (arg == "long") |
|
2962 { |
|
2963 if (--argc > 0) |
|
2964 { |
|
2965 arg = argv[idx++]; |
|
2966 |
|
2967 if (arg == "e") |
|
2968 { |
|
2969 init_format_state (); |
2387
|
2970 print_e = true; |
1755
|
2971 } |
|
2972 else if (arg == "E") |
|
2973 { |
|
2974 init_format_state (); |
2387
|
2975 print_e = true; |
|
2976 print_big_e = true; |
1
|
2977 } |
4509
|
2978 else if (arg == "g") |
|
2979 { |
|
2980 init_format_state (); |
|
2981 print_g = true; |
|
2982 } |
|
2983 else if (arg == "G") |
|
2984 { |
|
2985 init_format_state (); |
|
2986 print_g = true; |
|
2987 print_big_e = true; |
|
2988 } |
1
|
2989 else |
1755
|
2990 { |
|
2991 error ("format: unrecognized option `long %s'", |
|
2992 arg.c_str ()); |
|
2993 return; |
|
2994 } |
1186
|
2995 } |
1
|
2996 else |
1755
|
2997 init_format_state (); |
|
2998 |
4509
|
2999 set_output_prec_and_fw (15, 20); |
1755
|
3000 } |
|
3001 else if (arg == "hex") |
|
3002 { |
|
3003 init_format_state (); |
3608
|
3004 hex_format = 1; |
1755
|
3005 } |
|
3006 else if (arg == "native-hex") |
|
3007 { |
|
3008 init_format_state (); |
|
3009 hex_format = 2; |
|
3010 } |
|
3011 else if (arg == "bit") |
|
3012 { |
|
3013 init_format_state (); |
|
3014 bit_format = 1; |
|
3015 } |
|
3016 else if (arg == "native-bit") |
|
3017 { |
|
3018 init_format_state (); |
|
3019 bit_format = 2; |
|
3020 } |
|
3021 else if (arg == "+" || arg == "plus") |
|
3022 { |
4632
|
3023 if (--argc > 0) |
|
3024 { |
|
3025 arg = argv[idx++]; |
|
3026 |
|
3027 if (arg.length () == 3) |
|
3028 plus_format_chars = arg; |
|
3029 else |
|
3030 { |
|
3031 error ("format: invalid option for plus format"); |
|
3032 return; |
|
3033 } |
|
3034 } |
|
3035 else |
|
3036 plus_format_chars = "+ "; |
|
3037 |
1755
|
3038 init_format_state (); |
2387
|
3039 plus_format = true; |
1755
|
3040 } |
6788
|
3041 else if (arg == "rat") |
|
3042 { |
|
3043 init_format_state (); |
|
3044 rat_format = true; |
|
3045 } |
1755
|
3046 else if (arg == "bank") |
|
3047 { |
|
3048 init_format_state (); |
2387
|
3049 bank_format = true; |
1755
|
3050 } |
|
3051 else if (arg == "free") |
|
3052 { |
|
3053 init_format_state (); |
2387
|
3054 free_format = true; |
1755
|
3055 } |
|
3056 else if (arg == "none") |
|
3057 { |
|
3058 init_format_state (); |
2387
|
3059 free_format = true; |
1755
|
3060 } |
|
3061 else if (arg == "compact") |
|
3062 { |
2387
|
3063 compact_format = true; |
1755
|
3064 } |
|
3065 else if (arg == "loose") |
|
3066 { |
2387
|
3067 compact_format = false; |
1
|
3068 } |
|
3069 else |
1755
|
3070 error ("format: unrecognized format state `%s'", arg.c_str ()); |
1
|
3071 } |
|
3072 else |
|
3073 { |
|
3074 init_format_state (); |
|
3075 set_output_prec_and_fw (5, 10); |
|
3076 } |
|
3077 } |
|
3078 |
4208
|
3079 DEFCMD (format, args, , |
3372
|
3080 "-*- texinfo -*-\n\ |
|
3081 @deffn {Command} format options\n\ |
|
3082 Control the format of the output produced by @code{disp} and Octave's\n\ |
|
3083 normal echoing mechanism. Valid options are listed in the following\n\ |
|
3084 table.\n\ |
|
3085 \n\ |
|
3086 @table @code\n\ |
|
3087 @item short\n\ |
|
3088 Octave will try to print numbers with at\n\ |
4509
|
3089 least 5 significant figures within a field that is a maximum of 10\n\ |
|
3090 characters wide (not counting additional spacing that is added between\n\ |
|
3091 columns of a matrix).\n\ |
3372
|
3092 \n\ |
|
3093 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
3094 decimal point and all the numbers fit within the maximum field width,\n\ |
|
3095 it switches to an @samp{e} format.\n\ |
|
3096 \n\ |
|
3097 @item long\n\ |
|
3098 Octave will try to print numbers with at least 15 significant figures\n\ |
4509
|
3099 within a field that is a maximum of 20 characters wide (not counting\n\ |
|
3100 additional spacing that is added between columns of a matrix).\n\ |
3372
|
3101 \n\ |
|
3102 As will the @samp{short} format, Octave will switch to an @samp{e}\n\ |
|
3103 format if it is unable to format a matrix so that columns line up on the\n\ |
|
3104 decimal point and all the numbers fit within the maximum field width.\n\ |
|
3105 \n\ |
|
3106 @item long e\n\ |
|
3107 @itemx short e\n\ |
|
3108 The same as @samp{format long} or @samp{format short} but always display\n\ |
|
3109 output with an @samp{e} format. For example, with the @samp{short e}\n\ |
4509
|
3110 format, @code{pi} is displayed as @code{3.14e+00}.\n\ |
3372
|
3111 \n\ |
|
3112 @item long E\n\ |
|
3113 @itemx short E\n\ |
|
3114 The same as @samp{format long e} or @samp{format short e} but always\n\ |
|
3115 display output with an uppercase @samp{E} format. For example, with\n\ |
4509
|
3116 the @samp{long E} format, @code{pi} is displayed as\n\ |
3372
|
3117 @code{3.14159265358979E+00}.\n\ |
4509
|
3118 @item long g\n\ |
|
3119 @itemx short g\n\ |
7001
|
3120 Choose between normal @samp{long} (or @samp{short}) and\n\ |
4509
|
3121 @samp{long e} (or @samp{short e}) formats based on the magnitude\n\ |
|
3122 of the number. For example, with the @samp{short g} format,\n\ |
|
3123 @code{pi .^ [2; 4; 8; 16; 32]} is displayed as\n\ |
|
3124 \n\ |
|
3125 @example\n\ |
|
3126 @group\n\ |
|
3127 ans =\n\ |
|
3128 \n\ |
|
3129 3.1416\n\ |
|
3130 9.8696\n\ |
|
3131 97.409\n\ |
|
3132 9488.5\n\ |
|
3133 9.0032e+07\n\ |
|
3134 8.1058e+15\n\ |
|
3135 @end group\n\ |
|
3136 @end example\n\ |
|
3137 \n\ |
|
3138 @item long G\n\ |
|
3139 @itemx short G\n\ |
|
3140 The same as @samp{format long g} or @samp{format short g} but use an\n\ |
|
3141 uppercase @samp{E} format. For example, with the @samp{short G} format,\n\ |
|
3142 @code{pi .^ [2; 4; 8; 16; 32]} is displayed as\n\ |
|
3143 \n\ |
|
3144 @example\n\ |
|
3145 @group\n\ |
|
3146 ans =\n\ |
|
3147 \n\ |
|
3148 3.1416\n\ |
|
3149 9.8696\n\ |
|
3150 97.409\n\ |
|
3151 9488.5\n\ |
|
3152 9.0032E+07\n\ |
|
3153 8.1058E+15\n\ |
|
3154 @end group\n\ |
|
3155 @end example\n\ |
3372
|
3156 \n\ |
|
3157 @item free\n\ |
|
3158 @itemx none\n\ |
|
3159 Print output in free format, without trying to line up columns of\n\ |
|
3160 matrices on the decimal point. This also causes complex numbers to be\n\ |
|
3161 formatted like this @samp{(0.604194, 0.607088)} instead of like this\n\ |
|
3162 @samp{0.60419 + 0.60709i}.\n\ |
529
|
3163 \n\ |
3372
|
3164 @item bank\n\ |
|
3165 Print in a fixed format with two places to the right of the decimal\n\ |
|
3166 point.\n\ |
|
3167 \n\ |
|
3168 @item +\n\ |
4632
|
3169 @itemx + @var{chars}\n\ |
|
3170 @itemx plus\n\ |
|
3171 @itemx plus @var{chars}\n\ |
3372
|
3172 Print a @samp{+} symbol for nonzero matrix elements and a space for zero\n\ |
|
3173 matrix elements. This format can be very useful for examining the\n\ |
|
3174 structure of a large matrix.\n\ |
|
3175 \n\ |
4632
|
3176 The optional argument @var{chars} specifies a list of 3 characters to use\n\ |
|
3177 for printing values greater than zero, less than zero and equal to zero.\n\ |
|
3178 For example, with the @samp{+ \"+-.\"} format, @code{[1, 0, -1; -1, 0, 1]}\n\ |
|
3179 is displayed as\n\ |
|
3180 \n\ |
|
3181 @example\n\ |
|
3182 @group\n\ |
|
3183 ans =\n\ |
|
3184 \n\ |
|
3185 +.-\n\ |
|
3186 -.+\n\ |
|
3187 @end group\n\ |
|
3188 @end example\n\ |
|
3189 \n\ |
4833
|
3190 @itemx native-hex\n\ |
3372
|
3191 Print the hexadecimal representation numbers as they are stored in\n\ |
|
3192 memory. For example, on a workstation which stores 8 byte real values\n\ |
|
3193 in IEEE format with the least significant byte first, the value of\n\ |
|
3194 @code{pi} when printed in @code{hex} format is @code{400921fb54442d18}.\n\ |
|
3195 This format only works for numeric values.\n\ |
|
3196 \n\ |
4833
|
3197 @item hex\n\ |
|
3198 The same as @code{native-hex}, but always print the most significant\n\ |
|
3199 byte first.\n\ |
|
3200 @item native-bit\n\ |
3372
|
3201 Print the bit representation of numbers as stored in memory.\n\ |
|
3202 For example, the value of @code{pi} is\n\ |
|
3203 \n\ |
|
3204 @example\n\ |
|
3205 @group\n\ |
|
3206 01000000000010010010000111111011\n\ |
|
3207 01010100010001000010110100011000\n\ |
|
3208 @end group\n\ |
|
3209 @end example\n\ |
|
3210 \n\ |
|
3211 (shown here in two 32 bit sections for typesetting purposes) when\n\ |
|
3212 printed in bit format on a workstation which stores 8 byte real values\n\ |
|
3213 in IEEE format with the least significant byte first. This format only\n\ |
|
3214 works for numeric types.\n\ |
4833
|
3215 @item bit\n\ |
|
3216 The same as @code{native-bit}, but always print the most significant\n\ |
|
3217 bits first.\n\ |
|
3218 @item compact\n\ |
|
3219 Remove extra blank space around column number labels.\n\ |
|
3220 @item loose\n\ |
|
3221 Insert blank lines above and below column number labels (this is the\n\ |
|
3222 default).\n\ |
6788
|
3223 @item rat\n\ |
|
3224 Print a rational approximation. That is the values are approximated\n\ |
|
3225 by one small integer divided by another.\n\ |
3372
|
3226 @end table\n\ |
|
3227 \n\ |
|
3228 By default, Octave will try to print numbers with at least 5 significant\n\ |
|
3229 figures within a field that is a maximum of 10 characters wide.\n\ |
|
3230 \n\ |
|
3231 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
3232 decimal point and all the numbers fit within the maximum field width,\n\ |
|
3233 it switches to an @samp{e} format.\n\ |
|
3234 \n\ |
|
3235 If @code{format} is invoked without any options, the default format\n\ |
|
3236 state is restored.\n\ |
|
3237 @end deffn") |
529
|
3238 { |
2086
|
3239 octave_value_list retval; |
529
|
3240 |
1755
|
3241 int argc = args.length () + 1; |
|
3242 |
1968
|
3243 string_vector argv = args.make_argv ("format"); |
1755
|
3244 |
|
3245 if (error_state) |
|
3246 return retval; |
529
|
3247 |
|
3248 set_format_style (argc, argv); |
|
3249 |
|
3250 return retval; |
|
3251 } |
|
3252 |
5794
|
3253 DEFUN (fixed_point_format, args, nargout, |
|
3254 "-*- texinfo -*-\n\ |
|
3255 @deftypefn {Built-in Function} {@var{val} =} fixed_point_format ()\n\ |
|
3256 @deftypefnx {Built-in Function} {@var{old_val} =} fixed_point_format (@var{new_val})\n\ |
|
3257 Query or set the internal variable that controls whether Octave will\n\ |
|
3258 use a scaled format to print matrix values such that the largest\n\ |
|
3259 element may be written with a single leading digit with the scaling\n\ |
|
3260 factor is printed on the first line of output. For example,\n\ |
3321
|
3261 \n\ |
|
3262 @example\n\ |
|
3263 @group\n\ |
|
3264 octave:1> logspace (1, 7, 5)'\n\ |
|
3265 ans =\n\ |
|
3266 \n\ |
|
3267 1.0e+07 *\n\ |
|
3268 \n\ |
|
3269 0.00000\n\ |
|
3270 0.00003\n\ |
|
3271 0.00100\n\ |
|
3272 0.03162\n\ |
|
3273 1.00000\n\ |
|
3274 @end group\n\ |
|
3275 @end example\n\ |
|
3276 \n\ |
|
3277 @noindent\n\ |
|
3278 Notice that first value appears to be zero when it is actually 1. For\n\ |
|
3279 this reason, you should be careful when setting\n\ |
|
3280 @code{fixed_point_format} to a nonzero value.\n\ |
5794
|
3281 @end deftypefn") |
|
3282 { |
|
3283 return SET_INTERNAL_VARIABLE (fixed_point_format); |
|
3284 } |
|
3285 |
|
3286 DEFUN (print_empty_dimensions, args, nargout, |
|
3287 "-*- texinfo -*-\n\ |
|
3288 @deftypefn {Built-in Function} {@var{val} =} print_empty_dimensions ()\n\ |
|
3289 @deftypefnx {Built-in Function} {@var{old_val} =} print_empty_dimensions (@var{new_val})\n\ |
7001
|
3290 Query or set the internal variable that controls whether the\n\ |
3321
|
3291 dimensions of empty matrices are printed along with the empty matrix\n\ |
|
3292 symbol, @samp{[]}. For example, the expression\n\ |
|
3293 \n\ |
|
3294 @example\n\ |
|
3295 zeros (3, 0)\n\ |
|
3296 @end example\n\ |
|
3297 \n\ |
|
3298 @noindent\n\ |
|
3299 will print\n\ |
|
3300 \n\ |
|
3301 @example\n\ |
|
3302 ans = [](3x0)\n\ |
|
3303 @end example\n\ |
5794
|
3304 @end deftypefn") |
|
3305 { |
|
3306 return SET_INTERNAL_VARIABLE (print_empty_dimensions); |
|
3307 } |
|
3308 |
|
3309 DEFUN (split_long_rows, args, nargout, |
|
3310 "-*- texinfo -*-\n\ |
|
3311 @deftypefn {Built-in Function} {@var{val} =} split_long_rows ()\n\ |
|
3312 @deftypefnx {Built-in Function} {@var{old_val} =} split_long_rows (@var{new_val})\n\ |
|
3313 Query or set the internal variable that controls whether rows of a matrix\n\ |
|
3314 may be split when displayed to a terminal window. If the rows are split,\n\ |
|
3315 Octave will display the matrix in a series of smaller pieces, each of\n\ |
|
3316 which can fit within the limits of your terminal width and each set of\n\ |
|
3317 rows is labeled so that you can easily see which columns are currently\n\ |
|
3318 being displayed. For example:\n\ |
3321
|
3319 \n\ |
6670
|
3320 @example\n\ |
3321
|
3321 @group\n\ |
|
3322 octave:13> rand (2,10)\n\ |
|
3323 ans =\n\ |
|
3324 \n\ |
|
3325 Columns 1 through 6:\n\ |
|
3326 \n\ |
|
3327 0.75883 0.93290 0.40064 0.43818 0.94958 0.16467\n\ |
|
3328 0.75697 0.51942 0.40031 0.61784 0.92309 0.40201\n\ |
|
3329 \n\ |
|
3330 Columns 7 through 10:\n\ |
|
3331 \n\ |
|
3332 0.90174 0.11854 0.72313 0.73326\n\ |
|
3333 0.44672 0.94303 0.56564 0.82150\n\ |
|
3334 @end group\n\ |
6670
|
3335 @end example\n\ |
5794
|
3336 @end deftypefn") |
|
3337 { |
|
3338 return SET_INTERNAL_VARIABLE (split_long_rows); |
|
3339 } |
|
3340 |
|
3341 DEFUN (output_max_field_width, args, nargout, |
|
3342 "-*- texinfo -*-\n\ |
|
3343 @deftypefn {Built-in Function} {@var{val} =} output_max_field_width ()\n\ |
|
3344 @deftypefnx {Built-in Function} {@var{old_val} =} output_max_field_width (@var{new_val})\n\ |
|
3345 Query or set the internal variable that specifies the maximum width\n\ |
|
3346 of a numeric output field.\n\ |
|
3347 @seealso{format, output_precision}\n\ |
|
3348 @end deftypefn") |
|
3349 { |
|
3350 return SET_INTERNAL_VARIABLE_WITH_LIMITS (output_precision, 0, INT_MAX); |
|
3351 } |
|
3352 |
|
3353 DEFUN (output_precision, args, nargout, |
|
3354 "-*- texinfo -*-\n\ |
|
3355 @deftypefn {Built-in Function} {@var{val} =} output_precision ()\n\ |
|
3356 @deftypefnx {Built-in Function} {@var{old_val} =} output_precision (@var{new_val})\n\ |
|
3357 Query or set the internal variable that specifies the minimum number of\n\ |
|
3358 significant figures to display for numeric output.\n\ |
|
3359 @seealso{format, output_max_field_width}\n\ |
|
3360 @end deftypefn") |
|
3361 { |
|
3362 return SET_INTERNAL_VARIABLE_WITH_LIMITS (output_precision, -1, INT_MAX); |
|
3363 } |
|
3364 |
|
3365 DEFUN (struct_levels_to_print, args, nargout, |
|
3366 "-*- texinfo -*-\n\ |
|
3367 @deftypefn {Built-in Function} {@var{val} =} struct_levels_to_print ()\n\ |
|
3368 @deftypefnx {Built-in Function} {@var{old_val} =} struct_levels_to_print (@var{new_val})\n\ |
|
3369 Query or set the internal variable that specifies the number of\n\ |
|
3370 structure levels to display.\n\ |
|
3371 @end deftypefn") |
|
3372 { |
|
3373 return SET_INTERNAL_VARIABLE_WITH_LIMITS (struct_levels_to_print, |
|
3374 -1, INT_MAX); |
2165
|
3375 } |
|
3376 |
1
|
3377 /* |
|
3378 ;;; Local Variables: *** |
|
3379 ;;; mode: C++ *** |
|
3380 ;;; End: *** |
|
3381 */ |