Object-oriented Scientific Computing Library: Version 0.910
hist_2d.h
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2010-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_HIST_2D_H
00024 #define O2SCL_HIST_2D_H
00025 
00026 #include <iostream>
00027 #include <o2scl/convert_units.h>
00028 #include <o2scl/smart_interp.h>
00029 #include <o2scl/omatrix_tlate.h>
00030 #include <o2scl/twod_intp.h>
00031 #include <o2scl/uniform_grid.h>
00032 
00033 #ifndef DOXYGENP
00034 namespace o2scl {
00035 #endif
00036   
00037   /** \brief A two-dimensional histogram class
00038 
00039       Experimental.
00040 
00041       \future Write a function to create a 1-d histogram 
00042       from a 2-d histogram either by selecting one bin
00043       in one axis or by marginalizing over one direction.
00044    */
00045   class hist_2d {
00046     
00047   protected:
00048 
00049     /// Bin locations (Nx+1)
00050     ovector xa;
00051 
00052     /// Bin locations (Ny+1)
00053     ovector ya;
00054 
00055     /// Values (Nx,Ny)
00056     omatrix wgt;
00057 
00058     /// "Central" values for x-axis (N)
00059     ovector xrep;
00060 
00061     /// "Central" values for y-axis (N)
00062     ovector yrep;
00063     
00064     /// User-defined central values for x-axis (N)
00065     ovector user_xrep;
00066     
00067     /// User-defined central values for y-axis (N)
00068     ovector user_yrep;
00069     
00070     /// Number of x-bins
00071     size_t hsize_x;
00072 
00073     /// Number of y-bins
00074     size_t hsize_y;
00075 
00076     /// Rep mode for x
00077     size_t xrmode;
00078 
00079     /// Rep mode for y
00080     size_t yrmode;
00081 
00082     /// A pointer to the interpolation manager
00083     base_interp_mgr<ovector_const_view> *bim1;
00084     
00085     /// A pointer to the subvector interpolation manager
00086     base_interp_mgr<ovector_const_subvector> *bim2;
00087     
00088     /// Default interpolation manager
00089     def_interp_mgr<ovector_const_view,cspline_interp> dim1;
00090 
00091     /// Default interpolation manager
00092     def_interp_mgr<ovector_const_subvector,cspline_interp> dim2;
00093 
00094     /** \brief Allocate for a histogram of size \c nx, \c ny
00095         
00096         This function also sets all the weights to zero.
00097     */
00098     void allocate(size_t nx, size_t ny);
00099 
00100     /** \brief An internal function to automatically set
00101         \ref xrep and \ref yrep
00102     */
00103     void set_reps_auto();
00104 
00105   public:
00106 
00107     hist_2d();
00108 
00109     virtual ~hist_2d();
00110 
00111     hist_2d(const hist_2d &h);
00112 
00113     hist_2d &operator=(const hist_2d &h);
00114     
00115     /** \brief If true, allow abcissa larger than largest bin limit
00116         to correspond to the highest bin (default false).
00117     */
00118     bool extend_rhs;
00119 
00120     /// \name Initial bin setup
00121     //@{
00122     int set_bins(uniform_grid<double> gx, uniform_grid<double> gy);
00123 
00124     /// Set the bins from a vector
00125     template<class vec_t> int set_bins(size_t nx, vec_t &vx,
00126                                        size_t ny, vec_t &vy) {
00127       allocate(nx-1,ny-1);
00128       for(size_t i=0;i<nx;i++) xa[i]=vx[i];
00129       for(size_t i=0;i<ny;i++) ya[i]=vy[i];
00130       set_reps_auto();
00131       return gsl_success;
00132     }
00133     //@}
00134 
00135     /// \name Weight functions
00136     //@{
00137     /// Increment bin for \c x by value \c val
00138     int update_i(size_t i, size_t j, double val=1.0) {
00139       wgt[i][j]+=val;
00140       return 0;
00141     }
00142 
00143     /// Increment bin for \c x by value \c val
00144     int update(double x, double y, double val=1.0) {
00145       size_t i, j;
00146       get_bin_indices(x,y,i,j);
00147       return update_i(i,j,val);
00148     }
00149 
00150     /// Return contents of bin with index \c i
00151     const double &get_wgt_i(size_t i, size_t j) const;
00152 
00153     /// Return contents of bin for \c x
00154     const double &get_wgt(double x, double y) const {
00155       size_t i, j;
00156       get_bin_indices(x,y,i,j);
00157       return get_wgt_i(i,j);
00158     }
00159 
00160     /// Return contents of bin with index \c i
00161     double &get_wgt_i(size_t i, size_t j);
00162 
00163     /// Return contents of bin for \c x
00164     double &get_wgt(double x, double y) {
00165       size_t i, j;
00166       get_bin_indices(x,y,i,j);
00167       return get_wgt_i(i,j);
00168     }
00169 
00170     /// Set contents of bin with index \c i to value \c val
00171     int set_wgt_i(size_t i, size_t j, double val);
00172     
00173     /// Set contents of bin for \c x to value \c val
00174     int set_wgt(double x, double y, double val) {
00175       size_t i, j;
00176       get_bin_indices(x,y,i,j);
00177       return set_wgt_i(i,j,val);
00178     }
00179     
00180     /// Get a const reference to the full data vector
00181     const omatrix &get_wgts() const {
00182       return wgt;
00183     }
00184 
00185     /// Get a reference to the full data vector
00186     omatrix &get_wgts() {
00187       return wgt;
00188     }
00189     //@}
00190     
00191     /// \name Delete functions
00192     //@{
00193     /// Clear the data, but leave the bins as is
00194     int clear_wgts();
00195 
00196     /// Clear the entire histogram
00197     int clear();
00198     //@}
00199 
00200     /// \name Bin manipulation
00201     //@{
00202     /// Get the index of the bin which holds \c x
00203     void get_bin_indices(double x, double y, size_t &i, size_t &j) const;
00204 
00205     /// Get the index of the bin which holds \c x
00206     size_t get_x_bin_index(double x) const;
00207     
00208     /// Get the indey of the bin which holds \c y
00209     size_t get_y_bin_index(double y) const;
00210 
00211     /// Get the lower edge of bin of index \c i
00212     double &get_x_low_i(size_t i);
00213 
00214     /// Get the lower edge of bin of index \c i
00215     const double &get_x_low_i(size_t i) const;
00216 
00217     /// Get the upper edge of bin of index \c i
00218     double &get_x_high_i(size_t i);
00219     
00220     /// Get the upper edge of bin of index \c i
00221     const double &get_x_high_i(size_t i) const;
00222 
00223     /// Get the lower edge of bin of index \c j
00224     double &get_y_low_i(size_t j);
00225 
00226     /// Get the lower edge of bin of index \c j
00227     const double &get_y_low_i(size_t j) const;
00228 
00229     /// Get the upper edge of bin of index \c j
00230     double &get_y_high_i(size_t j);
00231     
00232     /// Get the upper edge of bin of index \c j
00233     const double &get_y_high_i(size_t j) const;
00234     //@}
00235 
00236     /// \name Rep modes (default is \c rmode_avg)
00237     //@{
00238     static const size_t rmode_avg=0;
00239     static const size_t rmode_user=1;
00240     static const size_t rmode_low=2;
00241     static const size_t rmode_high=3;
00242     static const size_t rmode_gmean=4;
00243     //@}
00244     
00245     /// \name Rep functions
00246     //@{
00247     /// Set the representative x-values for each bin
00248     template<class vec_t> int set_reps(size_t nx, vec_t &vx,
00249                                        size_t ny, vec_t &vy) {
00250       xrmode=rmode_user;
00251       yrmode=rmode_user;
00252       if (user_xrep.size()!=nx || user_yrep.size()!=ny) {
00253         std::string s="Expected vectors of size "+itos(user_xrep.size())+
00254           " and got a vector of size "+itos(nx)+" in hist::set_reps().";
00255         O2SCL_ERR_RET(s.c_str(),gsl_einval);
00256       }
00257       for(size_t i=0;i<nx;i++) user_xrep[i]=vx[i];
00258       for(size_t i=0;i<ny;i++) user_yrep[i]=vy[i];
00259       return gsl_success;
00260     }
00261 
00262     /// Set the representative x-values for each bin
00263     template<class vec_t> int set_x_reps(size_t nx, vec_t &vx) {
00264       xrmode=rmode_user;
00265       if (user_xrep.size()!=nx) {
00266         std::string s="Expected vectors of size "+itos(user_xrep.size())+
00267           " and got a vector of size "+itos(nx)+" in hist::set_reps().";
00268         O2SCL_ERR_RET(s.c_str(),gsl_einval);
00269       }
00270       for(size_t i=0;i<nx;i++) user_xrep[i]=vx[i];
00271       return gsl_success;
00272     }
00273 
00274     /// Set the representative y-values for each bin
00275     template<class vec_t> int set_y_reps(size_t ny, vec_t &vy) {
00276       yrmode=rmode_user;
00277       if (user_yrep.size()!=ny) {
00278         std::string s="Eypected vectors of size "+itos(user_yrep.size())+
00279           " and got a vector of size "+itos(ny)+" in hist::set_reps().";
00280         O2SCL_ERR_RET(s.c_str(),gsl_einval);
00281       }
00282       for(size_t i=0;i<ny;i++) user_yrep[i]=vy[i];
00283       return gsl_success;
00284     }
00285 
00286     /// Set mode used to compute bin reps
00287     int set_rep_mode(size_t x_mode, size_t y_mode);
00288 
00289     /// Get mode used to compute bin reps
00290     size_t get_x_rep_mode() const {
00291       return xrmode;
00292     }
00293 
00294     /// Get mode used to compute bin reps
00295     size_t get_y_rep_mode() const {
00296       return yrmode;
00297     }
00298 
00299     /// Get a reference to the full vector of bin specifications
00300     const ovector &get_x_bins() const {
00301       return xa;
00302     }
00303 
00304     /// Get a reference to the full vector of bin specifications
00305     const ovector &get_y_bins() const {
00306       return ya;
00307     }
00308 
00309     /// Return the histogram size of the x coordinate
00310     size_t size_x() const {
00311       return hsize_x;
00312     }
00313 
00314     /// Return the histogram size of the y coordinate
00315     size_t size_y() const {
00316       return hsize_y;
00317     }
00318 
00319     /// Get a reference to the user-specified reps for x coordinates
00320     const ovector &get_user_reps_x() const {
00321       return user_xrep;
00322     }
00323 
00324     /// Get a reference to the user-specified reps for y coordinates
00325     const ovector &get_user_reps_y() const {
00326       return user_yrep;
00327     }
00328 
00329     /// Return the rep of bin of index \c i
00330     double &get_x_rep_i(size_t i);
00331 
00332     /// Return the rep of bin of index \c i
00333     const double &get_x_rep_i(size_t i) const;
00334 
00335     /// Return the rep of bin of index \c j
00336     double &get_y_rep_i(size_t j);
00337 
00338     /// Return the rep of bin of index \c j
00339     const double &get_y_rep_i(size_t j) const;
00340     //@}
00341 
00342     /// Set up a twod_intp object for interpolation
00343     int setup_interp(twod_intp &ti, bool x_first=true) {
00344       ti.set_data(hsize_x,hsize_y,xrep,yrep,wgt,x_first);
00345       return gsl_success;
00346     }
00347 
00348   };
00349 
00350 #ifndef DOXYGENP
00351 }
00352 #endif
00353 
00354 #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).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.