MercuryDPM  Trunk
 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-2020, 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 <complex>
33 #include <limits>
34 
35 #include "NumericalVector.h"
36 #include "Vector.h"
37 #include "Quaternion.h"
38 
39 /*
40  * \brief
41  */
42 namespace constants
43 {
44 //Values from WolframAlpha
45 const Mdouble pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068;
46 const Mdouble sqrt_pi = 1.772453850905516027298167483341145182797549456122387128213807789852911284591032181374950656738544665;
47 const Mdouble sqr_pi = 9.869604401089358618834490999876151135313699407240790626413349376220044822419205243001773403718552232;
48 const Mdouble sqrt_2 = 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573;
49 const Mdouble sqrt_3 = 1.732050807568877293527446341505872366942805253810380628055806979451933016908800037081146186757248576;
50 const std::complex<Mdouble> i = {0.0, 1.0};
51 }
52 
56 namespace mathsFunc
57 {
61 Mdouble gamma(Mdouble gamma_in);
62 
67 
68 
72 Mdouble chi_squared(Mdouble x, unsigned int k);
73 
77 Mdouble chi_squared_prob(Mdouble x, unsigned int k);
78 
88 Mdouble goldenSectionSearch(Mdouble (* function)(const Mdouble), Mdouble min, Mdouble cur, Mdouble max,
89  Mdouble endCondition, Mdouble curVal = std::numeric_limits<Mdouble>::quiet_NaN());
90 
94 template<typename T>
95 int sign(T val)
96 {
97  return (T(0) < val) - (val < T(0));
98 }
99 
103 template<typename T>
104 T square(const T val)
105 {
106  return val * val;
107 }
108 
112 template<typename T>
113 T cubic(const T val)
114 {
115  return val * val * val;
116 }
117 
125 bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError);
126 
134 bool isEqual(Vec3D v1, Vec3D v2, Mdouble absError);
135 
143 bool isEqual(Matrix3D m1, Matrix3D m2, Mdouble absError);
144 
145 bool isEqual(MatrixSymmetric3D m1, MatrixSymmetric3D m2, Mdouble absError);
146 
147 bool isEqual(Quaternion v1, Quaternion v2, double absError);
148 
152 template<typename T>
153 constexpr T factorial(const T t)
154 {
155  return (t == 0) ? 1 : t * factorial(t - 1);
156 }
157 
158 //platform independent implementation of sine and cosine, taken from
159 // http://stackoverflow.com/questions/18662261/fastest-implementation-of-sine-cosine-and-square-root-in-c-doesnt-need-to-b
160 // (cosine was implemented wrongly on the website, here is a corrected version)
161 
162 // sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
163 Mdouble sin(Mdouble x);
164 
165 // cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
166 Mdouble cos(Mdouble x);
167 
168 Mdouble exp(Mdouble Exponent);
169 
170 Mdouble log(Mdouble Power);
171 
172 
174 // tan=sin/cos
175 template<typename T>
176 T tan(T x)
177 {
178  return sin(x) / cos(x);
179 }
180 
181 
185 Mdouble chebyshev(Mdouble x, const Mdouble coef[], int N);
186 
188 
189 Mdouble I0(Mdouble x);
190 
191 }
192 
193 /*
194  * \brief Namespace for functions required to calculate spherical harmonics
195  */
196 
198 {
199 
200 //Compute all the associated LegenderePolynomials up to order n, and only positive order m at location x
202 
203 //Compute all spherical harmonics up to order p, at angles theta and phi
205 
206 //Compute all squaredFactorials (see eqn 5.23 in a short course on fast multipole methods) up to order p
208 }
209 
210 #endif
Implementation of a 3D quaternion (by Vitaliy).
Definition: Quaternion.h:62
double Mdouble
Definition: GeneralDefine.h:34
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:84
Mdouble beta(Mdouble z, Mdouble w)
This is the beta function, returns the approximation based on cmath's implementation of ln(gamma) ...
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
Mdouble I0(Mdouble x)
const Mdouble sqrt_pi
Definition: ExtendedMath.h:46
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:95
T cubic(const T val)
calculates the cube of a number
Definition: ExtendedMath.h:113
Mdouble log(Mdouble Power)
bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError)
Compares the difference of two Mdouble with an absolute error, useful in UnitTests.
Mdouble chi_squared_prob(Mdouble x, unsigned int k)
This is the function which actually gives the probability back using a chi squared test...
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:64
constexpr T factorial(const T t)
factorial function
Definition: ExtendedMath.h:153
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44
Mdouble chi_squared(Mdouble x, unsigned int k)
This is a chi_squared function return the value x and degrees of freedom k.
const Mdouble pi
Definition: ExtendedMath.h:45
Mdouble I0_exp(Mdouble x)
T tan(T x)
Definition: ExtendedMath.h:176
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.
const Mdouble sqrt_2
Definition: ExtendedMath.h:48
NumericalVector< std::complex< Mdouble > > sphericalHarmonics(int p, Mdouble theta, Mdouble phi)
Mdouble gamma(Mdouble gamma_in)
This is the gamma function returns the true value for the half integer value.
NumericalVector associatedLegendrePolynomials(int n, Mdouble x)
Implementation of a 3D matrix.
Definition: Matrix.h:37
Definition: Vector.h:49
T square(const T val)
squares a number
Definition: ExtendedMath.h:104
const Mdouble sqrt_3
Definition: ExtendedMath.h:49
const Mdouble sqr_pi
Definition: ExtendedMath.h:47
Implementation of a 3D symmetric matrix.
NumericalVector computeSquaredFactorialValues(int p)