26 #if defined (HAVE_CONFIG_H)
67 static bool Vfixed_point_format =
false;
75 static bool Vsplit_long_rows =
true;
78 static bool free_format =
false;
81 static bool plus_format =
false;
84 static std::string plus_format_chars =
"+- ";
87 static bool rat_format =
false;
90 static int rat_string_len = -1;
93 static bool bank_format =
false;
96 static int hex_format = 0;
99 static int bit_format = 0;
105 static bool print_e =
false;
108 static bool print_g =
false;
111 static bool uppercase_format =
false;
114 static bool print_eng =
false;
117 calc_scale_exp (
const int&
x)
122 return x - 3*
static_cast<int> (
x/3);
139 template <
typename T>
141 engineering_exponent (T
x)
147 T absval = (
x < 0 ? -
x :
x);
148 int logabsval =
static_cast<int> (
std::floor (log10 (absval)));
154 ex = logabsval - 2 + ((-logabsval + 2) % 3);
156 ex = logabsval - (logabsval % 3);
162 template <
typename T>
166 return 1 + (print_eng
167 ? engineering_exponent (
x)
168 : static_cast<int> (std::
floor (log10 (
x))));
171 template <
typename T>
175 return engineering_exponent (m_val);
178 template <
typename T>
182 return m_val /
std::pow (
static_cast<T
> (10), exponent ());
185 template <
typename T>
189 octave::preserve_stream_state stream_state (os);
193 if (real_fmt.
width () >= 0)
197 os << std::setprecision (real_fmt.
precision ());
206 if (uppercase_format)
207 os << std::setw (0) <<
"E-";
209 os << std::setw (0) <<
"e-";
214 if (uppercase_format)
215 os << std::setw (0) <<
"E+";
217 os << std::setw (0) <<
"e+";
221 << std::setfill (
'0') << ex;
226 template <
typename T>
230 octave::preserve_stream_state stream_state (os);
234 if (real_fmt.
width () >= 0)
235 os << std::setw (real_fmt.
width ());
238 os << std::setprecision (real_fmt.
precision ());
247 template <
typename T>
251 octave::preserve_stream_state stream_state (os);
254 bool have_neg_sign = prf.
m_val < 0;
256 int fw = (rat_string_len > 0 ? rat_string_len : real_fmt.
width ());
265 os << std::setw (fw);
273 if (s.find (
'/') != std::string::npos)
275 if (s.length () > (
static_cast<unsigned int> (fw)))
282 if (s.length () > (
static_cast<unsigned int> (fw) - 2))
287 if (s.length () > (
static_cast<unsigned int> (fw) - 3))
298 template <
typename T>
308 T result = std::numeric_limits<T>::lowest ();
310 bool all_inf_or_nan =
true;
319 all_inf_or_nan =
false;
331 template <
typename T>
340 bool all_inf_or_nan =
true;
349 all_inf_or_nan =
false;
362 struct pr_output_traits
364 static const int DIGITS10;
365 static const int MAX_FIELD_WIDTH;
369 struct pr_output_traits<double>
371 static const int DIGITS10;
372 static const int MAX_FIELD_WIDTH;
375 const int pr_output_traits<double>::DIGITS10 = 16;
376 const int pr_output_traits<double>::MAX_FIELD_WIDTH = 21;
379 struct pr_output_traits<float>
381 static const int DIGITS10;
382 static const int MAX_FIELD_WIDTH;
385 const int pr_output_traits<float>::DIGITS10 = 8;
386 const int pr_output_traits<float>::MAX_FIELD_WIDTH = 13;
392 template <
typename T>
394 make_real_format (
int digits,
bool inf_or_nan,
bool int_only)
400 int fw = 0, ld = 0, rd = 0;
407 else if (bank_format)
409 fw = (digits < 0 ? 4 : digits + 3);
439 rd = (prec > digits ? prec - digits : prec);
444 rd = (prec > digits ? prec - digits : prec);
449 rd = (prec > digits ? prec - 1 : prec);
455 if (! (rat_format || bank_format || hex_format || bit_format)
456 && (print_e || print_g || print_eng
457 || ld + rd > pr_output_traits<T>::DIGITS10
458 || fw > pr_output_traits<T>::MAX_FIELD_WIDTH
459 || ld + rd > (1.5 * prec)))
488 fmt =
float_format (fw, ex, prec - 1, std::ios::scientific);
492 else if (! bank_format && (inf_or_nan || int_only))
497 if (uppercase_format)
505 template <
typename T>
516 T val_abs = (val < 0 ? -val : val);
518 int digits = (inf_or_nan || val_abs == 0) ? 0 : num_digits (val_abs);
520 return make_real_format<T> (digits, inf_or_nan, int_only);
537 template <
typename T>
539 make_real_matrix_format (
int x_max,
int x_min,
bool inf_or_nan,
540 int int_or_inf_or_nan)
542 T
scale = ((x_max == 0 || int_or_inf_or_nan)
543 ? 1 :
std::pow (10.0, calc_scale_exp (x_max - 1)));
549 int fw = 0, ld = 0, rd = 0;
556 else if (bank_format)
558 int digits = (x_max > x_min ? x_max : x_min);
559 fw = (digits <= 0 ? 5 : digits + 4);
572 else if (Vfixed_point_format && ! print_g)
576 if (inf_or_nan && fw < 4)
579 else if (int_or_inf_or_nan)
581 int digits = (x_max > x_min ? x_max : x_min);
582 fw = (digits <= 0 ? 2 : digits + 1);
583 if (inf_or_nan && fw < 4)
593 rd_max = (prec > x_max ? prec - x_max : prec);
599 rd_max = (prec > x_max ? prec - x_max : prec);
605 rd_max = (prec > 1 ? prec - 1 : prec);
613 rd_min = (prec > x_min ? prec - x_min : prec);
619 rd_min = (prec > x_min ? prec - x_min : prec);
625 rd_min = (prec > 1 ? prec - 1 : prec);
629 ld = (ld_max > ld_min ? ld_max : ld_min);
630 rd = (rd_max > rd_min ? rd_max : rd_min);
632 fw = 1 + ld + 1 + rd;
633 if (inf_or_nan && fw < 4)
637 if (! (rat_format || bank_format || hex_format || bit_format)
638 && (print_e || print_eng || print_g
639 || (! Vfixed_point_format
640 && (ld + rd > pr_output_traits<T>::DIGITS10
641 || fw > pr_output_traits<T>::MAX_FIELD_WIDTH
642 || ld + rd > (1.5 * prec)))))
649 if (x_max > 100 || x_min > 100)
655 if (inf_or_nan && fw < 6)
662 if (inf_or_nan && fw < 4)
664 fmt =
float_format (fw, prec - 1, std::ios::scientific);
668 else if (! bank_format && int_or_inf_or_nan)
673 if (uppercase_format)
679 template <
typename MT>
681 make_matrix_format (
const MT&
m)
688 bool inf_or_nan =
m.any_element_is_inf_or_nan ();
690 bool int_or_inf_or_nan =
m.all_elements_are_int_or_inf_or_nan ();
694 typedef typename MT::element_type ELT_T;
696 ELT_T max_abs = pr_max_internal (m_abs);
697 ELT_T min_abs = pr_min_internal (m_abs);
699 int x_max = (max_abs == 0 ? 0 : num_digits (max_abs));
701 int x_min = (min_abs == 0 ? 0 : num_digits (min_abs));
703 return make_real_matrix_format<ELT_T> (x_max, x_min, inf_or_nan,
711 return make_matrix_format (
m);
718 return make_matrix_format (
m);
721 template <
typename T>
723 make_complex_format (
int x_max,
int x_min,
int r_x,
724 bool inf_or_nan,
int int_only)
731 int i_fw = 0, r_fw = 0, ld = 0, rd = 0;
739 else if (bank_format)
743 r_fw = (digits <= 0 ? 5 : digits + 4);
748 r_fw = 2 *
sizeof (T);
749 i_fw = 2 *
sizeof (T);
754 r_fw = 8 *
sizeof (T);
755 i_fw = 8 *
sizeof (T);
760 int digits = (x_max > x_min ? x_max : x_min);
761 i_fw = (digits <= 0 ? 1 : digits);
763 if (inf_or_nan && i_fw < 3)
776 rd_max = (prec > x_max ? prec - x_max : prec);
782 rd_max = (prec > x_max ? prec - x_max : prec);
788 rd_max = (prec > 1 ? prec - 1 : prec);
796 rd_min = (prec > x_min ? prec - x_min : prec);
802 rd_min = (prec > x_min ? prec - x_min : prec);
808 rd_min = (prec > 1 ? prec - 1 : prec);
812 ld = (ld_max > ld_min ? ld_max : ld_min);
813 rd = (rd_max > rd_min ? rd_max : rd_min);
817 if (inf_or_nan && i_fw < 3)
824 if (! (rat_format || bank_format || hex_format || bit_format)
825 && (print_e || print_eng || print_g
826 || ld + rd > pr_output_traits<T>::DIGITS10
827 || r_fw > pr_output_traits<T>::MAX_FIELD_WIDTH
828 || i_fw > pr_output_traits<T>::MAX_FIELD_WIDTH
829 || ld + rd > (1.5 * prec)))
833 int width = prec + 6;
840 if (x_max > 100 || x_min > 100)
845 i_fw = 3 + prec + ex;
847 if (inf_or_nan && i_fw < 5)
852 r_fmt =
float_format (r_fw, ex, prec - 1, std::ios::fixed);
853 i_fmt =
float_format (i_fw, ex, prec - 1, std::ios::fixed);
857 i_fw = 1 + prec + ex;
859 if (inf_or_nan && i_fw < 3)
864 r_fmt =
float_format (r_fw, prec - 1, std::ios::scientific);
865 i_fmt =
float_format (i_fw, prec - 1, std::ios::scientific);
869 if (uppercase_format)
875 else if (! bank_format && int_only)
889 template <
typename T>
901 bool inf_or_nan = r_inf_or_nan || i_inf_or_nan;
906 T r_abs = (rp < 0 ? -rp : rp);
907 T i_abs = (ip < 0 ? -ip : ip);
909 int r_x = (r_abs == 0 ? 0 : num_digits (r_abs));
910 int i_x = (i_abs == 0 ? 0 : num_digits (i_abs));
925 return make_complex_format<T> (x_max, x_min, r_x, inf_or_nan, int_only);
942 template <
typename T>
944 make_complex_matrix_format (
int x_max,
int x_min,
int r_x_max,
945 int r_x_min,
bool inf_or_nan,
946 int int_or_inf_or_nan)
948 T
scale = ((x_max == 0 || int_or_inf_or_nan)
949 ? 1 :
std::pow (10.0, calc_scale_exp (x_max - 1)));
956 int i_fw = 0, r_fw = 0, ld = 0, rd = 0;
964 else if (bank_format)
966 int digits = (r_x_max > r_x_min ? r_x_max : r_x_min);
968 r_fw = (digits <= 0 ? 5 : digits + 4);
973 r_fw = 2 *
sizeof (T);
974 i_fw = 2 *
sizeof (T);
979 r_fw = 8 *
sizeof (T);
980 i_fw = 8 *
sizeof (T);
983 else if (Vfixed_point_format && ! print_g)
988 if (inf_or_nan && i_fw < 3)
994 else if (int_or_inf_or_nan)
996 int digits = (x_max > x_min ? x_max : x_min);
997 i_fw = (digits <= 0 ? 1 : digits);
999 if (inf_or_nan && i_fw < 3)
1012 rd_max = (prec > x_max ? prec - x_max : prec);
1018 rd_max = (prec > x_max ? prec - x_max : prec);
1024 rd_max = (prec > 1 ? prec - 1 : prec);
1032 rd_min = (prec > x_min ? prec - x_min : prec);
1038 rd_min = (prec > x_min ? prec - x_min : prec);
1044 rd_min = (prec > 1 ? prec - 1 : prec);
1048 ld = (ld_max > ld_min ? ld_max : ld_min);
1049 rd = (rd_max > rd_min ? rd_max : rd_min);
1053 if (inf_or_nan && i_fw < 3)
1060 if (! (rat_format || bank_format || hex_format || bit_format)
1061 && (print_e || print_eng || print_g
1062 || (! Vfixed_point_format
1063 && (ld + rd > pr_output_traits<T>::DIGITS10
1064 || r_fw > pr_output_traits<T>::MAX_FIELD_WIDTH
1065 || i_fw > pr_output_traits<T>::MAX_FIELD_WIDTH
1066 || ld + rd > (1.5 * prec)))))
1070 int width = prec + 6;
1077 if (x_max > 100 || x_min > 100)
1082 i_fw = 3 + prec + ex;
1084 if (inf_or_nan && i_fw < 5)
1089 r_fmt =
float_format (r_fw, ex, prec - 1, std::ios::fixed);
1090 i_fmt =
float_format (i_fw, ex, prec - 1, std::ios::fixed);
1094 i_fw = 1 + prec + ex;
1096 if (inf_or_nan && i_fw < 3)
1101 r_fmt =
float_format (r_fw, prec - 1, std::ios::scientific);
1102 i_fmt =
float_format (i_fw, prec - 1, std::ios::scientific);
1106 if (uppercase_format)
1112 else if (! bank_format && int_or_inf_or_nan)
1126 template <
typename CMT>
1128 make_complex_matrix_format (
const CMT& cm)
1133 typedef typename CMT::real_matrix_type RMT;
1134 typedef typename CMT::real_elt_type ELT_T;
1139 bool inf_or_nan = cm.any_element_is_inf_or_nan ();
1141 bool int_or_inf_or_nan = (rp.all_elements_are_int_or_inf_or_nan ()
1142 && ip.all_elements_are_int_or_inf_or_nan ());
1144 RMT r_m_abs = rp.abs ();
1145 ELT_T r_max_abs = pr_max_internal (r_m_abs);
1146 ELT_T r_min_abs = pr_min_internal (r_m_abs);
1148 RMT i_m_abs = ip.abs ();
1149 ELT_T i_max_abs = pr_max_internal (i_m_abs);
1150 ELT_T i_min_abs = pr_min_internal (i_m_abs);
1152 int r_x_max = (r_max_abs == 0 ? 0 : num_digits (r_max_abs));
1154 int r_x_min = (r_min_abs == 0 ? 0 : num_digits (r_min_abs));
1156 int i_x_max = (i_max_abs == 0 ? 0 : num_digits (i_max_abs));
1158 int i_x_min = (i_min_abs == 0 ? 0 : num_digits (i_min_abs));
1160 int x_max = (r_x_max > i_x_max ? r_x_max : i_x_max);
1161 int x_min = (r_x_min > i_x_min ? r_x_min : i_x_min);
1163 return make_complex_matrix_format<ELT_T> (x_max, x_min, r_x_max, r_x_min,
1164 inf_or_nan, int_or_inf_or_nan);
1171 return make_complex_matrix_format (cm);
1178 return make_complex_matrix_format (cm);
1188 template <
typename T>
1190 make_range_format (
int x_max,
int x_min,
int all_ints)
1192 double scale = ((x_max == 0 || all_ints)
1193 ? 1 :
std::pow (10.0, calc_scale_exp (x_max - 1)));
1199 int fw = 0, ld = 0, rd = 0;
1206 else if (bank_format)
1208 int digits = (x_max > x_min ? x_max : x_min);
1209 fw = (digits < 0 ? 5 : digits + 4);
1212 else if (hex_format)
1214 fw = 2 *
sizeof (T);
1217 else if (bit_format)
1219 fw = 8 *
sizeof (T);
1224 int digits = (x_max > x_min ? x_max : x_min);
1228 else if (Vfixed_point_format && ! print_g)
1239 rd_max = (prec > x_max ? prec - x_max : prec);
1245 rd_max = (prec > x_max ? prec - x_max : prec);
1251 rd_max = (prec > 1 ? prec - 1 : prec);
1259 rd_min = (prec > x_min ? prec - x_min : prec);
1265 rd_min = (prec > x_min ? prec - x_min : prec);
1271 rd_min = (prec > 1 ? prec - 1 : prec);
1275 ld = (ld_max > ld_min ? ld_max : ld_min);
1276 rd = (rd_max > rd_min ? rd_max : rd_min);
1281 if (! (rat_format || bank_format || hex_format || bit_format)
1282 && (print_e || print_eng || print_g
1283 || (! Vfixed_point_format
1284 && (ld + rd > pr_output_traits<T>::DIGITS10
1285 || fw > pr_output_traits<T>::MAX_FIELD_WIDTH
1286 || ld + rd > (1.5 * prec)))))
1293 if (x_max > 100 || x_min > 100)
1299 fmt =
float_format (fw, ex, prec - 1, std::ios::fixed);
1304 fmt =
float_format (fw, prec - 1, std::ios::scientific);
1308 else if (! bank_format && all_ints)
1313 if (uppercase_format)
1326 double r_min =
r.base ();
1327 double r_max =
r.limit ();
1336 bool all_ints =
r.all_elements_are_ints ();
1338 double max_abs = (r_max < 0 ? -r_max : r_max);
1339 double min_abs = (r_min < 0 ? -r_min : r_min);
1341 int x_max = (max_abs == 0 ? 0 : num_digits (max_abs));
1343 int x_min = (min_abs == 0 ? 0 : num_digits (min_abs));
1345 return make_range_format<double> (x_max, x_min, all_ints);
1348 template <
typename T>
1352 unsigned char i[
sizeof (T)];
1355 #define PRINT_CHAR_BITS(os, c) \
1358 unsigned char ctmp = c; \
1360 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \
1361 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \
1362 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \
1363 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \
1364 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \
1365 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \
1366 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \
1367 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \
1373 #define PRINT_CHAR_BITS_SWAPPED(os, c) \
1376 unsigned char ctmp = c; \
1378 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \
1379 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \
1380 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \
1381 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \
1382 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \
1383 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \
1384 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \
1385 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \
1391 template <
typename T>
1393 pr_any_float (std::ostream& os,
const float_format& fmt, T val)
1401 int fw = fmt.
width ();
1405 octave::preserve_stream_state stream_state (os);
1419 if (uppercase_format)
1420 os.flags (std::ios::right | std::ios::hex | std::ios::uppercase);
1422 os.flags (std::ios::right | std::ios::hex);
1427 for (std::size_t i = 0; i <
sizeof (T); i++)
1428 os << std::setw (2) <<
static_cast<int> (tmp.i[i]);
1432 for (
int i =
sizeof (T) - 1; i >= 0; i--)
1433 os << std::setw (2) <<
static_cast<int> (tmp.i[i]);
1436 else if (bit_format)
1446 for (std::size_t i = 0; i <
sizeof (T); i++)
1453 for (std::size_t i = 0; i <
sizeof (T); i++)
1458 for (
int i =
sizeof (T) - 1; i >= 0; i--)
1465 octave::preserve_stream_state stream_state (os);
1468 os << std::setw (fw) <<
"0";
1474 octave::preserve_stream_state stream_state (os);
1477 os << std::setw (fw) <<
"NA";
1481 else if (rat_format)
1482 os << pr_rational_float<T> (fmt, val);
1485 octave::preserve_stream_state stream_state (os);
1494 os << std::setw (fw) << s;
1500 octave::preserve_stream_state stream_state (os);
1503 os << std::setw (fw) <<
"NaN";
1508 os << pr_engineering_float<T> (fmt, val);
1510 os << pr_formatted_float<T> (fmt, val);
1513 template <
typename T>
1519 if (Vfixed_point_format && ! (print_g || print_e) &&
scale != 1)
1525 template <
typename T>
1531 if (Vfixed_point_format && ! (print_g || print_e) &&
scale != 1)
1537 template <
typename T>
1540 const std::complex<T>& cval)
1544 pr_float (os, fmt,
r);
1553 pr_imag_float (os, fmt, i);
1557 if (hex_format || bit_format)
1562 pr_imag_float (os, fmt, i);
1570 bool pr_as_read_syntax)
1574 if (pr_as_read_syntax)
1576 if (nr == 0 && nc == 0)
1579 os <<
"zeros (" << nr <<
", " << nc <<
')';
1586 os <<
'(' << nr <<
'x' << nc <<
')';
1591 print_empty_nd_array (std::ostream& os,
const dim_vector& dims,
1592 bool pr_as_read_syntax)
1596 if (pr_as_read_syntax)
1597 os <<
"zeros (" << dims.
str (
',') <<
')';
1603 os <<
'(' << dims.
str () <<
')';
1608 pr_scale_header (std::ostream& os,
double scale)
1610 if (Vfixed_point_format && ! (print_g || print_e) &&
scale != 1)
1612 octave::preserve_stream_state stream_state (os);
1615 << std::setw (8) << std::setprecision (1)
1616 << std::setiosflags (std::ios::scientific | std::ios::left)
1626 pr_col_num_header (std::ostream& os,
octave_idx_type total_width,
int max_width,
1629 if (total_width > max_width && Vsplit_long_rows)
1631 octave::preserve_stream_state stream_state (os);
1643 os << std::setw (extra_indent) <<
"";
1646 os <<
" Column " << col + 1 <<
":\n";
1647 else if (num_cols == 2)
1648 os <<
" Columns " << col + 1 <<
" and " << lim <<
":\n";
1650 os <<
" Columns " << col + 1 <<
" through " << lim <<
":\n";
1657 template <
typename T>
1659 pr_plus_format (std::ostream& os,
const T& val)
1662 os << plus_format_chars[0];
1663 else if (val < T (0))
1664 os << plus_format_chars[1];
1666 os << plus_format_chars[2];
1675 template <
typename T>
1679 return x < 0 ? -
x :
x;
1682 #define INSTANTIATE_ABS(T) \
1690 #define SPECIALIZE_UABS(T) \
1703 #define MAKE_INT_MATRIX_FORMAT(TYPE) \
1705 float_display_format \
1706 make_format (const intNDArray<TYPE>& nda) \
1708 bool isneg = false; \
1711 for (octave_idx_type i = 0; i < nda.numel (); i++) \
1714 = static_cast<int> \
1715 (std::floor (log10 (double (abs (nda(i).value ()))) + 1)); \
1717 if (new_digits > digits) \
1718 digits = new_digits; \
1721 isneg = (abs (nda(i).value ()) != nda(i).value ()); \
1724 return float_display_format (float_format (digits + isneg, 0, 0)); \
1736 #define MAKE_INT_SCALAR_FORMAT(TYPE) \
1738 float_display_format \
1739 make_format (const octave_int<TYPE>& val) \
1741 bool isneg = false; \
1743 = static_cast<int> \
1744 (std::floor (log10 (double (abs (val.value ()))) + 1)); \
1746 isneg = (abs (val.value ()) != val.value ()); \
1748 return float_display_format (float_format (digits + isneg, 0, 0)); \
1762 bool d,
bool pr_as_read_syntax)
1782 double d,
bool pr_as_read_syntax)
1784 if (pr_as_read_syntax)
1786 else if (plus_format)
1787 pr_plus_format (os,
d);
1793 pr_float (os, fmt,
d);
1799 float d,
bool pr_as_read_syntax)
1801 if (pr_as_read_syntax)
1803 else if (plus_format)
1804 pr_plus_format (os,
d);
1810 pr_float (os, fmt,
d);
1814 template <
typename MT>
1816 octave_print_free (std::ostream& os,
const MT&
m,
bool pr_as_read_syntax)
1821 if (pr_as_read_syntax)
1827 os <<
' ' <<
m.elem (i, j);
1833 if (pr_as_read_syntax)
1837 template <
typename MT>
1839 pr_plus_format_matrix (std::ostream& os,
const MT&
m)
1850 pr_plus_format (os,
m(i, j));
1864 int retval = r_fw + i_fw + 2;
1866 if (i_fw && ! (rat_format || bank_format || hex_format || bit_format))
1872 template <
typename MT>
1874 octave_print_matrix_internal (std::ostream& os,
const MT&
m,
1875 bool pr_as_read_syntax,
int extra_indent)
1880 if (nr == 0 || nc == 0)
1881 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
1882 else if (plus_format && ! pr_as_read_syntax)
1883 pr_plus_format_matrix (os,
m);
1887 int column_width = get_column_width (fmt);
1891 if (pr_as_read_syntax)
1894 max_width -= extra_indent;
1901 octave_print_free (os,
m, pr_as_read_syntax);
1906 if (total_width > max_width && Vsplit_long_rows)
1908 inc = max_width / column_width;
1913 if (pr_as_read_syntax)
1926 if (i == 0 && j == 0)
1936 pr_float (os, fmt,
m(i, j));
1955 octave::preserve_stream_state stream_state (os);
1963 pr_col_num_header (os, total_width, max_width, lim, col,
1968 os << std::setw (extra_indent) <<
"";
1976 pr_float (os, fmt,
m(i, j));
1987 template <
typename DMT>
1989 octave_print_diag_matrix_internal (std::ostream& os,
const DMT&
m,
1990 bool pr_as_read_syntax,
int extra_indent)
1995 if (nr == 0 || nc == 0)
1996 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
1997 else if (plus_format && ! pr_as_read_syntax)
1998 pr_plus_format_matrix (os,
m);
2002 =
make_format (
typename DMT::full_matrix_type (
m.diag ()));
2003 int column_width = get_column_width (fmt);
2007 if (pr_as_read_syntax)
2010 max_width -= extra_indent;
2017 octave_print_free (os,
m, pr_as_read_syntax);
2022 if (total_width > max_width && Vsplit_long_rows)
2024 inc = max_width / column_width;
2029 if (pr_as_read_syntax)
2052 pr_float (os, fmt,
m(j, j));
2066 octave::preserve_stream_state stream_state (os);
2068 os <<
"Diagonal Matrix\n";
2077 std::ostringstream tmp_oss;
2078 typename DMT::element_type zero = 0;
2079 pr_float (tmp_oss, fmt, zero);
2080 zero_fw = tmp_oss.str ().length ();
2087 pr_col_num_header (os, total_width, max_width, lim, col,
2092 os << std::setw (extra_indent) <<
"";
2101 pr_float (os, fmt,
m(i, j));
2103 os << std::setw (zero_fw) <<
'0';
2114 template <
typename NDA_T,
typename ELT_T,
typename MAT_T>
2117 bool pr_as_read_syntax)
2121 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax);
2125 int ndims = nda.ndims ();
2133 for (
int i = 2; i < ndims; i++)
2143 std::string nm =
"ans";
2149 std::ostringstream buf;
2151 for (
int k = 2; k < ndims; k++)
2169 for (
int k = 2; k < ndims; k++)
2192 bool pr_as_read_syntax,
int extra_indent)
2194 switch (nda.
ndims ())
2199 pr_as_read_syntax, extra_indent);
2203 print_nd_array <NDArray, double, Matrix> (os, nda, pr_as_read_syntax);
2210 bool pr_as_read_syntax,
int extra_indent)
2212 switch (nda.
ndims ())
2217 pr_as_read_syntax, extra_indent);
2221 print_nd_array <FloatNDArray, float, FloatMatrix> (os, nda, pr_as_read_syntax);
2226 template <
typename T>
2228 pr_plus_format (std::ostream& os,
const std::complex<T>& c)
2241 pr_plus_format (os, rp);
2248 const Complex& c,
bool pr_as_read_syntax)
2250 if (pr_as_read_syntax)
2252 else if (plus_format)
2253 pr_plus_format (os, c);
2259 pr_float (os, fmt, c);
2267 if (pr_as_read_syntax)
2269 else if (plus_format)
2270 pr_plus_format (os, c);
2276 pr_float (os, fmt, c);
2282 bool pr_as_read_syntax,
int extra_indent)
2287 if (nr == 0 || nc == 0)
2288 print_empty_matrix (os, nr, nc, pr_as_read_syntax);
2289 else if (plus_format && ! pr_as_read_syntax)
2290 pr_plus_format_matrix (os,
m);
2294 int column_width = fw + 2;
2298 if (pr_as_read_syntax)
2301 max_width -= extra_indent;
2308 octave_print_free (os,
m, pr_as_read_syntax);
2313 if (total_width > max_width && Vsplit_long_rows)
2315 inc = max_width / column_width;
2320 if (pr_as_read_syntax)
2360 octave::preserve_stream_state stream_state (os);
2362 os <<
"Permutation Matrix\n";
2370 pr_col_num_header (os, total_width, max_width, lim, col,
2375 os << std::setw (extra_indent) <<
"";
2383 os << std::setw (fw) <<
m(i, j);
2396 bool pr_as_read_syntax,
int extra_indent)
2398 switch (nda.
ndims ())
2403 pr_as_read_syntax, extra_indent);
2407 print_nd_array <ComplexNDArray, Complex, ComplexMatrix>
2408 (os, nda, pr_as_read_syntax);
2415 bool pr_as_read_syntax,
int extra_indent)
2417 switch (nda.
ndims ())
2422 pr_as_read_syntax, extra_indent);
2426 print_nd_array <FloatComplexNDArray, FloatComplex, FloatComplexMatrix>
2427 (os, nda, pr_as_read_syntax);
2436 bool pr_as_read_syntax,
int extra_indent)
2438 octave_print_matrix_internal (os,
m, pr_as_read_syntax, extra_indent);
2443 bool pr_as_read_syntax,
int extra_indent)
2445 octave_print_matrix_internal (os,
m, pr_as_read_syntax, extra_indent);
2450 bool pr_as_read_syntax,
int extra_indent)
2452 octave_print_diag_matrix_internal (os,
m, pr_as_read_syntax, extra_indent);
2457 bool pr_as_read_syntax,
int extra_indent)
2459 octave_print_diag_matrix_internal (os,
m, pr_as_read_syntax, extra_indent);
2464 bool pr_as_read_syntax,
int extra_indent)
2466 octave_print_matrix_internal (os, cm, pr_as_read_syntax, extra_indent);
2471 bool pr_as_read_syntax,
int extra_indent)
2473 octave_print_matrix_internal (os, cm, pr_as_read_syntax, extra_indent);
2478 bool pr_as_read_syntax,
int extra_indent)
2480 octave_print_diag_matrix_internal (os, cm, pr_as_read_syntax, extra_indent);
2485 bool pr_as_read_syntax,
int extra_indent)
2487 octave_print_diag_matrix_internal (os, cm, pr_as_read_syntax, extra_indent);
2492 bool pr_as_read_syntax,
int extra_indent)
2494 double base =
r.base ();
2495 double increment =
r.increment ();
2496 double limit =
r.limit ();
2497 double final_value =
r.final_value ();
2500 if (plus_format && ! pr_as_read_syntax)
2501 pr_plus_format_matrix (os,
r);
2506 if (pr_as_read_syntax)
2510 os << base <<
" : ";
2512 os << increment <<
" : ";
2517 pr_float (os, fmt, base);
2521 pr_float (os, fmt, increment);
2524 pr_float (os, fmt, limit);
2529 octave::preserve_stream_state stream_state (os);
2531 int column_width = get_column_width (fmt);
2539 os <<
' ' <<
r.elem (i);
2544 if (total_width > max_width && Vsplit_long_rows)
2546 inc = max_width / column_width;
2551 max_width -= extra_indent;
2559 while (col < num_elem)
2564 pr_col_num_header (os, total_width, max_width, lim, col,
2567 os << std::setw (extra_indent) <<
"";
2577 val = base + i * increment;
2579 if (i == num_elem - 1)
2584 pr_float (os, fmt, val);
2595 bool pr_as_read_syntax,
2604 bool pr_as_read_syntax,
2607 switch (nda.
ndims ())
2612 pr_as_read_syntax, extra_indent);
2624 bool pr_as_read_syntax,
2632 if (pr_as_read_syntax && nstr > 1)
2643 if (pr_as_read_syntax)
2660 if (pr_as_read_syntax && nstr > 1)
2665 os <<
"sorry, printing char matrices not implemented yet\n";
2671 bool pr_as_read_syntax,
int extra_indent,
2674 switch (nda.
ndims ())
2679 pr_as_read_syntax, extra_indent, pr_as_string);
2683 print_nd_array <charNDArray, char, charMatrix> (os, nda,
2691 bool pr_as_read_syntax,
int extra_indent)
2700 bool pr_as_read_syntax,
int )
2706 print_empty_nd_array (os, nda.
dims (), pr_as_read_syntax);
2707 else if (nda.
numel () == 1)
2713 int ndims = nda.
ndims ();
2721 for (
int i = 2; i < ndims; i++)
2729 std::string nm =
"ans";
2735 std::ostringstream buf;
2737 for (
int k = 2; k < ndims; k++)
2755 for (
int k = 2; k < ndims; k++)
2773 os <<
" " << page(ii, jj);
2786 template <
typename T>
2791 typedef T print_conv_type;
2794 #define PRINT_CONV(T1, T2) \
2797 octave_print_conv<T1> \
2800 typedef T2 print_conv_type; \
2808 template <
typename T>
2810 pr_int (std::ostream& os,
const T&
d,
int fw = 0)
2812 std::size_t sz =
d.byte_size ();
2813 const unsigned char *tmpi =
d.iptr ();
2823 octave::preserve_stream_state stream_state (os);
2826 if (uppercase_format)
2827 os.flags (std::ios::right | std::ios::hex | std::ios::uppercase);
2829 os.flags (std::ios::right | std::ios::hex);
2833 for (std::size_t i = 0; i < sz; i++)
2834 os << std::setw (2) <<
static_cast<int> (tmpi[i]);
2838 for (
int i = sz - 1; i >= 0; i--)
2839 os << std::setw (2) <<
static_cast<int> (tmpi[i]);
2842 else if (bit_format)
2846 for (std::size_t i = 0; i < sz; i++)
2853 for (std::size_t i = 0; i < sz; i++)
2858 for (
int i = sz - 1; i >= 0; i--)
2865 octave::preserve_stream_state stream_state (os);
2867 os << std::setw (fw)
2868 <<
typename octave_print_conv<T>::print_conv_type (
d);
2899 template <
typename T>
2906 pr_plus_format (os, val);
2910 os << typename octave_print_conv<octave_int<T>>::print_conv_type (val);
2920 #define PRINT_INT_SCALAR_INTERNAL(TYPE) \
2922 octave_print_internal (std::ostream& os, \
2923 const float_display_format& fmt, \
2924 const octave_int<TYPE>& val, bool dummy) \
2926 octave_print_internal_template (os, fmt, val, dummy); \
2938 template <typename T>
2941 bool pr_as_read_syntax,
int extra_indent)
2947 print_empty_nd_array (os, nda.dims (), pr_as_read_syntax);
2948 else if (nda.numel () == 1)
2951 else if (plus_format && ! pr_as_read_syntax)
2953 int ndims = nda.ndims ();
2961 for (
int i = 2; i < ndims; i++)
2971 std::string nm =
"ans(:,:,";
2973 std::ostringstream buf;
2975 for (
int k = 2; k < ndims; k++)
2997 for (
int k = 2; k < ndims; k++)
3008 pr_plus_format (os, page(ii, jj));
3011 if ((ii < nr - 1) || (i <
m -1))
3024 int ndims = nda.ndims ();
3032 for (
int i = 2; i < ndims; i++)
3040 fw = 2 * nda(0).byte_size ();
3041 else if (bit_format)
3042 fw = nda(0).nbits ();
3054 if (new_digits > digits)
3055 digits = new_digits;
3058 isneg = (
abs (nda(i).value ()) != nda(i).value ());
3061 fw = digits + isneg;
3064 int column_width = fw + (rat_format ? 0 : (bank_format ? 5 : 2));
3066 int max_width = octave::command_editor::terminal_cols () - extra_indent;
3068 if (total_width > max_width && Vsplit_long_rows)
3070 inc = max_width / column_width;
3079 std::string nm =
"ans(:,:,";
3081 std::ostringstream buf;
3083 for (
int k = 2; k < ndims; k++)
3105 for (
int k = 2; k < ndims; k++)
3112 if (pr_as_read_syntax)
3121 os << typename octave_print_conv<T>::print_conv_type (page(ii, jj));
3126 if (pr_as_read_syntax)
3131 octave::preserve_stream_state stream_state (os);
3141 pr_col_num_header (os, total_width, max_width, lim, col,
3146 os << std::setw (extra_indent) <<
"";
3152 pr_int (os, page(ii, jj), fw);
3154 if ((ii < n_rows - 1) || (i <
m -1))
3169 #define PRINT_INT_ARRAY_INTERNAL(TYPE) \
3170 OCTINTERP_API void \
3171 octave_print_internal (std::ostream& os, const intNDArray<TYPE>& nda, \
3172 bool pr_as_read_syntax, int extra_indent) \
3174 octave_print_internal_template (os, nda, pr_as_read_syntax, extra_indent); \
3200 DEFUN (rats, args, ,
3228 int nargin = args.length ();
3230 if (nargin < 1 || nargin > 2)
3236 error (
"rats: X must be numeric");
3242 if (arg.
ndims () > 2)
3252 rat_string_len = 13;
3254 rat_string_len = args(1).nint_value ();
3260 std::ostringstream buf;
3262 std::string s = buf.str ();
3264 std::list<std::string> lst;
3267 std::size_t s_len = s.length ();
3271 std::size_t
m = s.find (
'\n',
n);
3273 if (
m == std::string::npos)
3275 lst.push_back (s.substr (
n));
3280 lst.push_back (s.substr (
n,
m -
n));
3324 DEFUN (disp, args, nargout,
3325 classes: cell
char double function_handle int8 int16 int32 int64 logical single
struct uint8 uint16 uint32 uint64
3350 if (args.length () != 1)
3361 std::ostringstream buf;
3370 classes: cell
char double function_handle int8 int16 int32 int64 logical single
struct uint8 uint16 uint32 uint64
3391 if (args.length () != 2)
3403 error (
"fdisp: stream FID not open for writing");
3452 classes: cell
char double function_handle int8 int16 int32 int64 logical single
struct uint8 uint16 uint32 uint64
3490 int nargin = args.length ();
3497 if (nargin < 1 || nargin > 2)
3503 name = args(1).xstring_value (
"NAME must be a string");
3518 bool print_newlines =
false;
3524 interp.feval (
"disp",
ovl (value));
3561 init_format_state ()
3563 free_format =
false;
3564 plus_format =
false;
3566 bank_format =
false;
3574 static std::string format_string (
"short");
3582 init_format_state ();
3584 format_string =
"short";
3586 uppercase_format =
false;
3614 std::string arg = argv[idx++];
3615 std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
3617 if (arg ==
"default")
3620 init_format_state ();
3623 uppercase_format =
false;
3625 else if (arg ==
"short")
3628 init_format_state ();
3638 else if (arg ==
"g")
3640 init_format_state ();
3645 else if (arg ==
"eng")
3647 init_format_state ();
3655 else if (arg ==
"shorte")
3658 init_format_state ();
3662 else if (arg ==
"shortg")
3665 init_format_state ();
3669 else if (arg ==
"shorteng")
3672 init_format_state ();
3676 else if (arg ==
"long")
3679 init_format_state ();
3690 else if (arg ==
"g")
3696 else if (arg ==
"eng")
3705 else if (arg ==
"longe")
3708 init_format_state ();
3712 else if (arg ==
"longg")
3715 init_format_state ();
3719 else if (arg ==
"longeng")
3722 init_format_state ();
3726 else if (arg ==
"hex")
3729 init_format_state ();
3732 else if (arg ==
"native-hex")
3735 init_format_state ();
3738 else if (arg ==
"bit")
3741 init_format_state ();
3744 else if (arg ==
"native-bit")
3747 init_format_state ();
3750 else if (arg ==
"+" || arg ==
"plus")
3753 init_format_state ();
3759 if (arg.length () == 3)
3761 plus_format_chars = arg;
3766 plus_format_chars =
"+- ";
3769 plus_format_chars =
"+- ";
3771 else if (arg ==
"rat")
3774 init_format_state ();
3777 else if (arg ==
"bank")
3780 init_format_state ();
3783 else if (arg ==
"free")
3786 init_format_state ();
3789 else if (arg ==
"none")
3792 init_format_state ();
3795 else if (arg ==
"compact")
3797 else if (arg ==
"loose")
3799 else if (arg ==
"lowercase")
3800 uppercase_format =
false;
3801 else if (arg ==
"uppercase")
3802 uppercase_format =
true;
3804 error (
"format: unrecognized format state '%s'", arg.c_str ());
4030 int nargin = args.length ();
4034 int argc = nargin + 1;
4038 set_format_style (argc, argv);
4043 warning (
"format: cannot query and set format at the same time, ignoring set operation");
4046 retval(2) = (uppercase_format ?
"uppercase" :
"lowercase");
4051 retval(0) = format_string;
4116 DEFUN (fixed_point_format, args, nargout,
4156 "fixed_point_format");
4159 DEFUN (print_empty_dimensions, args, nargout,
4187 "print_empty_dimensions");
4190 DEFUN (split_long_rows, args, nargout,
4230 OCTAVE_END_NAMESPACE(
octave)
void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension)
charNDArray max(char d, const charNDArray &m)
charNDArray min(char d, const charNDArray &m)
Array< T, Alloc > index(const octave::idx_vector &i) const
Indexing without resizing.
int ndims() const
Size of the specified dimension.
octave_idx_type rows() const
octave_idx_type cols() const
bool isempty() const
Size of the specified dimension.
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
octave_idx_type numel() const
Number of elements in the array.
Template for N-dimensional array classes with like-type math operators.
void add(F &&fcn, Args &&... args)
void discard(std::size_t num)
std::string row_as_string(octave_idx_type, bool strip_ws=false) const
Vector representing the dimensions (size) of an Array.
std::string str(char sep='x') const
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
void print_with_name(std::ostream &os, const std::string &name) const
bool is_dq_string() const
octave_idx_type rows() const
octave_value reshape(const dim_vector &dv) const
octave_idx_type numel() const
bool print_name_tag(std::ostream &os, const std::string &name) const
void print(std::ostream &os, bool pr_as_read_syntax=false)
int get_file_number(const octave_value &fid) const
stream lookup(int fid, const std::string &who="") const
std::ostream * preferred_output_stream()
ColumnVector real(const ComplexColumnVector &a)
ColumnVector imag(const ComplexColumnVector &a)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void warning(const char *fmt,...)
void() error(const char *fmt,...)
#define error_unless(cond)
#define panic_impossible()
void scale(Matrix &m, double x, double y, double z)
ColumnVector transform(const Matrix &m, double x, double y, double z)
octave::idx_vector idx_vector
#define lo_ieee_signbit(x)
std::complex< T > floor(const std::complex< T > &x)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
float_format native_float_format()
@ flt_fmt_ieee_big_endian
std::string undo_string_escapes(const std::string &s)
bool valid_identifier(const char *s)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)
std::complex< double > Complex
std::complex< float > FloatComplex
octave_int< T > pow(const octave_int< T > &a, const octave_int< T > &b)
std::string rational_approx(T val, int len)
const octave_base_value const Array< octave_idx_type > & ra_idx
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
void set_output_prec(int prec)
float_display_format make_scalar_format(const T &val)
#define PRINT_INT_SCALAR_INTERNAL(TYPE)
#define MAKE_INT_SCALAR_FORMAT(TYPE)
template void pr_int(std::ostream &, const octave_int8 &, int)
#define PRINT_CONV(T1, T2)
void octave_print_internal_template(std::ostream &os, const float_display_format &fmt, const octave_int< T > &val, bool)
#define PRINT_CHAR_BITS(os, c)
#define SPECIALIZE_UABS(T)
void octave_print_internal(std::ostream &os, const float_display_format &fmt, bool d, bool pr_as_read_syntax)
template int8_t abs(int8_t)
#define MAKE_INT_MATRIX_FORMAT(TYPE)
float_display_format make_format(const double &d)
bool Vprint_empty_dimensions
#define INSTANTIATE_ABS(T)
#define PRINT_INT_ARRAY_INTERNAL(TYPE)
float_display_format make_complex_scalar_format(const std::complex< T > &c)
std::ostream & operator<<(std::ostream &os, const pr_engineering_float< T > &pef)
void print_nd_array(std::ostream &os, const NDA_T &nda, bool pr_as_read_syntax)
std::size_t format(std::ostream &os, const char *fmt,...)