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