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