3Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
6This file is part of Octave.
8Octave is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
13Octave is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18You should have received a copy of the GNU General Public License
19along with Octave; see the file COPYING. If not, see
20<http://www.gnu.org/licenses/>.
37#include "Array-util.h"
42#include "lo-mappers.h"
54#include "oct-stream.h"
58#include "unwind-prot.h"
62// TRUE means use a scaled fixed point format for `format long' and
64static bool Vfixed_point_format = false;
66// The maximum field width for a number printed by the default output
68static int Voutput_max_field_width = 10;
70// The precision of the numbers printed by the default output
72static int Voutput_precision = 5;
74// TRUE means that the dimensions of empty objects should be printed
75// like this: x = [](2x0).
76bool Vprint_empty_dimensions = true;
78// TRUE means that the rows of big matrices should be split into
79// smaller slices that fit on the screen.
80static bool Vsplit_long_rows = true;
82// How many levels of structure elements should we print?
83int Vstruct_levels_to_print = 2;
85// TRUE means don't do any fancy formatting.
86static bool free_format = false;
88// TRUE means print plus sign for nonzero, blank for zero.
89static bool plus_format = false;
91// First char for > 0, second for < 0, third for == 0.
92static std::string plus_format_chars = "+ ";
94// TRUE means always print in a rational approximation
95static bool rat_format = false;
97// Used to force the length of the rational approximation string for Frats
98static int rat_string_len = -1;
100// TRUE means always print like dollars and cents.
101static bool bank_format = false;
103// TRUE means print data in hexadecimal format.
104static int hex_format = 0;
106// TRUE means print data in binary-bit-pattern format.
107static int bit_format = 0;
109// TRUE means don't put newlines around the column number headers.
110static bool compact_format = false;
112// TRUE means use an e format.
113static bool print_e = false;
115// TRUE means use a g format.
116static bool print_g = false;
118// TRUE means print E instead of e for exponent field.
119static bool print_big_e = false;
121class pr_formatted_float;
122class pr_rational_float;
125current_output_max_field_width (void)
127 return Voutput_max_field_width;
131current_output_precision (void)
133 return Voutput_precision;
141 float_format (int w = current_output_max_field_width (),
142 int p = current_output_precision (), int f = 0)
143 : fw (w), prec (p), fmt (f), up (0), sp (0) { }
145 float_format (const float_format& ff)
146 : fw (ff.fw), prec (ff.prec), fmt (ff.fmt), up (ff.up), sp (ff.sp) { }
148 float_format& operator = (const float_format& ff)
162 ~float_format (void) { }
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; }
168 float_format& uppercase (void) { up = std::ios::uppercase; return *this; }
169 float_format& lowercase (void) { up = 0; return *this; }
171 float_format& precision (int p) { prec = p; return *this; }
173 float_format& width (int w) { fw = w; return *this; }
175 float_format& trailing_zeros (bool tz = true)
176 { sp = tz ? std::ios::showpoint : 0; return *this; }
178 friend std::ostream& operator << (std::ostream& os,
179 const pr_formatted_float& pff);
181 friend std::ostream& operator << (std::ostream& os,
182 const pr_rational_float& pff);
186 // Field width. Zero means as wide as necessary.
198 // Show trailing zeros.
207 const float_format& f;
211 pr_formatted_float (const float_format& f_arg, double val_arg)
212 : f (f_arg), val (val_arg) { }
216operator << (std::ostream& os, const pr_formatted_float& pff)
219 os << std::setw (pff.f.fw);
222 os << std::setprecision (pff.f.prec);
224 std::ios::fmtflags oflags =
225 os.flags (static_cast<std::ios::fmtflags>
226 (pff.f.fmt | pff.f.up | pff.f.sp));
235static inline std::string
236rational_approx (double val, int len)
245 else if (xisnan (val))
247 else if (val < INT_MIN || val > INT_MAX || D_NINT (val) == val)
249 std::ostringstream buf;
250 buf.flags (std::ios::fixed);
251 buf << std::setprecision (0) << xround(val);
258 double n = xround (val);
260 double frac = val - n;
263 std::ostringstream buf2;
264 buf2.flags (std::ios::fixed);
265 buf2 << std::setprecision (0) << static_cast<int>(n);
270 double flip = 1. / frac;
271 double step = xround (flip);
275 // Have we converged to 1/intmax ?
276 if (m > 100 || fabs (frac) < 1 / static_cast<double>(INT_MAX))
284 n = n * step + lastn;
285 d = d * step + lastd;
289 std::ostringstream buf;
290 buf.flags (std::ios::fixed);
291 buf << std::setprecision (0) << static_cast<int>(n)
292 << "/" << static_cast<int>(d);
297 // Double negative, string can be two characters longer..
298 if (buf.str().length() > static_cast<unsigned int>(len + 2) &&
302 else if (buf.str().length() > static_cast<unsigned int>(len) &&
311 // Move sign to the top
314 std::ostringstream buf;
315 buf.flags (std::ios::fixed);
316 buf << std::setprecision (0) << static_cast<int>(lastn)
317 << "/" << static_cast<int>(lastd);
330 const float_format& f;
334 pr_rational_float (const float_format& f_arg, double val_arg)
335 : f (f_arg), val (val_arg) { }
339operator << (std::ostream& os, const pr_rational_float& prf)
341 int fw = (rat_string_len > 0 ? rat_string_len : prf.f.fw);
342 std::string s = rational_approx (prf.val, fw);
345 os << std::setw (fw);
347 std::ios::fmtflags oflags =
348 os.flags (static_cast<std::ios::fmtflags>
349 (prf.f.fmt | prf.f.up | prf.f.sp));
351 if (fw > 0 && s.length() > static_cast<unsigned int>(fw))
361// Current format for real numbers and the real part of complex
363static float_format *curr_real_fmt = 0;
365// Current format for the imaginary part of complex numbers.
366static float_format *curr_imag_fmt = 0;
369pr_max_internal (const Matrix& m)
371 octave_idx_type nr = m.rows ();
372 octave_idx_type nc = m.columns ();
374 double result = -DBL_MAX;
376 bool all_inf_or_nan = true;
378 for (octave_idx_type j = 0; j < nc; j++)
379 for (octave_idx_type i = 0; i < nr; i++)
382 if (xisinf (val) || xisnan (val))
385 all_inf_or_nan = false;
398pr_min_internal (const Matrix& m)
400 octave_idx_type nr = m.rows ();
401 octave_idx_type nc = m.columns ();
403 double result = DBL_MAX;
405 bool all_inf_or_nan = true;
407 for (octave_idx_type j = 0; j < nc; j++)
408 for (octave_idx_type i = 0; i < nr; i++)
411 if (xisinf (val) || xisnan (val))
414 all_inf_or_nan = false;
426// FIXME -- it would be nice to share more code among these
430set_real_format (int digits, bool inf_or_nan, bool int_only, int &fw)
432 static float_format fmt;
434 int prec = Voutput_precision;
443 else if (bank_format)
445 fw = digits < 0 ? 4 : digits + 3;
446 if (inf_or_nan && fw < 4)
452 fw = 2 * sizeof (double);
457 fw = 8 * sizeof (double);
460 else if (inf_or_nan || int_only)
463 if (inf_or_nan && fw < 4)
472 rd = prec > digits ? prec - digits : prec;
478 rd = prec > digits ? prec - digits : prec;
479 digits = -digits + 1;
482 fw = 1 + ld + 1 + rd;
483 if (inf_or_nan && fw < 4)
487 if (! (rat_format || bank_format || hex_format || bit_format)
488 && (fw > Voutput_max_field_width || print_e || print_g))
491 fmt = float_format ();
498 fw = 2 + prec + exp_field;
499 if (inf_or_nan && fw < 4)
502 fmt = float_format (fw, prec - 1, std::ios::scientific);
508 else if (! bank_format && (inf_or_nan || int_only))
509 fmt = float_format (fw, rd);
511 fmt = float_format (fw, rd, std::ios::fixed);
513 curr_real_fmt = &fmt;
517set_format (double d, int& fw)
525 bool inf_or_nan = (xisinf (d) || xisnan (d));
527 bool int_only = (! inf_or_nan && D_NINT (d) == d);
529 double d_abs = d < 0.0 ? -d : d;
531 int digits = (inf_or_nan || d_abs == 0.0)
532 ? 0 : static_cast<int> (floor (log10 (d_abs) + 1.0));
534 set_real_format (digits, inf_or_nan, int_only, fw);
545set_real_matrix_format (int x_max, int x_min, bool inf_or_nan,
546 int int_or_inf_or_nan, int& fw)
548 static float_format fmt;
550 int prec = Voutput_precision;
559 else if (bank_format)
561 int digits = x_max > x_min ? x_max : x_min;
562 fw = digits <= 0 ? 4 : digits + 3;
563 if (inf_or_nan && fw < 4)
569 fw = 2 * sizeof (double);
574 fw = 8 * sizeof (double);
577 else if (Vfixed_point_format && ! print_g)
581 if (inf_or_nan && fw < 4)
584 else if (int_or_inf_or_nan)
586 int digits = x_max > x_min ? x_max : x_min;
587 fw = digits <= 0 ? 2 : digits + 1;
588 if (inf_or_nan && fw < 4)
598 rd_max = prec > x_max ? prec - x_max : prec;
604 rd_max = prec > x_max ? prec - x_max : prec;
612 rd_min = prec > x_min ? prec - x_min : prec;
618 rd_min = prec > x_min ? prec - x_min : prec;
622 ld = ld_max > ld_min ? ld_max : ld_min;
623 rd = rd_max > rd_min ? rd_max : rd_min;
625 fw = 1 + ld + 1 + rd;
626 if (inf_or_nan && fw < 4)
630 if (! (rat_format || bank_format || hex_format || bit_format)
633 || (! Vfixed_point_format && fw > Voutput_max_field_width)))
636 fmt = float_format ();
640 if (x_max > 100 || x_min > 100)
643 fw = 2 + prec + exp_field;
644 if (inf_or_nan && fw < 4)
647 fmt = float_format (fw, prec - 1, std::ios::scientific);
653 else if (! bank_format && int_or_inf_or_nan)
654 fmt = float_format (fw, rd);
656 fmt = float_format (fw, rd, std::ios::fixed);
658 curr_real_fmt = &fmt;
662set_format (const Matrix& m, int& fw, double& scale)
670 bool inf_or_nan = m.any_element_is_inf_or_nan ();
672 bool int_or_inf_or_nan = m.all_elements_are_int_or_inf_or_nan ();
674 Matrix m_abs = m.abs ();
675 double max_abs = pr_max_internal (m_abs);
676 double min_abs = pr_min_internal (m_abs);
678 int x_max = max_abs == 0.0
679 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0));
681 int x_min = min_abs == 0.0
682 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0));
684 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1);
686 set_real_matrix_format (x_max, x_min, inf_or_nan, int_or_inf_or_nan, fw);
690set_format (const Matrix& m)
694 set_format (m, fw, scale);
698set_complex_format (int x_max, int x_min, int r_x, bool inf_or_nan,
699 int int_only, int& r_fw, int& i_fw)
701 static float_format r_fmt;
702 static float_format i_fmt;
704 int prec = Voutput_precision;
714 else if (bank_format)
718 r_fw = digits <= 0 ? 4 : digits + 3;
719 if (inf_or_nan && r_fw < 4)
725 r_fw = 2 * sizeof (double);
726 i_fw = 2 * sizeof (double);
731 r_fw = 8 * sizeof (double);
732 i_fw = 8 * sizeof (double);
735 else if (inf_or_nan || int_only)
737 int digits = x_max > x_min ? x_max : x_min;
738 i_fw = digits <= 0 ? 1 : digits;
740 if (inf_or_nan && i_fw < 3)
753 rd_max = prec > x_max ? prec - x_max : prec;
759 rd_max = prec > x_max ? prec - x_max : prec;
767 rd_min = prec > x_min ? prec - x_min : prec;
773 rd_min = prec > x_min ? prec - x_min : prec;
777 ld = ld_max > ld_min ? ld_max : ld_min;
778 rd = rd_max > rd_min ? rd_max : rd_min;
782 if (inf_or_nan && i_fw < 3)
789 if (! (rat_format || bank_format || hex_format || bit_format)
790 && (r_fw > Voutput_max_field_width || print_e || print_g))
794 r_fmt = float_format ();
795 i_fmt = float_format ();
800 if (x_max > 100 || x_min > 100)
803 i_fw = prec + exp_field;
805 if (inf_or_nan && i_fw < 3)
811 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific);
812 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific);
821 else if (! bank_format && (inf_or_nan || int_only))
823 r_fmt = float_format (r_fw, rd);
824 i_fmt = float_format (i_fw, rd);
828 r_fmt = float_format (r_fw, rd, std::ios::fixed);
829 i_fmt = float_format (i_fw, rd, std::ios::fixed);
832 curr_real_fmt = &r_fmt;
833 curr_imag_fmt = &i_fmt;
837set_format (const Complex& c, int& r_fw, int& i_fw)
845 double rp = c.real ();
846 double ip = c.imag ();
848 bool inf_or_nan = (xisinf (c) || xisnan (c));
850 bool int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip);
852 double r_abs = rp < 0.0 ? -rp : rp;
853 double i_abs = ip < 0.0 ? -ip : ip;
855 int r_x = (xisinf (rp) || xisnan (rp) || r_abs == 0.0)
856 ? 0 : static_cast<int> (floor (log10 (r_abs) + 1.0));
858 int i_x = (xisinf (ip) || xisnan (ip) || i_abs == 0.0)
859 ? 0 : static_cast<int> (floor (log10 (i_abs) + 1.0));
874 set_complex_format (x_max, x_min, r_x, inf_or_nan, int_only, r_fw, i_fw);
878set_format (const Complex& c)
881 set_format (c, r_fw, i_fw);
885set_complex_matrix_format (int x_max, int x_min, int r_x_max,
886 int r_x_min, bool inf_or_nan,
887 int int_or_inf_or_nan, int& r_fw, int& i_fw)
889 static float_format r_fmt;
890 static float_format i_fmt;
892 int prec = Voutput_precision;
902 else if (bank_format)
904 int digits = r_x_max > r_x_min ? r_x_max : r_x_min;
906 r_fw = digits <= 0 ? 4 : digits + 3;
907 if (inf_or_nan && r_fw < 4)
913 r_fw = 2 * sizeof (double);
914 i_fw = 2 * sizeof (double);
919 r_fw = 8 * sizeof (double);
920 i_fw = 8 * sizeof (double);
923 else if (Vfixed_point_format && ! print_g)
928 if (inf_or_nan && i_fw < 3)
934 else if (int_or_inf_or_nan)
936 int digits = x_max > x_min ? x_max : x_min;
937 i_fw = digits <= 0 ? 1 : digits;
939 if (inf_or_nan && i_fw < 3)
952 rd_max = prec > x_max ? prec - x_max : prec;
958 rd_max = prec > x_max ? prec - x_max : prec;
966 rd_min = prec > x_min ? prec - x_min : prec;
972 rd_min = prec > x_min ? prec - x_min : prec;
976 ld = ld_max > ld_min ? ld_max : ld_min;
977 rd = rd_max > rd_min ? rd_max : rd_min;
981 if (inf_or_nan && i_fw < 3)
988 if (! (rat_format || bank_format || hex_format || bit_format)
991 || (! Vfixed_point_format && r_fw > Voutput_max_field_width)))
995 r_fmt = float_format ();
996 i_fmt = float_format ();
1001 if (x_max > 100 || x_min > 100)
1004 i_fw = prec + exp_field;
1006 if (inf_or_nan && i_fw < 3)
1012 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific);
1013 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific);
1022 else if (! bank_format && int_or_inf_or_nan)
1024 r_fmt = float_format (r_fw, rd);
1025 i_fmt = float_format (i_fw, rd);
1029 r_fmt = float_format (r_fw, rd, std::ios::fixed);
1030 i_fmt = float_format (i_fw, rd, std::ios::fixed);
1033 curr_real_fmt = &r_fmt;
1034 curr_imag_fmt = &i_fmt;
1038set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw, double& scale)
1046 Matrix rp = real (cm);
1047 Matrix ip = imag (cm);
1049 bool inf_or_nan = cm.any_element_is_inf_or_nan ();
1051 bool int_or_inf_or_nan = (rp.all_elements_are_int_or_inf_or_nan ()
1052 && ip.all_elements_are_int_or_inf_or_nan ());
1054 Matrix r_m_abs = rp.abs ();
1055 double r_max_abs = pr_max_internal (r_m_abs);
1056 double r_min_abs = pr_min_internal (r_m_abs);
1058 Matrix i_m_abs = ip.abs ();
1059 double i_max_abs = pr_max_internal (i_m_abs);
1060 double i_min_abs = pr_min_internal (i_m_abs);
1062 int r_x_max = r_max_abs == 0.0
1063 ? 0 : static_cast<int> (floor (log10 (r_max_abs) + 1.0));
1065 int r_x_min = r_min_abs == 0.0
1066 ? 0 : static_cast<int> (floor (log10 (r_min_abs) + 1.0));
1068 int i_x_max = i_max_abs == 0.0
1069 ? 0 : static_cast<int> (floor (log10 (i_max_abs) + 1.0));
1071 int i_x_min = i_min_abs == 0.0
1072 ? 0 : static_cast<int> (floor (log10 (i_min_abs) + 1.0));
1074 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max;
1075 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min;
1077 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : std::pow (10.0, x_max - 1);
1079 set_complex_matrix_format (x_max, x_min, r_x_max, r_x_min, inf_or_nan,
1080 int_or_inf_or_nan, r_fw, i_fw);
1084set_format (const ComplexMatrix& cm)
1088 set_format (cm, r_fw, i_fw, scale);
1092set_range_format (int x_max, int x_min, int all_ints, int& fw)
1094 static float_format fmt;
1096 int prec = Voutput_precision;
1105 else if (bank_format)
1107 int digits = x_max > x_min ? x_max : x_min;
1108 fw = digits < 0 ? 5 : digits + 4;
1111 else if (hex_format)
1113 fw = 2 * sizeof (double);
1116 else if (bit_format)
1118 fw = 8 * sizeof (double);
1123 int digits = x_max > x_min ? x_max : x_min;
1127 else if (Vfixed_point_format && ! print_g)
1138 rd_max = prec > x_max ? prec - x_max : prec;
1144 rd_max = prec > x_max ? prec - x_max : prec;
1152 rd_min = prec > x_min ? prec - x_min : prec;
1158 rd_min = prec > x_min ? prec - x_min : prec;
1162 ld = ld_max > ld_min ? ld_max : ld_min;
1163 rd = rd_max > rd_min ? rd_max : rd_min;
1168 if (! (rat_format || bank_format || hex_format || bit_format)
1171 || (! Vfixed_point_format && fw > Voutput_max_field_width)))
1174 fmt = float_format ();
1178 if (x_max > 100 || x_min > 100)
1181 fw = 3 + prec + exp_field;
1183 fmt = float_format (fw, prec - 1, std::ios::scientific);
1189 else if (! bank_format && all_ints)
1190 fmt = float_format (fw, rd);
1192 fmt = float_format (fw, rd, std::ios::fixed);
1194 curr_real_fmt = &fmt;
1198set_format (const Range& r, int& fw, double& scale)
1206 double r_min = r.base ();
1207 double r_max = r.limit ();
1216 bool all_ints = r.all_elements_are_ints ();
1218 double max_abs = r_max < 0.0 ? -r_max : r_max;
1219 double min_abs = r_min < 0.0 ? -r_min : r_min;
1221 int x_max = max_abs == 0.0
1222 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0));
1224 int x_min = min_abs == 0.0
1225 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0));
1227 scale = (x_max == 0 || all_ints) ? 1.0 : std::pow (10.0, x_max - 1);
1229 set_range_format (x_max, x_min, all_ints, fw);
1233set_format (const Range& r)
1237 set_format (r, fw, scale);
1243 unsigned char i[sizeof (double)];
1246#define PRINT_CHAR_BITS(os, c) \
1249 unsigned char ctmp = c; \
1251 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \
1252 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \
1253 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \
1254 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \
1255 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \
1256 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \
1257 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \
1258 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \
1264#define PRINT_CHAR_BITS_SWAPPED(os, c) \
1267 unsigned char ctmp = c; \
1269 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \
1270 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \
1271 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \
1272 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \
1273 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \
1274 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \
1275 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \
1276 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \
1283pr_any_float (const float_format *fmt, std::ostream& os, double d, int fw = 0)
1287 // Unless explicitly asked for, always print in big-endian
1288 // format for hex and bit formats.
1290 // {bit,hex}_format == 1: print big-endian
1291 // {bit,hex}_format == 2: print native
1298 // Unless explicitly asked for, always print in big-endian
1301 // FIXME -- is it correct to swap bytes for VAX
1302 // formats and not for Cray?
1304 // FIXME -- will bad things happen if we are
1305 // interrupted before resetting the format flags and fill
1308 oct_mach_info::float_format flt_fmt =
1309 oct_mach_info::native_float_format ();
1311 char ofill = os.fill ('0');
1313 std::ios::fmtflags oflags
1314 = os.flags (std::ios::right | std::ios::hex);
1317 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian
1318 || flt_fmt == oct_mach_info::flt_fmt_cray
1319 || flt_fmt == oct_mach_info::flt_fmt_unknown)
1321 for (size_t i = 0; i < sizeof (double); i++)
1322 os << std::setw (2) << static_cast<int> (tmp.i[i]);
1326 for (int i = sizeof (double) - 1; i >= 0; i--)
1327 os << std::setw (2) << static_cast<int> (tmp.i[i]);
1333 else if (bit_format)
1338 // FIXME -- is it correct to swap bytes for VAX
1339 // formats and not for Cray?
1341 oct_mach_info::float_format flt_fmt =
1342 oct_mach_info::native_float_format ();
1344 if (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian
1345 || flt_fmt == oct_mach_info::flt_fmt_cray
1346 || flt_fmt == oct_mach_info::flt_fmt_unknown)
1348 for (size_t i = 0; i < sizeof (double); i++)
1349 PRINT_CHAR_BITS (os, tmp.i[i]);
1355 for (size_t i = 0; i < sizeof (double); i++)
1356 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]);
1360 for (int i = sizeof (double) - 1; i >= 0; i--)
1361 PRINT_CHAR_BITS (os, tmp.i[i]);
1365 else if (octave_is_NA (d))
1368 os << std::setw (fw) << "NA";
1372 else if (rat_format)
1373 os << pr_rational_float (*fmt, d);
1374 else if (xisinf (d))
1383 os << std::setw (fw) << s;
1387 else if (xisnan (d))
1390 os << std::setw (fw) << "NaN";
1395 os << pr_formatted_float (*fmt, d);
1402pr_float (std::ostream& os, double d, int fw = 0, double scale = 1.0)
1404 if (Vfixed_point_format && ! print_g && scale != 1.0)
1407 pr_any_float (curr_real_fmt, os, d, fw);
1411pr_imag_float (std::ostream& os, double d, int fw = 0)
1413 pr_any_float (curr_imag_fmt, os, d, fw);
1417pr_complex (std::ostream& os, const Complex& c, int r_fw = 0,
1418 int i_fw = 0, double scale = 1.0)
1421 = (Vfixed_point_format && ! print_g && scale != 1.0) ? c / scale : c;
1423 double r = tmp.real ();
1425 pr_float (os, r, r_fw);
1429 double i = tmp.imag ();
1430 if (! (hex_format || bit_format) && lo_ieee_signbit (i))
1434 pr_imag_float (os, i, i_fw);
1438 if (hex_format || bit_format)
1443 pr_imag_float (os, i, i_fw);
1450print_empty_matrix (std::ostream& os, octave_idx_type nr, octave_idx_type nc, bool pr_as_read_syntax)
1452 assert (nr == 0 || nc == 0);
1454 if (pr_as_read_syntax)
1456 if (nr == 0 && nc == 0)
1459 os << "zeros (" << nr << ", " << nc << ")";
1465 if (Vprint_empty_dimensions)
1466 os << "(" << nr << "x" << nc << ")";
1471print_empty_nd_array (std::ostream& os, const dim_vector& dims,
1472 bool pr_as_read_syntax)
1474 assert (dims.any_zero ());
1476 if (pr_as_read_syntax)
1477 os << "zeros (" << dims.str (',') << ")";
1482 if (Vprint_empty_dimensions)
1483 os << "(" << dims.str () << ")";
1488pr_scale_header (std::ostream& os, double scale)
1490 if (Vfixed_point_format && ! print_g && scale != 1.0)
1493 << std::setw (8) << std::setprecision (1)
1494 << std::setiosflags (std::ios::scientific|std::ios::left)
1496 << std::resetiosflags (std::ios::scientific|std::ios::left)
1499 if (! compact_format)
1505pr_col_num_header (std::ostream& os, octave_idx_type total_width, int max_width,
1506 octave_idx_type lim, octave_idx_type col, int extra_indent)
1508 if (total_width > max_width && Vsplit_long_rows)
1518 octave_idx_type num_cols = lim - col;
1520 os << std::setw (extra_indent) << "";
1523 os << " Column " << col + 1 << ":\n";
1524 else if (num_cols == 2)
1525 os << " Columns " << col + 1 << " and " << lim << ":\n";
1527 os << " Columns " << col + 1 << " through " << lim << ":\n";
1529 if (! compact_format)
1535/* static */ inline void
1536pr_plus_format (std::ostream& os, const T& val)
1539 os << plus_format_chars[0];
1540 else if (val < T (0))
1541 os << plus_format_chars[1];
1543 os << plus_format_chars[2];
1547octave_print_internal (std::ostream& os, double d,
1548 bool /* pr_as_read_syntax */)
1552 pr_plus_format (os, d);
1565octave_print_internal (std::ostream& os, const Matrix& m,
1566 bool pr_as_read_syntax, int extra_indent)
1568 octave_idx_type nr = m.rows ();
1569 octave_idx_type nc = m.columns ();
1571 if (nr == 0 || nc == 0)
1572 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
1573 else if (plus_format && ! pr_as_read_syntax)
1575 for (octave_idx_type i = 0; i < nr; i++)
1577 for (octave_idx_type j = 0; j < nc; j++)
1581 pr_plus_format (os, m(i,j));
1592 set_format (m, fw, scale);
1593 int column_width = fw + 2;
1594 octave_idx_type total_width = nc * column_width;
1595 octave_idx_type max_width = command_editor::terminal_cols ();
1597 if (pr_as_read_syntax)
1600 max_width -= extra_indent;
1607 if (pr_as_read_syntax)
1612 if (pr_as_read_syntax)
1618 octave_idx_type inc = nc;
1619 if (total_width > max_width && Vsplit_long_rows)
1621 inc = max_width / column_width;
1626 if (pr_as_read_syntax)
1628 for (octave_idx_type i = 0; i < nr; i++)
1630 octave_idx_type col = 0;
1633 octave_idx_type lim = col + inc < nc ? col + inc : nc;
1635 for (octave_idx_type j = col; j < lim; j++)
1639 if (i == 0 && j == 0)
1643 if (j > col && j < lim)
1649 pr_float (os, m(i,j));
1668 pr_scale_header (os, scale);
1670 for (octave_idx_type col = 0; col < nc; col += inc)
1672 octave_idx_type lim = col + inc < nc ? col + inc : nc;
1674 pr_col_num_header (os, total_width, max_width, lim, col,
1677 for (octave_idx_type i = 0; i < nr; i++)
1679 os << std::setw (extra_indent) << "";
1681 for (octave_idx_type j = col; j < lim; j++)
1687 pr_float (os, m(i,j), fw, scale);
1699octave_print_internal (std::ostream& os, const DiagMatrix& m,
1700 bool pr_as_read_syntax, int extra_indent)
1702 octave_idx_type nr = m.rows ();
1703 octave_idx_type nc = m.columns ();
1705 if (nr == 0 || nc == 0)
1706 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
1707 else if (plus_format && ! pr_as_read_syntax)
1709 for (octave_idx_type i = 0; i < nr; i++)
1711 for (octave_idx_type j = 0; j < nc; j++)
1715 pr_plus_format (os, m(i,j));
1726 set_format (Matrix (m.diag ()), fw, scale);
1727 int column_width = fw + 2;
1728 octave_idx_type total_width = nc * column_width;
1729 octave_idx_type max_width = command_editor::terminal_cols ();
1731 if (pr_as_read_syntax)
1734 max_width -= extra_indent;
1741 if (pr_as_read_syntax)
1746 if (pr_as_read_syntax)
1752 octave_idx_type inc = nc;
1753 if (total_width > max_width && Vsplit_long_rows)
1755 inc = max_width / column_width;
1760 if (pr_as_read_syntax)
1764 octave_idx_type col = 0;
1767 octave_idx_type lim = col + inc < nc ? col + inc : nc;
1769 for (octave_idx_type j = col; j < lim; j++)
1777 if (j > col && j < lim)
1783 pr_float (os, m(j,j));
1797 os << "Diagonal Matrix\n\n";
1798 pr_scale_header (os, scale);
1800 // kluge. Get the true width of a number.
1804 std::ostringstream tmp_oss;
1805 pr_float (tmp_oss, 0.0, fw, scale);
1806 zero_fw = tmp_oss.str ().length ();
1809 for (octave_idx_type col = 0; col < nc; col += inc)
1811 octave_idx_type lim = col + inc < nc ? col + inc : nc;
1813 pr_col_num_header (os, total_width, max_width, lim, col,
1816 for (octave_idx_type i = 0; i < nr; i++)
1818 os << std::setw (extra_indent) << "";
1820 for (octave_idx_type j = col; j < lim; j++)
1827 pr_float (os, m(i,j), fw, scale);
1829 os << std::setw (zero_fw) << '0';
1840#define PRINT_ND_ARRAY(os, nda, NDA_T, ELT_T, MAT_T) \
1843 if (nda.is_empty ()) \
1844 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax); \
1848 int ndims = nda.ndims (); \
1850 dim_vector dims = nda.dims (); \
1852 Array<octave_idx_type> ra_idx (ndims, 0); \
1854 octave_idx_type m = 1; \
1856 for (int i = 2; i < ndims; i++) \
1859 octave_idx_type nr = dims(0); \
1860 octave_idx_type nc = dims(1); \
1862 for (octave_idx_type i = 0; i < m; i++) \
1866 std::string nm = "ans"; \
1872 std::ostringstream buf; \
1874 for (int k = 2; k < ndims; k++) \
1876 buf << ra_idx(k) + 1; \
1878 if (k < ndims - 1) \
1887 Array<idx_vector> idx (ndims); \
1889 idx(0) = idx_vector (':'); \
1890 idx(1) = idx_vector (':'); \
1892 for (int k = 2; k < ndims; k++) \
1893 idx(k) = idx_vector (ra_idx(k)); \
1896 = MAT_T (Array2<ELT_T> (nda.index (idx), nr, nc)); \
1898 page.print_with_name (os, nm); \
1901 NDA_T::increment_index (ra_idx, dims, 2); \
1908octave_print_internal (std::ostream& os, const NDArray& nda,
1909 bool pr_as_read_syntax, int extra_indent)
1911 switch (nda.ndims ())
1915 octave_print_internal (os, nda.matrix_value (),
1916 pr_as_read_syntax, extra_indent);
1920 PRINT_ND_ARRAY (os, nda, NDArray, double, Matrix);
1926/* static */ inline void
1927pr_plus_format<> (std::ostream& os, const Complex& c)
1929 double rp = c.real ();
1930 double ip = c.imag ();
1940 pr_plus_format (os, rp);
1946octave_print_internal (std::ostream& os, const Complex& c,
1947 bool /* pr_as_read_syntax */)
1951 pr_plus_format (os, c);
1964octave_print_internal (std::ostream& os, const ComplexMatrix& cm,
1965 bool pr_as_read_syntax, int extra_indent)
1967 octave_idx_type nr = cm.rows ();
1968 octave_idx_type nc = cm.columns ();
1970 if (nr == 0 || nc == 0)
1971 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
1972 else if (plus_format && ! pr_as_read_syntax)
1974 for (octave_idx_type i = 0; i < nr; i++)
1976 for (octave_idx_type j = 0; j < nc; j++)
1980 pr_plus_format (os, cm(i,j));
1991 set_format (cm, r_fw, i_fw, scale);
1992 int column_width = i_fw + r_fw;
1993 column_width += (rat_format || bank_format || hex_format
1994 || bit_format) ? 2 : 7;
1995 octave_idx_type total_width = nc * column_width;
1996 octave_idx_type max_width = command_editor::terminal_cols ();
1998 if (pr_as_read_syntax)
2001 max_width -= extra_indent;
2008 if (pr_as_read_syntax)
2013 if (pr_as_read_syntax)
2019 octave_idx_type inc = nc;
2020 if (total_width > max_width && Vsplit_long_rows)
2022 inc = max_width / column_width;
2027 if (pr_as_read_syntax)
2029 for (octave_idx_type i = 0; i < nr; i++)
2031 octave_idx_type col = 0;
2034 octave_idx_type lim = col + inc < nc ? col + inc : nc;
2036 for (octave_idx_type j = col; j < lim; j++)
2040 if (i == 0 && j == 0)
2044 if (j > col && j < lim)
2050 pr_complex (os, cm(i,j));
2069 pr_scale_header (os, scale);
2071 for (octave_idx_type col = 0; col < nc; col += inc)
2073 octave_idx_type lim = col + inc < nc ? col + inc : nc;
2075 pr_col_num_header (os, total_width, max_width, lim, col,
2078 for (octave_idx_type i = 0; i < nr; i++)
2080 os << std::setw (extra_indent) << "";
2082 for (octave_idx_type j = col; j < lim; j++)
2088 pr_complex (os, cm(i,j), r_fw, i_fw, scale);
2100octave_print_internal (std::ostream& os, const ComplexDiagMatrix& cm,
2101 bool pr_as_read_syntax, int extra_indent)
2103 octave_idx_type nr = cm.rows ();
2104 octave_idx_type nc = cm.columns ();
2106 if (nr == 0 || nc == 0)
2107 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
2108 else if (plus_format && ! pr_as_read_syntax)
2110 for (octave_idx_type i = 0; i < nr; i++)
2112 for (octave_idx_type j = 0; j < nc; j++)
2116 pr_plus_format (os, cm(i,j));
2127 set_format (ComplexMatrix (cm.diag ()), r_fw, i_fw, scale);
2128 int column_width = i_fw + r_fw;
2129 column_width += (rat_format || bank_format || hex_format
2130 || bit_format) ? 2 : 7;
2131 octave_idx_type total_width = nc * column_width;
2132 octave_idx_type max_width = command_editor::terminal_cols ();
2134 if (pr_as_read_syntax)
2137 max_width -= extra_indent;
2144 if (pr_as_read_syntax)
2147 os << ComplexMatrix (cm);
2149 if (pr_as_read_syntax)
2155 octave_idx_type inc = nc;
2156 if (total_width > max_width && Vsplit_long_rows)
2158 inc = max_width / column_width;
2163 if (pr_as_read_syntax)
2167 octave_idx_type col = 0;
2170 octave_idx_type lim = col + inc < nc ? col + inc : nc;
2172 for (octave_idx_type j = col; j < lim; j++)
2180 if (j > col && j < lim)
2186 pr_complex (os, cm(j,j));
2200 os << "Diagonal Matrix\n\n";
2201 pr_scale_header (os, scale);
2203 // kluge. Get the true width of a number.
2207 std::ostringstream tmp_oss;
2208 pr_complex (tmp_oss, Complex (0.0), r_fw, i_fw, scale);
2209 zero_fw = tmp_oss.str ().length ();
2212 for (octave_idx_type col = 0; col < nc; col += inc)
2214 octave_idx_type lim = col + inc < nc ? col + inc : nc;
2216 pr_col_num_header (os, total_width, max_width, lim, col,
2219 for (octave_idx_type i = 0; i < nr; i++)
2221 os << std::setw (extra_indent) << "";
2223 for (octave_idx_type j = col; j < lim; j++)
2230 pr_complex (os, cm(i,j), r_fw, i_fw, scale);
2232 os << std::setw (zero_fw) << '0';
2244octave_print_internal (std::ostream& os, const PermMatrix& m,
2245 bool pr_as_read_syntax, int extra_indent)
2247 octave_idx_type nr = m.rows ();
2248 octave_idx_type nc = m.columns ();
2250 if (nr == 0 || nc == 0)
2251 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
2252 else if (plus_format && ! pr_as_read_syntax)
2254 for (octave_idx_type i = 0; i < nr; i++)
2256 for (octave_idx_type j = 0; j < nc; j++)
2260 pr_plus_format (os, m(i,j));
2270 int column_width = fw + 2;
2271 octave_idx_type total_width = nc * column_width;
2272 octave_idx_type max_width = command_editor::terminal_cols ();
2274 if (pr_as_read_syntax)
2277 max_width -= extra_indent;
2284 if (pr_as_read_syntax)
2289 if (pr_as_read_syntax)
2295 octave_idx_type inc = nc;
2296 if (total_width > max_width && Vsplit_long_rows)
2298 inc = max_width / column_width;
2303 if (pr_as_read_syntax)
2305 Array<octave_idx_type> pvec = m.pvec ();
2306 bool colp = m.is_col_perm ();
2309 if (colp) os << ":, ";
2311 octave_idx_type col = 0;
2314 octave_idx_type lim = col + inc < nc ? col + inc : nc;
2316 for (octave_idx_type j = col; j < lim; j++)
2324 if (j > col && j < lim)
2340 if (! colp) os << ", :";
2345 os << "Permutation Matrix\n\n";
2347 for (octave_idx_type col = 0; col < nc; col += inc)
2349 octave_idx_type lim = col + inc < nc ? col + inc : nc;
2351 pr_col_num_header (os, total_width, max_width, lim, col,
2354 for (octave_idx_type i = 0; i < nr; i++)
2356 os << std::setw (extra_indent) << "";
2358 for (octave_idx_type j = col; j < lim; j++)
2364 os << std::setw (fw) << m(i,j);
2376octave_print_internal (std::ostream& os, const ComplexNDArray& nda,
2377 bool pr_as_read_syntax, int extra_indent)
2379 switch (nda.ndims ())
2383 octave_print_internal (os, nda.matrix_value (),
2384 pr_as_read_syntax, extra_indent);
2388 PRINT_ND_ARRAY (os, nda, ComplexNDArray, Complex, ComplexMatrix);
2394octave_print_internal (std::ostream& os, bool d, bool pr_as_read_syntax)
2396 octave_print_internal (os, double (d), pr_as_read_syntax);
2399// FIXME -- write single precision versions of the printing functions.
2402octave_print_internal (std::ostream& os, float d, bool pr_as_read_syntax)
2404 octave_print_internal (os, double (d), pr_as_read_syntax);
2408octave_print_internal (std::ostream& os, const FloatMatrix& m,
2409 bool pr_as_read_syntax, int extra_indent)
2411 octave_print_internal (os, Matrix (m), pr_as_read_syntax, extra_indent);
2415octave_print_internal (std::ostream& os, const FloatDiagMatrix& m,
2416 bool pr_as_read_syntax, int extra_indent)
2418 octave_print_internal (os, DiagMatrix (m), pr_as_read_syntax, extra_indent);
2422octave_print_internal (std::ostream& os, const FloatNDArray& nda,
2423 bool pr_as_read_syntax, int extra_indent)
2425 octave_print_internal (os, NDArray (nda), pr_as_read_syntax, extra_indent);
2429octave_print_internal (std::ostream& os, const FloatComplex& c,
2430 bool pr_as_read_syntax)
2432 octave_print_internal (os, Complex (c), pr_as_read_syntax);
2436octave_print_internal (std::ostream& os, const FloatComplexMatrix& cm,
2437 bool pr_as_read_syntax, int extra_indent)
2439 octave_print_internal (os, ComplexMatrix (cm), pr_as_read_syntax, extra_indent);
2443octave_print_internal (std::ostream& os, const FloatComplexDiagMatrix& cm,
2444 bool pr_as_read_syntax, int extra_indent)
2446 octave_print_internal (os, ComplexDiagMatrix (cm), pr_as_read_syntax, extra_indent);
2450octave_print_internal (std::ostream& os, const FloatComplexNDArray& nda,
2451 bool pr_as_read_syntax, int extra_indent)
2453 octave_print_internal (os, ComplexNDArray (nda), pr_as_read_syntax, extra_indent);
2457octave_print_internal (std::ostream& os, const Range& r,
2458 bool pr_as_read_syntax, int extra_indent)
2460 double base = r.base ();
2461 double increment = r.inc ();
2462 double limit = r.limit ();
2463 octave_idx_type num_elem = r.nelem ();
2465 if (plus_format && ! pr_as_read_syntax)
2467 for (octave_idx_type i = 0; i < num_elem; i++)
2471 double val = base + i * increment;
2473 pr_plus_format (os, val);
2480 set_format (r, fw, scale);
2482 if (pr_as_read_syntax)
2486 os << base << " : ";
2487 if (increment != 1.0)
2488 os << increment << " : ";
2493 pr_float (os, base, fw);
2495 if (increment != 1.0)
2497 pr_float (os, increment, fw);
2500 pr_float (os, limit, fw);
2505 int column_width = fw + 2;
2506 octave_idx_type total_width = num_elem * column_width;
2507 octave_idx_type max_width = command_editor::terminal_cols ();
2515 octave_idx_type inc = num_elem;
2516 if (total_width > max_width && Vsplit_long_rows)
2518 inc = max_width / column_width;
2523 max_width -= extra_indent;
2528 pr_scale_header (os, scale);
2530 octave_idx_type col = 0;
2531 while (col < num_elem)
2533 octave_idx_type lim = col + inc < num_elem ? col + inc : num_elem;
2535 pr_col_num_header (os, total_width, max_width, lim, col,
2538 os << std::setw (extra_indent) << "";
2540 for (octave_idx_type i = col; i < lim; i++)
2544 double val = base + i * increment;
2546 if (i == num_elem - 1)
2548 // See the comments in Range::matrix_value.
2550 if ((increment > 0 && val > limit)
2551 || (increment < 0 && val < limit))
2557 pr_float (os, val, fw, scale);
2567octave_print_internal (std::ostream& os, const boolMatrix& bm,
2568 bool pr_as_read_syntax,
2572 octave_print_internal (os, tmp, pr_as_read_syntax, extra_indent);
2576octave_print_internal (std::ostream& os, const boolNDArray& nda,
2577 bool pr_as_read_syntax,
2580 switch (nda.ndims ())
2584 octave_print_internal (os, nda.matrix_value (),
2585 pr_as_read_syntax, extra_indent);
2589 PRINT_ND_ARRAY (os, nda, boolNDArray, bool, boolMatrix);
2595octave_print_internal (std::ostream& os, const charMatrix& chm,
2596 bool pr_as_read_syntax,
2597 int /* extra_indent FIXME */,
2602 octave_idx_type nstr = chm.rows ();
2604 if (pr_as_read_syntax && nstr > 1)
2609 for (octave_idx_type i = 0; i < nstr; i++)
2613 std::string row = chm.row_as_string (i);
2615 if (pr_as_read_syntax)
2617 os << "\"" << undo_string_escapes (row) << "\"";
2632 if (pr_as_read_syntax && nstr > 1)
2637 os << "sorry, printing char matrices not implemented yet\n";
2642octave_print_internal (std::ostream& os, const charNDArray& nda,
2643 bool pr_as_read_syntax, int extra_indent,
2646 switch (nda.ndims ())
2650 octave_print_internal (os, nda.matrix_value (),
2651 pr_as_read_syntax, extra_indent, pr_as_string);
2655 PRINT_ND_ARRAY (os, nda, charNDArray, char, charMatrix);
2661octave_print_internal (std::ostream& os, const std::string& s,
2662 bool pr_as_read_syntax, int extra_indent)
2664 Array<std::string> nda (dim_vector (1, 1), s);
2666 octave_print_internal (os, nda, pr_as_read_syntax, extra_indent);
2670octave_print_internal (std::ostream& os, const Array<std::string>& nda,
2671 bool pr_as_read_syntax, int /* extra_indent */)
2673 // FIXME -- this mostly duplicates the code in the
2674 // PRINT_ND_ARRAY macro.
2676 if (nda.is_empty ())
2677 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax);
2678 else if (nda.length () == 1)
2684 int ndims = nda.ndims ();
2686 dim_vector dims = nda.dims ();
2688 Array<octave_idx_type> ra_idx (ndims, 0);
2690 octave_idx_type m = 1;
2692 for (int i = 2; i < ndims; i++)
2695 octave_idx_type nr = dims(0);
2696 octave_idx_type nc = dims(1);
2698 for (octave_idx_type i = 0; i < m; i++)
2700 std::string nm = "ans";
2706 std::ostringstream buf;
2708 for (int k = 2; k < ndims; k++)
2710 buf << ra_idx(k) + 1;
2721 Array<idx_vector> idx (ndims);
2723 idx(0) = idx_vector (':');
2724 idx(1) = idx_vector (':');
2726 for (int k = 2; k < ndims; k++)
2727 idx(k) = idx_vector (ra_idx(k));
2729 Array2<std::string> page (nda.index (idx), nr, nc);
2731 // FIXME -- need to do some more work to put these
2732 // in neatly aligned columns...
2734 octave_idx_type n_rows = page.rows ();
2735 octave_idx_type n_cols = page.cols ();
2737 os << nm << " =\n\n";
2739 for (octave_idx_type ii = 0; ii < n_rows; ii++)
2741 for (octave_idx_type jj = 0; jj < n_cols; jj++)
2742 os << " " << page(ii,jj);
2751 increment_index (ra_idx, dims, 2);
2761 typedef T print_conv_type;
2764#define PRINT_CONV(T1, T2) \
2767 octave_print_conv<T1> \
2770 typedef T2 print_conv_type; \
2773PRINT_CONV (octave_int8, octave_int16);
2774PRINT_CONV (octave_uint8, octave_uint16);
2779/* static */ inline void
2780pr_int (std::ostream& os, const T& d, int fw = 0)
2782 size_t sz = d.byte_size();
2783 const unsigned char * tmpi = d.iptr();
2785 // Unless explicitly asked for, always print in big-endian
2786 // format for hex and bit formats.
2788 // {bit,hex}_format == 1: print big-endian
2789 // {bit,hex}_format == 2: print native
2793 char ofill = os.fill ('0');
2795 std::ios::fmtflags oflags
2796 = os.flags (std::ios::right | std::ios::hex);
2798 if (hex_format > 1 || oct_mach_info::words_big_endian ())
2800 for (size_t i = 0; i < sz; i++)
2801 os << std::setw (2) << static_cast<int> (tmpi[i]);
2805 for (int i = sz - 1; i >= 0; i--)
2806 os << std::setw (2) << static_cast<int> (tmpi[i]);
2812 else if (bit_format)
2814 if (oct_mach_info::words_big_endian ())
2816 for (size_t i = 0; i < sz; i++)
2817 PRINT_CHAR_BITS (os, tmpi[i]);
2823 for (size_t i = 0; i < sz; i++)
2824 PRINT_CHAR_BITS_SWAPPED (os, tmpi[i]);
2828 for (int i = sz - 1; i >= 0; i--)
2829 PRINT_CHAR_BITS (os, tmpi[i]);
2835 os << std::setw (fw)
2836 << typename octave_print_conv<T>::print_conv_type (d);
2843// FIXME -- all this mess with abs is an attempt to avoid seeing
2845// warning: comparison of unsigned expression < 0 is always false
2847// from GCC. Isn't there a better way
2850/* static */ inline T
2853 return x < 0 ? -x : x;
2856#define INSTANTIATE_ABS(T) \
2857 template /* static */ inline T abs (T)
2859INSTANTIATE_ABS(signed char);
2860INSTANTIATE_ABS(short);
2861INSTANTIATE_ABS(int);
2862INSTANTIATE_ABS(long);
2863INSTANTIATE_ABS(long long);
2865#define SPECIALIZE_UABS(T) \
2867 /* static */ inline unsigned T \
2868 abs (unsigned T x) \
2873SPECIALIZE_UABS(char)
2874SPECIALIZE_UABS(short)
2876SPECIALIZE_UABS(long)
2877SPECIALIZE_UABS(long long)
2880pr_int (std::ostream&, const octave_int8&, int);
2883pr_int (std::ostream&, const octave_int16&, int);
2886pr_int (std::ostream&, const octave_int32&, int);
2889pr_int (std::ostream&, const octave_int64&, int);
2892pr_int (std::ostream&, const octave_uint8&, int);
2895pr_int (std::ostream&, const octave_uint16&, int);
2898pr_int (std::ostream&, const octave_uint32&, int);
2901pr_int (std::ostream&, const octave_uint64&, int);
2905octave_print_internal_template (std::ostream& os, const octave_int<T>& val,
2910 pr_plus_format (os, val);
2915 os << typename octave_print_conv<octave_int<T> >::print_conv_type (val);
2921#define PRINT_INT_SCALAR_INTERNAL(TYPE) \
2922 OCTINTERP_API void \
2923 octave_print_internal (std::ostream& os, const octave_int<TYPE>& val, bool dummy) \
2925 octave_print_internal_template (os, val, dummy); \
2928PRINT_INT_SCALAR_INTERNAL (int8_t)
2929PRINT_INT_SCALAR_INTERNAL (uint8_t)
2930PRINT_INT_SCALAR_INTERNAL (int16_t)
2931PRINT_INT_SCALAR_INTERNAL (uint16_t)
2932PRINT_INT_SCALAR_INTERNAL (int32_t)
2933PRINT_INT_SCALAR_INTERNAL (uint32_t)
2934PRINT_INT_SCALAR_INTERNAL (int64_t)
2935PRINT_INT_SCALAR_INTERNAL (uint64_t)
2938/* static */ inline void
2939octave_print_internal_template (std::ostream& os, const intNDArray<T>& nda,
2940 bool pr_as_read_syntax, int extra_indent)
2942 // FIXME -- this mostly duplicates the code in the
2943 // PRINT_ND_ARRAY macro.
2945 if (nda.is_empty ())
2946 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax);
2947 else if (nda.length () == 1)
2948 octave_print_internal_template (os, nda(0), pr_as_read_syntax);
2949 else if (plus_format && ! pr_as_read_syntax)
2951 int ndims = nda.ndims ();
2953 Array<octave_idx_type> ra_idx (ndims, 0);
2955 dim_vector dims = nda.dims ();
2957 octave_idx_type m = 1;
2959 for (int i = 2; i < ndims; i++)
2962 octave_idx_type nr = dims(0);
2963 octave_idx_type nc = dims(1);
2965 for (octave_idx_type i = 0; i < m; i++)
2969 std::string nm = "ans(:,:,";
2971 std::ostringstream buf;
2973 for (int k = 2; k < ndims; k++)
2975 buf << ra_idx(k) + 1;
2985 os << nm << " =\n\n";
2988 Array<idx_vector> idx (ndims);
2990 idx(0) = idx_vector (':');
2991 idx(1) = idx_vector (':');
2993 for (int k = 2; k < ndims; k++)
2994 idx(k) = idx_vector (ra_idx(k));
2996 Array2<T> page (nda.index (idx), nr, nc);
2998 for (octave_idx_type ii = 0; ii < nr; ii++)
3000 for (octave_idx_type jj = 0; jj < nc; jj++)
3004 pr_plus_format (os, page(ii,jj));
3007 if ((ii < nr - 1) || (i < m -1))
3014 increment_index (ra_idx, dims, 2);
3020 int ndims = nda.ndims ();
3022 dim_vector dims = nda.dims ();
3024 Array<octave_idx_type> ra_idx (ndims, 0);
3026 octave_idx_type m = 1;
3028 for (int i = 2; i < ndims; i++)
3031 octave_idx_type nr = dims(0);
3032 octave_idx_type nc = dims(1);
3036 fw = 2 * nda(0).byte_size ();
3037 else if (bit_format)
3038 fw = nda(0).nbits ();
3044 for (octave_idx_type i = 0; i < dims.numel (); i++)
3046 int new_digits = static_cast<int>
3047 (floor (log10 (double (abs (nda(i).value ()))) + 1.0));
3049 if (new_digits > digits)
3050 digits = new_digits;
3053 isneg = (abs (nda(i).value ()) != nda(i).value ());
3056 fw = digits + isneg;
3059 int column_width = fw + (rat_format ? 0 : (bank_format ? 5 : 2));
3060 octave_idx_type total_width = nc * column_width;
3061 int max_width = command_editor::terminal_cols () - extra_indent;
3062 octave_idx_type inc = nc;
3063 if (total_width > max_width && Vsplit_long_rows)
3065 inc = max_width / column_width;
3070 for (octave_idx_type i = 0; i < m; i++)
3074 std::string nm = "ans(:,:,";
3076 std::ostringstream buf;
3078 for (int k = 2; k < ndims; k++)
3080 buf << ra_idx(k) + 1;
3090 os << nm << " =\n\n";
3093 Array<idx_vector> idx (ndims);
3095 idx(0) = idx_vector (':');
3096 idx(1) = idx_vector (':');
3098 for (int k = 2; k < ndims; k++)
3099 idx(k) = idx_vector (ra_idx(k));
3101 Array2<T> page (nda.index (idx), nr, nc);
3105 if (pr_as_read_syntax)
3108 for (octave_idx_type ii = 0; ii < nr; ii++)
3110 for (octave_idx_type jj = 0; jj < nc; jj++)
3114 os << typename octave_print_conv<T>::print_conv_type (page(ii,jj));
3119 if (pr_as_read_syntax)
3124 octave_idx_type n_rows = page.rows ();
3125 octave_idx_type n_cols = page.cols ();
3127 for (octave_idx_type col = 0; col < n_cols; col += inc)
3129 octave_idx_type lim = col + inc < n_cols ? col + inc : n_cols;
3131 pr_col_num_header (os, total_width, max_width, lim, col,
3134 for (octave_idx_type ii = 0; ii < n_rows; ii++)
3136 os << std::setw (extra_indent) << "";
3138 for (octave_idx_type jj = col; jj < lim; jj++)
3142 pr_int (os, page(ii,jj), fw);
3144 if ((ii < n_rows - 1) || (i < m -1))
3153 increment_index (ra_idx, dims, 2);
3159#define PRINT_INT_ARRAY_INTERNAL(TYPE) \
3160 OCTINTERP_API void \
3161 octave_print_internal (std::ostream& os, const intNDArray<TYPE>& nda, \
3162 bool pr_as_read_syntax, int extra_indent) \
3164 octave_print_internal_template (os, nda, pr_as_read_syntax, extra_indent); \
3167PRINT_INT_ARRAY_INTERNAL (octave_int8)
3168PRINT_INT_ARRAY_INTERNAL (octave_uint8)
3169PRINT_INT_ARRAY_INTERNAL (octave_int16)
3170PRINT_INT_ARRAY_INTERNAL (octave_uint16)
3171PRINT_INT_ARRAY_INTERNAL (octave_int32)
3172PRINT_INT_ARRAY_INTERNAL (octave_uint32)
3173PRINT_INT_ARRAY_INTERNAL (octave_int64)
3174PRINT_INT_ARRAY_INTERNAL (octave_uint64)
3177octave_print_internal (std::ostream&, const Cell&, bool, int, bool)
3179 panic_impossible ();
3182DEFUN (rats, args, nargout,
3184@deftypefn {Built-in Function} {} rats (@var{x}, @var{len})\n\
3185Convert @var{x} into a rational approximation represented as a string.\n\
3186You can convert the string back into a matrix as follows:\n\
3190 r = rats(hilb(4));\n\
3195The optional second argument defines the maximum length of the string\n\
3196representing the elements of @var{x}. By default @var{len} is 9.\n\
3197@seealso{format, rat}\n\
3200 octave_value retval;
3202 int nargin = args.length ();
3204 if (nargin < 1 || nargin > 2 || nargout > 1)
3208 unwind_protect frame;
3210 frame.protect_var (rat_string_len);
3215 rat_string_len = args(1).nint_value ();
3219 octave_value arg = args(0);
3221 if (arg.is_numeric_type ())
3223 frame.protect_var (rat_format);
3227 std::ostringstream buf;
3228 args(0).print (buf);
3229 std::string s = buf.str ();
3231 std::list<std::string> lst;
3234 size_t s_len = s.length ();
3238 size_t m = s.find ('\n', n);
3240 if (m == std::string::npos)
3242 lst.push_back (s.substr (n));
3247 lst.push_back (s.substr (n, m - n));
3252 retval = string_vector (lst);
3255 error ("rats: expecting numeric input");
3262DEFUN (disp, args, nargout,
3264@deftypefn {Built-in Function} {} disp (@var{x})\n\
3265Display the value of @var{x}. For example,\n\
3269disp (\"The value of pi is:\"), disp (pi)\n\
3271 @print{} the value of pi is:\n\
3277Note that the output from @code{disp} always ends with a newline.\n\
3279If an output value is requested, @code{disp} prints nothing and\n\
3280returns the formatted output in a string.\n\
3284 octave_value_list retval;
3286 int nargin = args.length ();
3288 if (nargin == 1 && nargout < 2)
3291 args(0).print (octave_stdout);
3294 octave_value arg = args(0);
3295 std::ostringstream buf;
3297 retval = octave_value (buf.str (), arg.is_dq_string () ? '"' : '\'');
3306DEFUN (fdisp, args, ,
3308@deftypefn {Built-in Function} {} fdisp (@var{fid}, @var{x})\n\
3309Display the value of @var{x} on the stream @var{fid}. For example,\n\
3313fdisp (stdout, \"The value of pi is:\"), fdisp (stdout, pi)\n\
3315 @print{} the value of pi is:\n\
3321Note that the output from @code{fdisp} always ends with a newline.\n\
3325 octave_value_list retval;
3327 int nargin = args.length ();
3331 int fid = octave_stream_list::get_file_number (args (0));
3333 octave_stream os = octave_stream_list::lookup (fid, "fdisp");
3337 std::ostream *osp = os.output_stream ();
3340 args(1).print (*osp);
3342 error ("fdisp: stream not open for writing");
3355%! for r = [0, Inf -Inf, NaN]
3356%! for i = [0, Inf -Inf, NaN]
3357%! fdisp (fd, complex (r, i));
3364init_format_state (void)
3366 free_format = false;
3367 plus_format = false;
3369 bank_format = false;
3372 compact_format = false;
3374 print_big_e = false;
3379set_output_prec_and_fw (int prec, int fw)
3381 Voutput_precision = prec;
3382 Voutput_max_field_width = fw;
3386set_format_style (int argc, const string_vector& argv)
3392 std::string arg = argv[idx++];
3402 init_format_state ();
3405 else if (arg == "E")
3407 init_format_state ();
3411 else if (arg == "g")
3413 init_format_state ();
3416 else if (arg == "G")
3418 init_format_state ();
3424 error ("format: unrecognized option `short %s'",
3430 init_format_state ();
3432 set_output_prec_and_fw (5, 10);
3434 else if (arg == "long")
3442 init_format_state ();
3445 else if (arg == "E")
3447 init_format_state ();
3451 else if (arg == "g")
3453 init_format_state ();
3456 else if (arg == "G")
3458 init_format_state ();
3464 error ("format: unrecognized option `long %s'",
3470 init_format_state ();
3472 set_output_prec_and_fw (15, 20);
3474 else if (arg == "hex")
3476 init_format_state ();
3479 else if (arg == "native-hex")
3481 init_format_state ();
3484 else if (arg == "bit")
3486 init_format_state ();
3489 else if (arg == "native-bit")
3491 init_format_state ();
3494 else if (arg == "+" || arg == "plus")
3500 if (arg.length () == 3)
3501 plus_format_chars = arg;
3504 error ("format: invalid option for plus format");
3509 plus_format_chars = "+ ";
3511 init_format_state ();
3514 else if (arg == "rat")
3516 init_format_state ();
3519 else if (arg == "bank")
3521 init_format_state ();
3524 else if (arg == "free")
3526 init_format_state ();
3529 else if (arg == "none")
3531 init_format_state ();
3534 else if (arg == "compact")
3536 compact_format = true;
3538 else if (arg == "loose")
3540 compact_format = false;
3543 error ("format: unrecognized format state `%s'", arg.c_str ());
3547 init_format_state ();
3548 set_output_prec_and_fw (5, 10);
3552DEFUN (format, args, ,
3554@deffn {Command} format\n\
3555@deffnx {Command} format options\n\
3556Reset or specify the format of the output produced by @code{disp} and\n\
3557Octave's normal echoing mechanism. This command only affects the display\n\
3558of numbers but not how they are stored or computed. To change the internal\n\
3559representation from the default double use one of the conversion functions\n\
3560such as @code{single}, @code{uint8}, @code{int64}, etc.\n\
3562By default, Octave displays 5 significant digits in a human readable form\n\
3563(option @samp{short} paired with @samp{loose} format for matrices).\n\
3564If @code{format} is invoked without any options, this default format\n\
3567Valid formats for floating point numbers are listed in the following\n\
3572Fixed point format with 5 significant figures in a field that is a maximum\n\
3573of 10 characters wide. (default).\n\
3575If Octave is unable to format a matrix so that columns line up on the\n\
3576decimal point and all numbers fit within the maximum field width then\n\
3577it switches to an exponential @samp{e} format.\n\
3580Fixed point format with 15 significant figures in a field that is a maximum\n\
3581of 20 characters wide.\n\
3583As with the @samp{short} format, Octave will switch to an exponential\n\
3584@samp{e} format if it is unable to format a matrix properly using the\n\
3589Exponential format. The number to be represented is split between a mantissa\n\
3590and an exponent (power of 10). The mantissa has 5 significant digits in the\n\
3591short format and 15 digits in the long format.\n\
3592For example, with the @samp{short e} format, @code{pi} is displayed as\n\
3593@code{3.1416e+00}.\n\
3597Identical to @samp{short e} or @samp{long e} but displays an uppercase\n\
3598@samp{E} to indicate the exponent.\n\
3599For example, with the @samp{long E} format, @code{pi} is displayed as\n\
3600@code{3.14159265358979E+00}.\n\
3604Optimally choose between fixed point and exponential format based on\n\
3605the magnitude of the number.\n\
3606For example, with the @samp{short g} format,\n\
3607@code{pi .^ [2; 4; 8; 16; 32]} is displayed as\n\
3623Identical to @samp{short g} or @samp{long g} but displays an uppercase\n\
3624@samp{E} to indicate the exponent.\n\
3628Print output in free format, without trying to line up columns of\n\
3629matrices on the decimal point. This also causes complex numbers to be\n\
3630formatted as numeric pairs like this @samp{(0.60419, 0.60709)} instead\n\
3631of like this @samp{0.60419 + 0.60709i}.\n\
3634The following formats affect all numeric output (floating point and\n\
3639@itemx + @var{chars}\n\
3641@itemx plus @var{chars}\n\
3642Print a @samp{+} symbol for nonzero matrix elements and a space for zero\n\
3643matrix elements. This format can be very useful for examining the\n\
3644structure of a large sparse matrix.\n\
3646The optional argument @var{chars} specifies a list of 3 characters to use\n\
3647for printing values greater than zero, less than zero and equal to zero.\n\
3648For example, with the @samp{+ \"+-.\"} format, @code{[1, 0, -1; -1, 0, 1]}\n\
3661Print in a fixed format with two digits to the right of the decimal\n\
3665Print the hexadecimal representation of numbers as they are stored in\n\
3666memory. For example, on a workstation which stores 8 byte real values\n\
3667in IEEE format with the least significant byte first, the value of\n\
3668@code{pi} when printed in @code{native-hex} format is @code{400921fb54442d18}.\n\
3671The same as @code{native-hex}, but always print the most significant\n\
3674Print the bit representation of numbers as stored in memory.\n\
3675For example, the value of @code{pi} is\n\
367901000000000010010010000111111011\n\
368001010100010001000010110100011000\n\