From 16f7fb7d531c703648490b081f4a6732490b2711 Mon Sep 17 00:00:00 2001 From: bangerth Date: Tue, 23 Apr 2013 01:45:52 +0000 Subject: [PATCH] Patch by Fahad Alrashed: Save state of output stream around writing data in FullMatrix::print. git-svn-id: https://svn.dealii.org/trunk@29363 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 7 +++ deal.II/include/deal.II/lac/full_matrix.h | 20 +++++++-- tests/lac/full_matrix_print.cc | 54 +++++++++++++++++++++++ tests/lac/full_matrix_print/cmp/generic | 10 +++++ 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 tests/lac/full_matrix_print.cc create mode 100644 tests/lac/full_matrix_print/cmp/generic diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 2c16962a81..044582e2b9 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -111,6 +111,13 @@ this function.
    +
  1. 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. +
    +(Fahad Alrashed, 2013/04/22) +
  2. +
  3. New: LogStream now has member functions LogStream::width, LogStream::precision and LogStream::flags that make it look more like normal objects of type std::ostream. diff --git a/deal.II/include/deal.II/lac/full_matrix.h b/deal.II/include/deal.II/lac/full_matrix.h index a8db6a6ea2..7a01b5127a 100644 --- a/deal.II/include/deal.II/lac/full_matrix.h +++ b/deal.II/include/deal.II/lac/full_matrix.h @@ -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 void print (STREAM &s, @@ -1847,12 +1849,24 @@ FullMatrix::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; im(); ++i) { for (unsigned int j=0; jn(); ++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 index 0000000000..2fc8916b1d --- /dev/null +++ b/tests/lac/full_matrix_print.cc @@ -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 +#include +#include + +#include +#include + +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 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 index 0000000000..12516023eb --- /dev/null +++ b/tests/lac/full_matrix_print/cmp/generic @@ -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 -- 2.39.5