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