yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
MathUtils.h File Reference
#include "YODA/Exceptions.h"
#include "YODA/Config/BuildConfig.h"
#include <algorithm>
#include <functional>
#include <numeric>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
#include <limits>
#include <map>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

Go to the source code of this file.

Namespaces

namespace  YODA
 Anonymous namespace to limit visibility.
 

Enumerations

enum  YODA::Sign { YODA::MINUS = -1 , YODA::ZERO = 0 , YODA::PLUS = 1 }
 Enum for signs of numbers. More...
 

Functions

Miscellaneous numerical helpers
template<typename NUM >
NUM YODA::sqr (NUM a)
 Named number-type squaring operation.
 
template<typename Num >
Num YODA::add_quad (Num a, Num b)
 Named number-type addition in quadrature operation.
 
template<typename Num >
Num YODA::add_quad (Num a, Num b, Num c)
 Named number-type addition in quadrature operation.
 
int YODA::sign (double val)
 Find the sign of a number.
 
int YODA::sign (int val)
 Find the sign of a number.
 
int YODA::sign (long val)
 Find the sign of a number.
 
Binning helper functions
std::vector< double > YODA::linspace (size_t nbins, double xmin, double xmax, bool include_end=true)
 Make a list of nbins + 1 values uniformly spaced between xmin and xmax inclusive.
 
std::vector< double > YODA::logspace (size_t nbins, double xmin, double xmax, bool include_end=true)
 Make a list of nbins + 1 values uniformly spaced in log(x) between xmin and xmax inclusive.
 
std::vector< double > YODA::pdfspace (size_t nbins, double xmin, double xmax, std::function< double(double)> &fn, size_t nsample=10000)
 Make a list of nbins + 1 values spaced with density ~ f(x) between xmin and end inclusive.
 
template<typename NUM >
int YODA::index_between (const NUM &val, const std::vector< NUM > &binedges)
 Return the bin index of the given value, val, given a vector of bin edges.
 
Statistics functions
double YODA::effNumEntries (const double sumW, const double sumW2)
 Calculate the effective number of entries of a sample.
 
double YODA::effNumEntries (const std::vector< double > &weights)
 Calculate the effective number of entries of a sample.
 
double YODA::mean (const std::vector< int > &sample)
 Calculate the mean of a sample.
 
double YODA::mean (const double sumWX, const double sumW)
 Calculate the weighted mean of a sample.
 
double YODA::mean (const std::vector< double > &sample, const std::vector< double > &weights)
 Calculate the weighted mean of a sample.
 
double YODA::variance (const double sumWX, const double sumW, const double sumWX2, const double sumW2)
 Calculate the weighted variance of a sample.
 
double YODA::variance (const std::vector< double > &sample, const std::vector< double > &weights)
 Calculate the weighted variance of a sample.
 
double YODA::stdDev (const double sumWX, const double sumW, const double sumWX2, const double sumW2)
 Calculate the weighted standard deviation of a sample.
 
double YODA::stdDev (const std::vector< double > &sample, const std::vector< double > &weights)
 Calculate the weighted variance of a sample.
 
double YODA::stdErr (const double sumWX, const double sumW, const double sumWX2, const double sumW2)
 Calculate the weighted standard error of a sample.
 
double YODA::stdErr (const std::vector< double > &sample, const std::vector< double > &weights)
 Calculate the weighted variance of a sample.
 
double YODA::RMS (const double sumWX2, const double sumW, const double sumW2)
 Calculate the weighted RMS of a sample.
 
double YODA::RMS (const std::vector< double > &sample, const std::vector< double > &weights)
 Calculate the weighted RMS of a sample.
 
double YODA::covariance (const std::vector< int > &sample1, const std::vector< int > &sample2)
 Calculate the covariance (variance) between two samples.
 
double YODA::correlation (const std::vector< int > &sample1, const std::vector< int > &sample2)
 Calculate the correlation strength between two samples.
 
double YODA::naiveChi2 (const std::vector< double > &sample1, const std::vector< double > &sample2, const std::vector< double > &s1errors=std::vector< double >{}, const std::vector< double > &s2errors=std::vector< double >{})
 Calculate the error-weighted chi2 statistic between two samples.
 
double YODA::naiveChi2reduced (const std::vector< double > &sample1, const std::vector< double > &sample2, const std::vector< double > &s1errors=std::vector< double >{}, const std::vector< double > &s2errors=std::vector< double >{})
 Calculate the error-weighted reduced chi2 statistic between two samples.
 

Variables

static const double YODA::MAXDOUBLE = DBL_MAX
 
static const double YODA::MAXINT = INT_MAX
 
static const double YODA::PI = M_PI
 A pre-defined value of $ \pi $.
 
static const double YODA::TWOPI = 2*M_PI
 A pre-defined value of $ 2\pi $.
 
static const double YODA::HALFPI = M_PI_2
 A pre-defined value of $ \pi/2 $.
 

Ranges and intervals

enum  YODA::RangeBoundary { YODA::OPEN =0 , YODA::SOFT =0 , YODA::CLOSED =1 , YODA::HARD =1 }
 
template<typename NUM >
bool YODA::inRange (NUM value, NUM low, NUM high, RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN)
 Determine if value is in the range low to high, for floating point numbers.
 
template<typename NUM >
bool YODA::inRange (NUM value, std::pair< NUM, NUM > lowhigh, RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN)
 Alternative version of inRange for doubles, which accepts a pair for the range arguments.
 
bool YODA::inRange (int value, int low, int high, RangeBoundary lowbound=CLOSED, RangeBoundary highbound=CLOSED)
 Determine if value is in the range low to high, for integer types.
 
bool YODA::inRange (int value, std::pair< int, int > lowhigh, RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN)
 Alternative version of inRange for ints, which accepts a pair for the range arguments.
 

Comparison functions for safe (floating point) equality tests

static std::function< bool(const double, const double)> YODA::fuzzyEqComp
 Comparator wrapper to use with STL algorithms, e.g. std::equal etc.
 
template<typename NUM >
std::enable_if_t< std::is_floating_point_v< NUM >, bool > YODA::isZero (NUM val, double tolerance=1e-8)
 Compare a number to zero.
 
template<typename NUM >
std::enable_if_t< std::is_integral_v< NUM >, bool > YODA::isZero (NUM val, double=1e-5)
 Compare a number to zero.
 
template<typename NUM >
std::enable_if_t< std::is_floating_point_v< NUM >, bool > YODA::isNaN (NUM val)
 Check if a number is NaN.
 
template<typename NUM >
std::enable_if_t< std::is_floating_point_v< NUM >, bool > YODA::notNaN (NUM val)
 Check if a number is non-NaN.
 
template<typename N1 , typename N2 >
std::enable_if_t< std::is_arithmetic_v< N1 > &&std::is_arithmetic_v< N2 > &&(std::is_floating_point_v< N1 >||std::is_floating_point_v< N2 >), bool > YODA::fuzzyEquals (N1 a, N2 b, double tolerance=1e-5)
 Compare two numbers for equality with a degree of fuzziness.
 
template<typename N1 , typename N2 >
std::enable_if_t< std::is_integral_v< N1 > &&std::is_integral_v< N2 >, bool > YODA::fuzzyEquals (N1 a, N2 b, double)
 Compare two numbers for equality with a degree of fuzziness.
 
template<typename N1 , typename N2 >
std::enable_if_t< std::is_arithmetic_v< N1 > &&std::is_arithmetic_v< N2 >, bool > YODA::fuzzyGtrEquals (N1 a, N2 b, double tolerance=1e-5)
 Compare two numbers for >= with a degree of fuzziness.
 
template<typename N1 , typename N2 >
std::enable_if_t< std::is_arithmetic_v< N1 > &&std::is_arithmetic_v< N2 >, bool > YODA::fuzzyLessEquals (N1 a, N2 b, double tolerance=1e-5)
 Compare two floating point numbers for <= with a degree of fuzziness.
 
double YODA::approx (double a, int n=5)
 Returns a number floored at the nth decimal place.