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