rivet is hosted by Hepforge, IPPP Durham
MathUtils.hh
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #ifndef RIVET_MathUtils_HH
00003 #define RIVET_MathUtils_HH
00004 
00005 #include "Rivet/Math/MathHeader.hh"
00006 #include "Rivet/RivetBoost.hh"
00007 #include <cassert>
00008 
00009 namespace Rivet {
00010 
00011 
00012   /// @name Comparison functions for safe floating point equality tests
00013   //@{
00014 
00015   /// Compare a floating point number to zero with a degree
00016   /// of fuzziness expressed by the absolute @a tolerance parameter.
00017   inline bool isZero(double val, double tolerance=1E-8) {
00018     return (fabs(val) < tolerance);
00019   }
00020 
00021   /// Compare an integral-type number to zero.
00022   ///
00023   /// Since there is no risk of floating point error, this function just exists
00024   /// in case @c isZero is accidentally used on an integer type, to avoid
00025   /// implicit type conversion. The @a tolerance parameter is ignored.
00026   inline bool isZero(long val, double UNUSED(tolerance)=1E-8) {
00027     return val == 0;
00028   }
00029 
00030 
00031   /// @brief Compare two floating point numbers for equality with a degree of fuzziness
00032   ///
00033   /// The @a tolerance parameter is fractional, based on absolute values of the args.
00034   inline bool fuzzyEquals(double a, double b, double tolerance=1E-5) {
00035     const double absavg = (fabs(a) + fabs(b))/2.0;
00036     const double absdiff = fabs(a - b);
00037     const bool rtn = (isZero(a) && isZero(b)) || absdiff < tolerance*absavg;
00038     // cout << a << " == " << b << "? " << rtn << endl;
00039     return rtn;
00040   }
00041 
00042   /// @brief Compare two integral-type numbers for equality with a degree of fuzziness.
00043   ///
00044   /// Since there is no risk of floating point error with integral types,
00045   /// this function just exists in case @c fuzzyEquals is accidentally
00046   /// used on an integer type, to avoid implicit type conversion. The @a
00047   /// tolerance parameter is ignored, even if it would have an
00048   /// absolute magnitude greater than 1.
00049   inline bool fuzzyEquals(long a, long b, double UNUSED(tolerance)=1E-5) {
00050     return a == b;
00051   }
00052 
00053 
00054   /// @brief Compare two floating point numbers for >= with a degree of fuzziness
00055   ///
00056   /// The @a tolerance parameter on the equality test is as for @c fuzzyEquals.
00057   inline bool fuzzyGtrEquals(double a, double b, double tolerance=1E-5) {
00058     return a > b || fuzzyEquals(a, b, tolerance);
00059   }
00060 
00061   /// @brief Compare two integral-type numbers for >= with a degree of fuzziness.
00062   ///
00063   /// Since there is no risk of floating point error with integral types,
00064   /// this function just exists in case @c fuzzyGtrEquals is accidentally
00065   /// used on an integer type, to avoid implicit type conversion. The @a
00066   /// tolerance parameter is ignored, even if it would have an
00067   /// absolute magnitude greater than 1.
00068   inline bool fuzzyGtrEquals(long a, long b, double UNUSED(tolerance)=1E-5) {
00069     return a >= b;
00070   }
00071 
00072 
00073   /// @brief Compare two floating point numbers for <= with a degree of fuzziness
00074   ///
00075   /// The @a tolerance parameter on the equality test is as for @c fuzzyEquals.
00076   inline bool fuzzyLessEquals(double a, double b, double tolerance=1E-5) {
00077     return a < b || fuzzyEquals(a, b, tolerance);
00078   }
00079 
00080   /// @brief Compare two integral-type numbers for <= with a degree of fuzziness.
00081   ///
00082   /// Since there is no risk of floating point error with integral types,
00083   /// this function just exists in case @c fuzzyLessEquals is accidentally
00084   /// used on an integer type, to avoid implicit type conversion. The @a
00085   /// tolerance parameter is ignored, even if it would have an
00086   /// absolute magnitude greater than 1.
00087   inline bool fuzzyLessEquals(long a, long b, double UNUSED(tolerance)=1E-5) {
00088     return a <= b;
00089   }
00090 
00091   //@}
00092 
00093 
00094   /// @name Ranges and intervals
00095   //@{
00096 
00097   /// Represents whether an interval is open (non-inclusive) or closed (inclusive).
00098   ///
00099   /// For example, the interval \f$ [0, \pi) \f$ is closed (an inclusive
00100   /// boundary) at 0, and open (a non-inclusive boundary) at \f$ \pi \f$.
00101   enum RangeBoundary { OPEN=0, SOFT=0, CLOSED=1, HARD=1 };
00102 
00103 
00104   /// @brief Determine if @a value is in the range @a low to @a high, for floating point numbers
00105   ///
00106   /// Interval boundary types are defined by @a lowbound and @a highbound.
00107   /// @todo Optimise to one-line at compile time?
00108   template<typename NUM>
00109   inline bool inRange(NUM value, NUM low, NUM high,
00110                       RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN) {
00111     if (lowbound == OPEN && highbound == OPEN) {
00112       return (value > low && value < high);
00113     } else if (lowbound == OPEN && highbound == CLOSED) {
00114       return (value > low && fuzzyLessEquals(value, high));
00115     } else if (lowbound == CLOSED && highbound == OPEN) {
00116       return (fuzzyGtrEquals(value, low) && value < high);
00117     } else { // if (lowbound == CLOSED && highbound == CLOSED) {
00118       return (fuzzyGtrEquals(value, low) && fuzzyLessEquals(value, high));
00119     }
00120   }
00121 
00122   /// Alternative version of inRange for doubles, which accepts a pair for the range arguments.
00123   template<typename NUM>
00124   inline bool inRange(NUM value, pair<NUM, NUM> lowhigh,
00125                       RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN) {
00126     return inRange(value, lowhigh.first, lowhigh.second, lowbound, highbound);
00127   }
00128 
00129 
00130   /// @brief Determine if @a value is in the range @a low to @a high, for integer types
00131   ///
00132   /// Interval boundary types are defined by @a lowbound and @a highbound.
00133   /// @todo Optimise to one-line at compile time?
00134   inline bool inRange(int value, int low, int high,
00135                       RangeBoundary lowbound=CLOSED, RangeBoundary highbound=CLOSED) {
00136     if (lowbound == OPEN && highbound == OPEN) {
00137       return (value > low && value < high);
00138     } else if (lowbound == OPEN && highbound == CLOSED) {
00139       return (value > low && value <= high);
00140     } else if (lowbound == CLOSED && highbound == OPEN) {
00141       return (value >= low && value < high);
00142     } else { // if (lowbound == CLOSED && highbound == CLOSED) {
00143       return (value >= low && value <= high);
00144     }
00145   }
00146 
00147   /// Alternative version of @c inRange for ints, which accepts a pair for the range arguments.
00148   inline bool inRange(int value, pair<int, int> lowhigh,
00149                       RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN) {
00150     return inRange(value, lowhigh.first, lowhigh.second, lowbound, highbound);
00151   }
00152 
00153   //@}
00154 
00155 
00156   /// @name Miscellaneous numerical helpers
00157   //@{
00158 
00159   /// Named number-type squaring operation.
00160   template <typename NUM>
00161   inline NUM sqr(NUM a) {
00162     return a*a;
00163   }
00164 
00165   /// Named number-type addition in quadrature operation.
00166   template <typename Num>
00167   inline Num add_quad(Num a, Num b) {
00168     return sqrt(a*a + b*b);
00169   }
00170 
00171   /// Named number-type addition in quadrature operation.
00172   template <typename Num>
00173   inline Num add_quad(Num a, Num b, Num c) {
00174     return sqrt(a*a + b*b + c*c);
00175   }
00176 
00177   /// A more efficient version of pow for raising numbers to integer powers.
00178   template <typename Num>
00179   inline Num intpow(Num val, unsigned int exp) {
00180     assert(exp >= 0);
00181     if (exp == 0) return (Num) 1;
00182     else if (exp == 1) return val;
00183     return val * intpow(val, exp-1);
00184   }
00185 
00186   /// Find the sign of a number
00187   inline int sign(double val) {
00188     if (isZero(val)) return ZERO;
00189     const int valsign = (val > 0) ? PLUS : MINUS;
00190     return valsign;
00191   }
00192 
00193   /// Find the sign of a number
00194   inline int sign(int val) {
00195     if (val == 0) return ZERO;
00196     return (val > 0) ? PLUS : MINUS;
00197   }
00198 
00199   /// Find the sign of a number
00200   inline int sign(long val) {
00201     if (val == 0) return ZERO;
00202     return (val > 0) ? PLUS : MINUS;
00203   }
00204 
00205   //@}
00206 
00207 
00208   /// @name Binning helper functions
00209   //@{
00210 
00211   /// @brief Make a list of @a nbins + 1 values equally spaced between @a start and @a end inclusive.
00212   ///
00213   /// NB. The arg ordering and the meaning of the nbins variable is "histogram-like",
00214   /// as opposed to the Numpy/Matlab version.
00215   inline vector<double> linspace(size_t nbins, double start, double end) {
00216     assert(end >= start);
00217     assert(nbins > 0);
00218     vector<double> rtn;
00219     const double interval = (end-start)/static_cast<double>(nbins);
00220     double edge = start;
00221     while (inRange(edge, start, end, CLOSED, CLOSED)) {
00222       rtn.push_back(edge);
00223       edge += interval;
00224     }
00225     assert(rtn.size() == nbins+1);
00226     return rtn;
00227   }
00228 
00229 
00230   /// @brief Make a list of @a nbins + 1 values exponentially spaced between @a start and @a end inclusive.
00231   ///
00232   /// NB. The arg ordering and the meaning of the nbins variable is "histogram-like",
00233   /// as opposed to the Numpy/Matlab version, and the start and end arguments are expressed
00234   /// in "normal" space, rather than as the logarithms of the start/end values as in Numpy/Matlab.
00235   inline vector<double> logspace(size_t nbins, double start, double end) {
00236     assert(end >= start);
00237     assert(start > 0);
00238     assert(nbins > 0);
00239     const double logstart = std::log(start);
00240     const double logend = std::log(end);
00241     const vector<double> logvals = linspace(nbins, logstart, logend);
00242     assert(logvals.size() == nbins+1);
00243     vector<double> rtn;
00244     foreach (double logval, logvals) {
00245       rtn.push_back(std::exp(logval));
00246     }
00247     assert(rtn.size() == nbins+1);
00248     return rtn;
00249   }
00250 
00251 
00252   /// @brief Return the bin index of the given value, @a val, given a vector of bin edges
00253   ///
00254   /// NB. The @a binedges vector must be sorted
00255   template <typename NUM>
00256   inline int index_between(const NUM& val, const vector<NUM>& binedges) {
00257     if (!inRange(val, binedges.front(), binedges.back())) return -1; //< Out of histo range
00258     int index = -1;
00259     for (size_t i = 1; i < binedges.size(); ++i) {
00260       if (val < binedges[i]) {
00261         index = i-1;
00262         break;
00263       }
00264     }
00265     assert(inRange(index, -1, binedges.size()-1));
00266     return index;
00267   }
00268 
00269   //@}
00270 
00271 
00272   /// @name Statistics functions
00273   //@{
00274 
00275   /// Calculate the mean of a sample
00276   inline double mean(const vector<int>& sample) {
00277     double mean = 0.0;
00278     for (size_t i=0; i<sample.size(); ++i) {
00279       mean += sample[i];
00280     }
00281     return mean/sample.size();
00282   }
00283 
00284   // Calculate the error on the mean, assuming poissonian errors
00285   inline double mean_err(const vector<int>& sample) {
00286     double mean_e = 0.0;
00287     for (size_t i=0; i<sample.size(); ++i) {
00288       mean_e += sqrt(sample[i]);
00289     }
00290     return mean_e/sample.size();
00291   }
00292 
00293   /// Calculate the covariance (variance) between two samples
00294   inline double covariance(const vector<int>& sample1, const vector<int>& sample2) {
00295     const double mean1 = mean(sample1);
00296     const double mean2 = mean(sample2);
00297     const size_t N = sample1.size();
00298     double cov = 0.0;
00299     for (size_t i = 0; i < N; i++) {
00300       const double cov_i = (sample1[i] - mean1)*(sample2[i] - mean2);
00301       cov += cov_i;
00302     }
00303     if (N > 1) return cov/(N-1);
00304     else return 0.0;
00305   }
00306 
00307   /// Calculate the error on the covariance (variance) of two samples, assuming poissonian errors
00308   inline double covariance_err(const vector<int>& sample1, const vector<int>& sample2) {
00309     const double mean1 = mean(sample1);
00310     const double mean2 = mean(sample2);
00311     const double mean1_e = mean_err(sample1);
00312     const double mean2_e = mean_err(sample2);
00313     const size_t N = sample1.size();
00314     double cov_e = 0.0;
00315     for (size_t i = 0; i < N; i++) {
00316       const double cov_i = (sqrt(sample1[i]) - mean1_e)*(sample2[i] - mean2) +
00317         (sample1[i] - mean1)*(sqrt(sample2[i]) - mean2_e);
00318       cov_e += cov_i;
00319     }
00320     if (N > 1) return cov_e/(N-1);
00321     else return 0.0;
00322   }
00323 
00324 
00325   /// Calculate the correlation strength between two samples
00326   inline double correlation(const vector<int>& sample1, const vector<int>& sample2) {
00327     const double cov = covariance(sample1, sample2);
00328     const double var1 = covariance(sample1, sample1);
00329     const double var2 = covariance(sample2, sample2);
00330     const double correlation = cov/sqrt(var1*var2);
00331     const double corr_strength = correlation*sqrt(var2/var1);
00332     return corr_strength;
00333   }
00334 
00335   /// Calculate the error of the correlation strength between two samples assuming Poissonian errors
00336   inline double correlation_err(const vector<int>& sample1, const vector<int>& sample2) {
00337     const double cov = covariance(sample1, sample2);
00338     const double var1 = covariance(sample1, sample1);
00339     const double var2 = covariance(sample2, sample2);
00340     const double cov_e = covariance_err(sample1, sample2);
00341     const double var1_e = covariance_err(sample1, sample1);
00342     const double var2_e = covariance_err(sample2, sample2);
00343 
00344     // Calculate the correlation
00345     const double correlation = cov/sqrt(var1*var2);
00346     // Calculate the error on the correlation
00347     const double correlation_err = cov_e/sqrt(var1*var2) -
00348       cov/(2*pow(3./2., var1*var2)) * (var1_e * var2 + var1 * var2_e);
00349 
00350 
00351     // Calculate the error on the correlation strength
00352     const double corr_strength_err = correlation_err*sqrt(var2/var1) +
00353       correlation/(2*sqrt(var2/var1)) * (var2_e/var1 - var2*var1_e/pow(2, var2));
00354 
00355     return corr_strength_err;
00356   }
00357   //@}
00358 
00359 
00360   /// @name Angle range mappings
00361   //@{
00362 
00363   /// @brief Reduce any number to the range [-2PI, 2PI]
00364   ///
00365   /// Achieved by repeated addition or subtraction of 2PI as required. Used to
00366   /// normalise angular measures.
00367   inline double _mapAngleM2PITo2Pi(double angle) {
00368     double rtn = fmod(angle, TWOPI);
00369     if (isZero(rtn)) return 0;
00370     assert(rtn >= -TWOPI && rtn <= TWOPI);
00371     return rtn;
00372   }
00373 
00374   /// Map an angle into the range (-PI, PI].
00375   inline double mapAngleMPiToPi(double angle) {
00376     double rtn = _mapAngleM2PITo2Pi(angle);
00377     if (isZero(rtn)) return 0;
00378     rtn = (rtn >   PI ? rtn-TWOPI :
00379            rtn <= -PI ? rtn+TWOPI : rtn);
00380     assert(rtn > -PI && rtn <= PI);
00381     return rtn;
00382   }
00383 
00384   /// Map an angle into the range [0, 2PI).
00385   inline double mapAngle0To2Pi(double angle) {
00386     double rtn = _mapAngleM2PITo2Pi(angle);
00387     if (isZero(rtn)) return 0;
00388     if (rtn < 0) rtn += TWOPI;
00389     if (rtn == TWOPI) rtn = 0;
00390     assert(rtn >= 0 && rtn < TWOPI);
00391     return rtn;
00392   }
00393 
00394   /// Map an angle into the range [0, PI].
00395   inline double mapAngle0ToPi(double angle) {
00396     double rtn = fabs(mapAngleMPiToPi(angle));
00397     if (isZero(rtn)) return 0;
00398     assert(rtn > 0 && rtn <= PI);
00399     return rtn;
00400   }
00401 
00402   //@}
00403 
00404 
00405   /// @name Phase space measure helpers
00406   //@{
00407 
00408   /// @brief Calculate the difference between two angles in radians
00409   ///
00410   /// Returns in the range [0, PI].
00411   inline double deltaPhi(double phi1, double phi2) {
00412     return mapAngle0ToPi(phi1 - phi2);
00413   }
00414 
00415   /// Calculate the difference between two pseudorapidities,
00416   /// returning the unsigned value.
00417   inline double deltaEta(double eta1, double eta2) {
00418     return fabs(eta1 - eta2);
00419   }
00420 
00421   /// Calculate the distance between two points in 2D rapidity-azimuthal
00422   /// ("\f$ \eta-\phi \f$") space. The phi values are given in radians.
00423   inline double deltaR(double rap1, double phi1, double rap2, double phi2) {
00424     const double dphi = deltaPhi(phi1, phi2);
00425     return sqrt( sqr(rap1-rap2) + sqr(dphi) );
00426   }
00427 
00428   /// Calculate a rapidity value from the supplied energy @a E and longitudinal momentum @a pz.
00429   inline double rapidity(double E, double pz) {
00430     if (isZero(E - pz)) {
00431       throw std::runtime_error("Divergent positive rapidity");
00432       return MAXDOUBLE;
00433     }
00434     if (isZero(E + pz)) {
00435       throw std::runtime_error("Divergent negative rapidity");
00436       return -MAXDOUBLE;
00437     }
00438     return 0.5*log((E+pz)/(E-pz));
00439   }
00440 
00441   //@}
00442 
00443 
00444 }
00445 
00446 
00447 #endif