changeset 8625:4d90d21a9cd9

special printing of diagonal matrices
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 29 Jan 2009 11:49:06 +0100
parents ff7d90d92db8
children 1dce30ab0e72
files src/ChangeLog src/ov-base-diag.cc src/pr-output.cc src/pr-output.h
diffstat 4 files changed, 332 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,15 @@
+2009-01-29  Jaroslav Hajek  <highegg@gmail.com>
+
+	* pr-output.cc:
+	(void octave_print_internal (..., const DiagMatrix& m,...),
+	(void octave_print_internal (..., const ComplexDiagMatrix& m,...),
+	(void octave_print_internal (..., const FloatDiagMatrix& m,...),
+	(void octave_print_internal (..., const FloatComplexDiagMatrix& m,...)):
+	New functions.
+	* pr-output.h: Declare them.
+	* ov-base-diag.cc (octave_base_diag::print_raw): Call
+	octave_print_internal.
+
 2009-01-28  John W. Eaton  <jwe@octave.org>
 
 	* Makefile.in (install, uninstall): Handle SHLLIBPRE and SHLBINPRE
--- a/src/ov-base-diag.cc
+++ b/src/ov-base-diag.cc
@@ -489,7 +489,8 @@
 octave_base_diag<DMT, MT>::print_raw (std::ostream& os,
                                       bool pr_as_read_syntax) const
 {
-  return to_dense ().print_raw (os, pr_as_read_syntax);
+  return octave_print_internal (os, matrix, pr_as_read_syntax,
+                                current_print_indent_level ());
 }
 
 template <class DMT, class MT>
--- a/src/pr-output.cc
+++ b/src/pr-output.cc
@@ -1694,6 +1694,147 @@
     }
 }
 
+void
+octave_print_internal (std::ostream& os, const DiagMatrix& m,
+		       bool pr_as_read_syntax, int extra_indent)
+{
+  octave_idx_type nr = m.rows ();
+  octave_idx_type nc = m.columns ();
+
+  if (nr == 0 || nc == 0)
+    print_empty_matrix (os, nr, nc, pr_as_read_syntax);
+  else if (plus_format && ! pr_as_read_syntax)
+    {
+      for (octave_idx_type i = 0; i < nr; i++)
+	{
+	  for (octave_idx_type j = 0; j < nc; j++)
+	    {
+	      OCTAVE_QUIT;
+
+	      pr_plus_format (os, m(i,j));
+	    }
+
+	  if (i < nr - 1)
+	    os << "\n";
+	}
+    }
+  else
+    {
+      int fw;
+      double scale = 1.0;
+      set_format (Matrix (m.diag ()), fw, scale);
+      int column_width = fw + 2;
+      octave_idx_type total_width = nc * column_width;
+      octave_idx_type max_width = command_editor::terminal_cols ();
+
+      if (pr_as_read_syntax)
+	max_width -= 4;
+      else
+	max_width -= extra_indent;
+
+      if (max_width < 0)
+	max_width = 0;
+
+      if (free_format)
+	{
+	  if (pr_as_read_syntax)
+	    os << "[\n";
+
+	  os << Matrix (m);
+
+	  if (pr_as_read_syntax)
+	    os << "]";
+
+	  return;
+	}
+
+      octave_idx_type inc = nc;
+      if (total_width > max_width && Vsplit_long_rows)
+	{
+	  inc = max_width / column_width;
+	  if (inc == 0)
+	    inc++;
+	}
+
+      if (pr_as_read_syntax)
+	{
+          os << "diag (";
+
+          octave_idx_type col = 0;
+          while (col < nc)
+            {
+              octave_idx_type lim = col + inc < nc ? col + inc : nc;
+
+              for (octave_idx_type j = col; j < lim; j++)
+                {
+                  OCTAVE_QUIT;
+
+                  if (j == 0)
+                    os << "[ ";
+                  else
+                    {
+                      if (j > col && j < lim)
+                        os << ", ";
+                      else
+                        os << "  ";
+                    }
+
+                  pr_float (os, m(j,j));
+                }
+
+              col += inc;
+
+              if (col >= nc)
+                  os << " ]";
+              else
+                os << " ...\n";
+            }
+          os << ")";
+	}
+      else
+	{
+	  pr_scale_header (os, scale);
+
+          // kluge. Get the true width of a number.
+          int zero_fw;
+
+            { 
+              std::ostringstream tmp_oss;
+              pr_float (tmp_oss, 0.0, fw, scale);
+              zero_fw = tmp_oss.str ().length ();
+            }
+
+	  for (octave_idx_type col = 0; col < nc; col += inc)
+	    {
+	      octave_idx_type lim = col + inc < nc ? col + inc : nc;
+
+	      pr_col_num_header (os, total_width, max_width, lim, col,
+				 extra_indent);
+
+	      for (octave_idx_type i = 0; i < nr; i++)
+		{
+		  os << std::setw (extra_indent) << "";
+
+		  for (octave_idx_type j = col; j < lim; j++)
+		    {
+		      OCTAVE_QUIT;
+
+		      os << "  ";
+
+                      if (i == j)
+                        pr_float (os, m(i,j), fw, scale);
+                      else
+                        os << std::setw (zero_fw) << '0';
+
+		    }
+
+		  if (i < nr - 1)
+		    os << "\n";
+		}
+	    }
+	}
+    }
+}
 #define PRINT_ND_ARRAY(os, nda, NDA_T, ELT_T, MAT_T) \
   do \
     { \
@@ -1954,6 +2095,149 @@
 }
 
 void
+octave_print_internal (std::ostream& os, const ComplexDiagMatrix& cm,
+		       bool pr_as_read_syntax, int extra_indent)
+{
+  octave_idx_type nr = cm.rows ();
+  octave_idx_type nc = cm.columns ();
+
+ if (nr == 0 || nc == 0)
+    print_empty_matrix (os, nr, nc, pr_as_read_syntax);
+  else if (plus_format && ! pr_as_read_syntax)
+    {
+      for (octave_idx_type i = 0; i < nr; i++)
+	{
+	  for (octave_idx_type j = 0; j < nc; j++)
+	    {
+	      OCTAVE_QUIT;
+
+	      pr_plus_format (os, cm(i,j));
+	    }
+
+	  if (i < nr - 1)
+	    os << "\n";
+	}
+    }
+  else
+    {
+      int r_fw, i_fw;
+      double scale = 1.0;
+      set_format (ComplexMatrix (cm.diag ()), r_fw, i_fw, scale);
+      int column_width = i_fw + r_fw;
+      column_width += (rat_format || bank_format || hex_format 
+		       || bit_format) ? 2 : 7;
+      octave_idx_type total_width = nc * column_width;
+      octave_idx_type max_width = command_editor::terminal_cols ();
+
+      if (pr_as_read_syntax)
+	max_width -= 4;
+      else
+	max_width -= extra_indent;
+
+      if (max_width < 0)
+	max_width = 0;
+
+      if (free_format)
+	{
+	  if (pr_as_read_syntax)
+	    os << "[\n";
+
+	  os << ComplexMatrix (cm);
+
+	  if (pr_as_read_syntax)
+	    os << "]";
+
+	  return;
+	}
+
+      octave_idx_type inc = nc;
+      if (total_width > max_width && Vsplit_long_rows)
+	{
+	  inc = max_width / column_width;
+	  if (inc == 0)
+	    inc++;
+	}
+
+      if (pr_as_read_syntax)
+	{
+          os << "diag (";
+
+          octave_idx_type col = 0;
+          while (col < nc)
+            {
+              octave_idx_type lim = col + inc < nc ? col + inc : nc;
+
+              for (octave_idx_type j = col; j < lim; j++)
+                {
+                  OCTAVE_QUIT;
+
+                  if (j == 0)
+                    os << "[ ";
+                  else
+                    {
+                      if (j > col && j < lim)
+                        os << ", ";
+                      else
+                        os << "  ";
+                    }
+
+                  pr_complex (os, cm(j,j));
+                }
+
+              col += inc;
+
+              if (col >= nc)
+                  os << " ]";
+              else
+                os << " ...\n";
+            }
+          os << ")";
+	}
+      else
+	{
+	  pr_scale_header (os, scale);
+
+          // kluge. Get the true width of a number.
+          int zero_fw;
+
+            { 
+              std::ostringstream tmp_oss;
+              pr_complex (tmp_oss, Complex (0.0), r_fw, i_fw, scale);
+              zero_fw = tmp_oss.str ().length ();
+            }
+
+	  for (octave_idx_type col = 0; col < nc; col += inc)
+	    {
+	      octave_idx_type lim = col + inc < nc ? col + inc : nc;
+
+	      pr_col_num_header (os, total_width, max_width, lim, col,
+				 extra_indent);
+
+	      for (octave_idx_type i = 0; i < nr; i++)
+		{
+		  os << std::setw (extra_indent) << "";
+
+		  for (octave_idx_type j = col; j < lim; j++)
+		    {
+		      OCTAVE_QUIT;
+
+		      os << "  ";
+
+                      if (i == j)
+                        pr_complex (os, cm(i,j), r_fw, i_fw, scale);
+                      else
+                        os << std::setw (zero_fw) << '0';
+		    }
+
+		  if (i < nr - 1) 
+		    os << "\n";
+		}
+	    }
+	}
+    }
+}
+
+void
 octave_print_internal (std::ostream& os, const ComplexNDArray& nda,
 		       bool pr_as_read_syntax, int extra_indent)
 {
@@ -1993,6 +2277,13 @@
 }
 
 void
+octave_print_internal (std::ostream& os, const FloatDiagMatrix& m,
+		       bool pr_as_read_syntax, int extra_indent)
+{ 
+  octave_print_internal (os, DiagMatrix (m), pr_as_read_syntax, extra_indent); 
+}
+
+void
 octave_print_internal (std::ostream& os, const FloatNDArray& nda,
 		       bool pr_as_read_syntax, int extra_indent)
 {
@@ -2014,6 +2305,13 @@
 }
 
 void
+octave_print_internal (std::ostream& os, const FloatComplexDiagMatrix& cm,
+		       bool pr_as_read_syntax, int extra_indent)
+{
+  octave_print_internal (os, ComplexDiagMatrix (cm), pr_as_read_syntax, extra_indent);
+}
+
+void
 octave_print_internal (std::ostream& os, const FloatComplexNDArray& nda,
 		       bool pr_as_read_syntax, int extra_indent)
 {
--- a/src/pr-output.h
+++ b/src/pr-output.h
@@ -66,11 +66,21 @@
 		       int extra_indent = 0);
 
 extern OCTINTERP_API void
+octave_print_internal (std::ostream& os, const DiagMatrix& m,
+		       bool pr_as_read_syntax = false,
+		       int extra_indent = 0);
+
+extern OCTINTERP_API void
 octave_print_internal (std::ostream& os, const FloatMatrix& m,
 		       bool pr_as_read_syntax = false,
 		       int extra_indent = 0);
 
 extern OCTINTERP_API void
+octave_print_internal (std::ostream& os, const FloatDiagMatrix& m,
+		       bool pr_as_read_syntax = false,
+		       int extra_indent = 0);
+
+extern OCTINTERP_API void
 octave_print_internal (std::ostream& os, const NDArray& nda,
 		       bool pr_as_read_syntax = false,
 		       int extra_indent = 0);
@@ -94,11 +104,21 @@
 		       int extra_indent = 0);
 
 extern OCTINTERP_API void
+octave_print_internal (std::ostream& os, const ComplexDiagMatrix& cm,
+		       bool pr_as_read_syntax = false,
+		       int extra_indent = 0);
+
+extern OCTINTERP_API void
 octave_print_internal (std::ostream& os, const FloatComplexMatrix& cm,
 		       bool pr_as_read_syntax = false,
 		       int extra_indent = 0);
 
 extern OCTINTERP_API void
+octave_print_internal (std::ostream& os, const FloatComplexDiagMatrix& cm,
+		       bool pr_as_read_syntax = false,
+		       int extra_indent = 0);
+
+extern OCTINTERP_API void
 octave_print_internal (std::ostream& os, const ComplexNDArray& nda,
 		       bool pr_as_read_syntax = false,
 		       int extra_indent = 0);