ExtendedMath.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef MECURYDPM_EXTENDEDMATH_H
6 #define MECURYDPM_EXTENDEDMATH_H
7 
8 #include <iostream> //std::istream and std::stringstream
9 #include <fstream> //std::fstream
10 #include <cmath>
11 #include <complex>
12 #include <limits>
13 
14 #include "NumericalVector.h"
15 #include "Vector.h"
16 #include "Quaternion.h"
17 
18 /*
19  * \brief
20  */
21 namespace constants
22 {
23 const Mdouble pi = 3.1415926535897932384626433832795;
24 const Mdouble sqrt_pi = 1.7724538509055160272981674833411;
25 const Mdouble sqr_pi = 9.8696044010893586188344909998762;
26 const Mdouble sqrt_2 = 1.4142135623730950488016887242097;
27 const Mdouble sqrt_3 = 1.7320508075688772935274463415059;
28 const Mdouble cbrt_2 = 1.2599210498948731906665443602833;
29 const Mdouble R = 8.31446261815324;
31 const std::complex<Mdouble> i = {0.0, 1.0};
32 const Mdouble degree = pi / 180.; // degree-to-radian conversion
33 }
34 
38 namespace mathsFunc
39 {
43 Mdouble gamma(Mdouble gamma_in);
44 
49 
50 
54 Mdouble chi_squared(Mdouble x, unsigned int k);
55 
59 Mdouble chi_squared_prob(Mdouble x, unsigned int k);
60 
71  Mdouble endCondition, Mdouble curVal = std::numeric_limits<Mdouble>::quiet_NaN());
72 
76 template<typename T>
77 int sign(T val)
78 {
79  return (T(0) < val) - (val < T(0));
80 }
81 
85 template<typename T>
86 T square(const T val)
87 {
88  return val * val;
89 }
90 
94 template<typename T>
95 T cubic(const T val)
96 {
97  return val * val * val;
98 }
99 
107 bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError);
108 
116 bool isEqual(Vec3D v1, Vec3D v2, Mdouble absError);
117 
125 bool isEqual(Matrix3D m1, Matrix3D m2, Mdouble absError);
126 
128 
129 bool isEqual(Quaternion v1, Quaternion v2, double absError);
130 
134 template<typename T>
135 constexpr T factorial(const T t)
136 {
137  return (t == 0) ? 1 : t * factorial(t - 1);
138 }
139 
140 //platform independent implementation of sine and cosine, taken from
141 // http://stackoverflow.com/questions/18662261/fastest-implementation-of-sine-cosine-and-square-root-in-c-doesnt-need-to-b
142 // (cosine was implemented wrongly on the website, here is a corrected version)
143 
144 // sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
146 
147 // cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
149 
150 Mdouble exp(Mdouble Exponent);
151 
152 Mdouble log(Mdouble Power);
153 
154 
156 // tan=sin/cos
157 template<typename T>
159 {
160  return sin(x) / cos(x);
161 }
162 
163 
167 Mdouble chebyshev(Mdouble x, const Mdouble coef[], int N);
168 
170 
172 
173 }
174 
175 /*
176  * \brief Namespace for functions required to calculate spherical harmonics
177  */
178 
180 {
181 
182 //Compute all the associated LegenderePolynomials up to order n, and only positive order m at location x
184 
185 //Compute all spherical harmonics up to order p, at angles theta and phi
187 
188 //Compute all squaredFactorials (see eqn 5.23 in a short course on fast multipole methods) up to order p
190 }
191 
192 #endif
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
double Mdouble
Definition: GeneralDefine.h:13
Matrix3d m1
Definition: IOFormat.cpp:2
RowVector3d w
Definition: Matrix_resize_int.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
MatrixType m2(n_dims)
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
Implementation of a 3D matrix.
Definition: Kernel/Math/Matrix.h:17
Implementation of a 3D symmetric matrix.
Definition: MatrixSymmetric.h:16
Definition: NumericalVector.h:44
This class contains the 4 components of a quaternion and the standard operators and functions needed ...
Definition: Kernel/Math/Quaternion.h:42
Definition: Kernel/Math/Vector.h:30
@ N
Definition: constructor.cpp:22
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
char char char int int * k
Definition: level2_impl.h:374
double theta
Definition: two_d_biharmonic.cc:236
val
Definition: calibrate.py:119
Definition: GeneralDefine.h:21
const Mdouble R
Definition: ExtendedMath.h:29
const Mdouble sqr_pi
Definition: ExtendedMath.h:25
const Mdouble sqrt_3
Definition: ExtendedMath.h:27
const Mdouble pi
Definition: ExtendedMath.h:23
const Mdouble cbrt_2
Definition: ExtendedMath.h:28
const std::complex< Mdouble > i
Definition: ExtendedMath.h:31
const Mdouble stefanBoltzmanConstant
Definition: ExtendedMath.h:30
const Mdouble sqrt_pi
Definition: ExtendedMath.h:24
const Mdouble sqrt_2
Definition: ExtendedMath.h:26
const Mdouble degree
Definition: ExtendedMath.h:32
Namespace for some extra maths function that are often needed
Definition: ExtendedMath.h:39
Mdouble log(Mdouble Power)
Definition: ExtendedMath.cc:83
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.
Definition: ExtendedMath.cc:185
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:43
Mdouble beta(Mdouble z, Mdouble w)
This is the beta function, returns the approximation based on cmath's implementation of ln(gamma)
Definition: ExtendedMath.cc:143
T square(const T val)
squares a number
Definition: ExtendedMath.h:86
T tan(T x)
Definition: ExtendedMath.h:158
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:63
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:77
Mdouble I0(Mdouble x)
Definition: ExtendedMath.cc:370
bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError)
Compares the difference of two Mdouble with an absolute error, useful in UnitTests.
Definition: ExtendedMath.cc:230
Mdouble gamma(Mdouble gamma_in)
This is the gamma function returns the true value for the half integer value.
Definition: ExtendedMath.cc:116
Mdouble I0_exp(Mdouble x)
Definition: ExtendedMath.cc:290
Mdouble chebyshev(Mdouble x, const Mdouble coef[], int N)
Namespace for evaluating the zeroth modified Bessel function of the first kind, I0(x),...
Definition: ExtendedMath.cc:272
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:23
constexpr T factorial(const T t)
factorial function
Definition: ExtendedMath.h:135
Mdouble chi_squared_prob(Mdouble x, unsigned int k)
This is the function which actually gives the probability back using a chi squared test.
Definition: ExtendedMath.cc:167
Mdouble chi_squared(Mdouble x, unsigned int k)
This is a chi_squared function return the value x and degrees of freedom k.
Definition: ExtendedMath.cc:151
T cubic(const T val)
calculates the cube of a number
Definition: ExtendedMath.h:95
list x
Definition: plotDoE.py:28
t
Definition: plotPSD.py:36
Definition: ExtendedMath.h:180
NumericalVector< std::complex< Mdouble > > sphericalHarmonics(int p, Mdouble theta, Mdouble phi)
Definition: ExtendedMath.cc:424
NumericalVector computeSquaredFactorialValues(int p)
Definition: ExtendedMath.cc:452
NumericalVector associatedLegendrePolynomials(int n, Mdouble x)
Definition: ExtendedMath.cc:380