misc.h

Go to the documentation of this file.
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_MISC_H
00024 #define O2SCL_MISC_H
00025 /** \file misc.h
00026     \brief Miscellaneous functions
00027 */
00028 
00029 #include <iostream>
00030 #include <cmath>
00031 #include <string>
00032 #include <fstream>
00033 #include <sstream>
00034 #include <o2scl/err_hnd.h>
00035 #include <o2scl/lib_settings.h>
00036 #include <gsl/gsl_ieee_utils.h>
00037 
00038 #ifndef DOXYGENP
00039 namespace o2scl {
00040 #endif
00041 
00042   /** 
00043       \brief Calculate a Fermi-Dirac distribution function safely
00044       
00045       \f$ \left[1+\exp\left(E/T-\mu/T\right)\right]^{-1} \f$ 
00046       
00047       This calculates a Fermi-Dirac distribution function guaranteeing
00048       that numbers larger than \f$ \exp(\mathrm{limit}) \f$ and
00049       smaller than \f$ \exp(-\mathrm{limit}) \f$ will be avoided. The
00050       default value of \c limit ensures accuracy to within 1 part in
00051       \f$ 10^{17} \f$ compared to the maximum of the distribution
00052       (which is unity).
00053       
00054       Note that this function may return Inf or NAN if \c limit is too 
00055       large, depending on the machine precision.
00056   */
00057   double fermi_function(double E, double mu, double T, double limit=40.0);
00058 
00059   /** 
00060       \brief Reformat the columns for output of width \c size 
00061 
00062       Given a string array \c in_cols of size \c nin, screenify()
00063       reformats the array into columns creating a new string array \c
00064       outc with size \c nout.
00065       
00066       For example, for an array of 10 strings 
00067       \verbatim
00068       test1
00069       test_of_string2
00070       test_of_string3
00071       test_of_string4
00072       test5
00073       test_of_string6
00074       test_of_string7
00075       test_of_string8
00076       test_of_string9
00077       test_of_string10
00078       \endverbatim
00079       screenify() will create an array of 3 new strings:
00080       \verbatim
00081       test1            test_of_string4  test_of_string7  test_of_string10
00082       test_of_string2  test5            test_of_string8
00083       test_of_string3  test_of_string6  test_of_string9
00084       \endverbatim
00085       
00086       The string array given in outc must be deleted with 
00087       delete[] after usage.
00088       
00089       If the value of \c max_size is less than the 
00090       length of the longest input string (plus one for
00091       a space character), then the output strings may
00092       have a larger length than \c max_size.
00093 
00094       \todo Consider making this into a class so that the 
00095       memory for the output columns is automatically 
00096       handled.
00097   */
00098   void screenify(const std::string *in_cols, int nin, 
00099                  std::string *&outc, int &nout, int max_size=80);
00100   
00101   /** 
00102       \brief Count the number of words in the string \c str 
00103    
00104       Words are defined as groups of characters separated by
00105       whitespace, where whitespace is any combination of adjacent
00106       spaces, tabs, carriage returns, etc. On most systems, whitespace
00107       is usually defined as any character corresponding to the
00108       integers 9 (horizontal tab), 10 (line feed), 11 (vertical tab),
00109       12 (form feed), 13 (carriage return), and 32 (space bar).  The
00110       test program \c misc_ts enumerates the characters between 0 and
00111       255 (inclusive) that count as whitespace for this purpose.
00112 
00113       Note that this function is used in text_in_file::string_in 
00114       to perform string input.
00115   */
00116   int count_words(std::string str);
00117   
00118   /** 
00119       \brief Naive string comparison 
00120       
00121       This is used internally for the STL routines which require a way
00122       to compare strings in the class \ref table and in the I/O
00123       classes.
00124   */
00125   typedef struct {
00126     /// Return \c s1<s2
00127     bool operator()(const std::string s1, const std::string s2) const {
00128       return s1<s2;
00129     }
00130   } string_comp;
00131   
00132   /** \brief Take a string of binary quads and compress them to 
00133       hexadecimal digits
00134 
00135       This function proceeds from left to right, ignoring parts of the
00136       string that do not consist of squences of four '1's or '0's.
00137   */
00138   std::string binary_to_hex(std::string s);
00139 
00140   /// Create a new C-style 2-dimensional array
00141   template<class type> type **new_2d_array(size_t nr, size_t nc) {
00142     type **t;
00143     t=new type *[nr];
00144     for(size_t i=0;i<nr;i++) t[i]=new type[nc];
00145     return t;
00146   }
00147 
00148   /// Create a new C-style 2-dimensional array
00149   template<class type> int delete_2d_array(type **t, size_t nr) {
00150     for(size_t i=0;i<nr;i++) delete[] t[i];
00151     delete[] t;
00152     return 0;
00153   }
00154 
00155   /** 
00156       \brief Generate number sequence for testing
00157       
00158       A class which generates \c tot numbers from -1 to 1, making sure
00159       to include -1, 1, 0, and numbers near -1, 0 and 1 (so long as \c
00160       tot is sufficiently large). If gen() is called more than \c tot
00161       times, it just recycles through the list again.  The template
00162       argument \c tot should probably be greater than or equal to
00163       three.
00164       
00165       At present, this is used to generate combinations of
00166       pathological coefficients for testing the polynomial solvers.
00167       
00168   */
00169   template<size_t tot> class gen_test_number {
00170 
00171 #ifndef DOXYGEN_INTERNAL
00172 
00173   protected:
00174 
00175     /// Count number of numbers generated
00176     int n;
00177 
00178     /// A constant factor for the argument to \c tanh().
00179     double fact;
00180 
00181 #endif
00182 
00183   public:
00184 
00185     gen_test_number() {
00186       n=0;
00187       fact=((double)tot)/4.0/5.0;
00188     }
00189 
00190     /// Return the next number in the sequence
00191     double gen() {
00192       double x, dtot=((double)tot), dn=((double)n);
00193       if (n==0) {
00194         x=-1.0;
00195       } else if (n==tot/2) {
00196         x=0.0;
00197       } else if (n==tot-1) {
00198         x=1.0;
00199       } else if (n==tot) {
00200         x=-1.0;
00201         n=0;
00202       } else if (n<((int)tot)/2) {
00203         x=(tanh((dn-dtot/4.0)/fact)-1.0)/2.0;
00204       } else {
00205         x=(tanh((dn-3.0*dtot/4.0)/fact)+1.0)/2.0;
00206       }
00207       n++;
00208       return x;
00209     }
00210   };
00211   
00212 #ifndef DOXYGENP
00213 }
00214 #endif
00215 
00216 #endif
00217 

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.