Math.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Very basic math abstraction layer.
5  *
6  * \par
7  * This file serves as a minimal abstraction layer.
8  * Inclusion of this file makes some frequently used
9  * functions, constants, and header file inclusions
10  * OS-, compiler-, and version-independent.
11  *
12  *
13  *
14  *
15  * \author -
16  * \date -
17  *
18  *
19  * \par Copyright 1995-2017 Shark Development Team
20  *
21  * <BR><HR>
22  * This file is part of Shark.
23  * <http://shark-ml.org/>
24  *
25  * Shark is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU Lesser General Public License as published
27  * by the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * Shark is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33  * GNU Lesser General Public License for more details.
34  *
35  * You should have received a copy of the GNU Lesser General Public License
36  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
37  *
38  */
39 #ifndef SHARK_CORE_MATH_H
40 #define SHARK_CORE_MATH_H
41 
42 #include <boost/math/constants/constants.hpp>
43 #include <boost/math/special_functions/sign.hpp>
44 #include <boost/utility/enable_if.hpp>
45 #include <limits>
46 #include <cmath>
47 #include <type_traits>
48 #include <shark/Core/Exception.h>
49 
50 namespace shark {
51 
52  // define doxygen group for shark global functions, var, etc. (should only appear once in all shark files)
53  /**
54  * \defgroup shark_globals shark_globals
55  *
56  * \brief Several mathematical, linear-algebra, or other functions within Shark are not part of any particular class. They are collected here in the doxygen group "shark_globals"
57  *
58  * @{
59  *
60  */
61 
62  /**
63  * \brief Constant for sqrt( 2 * pi ).
64  */
65  static const double SQRT_2_PI = boost::math::constants::root_two_pi<double>();
66 // static const double SQRT_PI = boost::math::constants::root_pi<double>();
67 
68  /// Maximum allowed input value for exp.
69  template<class T>
71  return boost::math::constants::ln_two<T>()*std::numeric_limits<T>::max_exponent;
72  }
73  /// Minimum value for exp(x) allowed so that it is not 0.
74  template<class T>
76  return boost::math::constants::ln_two<T>()*std::numeric_limits<T>::min_exponent;
77  }
78 
79  /// Calculates x^2.
80  template <class T>
81  inline typename boost::enable_if<std::is_arithmetic<T>, T>::type sqr( const T & x) {
82  return x * x;
83  }
84 
85  /// Calculates x^3.
86  template <class T> inline T cube( const T & x) {
87  return x * x * x;
88  }
89 
90  ///\brief Logistic function/logistic function.
91  ///
92  ///Calculates the sigmoid function 1/(1+exp(-x)). The type must be arithmetic. For example
93  ///float,double,long double, int,... but no custom Type.
94  template<class T>
95  typename boost::enable_if<std::is_arithmetic<T>, T>::type sigmoid(T x){
96  if(x < minExpInput<T>()) {
97  return 1;
98  }
99  if(x > maxExpInput<T>()) {
100  return 0;
101  }
102  return 1. / (1.+ std::exp(-x));
103  }
104 
105  ///\brief Thresholded exp function, over- and underflow safe.
106  ///
107  ///Replaces the value of exp(x) for numerical reasons by the a threshold value if it gets too large.
108  ///Use it only, if there is no other way to get the function stable!
109  ///
110  ///@param x the exponent
111  template<class T>
112  T safeExp(T x ){
113  if(x > maxExpInput<T>()){
114  //std::cout<<"warning, x too high"<<std::endl;
115  return 0.9 * std::numeric_limits<long double>::max();
116  }
117  //Allow Koenig Lookup
118  using std::exp;
119  return exp(x);
120  }
121  ///\brief Thresholded log function, over- and underflow safe.
122  ///
123  ///Replaces the value of log(x) for numerical reasons by the a threshold value if it gets too low.
124  ///Use it only, if there is no other way to get the function stable!
125  ///@param x the exponent
126  template<class T>
127  T safeLog(T x)
128  {
129 
130  if(x> -std::numeric_limits<T>::epsilon() && x < std::numeric_limits<T>::epsilon()){
131  //std::cout<<"warning, x too low"<<std::endl;
132  return boost::math::sign(x)*std::numeric_limits<T>::min_exponent;
133  }
134 
135  //Allow Koenig Lookup
136  using std::log;
137  return log(x);
138  };
139 
140  ///\brief Numerically stable version of the function log(1+exp(x)).
141  ///
142  ///Numerically stable version of the function log(1+exp(x)).
143  ///This function is the integral of the famous sigmoid function.
144  ///The type must be arithmetic. For example
145  ///float,double,long double, int,... but no custom Type.
146  template<class T>
147  typename boost::enable_if<std::is_arithmetic<T>, T>::type softPlus(T x){
148  if(x > maxExpInput<T>()){
149  return x;
150  }
151  if(x < minExpInput<T>()){
152  return 0;
153  }
154  return std::log(1+std::exp(x));
155  }
156 
157  ///\brief Numerically stable version of the function log(1+exp(x)). calculated with float precision to save some time
158  ///
159  ///Numerically stable version of the function log(1+exp(x)).
160  ///This function is the integral of the famous sigmoid function.
161  inline double softPlus(double x){
162  if(x > 15){
163  return x;
164  }
165  if(x < -17){
166  return 0;
167  }
168  return std::log(1.0f+std::exp(float(x)));
169  }
170 
171  ///brief lets x have the same sign as y.
172  ///
173  ///This is the famous well known copysign function from fortran.
174  template<class T>
175  T copySign(T x, T y){
176  using std::abs;
177  if(y > 0){
178  return abs(x);
179  }
180  return -abs(x);
181  }
182 }
183 /** @}*/
184 
185 #endif