Object-oriented Scientific Computing Library: Version 0.908
hist_grid.h
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2010, 2011, 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_HIST_GRID_H
00024 #define O2SCL_HIST_GRID_H
00025 
00026 #ifndef DOXYGENP
00027 namespace o2scl {
00028 #endif
00029   
00030   /** \brief A histogram grid specification
00031 
00032       This class is used to specify a grid for the histogram
00033       classes built upon \ref hist_base. Since all constructors
00034       are private, use \ref hist_base::grid_size() and other
00035       related functions to create a histogram grid object.
00036    */
00037   class hist_grid {
00038 
00039   private:
00040 
00041     /// Ensure this class is not instantiated by the end-user
00042     hist_grid() {
00043     }
00044     
00045   public:
00046 
00047     friend class hist_base;
00048 
00049     /// The low-side of the first bin
00050     double start;
00051     /// The high-side of the last bin
00052     double end;
00053     /// The size of each bin
00054     double size;
00055     /// The number of bins
00056     size_t n;
00057     /// If true, use a logarithmic scale
00058     bool log;
00059 
00060   };
00061 
00062   /** \brief A base class for histograms
00063 
00064       This base class contains a few functions for creating histogram
00065       grids.
00066    */
00067   class hist_base {
00068 
00069   public:
00070 
00071     /// Construct a histogram grid based on a fixed bin size
00072     hist_grid grid_size(double start, double size, size_t n, bool log=false) {
00073       hist_grid h;
00074       h.start=start;
00075       h.size=size;
00076       h.n=n;
00077       h.log=log;
00078       if (log) {
00079         h.end=start*std::pow(size,((double)n));
00080       } else {
00081         h.end=start+n*size;
00082       }
00083       return h;
00084     }
00085 
00086     /// Construct a histogram grid based on fixed start and end points
00087     hist_grid grid_end(double start, double end, size_t n, bool log=false) {
00088       hist_grid h;
00089       h.start=start;
00090       h.end=end;
00091       h.n=n;
00092       h.log=log;
00093       if (log) {
00094         h.size=std::pow(end/start,1.0/((double)n));
00095       } else {
00096         h.size=(end-start)/((double)n);
00097       }
00098       return h;
00099     }
00100     
00101     /** \brief Construct a histogram grid based on fixed start and end 
00102         points and a fixed bin size
00103     */
00104     hist_grid grid_end_size(double start, double end, double size, 
00105                             bool log=false) {
00106       hist_grid h;
00107       h.start=start;
00108       h.end=end;
00109       h.size=size;
00110       h.log=log;
00111       if (log) {
00112         h.n=((size_t)(std::log(end/start)/std::log(size)*(1.0+1.0e-12)));
00113       } else {
00114         h.n=((size_t)(fabs(start-end)/size*(1.0+1.0e-12)));
00115       }
00116       return h;
00117     }
00118 
00119     /** \brief An internal function used by the histogram classes
00120         to generate a uniform grid
00121      */
00122     template<class vec_t>
00123       int uniform_grid_internal(double start, double end, double bin_size,
00124                                 size_t n, vec_t &v) {
00125 
00126       bin_size=fabs(bin_size);
00127 
00128       if (start<end) {
00129         v[0]=start;
00130         for(size_t i=1;i<n;i++) {
00131           v[i]=start+i*bin_size;
00132         }
00133         v[n]=end;
00134       } else {
00135         v[0]=start;
00136         for(size_t i=1;i<n;i++) {
00137           v[i]=start-i*bin_size;
00138         }
00139         v[n]=end;
00140       }
00141 
00142       return 0;
00143     }
00144 
00145     /** \brief An internal function used by the histogram classes
00146         to generate a logarithmic grid
00147      */
00148     template<class vec_t>
00149       int logarithmic_grid_internal(double start, double end,
00150                                     double bin_factor, size_t n,
00151                                     vec_t &v) {
00152 
00153       bin_factor=fabs(bin_factor);
00154 
00155       if (start<end) {
00156         v[0]=start;
00157         for(size_t i=1;i<n;i++) {
00158           v[i]=start*std::pow(bin_factor,((double)i));
00159         }
00160         v[n]=end;
00161       } else {
00162         v[0]=start;
00163         for(size_t i=1;i<n;i++) {
00164           v[i]=start/std::pow(bin_factor,((double)i));
00165         }
00166         v[n]=end;
00167       }
00168 
00169       return 0;
00170     }
00171 
00172   };
00173   
00174 #ifndef DOXYGENP
00175 }
00176 #endif
00177 
00178 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information). Project hosting by Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads .