Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_MatrixMarket_SetScientific.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef __Teuchos_MatrixMarket_SetScientific_hpp
43 #define __Teuchos_MatrixMarket_SetScientific_hpp
44 
45 #include <Teuchos_ConfigDefs.hpp>
46 #include <Teuchos_as.hpp>
47 #include <Teuchos_ScalarTraits.hpp>
48 #include <string>
49 
50 
51 namespace Teuchos {
52  namespace MatrixMarket {
53  namespace details {
64  template<class Scalar, const bool isFloatingPoint>
66  public:
68  typedef Scalar scalar_type;
73  SetScientificImpl (std::ostream& out);
80  };
81 
87  template<class Scalar>
88  class SetScientificImpl<Scalar, true> {
89  public:
90  typedef Scalar scalar_type;
91 
92  SetScientificImpl (std::ostream& out) :
93  out_ (out), originalFlags_ (out.flags())
94  {
96  typedef typename STS::magnitudeType magnitude_type;
98 
99  // Print floating-point values in scientific notation.
100  out << std::scientific;
101 
102  // We're writing decimal digits, so compute the number of
103  // digits we need to get reasonable accuracy when reading
104  // values back in.
105  //
106  // There is actually an algorithm, due to Guy Steele (yes,
107  // Java's Guy Steele) et al., for idempotent printing of
108  // finite-length floating-point values. We should actually
109  // implement that algorithm, but I don't have time for that
110  // now. Currently, I just print no more than (one decimal
111  // digit more than (the number of decimal digits justified
112  // by the precision of magnitude_type)).
113  //
114  // We need to use STM's log10() rather than (say) std::log10
115  // here, because STM::base() returns a magnitude_type, not
116  // one of C++'s standard integer types.
117  const magnitude_type numDecDigits = STM::t() * STM::log10 (STM::base());
118 
119  // Round and add one. The cast to int should not overflow
120  // unless STM::t() is _extremely_ large, so we don't need to
121  // check for that case here.
122  const magnitude_type one = STM::one();
123  const magnitude_type two = one + one;
124  // Cast from magnitude_type to int, since std::ostream's
125  // precision() method expects an int input.
126  const int prec = 1 + Teuchos::as<int> (magnitude_type((two*numDecDigits + one) / two));
127 
128  // Set the number of (decimal) digits after the decimal
129  // point to print.
130  out.precision (prec);
131  }
132 
133  ~SetScientificImpl () {
134  out_.flags (originalFlags_);
135  }
136 
137  private:
139  std::ostream& out_;
140 
142  std::ios_base::fmtflags originalFlags_;
143  };
144 
146  template<class Scalar>
147  class SetScientificImpl<Scalar, false> {
148  public:
149  typedef Scalar scalar_type;
150  SetScientificImpl (std::ostream&) {}
151  ~SetScientificImpl () {}
152  };
153 
174  template<class Scalar>
176  public SetScientificImpl<Scalar, ! Teuchos::ScalarTraits<Scalar>::isOrdinal> {
177  private:
180 
181  public:
183  typedef Scalar scalar_type;
184 
189  SetScientific (std::ostream& out) : base_type (out) {}
190 
197  // Parent class' destructor will be called automatically.
198  }
199  };
200 
201 // // Specialization for Scalar=int. Specializations for built-in
202 // // integer types let Tpetra::MatrixMarket::Reader or
203 // // Tpetra::MatrixMarket::Writer work with a Tpetra::CrsMatrix
204 // // whose Scalar template parameter is a built-in integer type.
205 // // These specializations are trivial because there are no
206 // // decimal digits to print.
207 // template<>
208 // class SetScientific<int> {
209 // public:
210 // typedef int scalar_type;
211 // SetScientific (std::ostream& out) {}
212 // ~SetScientific () {}
213 // };
214 
215 // // Specialization for Scalar=unsigned int.
216 // template<>
217 // class SetScientific<unsigned int> {
218 // public:
219 // typedef unsigned int scalar_type;
220 // SetScientific (std::ostream& out) {}
221 // ~SetScientific () {}
222 // };
223 
224 // // Specialization for Scalar=long.
225 // template<>
226 // class SetScientific<long> {
227 // public:
228 // typedef long scalar_type;
229 // SetScientific (std::ostream& out) {}
230 // ~SetScientific () {}
231 // };
232 
233 // // Specialization for Scalar=unsigned long.
234 // template<>
235 // class SetScientific<unsigned long> {
236 // public:
237 // typedef unsigned long scalar_type;
238 // SetScientific (std::ostream& out) {}
239 // ~SetScientific () {}
240 // };
241 
242 // #ifdef HAVE_TEUCHOS_LONG_LONG_INT
243 
244 // // Specialization for Scalar=long long.
245 // template<>
246 // class SetScientific<long long> {
247 // public:
248 // typedef long long scalar_type;
249 // SetScientific (std::ostream& out) {}
250 // ~SetScientific () {}
251 // };
252 
253 // // Specialization for Scalar=unsigned long long.
254 // template<>
255 // class SetScientific<unsigned long long> {
256 // public:
257 // typedef unsigned long long scalar_type;
258 // SetScientific (std::ostream& out) {}
259 // ~SetScientific () {}
260 // };
261 
262 // #endif // HAVE_TEUCHOS_LONG_LONG_INT
263 
264 
265  } // namespace details
266  } // namespace MatrixMarket
267 } // namespace Teuchos
268 
269 #endif // __Teuchos_MatrixMarket_SetScientific_hpp
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
This structure defines some basic traits for a scalar field type.
Scalar scalar_type
The Scalar type with which SetScientific was specialized.
Scalar scalar_type
The Scalar type with which SetScientific was specialized.
Teuchos implementation details.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Defines basic traits for the scalar field type.
Definition of Teuchos::as, for conversions between types.
Matrix Market file utilities.
Temporarily make an output stream use scientific notation with sufficient precision.