iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MathUtils.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies <voorhies@usc.edu>
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_UTIL_MATHUTILS_H
37 #define INCLUDE_NRT_CORE_UTIL_MATHUTILS_H
38 
39 #include <cmath>
40 
41 /*! \defgroup mathutil Miscellaneous maths utilities and constants
42  \ingroup util */
43 
44 // ######################################################################
45 /*! \defgroup constants128 Precomputed constants 128 bits IEEE quad
46 
47  Each constant is 128 bits IEEE quad. On a 64 bit system, a double is 128 bits. Thus, each number is a double on a
48  modern system.
49  \ingroup mathutil */
50 /*! @{ */ // **********************************************************************
51 
52 //! Pi
53 #define NRT_D_PI 3.1415926535897932384626433832795029
54 //! e
55 #define NRT_D_E 2.7182818284590452353602874713526625
56 //! Log Root of 2 Pi
57 #define NRT_D_LOG_SQRT_2_PI 0.9189385332046727417803297364056176
58 //! Root of 2*Pi
59 #define NRT_D_SQRT_2_PI 2.5066282746310002416123552393401041
60 //! Root of Pi
61 #define NRT_D_SQRT_PI 1.7724538509055160272981674833411451
62 //! Root of 2 DEFINED in cmath as M_SQRT2, but is not 128 bit
63 #define NRT_D_SQRT_2 1.4142135623730950488016887242096981
64 //! Natural Log of 2*Pi
65 #define NRT_D_LOG_2_PI 1.8378770664093454835606594728112353
66 //! Natural Log of Pi
67 #define NRT_D_LOG_PI 1.1447298858494001741434273513530587
68 //! Natural Log of 2 DEFINED in cmath as M_LN2, but is not 128 bit
69 #define NRT_D_LOG_2 0.6931471805599453094172321214581766
70 //! Natural Log of sqrt of 2
71 #define NRT_D_LOG_SQRT_2 0.3465735902799726547086160607290883
72 //! 2*Pi
73 #define NRT_D_2_PI 6.2831853071795862319959269370883703
74 //! The Euler-Mascheroni Constant (Not the same as e)
75 #define NRT_D_EULER_GAMMA 0.5772156649015328606065120900824024
76 //! Number of radians in one degree
77 #define NRT_D_DEGREE 0.0174532925199432957692369076848861
78 //! The golden ratio
79 #define NRT_D_GOLDEN_RATIO 1.6180339887498948482045868343656381
80 //! Catalan's Constant
81 #define NRT_D_CATALAN 0.9159655941772190150546035149323841
82 //! Glaisher-Kinkelin Constant
83 #define NRT_D_GLAISHER 1.2824271291006226368753425688697917
84 //! Khinchin's Constant
85 #define NRT_D_KHINCHIN 2.6854520010653064453097148354817956
86 //! Ramanujan-Soldner's Constant
87 #define NRT_D_SOLDNER 1.4513692348833810502839684858920274
88 //! Apery's Constant
89 #define NRT_D_APERY 1.2020569031595942853997381615114499
90 //! Gauss-Kuzmin-Wirsing constant
91 #define NRT_D_GAUSS_KUZMAN 0.3036630028987326585974481219015562
92 //! Fransén-Robinson constant
93 #define NRT_D_FRANSEN 2.8077702420285193652215011865577729
94 //! Sierpin'ski's constant
95 #define NRT_D_SERPINSKY 2.5849817595792532170658935873831711
96 //! Mills' constant
97 #define NRT_D_MILL 1.3063778838630806904686144926026057
98 //! Omega constant
99 #define NRT_D_OMEGA 0.5671432904097838729999686622103555
100 //! Golomb-Dickman constant
101 #define NRT_D_GOLOMB_DICKMAN 0.6243299885435508709929363831008372
102 
103 /*! @} */ // **********************************************************************
104 
105 // ######################################################################
106 /*! \defgroup constants256 Precomputed constants 256 bits IEEE
107  Each constant is 256 bits. This makes it a double long floating point on any modern 64 bit CPU.
108  \ingroup mathutil */
109 
110 /*! @{ */ // **********************************************************************
111 
112 //! Pi
113 #define NRT_L_PI 3.14159265358979323846264338327950288419716939937510582097494459230781L
114 //! e
115 #define NRT_L_E 2.71828182845904523536028747135266249775724709369995957496696762772407L
116 //! Log Root of 2 Pi
117 #define NRT_L_LOG_SQRT_2_PI 0.91893853320467274178032973640561763986139747363778341281715154048276L
118 //! Root of 2*Pi
119 #define NRT_L_SQRT_2_PI 2.50662827463100050241576528481104525300698674060993831662992357634229L
120 //! Root of Pi
121 #define NRT_L_SQRT_PI 1.77245385090551602729816748334114518279754945612238712821380778985291L
122 //! Root of 2 DEFINED in cmath as M_SQRT2, but is not > 128 bit
123 #define NRT_L_SQRT_2 1.41421356237309514547462185873882845044136047363281250000000000000000L
124 //! Natural Log of 2*Pi
125 #define NRT_L_LOG_2_PI 1.83787706640934548356065947281123527972279494727556682563430308096553L
126 //! Natural Log of Pi
127 #define NRT_L_LOG_PI 1.14472988584940017414342735135305871164729481291531157151362307147214L
128 //! Natural Log of 2 DEFINED in cmath as M_LN2, but is not > 128 bit
129 #define NRT_L_LOG_2 0.69314718055994528622676398299518041312694549560546875000000000000000L
130 //! 2*Pi
131 #define NRT_L_2_PI 6.28318530717958647692528676655900576839433879875021164194988918461560L
132 //! Number of radians in one degree
133 #define NRT_L_DEGREE 0.01745329251994329576923690768488612713442871888541725456097191440171L
134 //! The Euler-Mascheroni Constant (Not the same as e)
135 #define NRT_L_EULER_GAMMA 0.57721566490153286060651209008240243104215933593992359880576723488486L
136 //! The golden ratio
137 #define NRT_L_GOLDEN_RATIO 1.61803398874989484820458683436563811772030917980576286213544862270526L
138 //! Catalan's Constant
139 #define NRT_L_CATALAN 0.91596559417721901505460351493238411077414937428167213426649811962176L
140 //! Glaisher-Kinkelin Constant
141 #define NRT_L_GLAISHER 1.28242712910062263687534256886979172776768892732500119206374002174040L
142 //! Khinchin's Constant
143 #define NRT_L_KHINCHIN 2.68545200106530644530971483548179569382038229399446295305115234555721L
144 
145 /*! @} */ // **********************************************************************
146 
147 namespace nrt
148 {
149  //! Compute log of Gamma
150  /*! \ingroup mathutil */
151  double lngamma(double x);
152 
153  //! Find the value of a normal PDF at x.
154  /*! @param x The point at which to find the value of the normal PDF
155  @param mu The mean of the normal distribution
156  @param var The variance (stddev^2) of the normal distribution
157  \ingroup mathutil */
158  double normalPDF(double x, double mu, double var);
159 
160  //! Find the value of a normal CDF at x.
161  /*! @param x The point at which to find the value of the normal CDF
162  @param mu The mean of the normal distribution
163  @param var The variance (stddev^2) of the normal distribution
164  \ingroup mathutil */
165  double normalCDF(double x, double mu, double var);
166 
167  //! Find the value of a von Mises distribution at x.
168  /*! The von Mises distribution is a continuous probability distribution on a circle, and is a close
169  approximation to a normal distribution which is wrapped between -PI to PI.
170 
171  @param x The point at which to find the value of the von Mises PDF
172  @param mu The 'mean' of the distribution
173  @param k The 'concentration' of the distribution. As k increases, the von Mises distribution approaches a normal
174  distribution with mean mu, and variance 1/k.
175  \ingroup mathutil */
176  double vonMisesPDF(double x, double mu, double k);
177 
178  //! Clamp a value to within a min-max range
179  /*! \ingroup mathutil */
180  template <class T>
181  T clamped(T const & val, T const & min, T const & max);
182 }
183 
184  /*! @} */ // **********************************************************************
185 
187 
188 #endif // INCLUDE_NRT_CORE_UTIL_MATHUTILS_H