util.h

Go to the documentation of this file.
00001 ///
00002 /// \file   util.h
00003 /// \brief  Collects a variety of constants and function in namespace Util.
00004 ///
00005 /// Provides definitions for a variety of numerical constants related to 
00006 /// double precision and integer arithmetic and for a small collection of 
00007 /// utility functions.
00008 ///
00009 /// All are declared in namespace Util with an eye towards avoiding
00010 /// naming conflicts.
00011 ///
00012 /// \author Kent Holsinger
00013 /// \date   2004-06-26
00014 ///
00015 
00016 // This file is part of MCMC++, a library for constructing C++ programs
00017 // that implement MCMC analyses of Bayesian statistical models.
00018 // Copyright (c) 2004-2006 Kent E. Holsinger
00019 //
00020 // MCMC++ is free software; you can redistribute it and/or modify
00021 // it under the terms of the GNU General Public License as published by
00022 // the Free Software Foundation; either version 2 of the License, or
00023 // (at your option) any later version.
00024 //
00025 // MCMC++ is distributed in the hope that it will be useful,
00026 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00028 // GNU General Public License for more details.
00029 //
00030 // You should have received a copy of the GNU General Public License
00031 // along with MCMC++; if not, write to the Free Software
00032 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00033 //
00034 
00035 #if !defined(__UTIL_H)
00036 #define __UTIL_H
00037 
00038 // standard includes
00039 #include <algorithm>
00040 #include <cmath>
00041 #include <iostream>
00042 #include <limits>
00043 #include <vector>
00044 
00045 /// \namespace Util
00046 ///
00047 /// Used to isolate utility functions and numerical constants.
00048 ///
00049 /// Provides a variety of numerical constants related to double precision 
00050 /// and integer arithmetic, a small collection of utility functions, and 
00051 /// for probability density functions.
00052 ///
00053 namespace Util {
00054 
00055   const double dbl_eps = std::numeric_limits<double>::epsilon();   ///< minimum representable value of 1.0 - x
00056   const double dbl_max = std::numeric_limits<double>::max(); ///< maximum value of double
00057   const double dbl_min = std::numeric_limits<double>::min(); ///< minimum (positive) value of double
00058   const int int_max = std::numeric_limits<int>::max(); ///< maximum value of integer
00059   const int int_min = std::numeric_limits<int>::min(); ///< minimum value of integer
00060   const unsigned uint_max = std::numeric_limits<unsigned>::max(); ///< maximum value of unsigned integer
00061   const long long_max = std::numeric_limits<long>::max(); ///< maximum value of long integer
00062   const long long_min = std::numeric_limits<long>::min(); ///< minimum value of long integer
00063   const unsigned long ulong_max = std::numeric_limits<unsigned long>::max(); ///< maximum value of unsigned long integer
00064   const double log_dbl_max = log(dbl_max); ///< log(dbl_max)
00065   const double log_dbl_min = log(dbl_min); ///< log(dbl_min)
00066 
00067   /// Returns minimum element in v.
00068   ///
00069   /// This is a simple wrapper around std::min_element()
00070   ///
00071   /// \param v  The vector whose minimum element is sought
00072   ///
00073   template <class C>
00074   inline C vectorMin(std::vector<C>& v) {
00075     return *std::min_element(v.begin(), v.end());
00076   }
00077 
00078   /// Returns maximum element in v.
00079   ///
00080   /// This is a simple wrapper around std::max_element()
00081   ///
00082   /// \param v  The vector whose maximum element is sought
00083   ///
00084   template <class C>
00085   inline C vectorMax(std::vector<C>& v) {
00086     return *std::max_element(v.begin(), v.end());
00087   }
00088 
00089   double round(double x);
00090   double safeLog(double x);
00091   double sqr(double x);
00092   
00093   /// Empty a vector, and ensure that its capacity is zero
00094   ///
00095   /// \param v  The vector to be emptied
00096   ///
00097   /// Uses the trick on p. 487 of Stroustrup (3rd ed.): create a temporary
00098   /// vector with zero capacity and swap it with the one to be erased.
00099   ///
00100   template <class C>
00101   void FlushVector(std::vector<C>& v) {
00102     std::vector<C> tmp;
00103     v.swap(tmp);
00104   }
00105 
00106   /// Assertion template for error checking
00107   ///
00108   /// \param assert     the assertion to check
00109   ///
00110   /// This template throws an exception of type Except when the assertion
00111   /// of type Assertion is false. It is shamelessly adapted (stolen) from 
00112   /// Stroustrup, The C++ Programming Language, 3rd ed.
00113   ///
00114   template <class Except, class Assertion>
00115   inline void Assert(Assertion assert) {
00116     if (!assert) throw Except();
00117   }
00118 
00119   /// Cast all elements of a vector from one type to another
00120   ///
00121   /// \param x   the vector with elements to cast
00122   ///
00123   /// This template will cast elements of type From to elements of type
00124   /// To and return the resulting vector
00125   ///
00126   template <class To, class From>
00127   inline std::vector<To> vector_cast(std::vector<From>& x) {
00128     unsigned end = x.size();
00129     std::vector<To> y(end);
00130     for (unsigned i = 0; i < end; ++i) {
00131       y[i] = static_cast<To>(x[i]);
00132     }
00133     return y;
00134   }
00135   
00136   /// \class PrintForVector
00137   ///
00138   /// This template provides a helper class for the template function
00139   /// below for std::vector<T> operator <<
00140   ///
00141   template <class T>
00142   class PrintForVector {
00143   public:
00144     /// Constructor
00145     ///
00146     explicit PrintForVector(std::ostream& os) 
00147       : os_(os), first_(true)
00148     {}
00149     /// allows for_each to provide appropriate output for vector elements
00150     ///
00151     void operator()(const T& x) {
00152       if (!first_) {
00153         os_ << ", ";
00154       } else {
00155         first_ = false;
00156       }
00157       os_ << x;
00158     }
00159   private:
00160     std::ostream& os_;
00161     bool first_;
00162   }; 
00163 
00164 }
00165 
00166 /// ostream& operator<< for vectors
00167 ///
00168 /// \param os  the ostream
00169 /// \param x   the vector
00170 ///
00171 template <class T>
00172 std::ostream& operator<<(std::ostream& os, std::vector<T> x) {
00173   os << "(";
00174   std::for_each(x.begin(), x.end(), Util::PrintForVector<T>(os));
00175   os << ")";
00176   return os;
00177 }
00178 
00179 #endif
00180 
00181 // Local Variables: //
00182 // mode: c++ //
00183 // End: //

Generated on Tue Mar 27 16:03:38 2007 for mcmc by  doxygen 1.5.1