]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Patch by Fahad Alrashed: Save state of output stream around writing data in FullMatri...
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 23 Apr 2013 01:45:52 +0000 (01:45 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 23 Apr 2013 01:45:52 +0000 (01:45 +0000)
git-svn-id: https://svn.dealii.org/trunk@29363 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/lac/full_matrix.h
tests/lac/full_matrix_print.cc [new file with mode: 0644]
tests/lac/full_matrix_print/cmp/generic [new file with mode: 0644]

index 2c16962a81880d87a619dfed3c3de552435b04d8..044582e2b95314d6fae760c10e441725344a8812 100644 (file)
@@ -111,6 +111,13 @@ this function.
 
 <ol>
 
+<li> Fixed: FullMatrix::print now saves and restores the precision
+and width associated with the stream it prints to around setting
+the values passed as arguments.
+<br>
+(Fahad Alrashed, 2013/04/22)
+</li>
+
 <li> New: LogStream now has member functions LogStream::width,
 LogStream::precision and LogStream::flags that make it look more
 like normal objects of type <code>std::ostream</code>.
index a8db6a6ea27c64b15e141223b99a7cb281e916a2..7a01b5127ac799c15bc65e9afc18899565d1ae56 100644 (file)
@@ -692,8 +692,10 @@ public:
   number trace () const;
 
   /**
-   * Output of the matrix in
-   * user-defined format.
+   * Output of the matrix in user-defined format given by the specified
+   * precision and width. This function saves width and precision of the
+   * stream before setting these given values for output, and restores the
+   * previous values after output.
    */
   template <class STREAM>
   void print (STREAM             &s,
@@ -1847,12 +1849,24 @@ FullMatrix<number>::print (STREAM             &s,
 {
   Assert (!this->empty(), ExcEmptyMatrix());
 
+  // save the state of out stream
+  const unsigned int old_precision = s.precision (p);
+  const unsigned int old_width = s.width (w);
+
   for (unsigned int i=0; i<this->m(); ++i)
     {
       for (unsigned int j=0; j<this->n(); ++j)
-        s << std::setw(w) << std::setprecision(p) << this->el(i,j);
+       {
+         s.width(w);
+         s.precision(p);
+         s << this->el(i,j);
+       }
       s << std::endl;
     }
+
+  // reset output format
+  s.precision(old_precision);
+  s.width(old_width);
 }
 
 
diff --git a/tests/lac/full_matrix_print.cc b/tests/lac/full_matrix_print.cc
new file mode 100644 (file)
index 0000000..2fc8916
--- /dev/null
@@ -0,0 +1,54 @@
+//----------------------------  full_matrix_print.cc,v  ---------------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2013 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  full_matrix_print.cc,v  ---------------------------
+
+// verify that FullMatrix::print saves the stream state around the output
+
+#include "../tests.h"
+#include <cmath>
+#include <fstream>
+#include <iomanip>
+
+#include <deal.II/base/logstream.h>
+#include <deal.II/lac/full_matrix.h>
+
+const double entries[9] = { 11.1,12.2,13.3,21.456,22.12345678901,23,31,32,33 };
+
+
+int
+main ()
+{
+  std::ofstream logfile("full_matrix_print/output");
+  deallog << std::fixed;
+  deallog << std::setprecision(3);
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  logfile << numbers::PI << std::endl;
+  
+  FullMatrix<double> T(3,3,entries);
+
+  // try writing to a regular stream
+  T.print(logfile, 15, 8);
+
+  // print something normal -- should use the old settings, 6 digits of
+  // precision
+  logfile << numbers::PI << std::endl;
+
+  // now try the same with a LogStream
+  T.print(deallog, 15, 8);
+
+  logfile << numbers::PI << std::endl;
+}
+
+      
diff --git a/tests/lac/full_matrix_print/cmp/generic b/tests/lac/full_matrix_print/cmp/generic
new file mode 100644 (file)
index 0000000..1251602
--- /dev/null
@@ -0,0 +1,10 @@
+
+3.14159
+11.100000      12.200000      13.300000      
+21.456000      22.123457      23.000000      
+31.000000      32.000000      33.000000      
+3.14159
+DEAL::11.100000      12.200000      13.300000      
+DEAL::21.456000      22.123457      23.000000      
+DEAL::31.000000      32.000000      33.000000      
+3.14159

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.