MercuryDPM  Alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExtendedMath.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef EXTENDEDMATH_H
27 #define EXTENDEDMATH_H
28 
29 #include <iostream> //std::istream and std::stringstream
30 #include <fstream> //std::fstream
31 #include <cmath>
32 #include <limits>
33 
34 #include "Vector.h"
35 
36 /*
37  * \brief
38  */
39 namespace constants
40 {
41  //Values from WolframAlpha
42  const Mdouble pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068;
43  const Mdouble sqrt_pi = 1.772453850905516027298167483341145182797549456122387128213807789852911284591032181374950656738544665;
44  const Mdouble sqr_pi = 9.869604401089358618834490999876151135313699407240790626413349376220044822419205243001773403718552232;
45  const Mdouble sqrt_2 = 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573;
46  const Mdouble sqrt_3 = 1.732050807568877293527446341505872366942805253810380628055806979451933016908800037081146186757248576;
47 }
48 
52 namespace mathsFunc
53 {
57  Mdouble gamma(Mdouble gamma_in);
58 
62  Mdouble chi_squared(const Mdouble x, const unsigned int k);
63 
67  Mdouble chi_squared_prob(const Mdouble x, const unsigned int k);
68 
78  Mdouble goldenSectionSearch(Mdouble (*function)(const Mdouble), Mdouble min, Mdouble cur, Mdouble max, Mdouble endCondition, Mdouble curVal = std::numeric_limits<Mdouble>::quiet_NaN());
79 
83  template<typename T> int sign(T val)
84  {
85  return (T(0) < val) - (val < T(0));
86  }
87 
91  template<typename T> T square(T val)
92  {
93  return val * val;
94  }
95 
99  template<typename T> T cubic(T val)
100  {
101  return val * val * val;
102  }
103 
111  bool isEqual(Mdouble v1,Mdouble v2, Mdouble absError);
119  bool isEqual(Vec3D v1, Vec3D v2, Mdouble absError);
120 
124  template<typename T> constexpr T factorial(const T t)
125  {
126  return (t == 0) ? 1 : t * factorial(t - 1);
127  }
128 
129  //platform independent implementation of sine and cosine, taken from
130  // http://stackoverflow.com/questions/18662261/fastest-implementation-of-sine-cosine-and-square-root-in-c-doesnt-need-to-b
131  // (cosine was implemented wrongly on the website, here is a corrected version)
132 
133  // sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
134  Mdouble sin(Mdouble x);
135 
136  // cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
137  Mdouble cos(Mdouble x);
138 
139  Mdouble exp(Mdouble Exponent);
140 
141  Mdouble log(Mdouble Power);
142 
143 
145  // tan=sin/cos
146  template<typename T> T tan(T x) {
147  return sin(x)/cos(x);
148  }
149 
150 
154  Mdouble chebyshev(Mdouble x, const Mdouble coef[], int N);
155 
157 
158  Mdouble I0(Mdouble x);
159 
160 }
161 
162 #endif
Mdouble chi_squared_prob(const Mdouble x, const unsigned int k)
This is the function which actually gives the probability back using a chi squared test...
Mdouble goldenSectionSearch(Mdouble(*function)(const Mdouble), Mdouble min, Mdouble cur, Mdouble max, Mdouble endCondition, Mdouble curVal=std::numeric_limits< Mdouble >::quiet_NaN())
This function performs a golden section search to find the location of the minimum of a function...
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:78
Mdouble I0(Mdouble x)
const Mdouble sqrt_pi
Definition: ExtendedMath.h:43
double Mdouble
int sign(T val)
This is a sign function, it returns -1 for negative numbers, 1 for positive numbers and 0 for 0...
Definition: ExtendedMath.h:83
Mdouble log(Mdouble Power)
Definition: ExtendedMath.cc:97
bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError)
Compares the difference of two Mdouble with an absolute error, useful in UnitTests.
T square(T val)
squares a number
Definition: ExtendedMath.h:91
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:60
constexpr T factorial(const T t)
factorial function
Definition: ExtendedMath.h:124
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:42
const Mdouble pi
Definition: ExtendedMath.h:42
Mdouble I0_exp(Mdouble x)
T tan(T x)
Definition: ExtendedMath.h:146
Mdouble chebyshev(Mdouble x, const Mdouble coef[], int N)
Namespace for evaluating the zeroth modified Bessel function of the first kind, I0(x), required in StatisticsPoint.hcc.
T cubic(T val)
calculates the cube of a number
Definition: ExtendedMath.h:99
const Mdouble sqrt_2
Definition: ExtendedMath.h:45
Mdouble gamma(Mdouble gamma_in)
This is the gamma function returns the true value for the half integer value.
Mdouble chi_squared(const Mdouble x, const unsigned int k)
This is a chi_squared function return the value x and degrees of freedom k.
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
const Mdouble sqrt_3
Definition: ExtendedMath.h:46
const Mdouble sqr_pi
Definition: ExtendedMath.h:44