![]() |
Object-oriented Scientific Computing Library: Version 0.909
|
00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 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_GRID_H 00024 #define O2SCL_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 grid { 00038 00039 private: 00040 00041 // No public default constructor 00042 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 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 grid_end : public grid<data_t> { 00160 public: 00161 grid_end(data_t start, data_t end, size_t n_bins) : 00162 grid<data_t>(start,end,(end-start)/((data_t)n_bins),n_bins,false) { 00163 } 00164 }; 00165 00166 /** \brief Linear grid with fixed number of bins and fixed bin size 00167 */ 00168 template<class data_t=double> class grid_width : public grid<data_t> { 00169 public: 00170 grid_width(data_t start, data_t width, size_t n_bins) : 00171 grid<data_t>(start,start+n_bins*width,width,n_bins,false) { 00172 } 00173 }; 00174 00175 /** \brief Linear grid with fixed endpoint and fixed bin size 00176 */ 00177 template<class data_t=double> class grid_end_width : public grid<data_t> { 00178 public: 00179 grid_end_width(data_t start, data_t end, data_t width) : 00180 grid<data_t>(start,end,width,((size_t)((end-start)/width*1.01)),false) { 00181 } 00182 }; 00183 00184 /** \brief Logarithmic grid with fixed number of bins and fixed endpoint 00185 */ 00186 template<class data_t=double> class grid_log_end : public grid<data_t> { 00187 public: 00188 grid_log_end(data_t start, data_t end, size_t n_bins) : 00189 grid<data_t>(start,end,std::pow(end/start,1.0/((data_t)n_bins)), 00190 n_bins,true) { 00191 } 00192 }; 00193 00194 /** \brief Logarithmic grid with fixed number of bins and fixed bin size 00195 */ 00196 template<class data_t=double> class grid_log_width : public grid<data_t> { 00197 public: 00198 grid_log_width(data_t start, data_t width, size_t n_bins) : 00199 grid<data_t>(start,start*std::pow(width,n_bins),width,n_bins,true) { 00200 } 00201 }; 00202 00203 /** \brief Logarithmic grid with fixed endpoint and fixed bin size 00204 */ 00205 template<class data_t=double> class grid_log_end_width : 00206 public grid<data_t> { 00207 public: 00208 grid_log_end_width(data_t start, data_t end, data_t width) : 00209 grid<data_t>(start,end,width, 00210 ((size_t)(log(end/start)/log(width)*1.01)),true) { 00211 } 00212 }; 00213 00214 #ifndef DOXYGENP 00215 } 00216 #endif 00217 00218 #endif
Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).