Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Math/Utils.hh
Go to the documentation of this file.
1 ///
2 /// @file Utils.hh
3 /// @brief Hammer math utilities class
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #ifndef HAMMER_MATH_UTILS
13 #define HAMMER_MATH_UTILS
14 
15 #include <complex>
16 #include <limits>
17 
18 namespace Hammer {
19 
20  static const double precision = 0.001;
21 
22  /// @brief
23  /// @param[in] val
24  /// @return
25  inline bool isZero(const std::complex<double> val) {
26  return ((fabs(val.real()) < std::numeric_limits<double>::min()) &&
27  (fabs(val.imag()) < std::numeric_limits<double>::min()));
28 
29  }
30 
31  /// @brief
32  /// @param[in] val
33  /// @return
34  inline bool isZero(const double val) {
35  return (fabs(val) < std::numeric_limits<double>::min());
36  }
37 
38  /// @brief
39  /// @param[in] val1
40  /// @param[in] val2
41  /// @return
42  inline bool fuzzyLess(const double val1, const double val2) {
43  return (val1-val2 < -1.*std::max(precision, std::numeric_limits<double>::min()));
44  }
45 
46  /// @brief
47  /// @param[in] val1
48  /// @param[in] val2
49  /// @return
50  double compareVals(const double val1, const double val2);
51 
52  /// @brief
53  /// @param[in] val1
54  /// @param[in] val2
55  /// @return
56  std::complex<double> compareVals(const std::complex<double> val1, const std::complex<double> val2);
57 
58 
59  inline double regularize(const double regularVal, const double problematicValue,
60  const double delta = std::numeric_limits<double>::min(), int direction = 0) {
61  if (fabs(problematicValue - regularVal) > delta) {
62  return regularVal;
63  }
64  return problematicValue + delta * (direction == 0 ? ((problematicValue > regularVal) ? -1.0 : 1.0) : 1.*direction);
65  }
66 
67  template <typename T>
68  typename std::enable_if<std::is_arithmetic<typename std::remove_reference<T>::type>::value, double>::type
69  toDouble(T value) {
70  return static_cast<double>(value);
71  }
72 
73  template <typename T>
74  typename std::enable_if<
75  std::is_same<typename std::remove_reference<typename std::remove_cv<T>::type>::type, std::string>::value,
76  double>::type
77  toDouble(T value) {
78  return stod(value);
79  }
80 
81  template <typename T>
82  typename std::enable_if<std::is_same<typename std::remove_reference<typename std::remove_cv<T>::type>::type,
83  std::complex<double>>::value,
84  double>::type
85  toDouble(T value) {
86  return value.real();
87  }
88 
89  template <typename T>
90  typename std::enable_if<std::is_unsigned<T>::value && std::is_integral<T>::value, T>::type minPadding(T value) {
91  if (value == 0u)
92  return value;
93  if (value == 1u)
94  return 0;
95  T pad = 0u;
96  --value;
97  while (value != 0u) {
98  value = static_cast<T>(value >> 1);
99  ++pad;
100  }
101  return pad;
102  }
103 
104 
105 } // namespace Hammer
106 
107 #endif
std::enable_if< std::is_arithmetic< typename std::remove_reference< T >::type >::value, double >::type toDouble(T value)
Definition: Math/Utils.hh:69
double compareVals(const double val1, const double val2)
Definition: Utils.cc:18
bool fuzzyLess(const double val1, const double val2)
Definition: Math/Utils.hh:42
double regularize(const double regularVal, const double problematicValue, const double delta=std::numeric_limits< double >::min(), int direction=0)
Definition: Math/Utils.hh:59
bool isZero(const std::complex< double > val)
Definition: Math/Utils.hh:25
std::enable_if< std::is_unsigned< T >::value &&std::is_integral< T >::value, T >::type minPadding(T value)
Definition: Math/Utils.hh:90
static const double precision
Definition: Math/Utils.hh:20