![]() |
Object-oriented Scientific Computing Library: Version 0.910
|
00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2011-2012, 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_UNIFORM_GRID_H 00024 #define O2SCL_UNIFORM_GRID_H 00025 00026 #include <o2scl/err_hnd.h> 00027 00028 #ifndef DOXYGENP 00029 namespace o2scl { 00030 #endif 00031 00032 /** \brief A class representing a uniform linear or logarithmic grid 00033 00034 \note This class has no public constructors and is to be 00035 instantiated through its children. 00036 */ 00037 template<class data_t=double> class uniform_grid { 00038 00039 private: 00040 00041 // No public default constructor 00042 uniform_grid() {} 00043 00044 protected: 00045 00046 /// The low-side of the first bin 00047 data_t g_start; 00048 /// The high-side of the last bin 00049 data_t g_end; 00050 /// The width of each bin 00051 data_t g_width; 00052 /// The number of bins 00053 size_t g_n_bins; 00054 /// If true, use a logarithmic scale 00055 bool g_log; 00056 00057 /** \brief Construct a grid with specified values 00058 00059 \note This function is not public because it might create 00060 grids that are non-sensical. We require users to create grid 00061 objects using one of the children which don't allow 00062 non-sensical grids. 00063 */ 00064 uniform_grid(data_t start, data_t end, data_t width, size_t n_bins, 00065 bool log=false) { 00066 if (n_bins==0) { 00067 O2SCL_ERR("Requested zero bins in grid::grid().",gsl_einval); 00068 } 00069 g_start=start; 00070 g_end=end; 00071 g_width=width; 00072 g_n_bins=n_bins; 00073 g_log=log; 00074 } 00075 00076 public: 00077 00078 /** \brief Get the number of bins (regions in between grid points) 00079 */ 00080 size_t get_nbins() { 00081 return g_n_bins; 00082 } 00083 00084 /** \brief Get the number of points in the grid (always get_nbins()+1) 00085 */ 00086 size_t get_npoints() { 00087 return g_n_bins+1; 00088 } 00089 00090 /** \brief Return true if the grid is logarithmic 00091 */ 00092 bool is_log() { 00093 return g_log; 00094 } 00095 00096 /** \brief Fill a vector with the specified grid 00097 */ 00098 template<class vec_t> void vector(vec_t &v) { 00099 if (!g_log) { 00100 if (g_start<g_end) { 00101 v[0]=g_start; 00102 for(size_t i=1;i<g_n_bins;i++) { 00103 v[i]=g_start+i*g_width; 00104 } 00105 v[g_n_bins]=g_end; 00106 } else { 00107 v[0]=g_start; 00108 for(size_t i=1;i<g_n_bins;i++) { 00109 v[i]=g_start-i*g_width; 00110 } 00111 v[g_n_bins]=g_end; 00112 } 00113 } else { 00114 if (g_start<g_end) { 00115 v[0]=g_start; 00116 for(size_t i=1;i<g_n_bins;i++) { 00117 v[i]=g_start*std::pow(g_width,((data_t)i)); 00118 } 00119 v[g_n_bins]=g_end; 00120 } else { 00121 v[0]=g_start; 00122 for(size_t i=1;i<g_n_bins;i++) { 00123 v[i]=g_start/std::pow(g_width,((data_t)i)); 00124 } 00125 v[g_n_bins]=g_end; 00126 } 00127 } 00128 } 00129 00130 /** \brief Get the grid point with index \c i 00131 (\f$ i \in [0,\mathrm{n_bins}] \f$) 00132 */ 00133 const data_t operator[](size_t i) const { 00134 if (!g_log) { 00135 if (g_start<g_end) { 00136 if (i==0) return g_start; 00137 else if (i==g_n_bins) return g_end; 00138 return g_start+i*g_width; 00139 } else { 00140 if (i==0) return g_start; 00141 else if (i==g_n_bins) return g_end; 00142 return g_start-i*g_width; 00143 } 00144 } 00145 if (g_start<g_end) { 00146 if (i==0) return g_start; 00147 else if (i==g_n_bins) return g_end; 00148 return g_start*std::pow(g_width,((data_t)i)); 00149 } 00150 if (i==0) return g_start; 00151 else if (i==g_n_bins) return g_end; 00152 return g_start/std::pow(g_width,((data_t)i)); 00153 } 00154 00155 }; 00156 00157 /** \brief Linear grid with fixed number of bins and fixed endpoint 00158 */ 00159 template<class data_t=double> class uniform_grid_end : 00160 public uniform_grid<data_t> { 00161 public: 00162 uniform_grid_end(data_t start, data_t end, size_t n_bins) : 00163 uniform_grid<data_t>(start,end,(end-start)/((data_t)n_bins), 00164 n_bins,false) { 00165 } 00166 }; 00167 00168 /** \brief Linear grid with fixed number of bins and fixed bin size 00169 */ 00170 template<class data_t=double> class uniform_grid_width : 00171 public uniform_grid<data_t> { 00172 public: 00173 uniform_grid_width(data_t start, data_t width, size_t n_bins) : 00174 uniform_grid<data_t>(start,start+n_bins*width,width,n_bins,false) { 00175 } 00176 }; 00177 00178 /** \brief Linear grid with fixed endpoint and fixed bin size 00179 */ 00180 template<class data_t=double> class uniform_grid_end_width : 00181 public uniform_grid<data_t> { 00182 public: 00183 uniform_grid_end_width(data_t start, data_t end, data_t width) : 00184 uniform_grid<data_t>(start,end,width, 00185 ((size_t)((end-start)/width*1.01)),false) { 00186 } 00187 }; 00188 00189 /** \brief Logarithmic grid with fixed number of bins and fixed endpoint 00190 */ 00191 template<class data_t=double> class uniform_grid_log_end : 00192 public uniform_grid<data_t> { 00193 public: 00194 uniform_grid_log_end(data_t start, data_t end, size_t n_bins) : 00195 uniform_grid<data_t>(start,end,std::pow(end/start,1.0/((data_t)n_bins)), 00196 n_bins,true) { 00197 } 00198 }; 00199 00200 /** \brief Logarithmic grid with fixed number of bins and fixed bin size 00201 */ 00202 template<class data_t=double> class uniform_grid_log_width : 00203 public uniform_grid<data_t> { 00204 public: 00205 uniform_grid_log_width(data_t start, data_t width, size_t n_bins) : 00206 uniform_grid<data_t>(start,start*std::pow(width,n_bins), 00207 width,n_bins,true) { 00208 } 00209 }; 00210 00211 /** \brief Logarithmic grid with fixed endpoint and fixed bin size 00212 */ 00213 template<class data_t=double> class uniform_grid_log_end_width : 00214 public uniform_grid<data_t> { 00215 public: 00216 uniform_grid_log_end_width(data_t start, data_t end, data_t width) : 00217 uniform_grid<data_t>(start,end,width, 00218 ((size_t)(log(end/start)/log(width)*1.01)),true) { 00219 } 00220 }; 00221 00222 #ifndef DOXYGENP 00223 } 00224 #endif 00225 00226 #endif
Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).