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