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