MercuryDPM  Alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mathsFunc Namespace Reference

Namespace for some extra maths function that are often needed. More...

Functions

Mdouble gamma (Mdouble gamma_in)
 This is the gamma function returns the true value for the half integer value. More...
 
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. More...
 
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. More...
 
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. More...
 
template<typename T >
int sign (T val)
 This is a sign function, it returns -1 for negative numbers, 1 for positive numbers and 0 for 0. More...
 
template<typename T >
square (T val)
 squares a number More...
 
template<typename T >
cubic (T val)
 calculates the cube of a number More...
 
bool isEqual (Mdouble v1, Mdouble v2, Mdouble absError)
 Compares the difference of two Mdouble with an absolute error, useful in UnitTests. More...
 
bool isEqual (Vec3D v1, Vec3D v2, Mdouble absError)
 Compares the difference of two Vec3D with an absolute error, useful in UnitTests. More...
 
template<typename T >
constexpr T factorial (const T t)
 factorial function More...
 
Mdouble sin (Mdouble x)
 
Mdouble cos (Mdouble x)
 
Mdouble exp (Mdouble Exponent)
 
Mdouble log (Mdouble Power)
 
template<typename T >
tan (T x)
 
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. More...
 
Mdouble I0_exp (Mdouble x)
 
Mdouble I0 (Mdouble x)
 

Detailed Description

Namespace for some extra maths function that are often needed.

Function Documentation

Mdouble mathsFunc::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.

Definition at line 242 of file ExtendedMath.cc.

References assert.

Referenced by I0_exp().

243 {
244  const Mdouble *p = coef;
245  Mdouble b0 = *p++;
246  Mdouble b1 = 0, b2;
247  int i = N - 1;
248 
249  assert(i > 0);
250  do
251  {
252  b2 = b1;
253  b1 = b0;
254  b0 = x * b1 - b2 + *p++;
255  }
256  while ( --i);
257 
258  return (0.5 * (b0 - b2));
259 }
double Mdouble
#define assert(e,...)
Definition: Logger.h:584
Mdouble mathsFunc::chi_squared ( const Mdouble  x,
const unsigned int  k 
)

This is a chi_squared function return the value x and degrees of freedom k.

Definition at line 153 of file ExtendedMath.cc.

References exp(), and gamma().

Referenced by chi_squared_prob().

154 {
155 
156  Mdouble prefactor = pow(2, k / 2.0) * gamma(k / 2.0);
157  Mdouble mainfactor = pow(x, k / 2.0 - 1) * exp(x / -2.0);
158 
159  return mainfactor / prefactor;
160 
161 }
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:78
double Mdouble
Mdouble gamma(Mdouble gamma_in)
This is the gamma function returns the true value for the half integer value.
Mdouble mathsFunc::chi_squared_prob ( const Mdouble  x_max,
const unsigned int  k 
)

This is the function which actually gives the probability back using a chi squared test.

This calulates the probabity based on a chi squared test First we calculated the cummelative chi_squared function.

This is the function which actually gives the probability back It is calculated by calling the normal chi_squated function and using the trapezoidal rule. The final results is 1-the cummulative chi_squared function

Definition at line 169 of file ExtendedMath.cc.

References chi_squared().

Referenced by RNG::test().

170 {
171 
172 //The current value was picked by tried were it stopped effect the 4 d.p.
173  const int num_steps_per_unit = 100;
174  Mdouble sum = 0;
175  Mdouble x = 0;
176  long int num_steps = static_cast<int>(num_steps_per_unit * x_max);
177 //Use trapezional rule, but ignoring the ends
178  for (int i = 0; i < num_steps; i++)
179  {
180  x = x_max / num_steps * (i + 0.5);
181  sum = sum + chi_squared(x, k);
182  }
183  return 1.0 - sum * x_max / num_steps;
184 
185 }
double Mdouble
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.
template<typename T >
T mathsFunc::cubic ( val)

calculates the cube of a number

Definition at line 99 of file ExtendedMath.h.

Referenced by ChuteBottom::setupInitialConditions().

100  {
101  return val * val * val;
102  }
Mdouble mathsFunc::exp ( Mdouble  Exponent)
template<typename T >
constexpr T mathsFunc::factorial ( const T  t)

factorial function

Definition at line 124 of file ExtendedMath.h.

Referenced by HGridOptimiser::histNumberParticlesPerCell().

125  {
126  return (t == 0) ? 1 : t * factorial(t - 1);
127  }
constexpr T factorial(const T t)
factorial function
Definition: ExtendedMath.h:124
Mdouble mathsFunc::gamma ( Mdouble  gamma_in)

This is the gamma function returns the true value for the half integer value.

This is the gamma function, gives 'exact' answers for the half integer values This is done using the recussion relation and the known values for 1 and 0.5 Note, return NaN for non-half integer values.

Definition at line 130 of file ExtendedMath.cc.

References constants::sqrt_pi.

Referenced by chi_squared(), and HopperInsertionBoundary::generateParticle().

131 {
132  const Mdouble ep = 1e-5;
133 
134  if (gamma_in > 1.0 + ep)
135  {
136  return ((gamma_in - 1) * gamma(gamma_in - 1));
137  }
138  else
139  {
140 
141  if ((gamma_in - ep < 1.0) && (gamma_in + ep > 1.0))
142  return 1.0;
143  else if ((gamma_in - ep < 0.5) && (gamma_in + ep > 0.5))
144  return constants::sqrt_pi;
145  else
146  return std::numeric_limits<Mdouble>::quiet_NaN();
147  }
148 } //end func gamma
const Mdouble sqrt_pi
Definition: ExtendedMath.h:43
double Mdouble
Mdouble gamma(Mdouble gamma_in)
This is the gamma function returns the true value for the half integer value.
Mdouble mathsFunc::goldenSectionSearch ( Mdouble(*)(const Mdouble function,
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.

Parameters
[in]functionA function pointer to the function of which you want to calculate the location of its minimum.
[in]minThe minimum location
[in]curThe current location
[in]maxThe maximum location
[in]endConditionThe algorithm terminates when abs(max - min) < endCondition
[in]curValThe value of the function at the current location (on default this value is calculated internally)

Definition at line 187 of file ExtendedMath.cc.

188 {
189  if (std::abs(max - min) < endCondition)
190  {
191  return 0.5 * (min + max);
192  }
193  std::cout << "Min=" << min << " Max=" << max << " diff=" << max - min << std::endl;
194  Mdouble resphi = 2 - 0.5 * (1 + std::sqrt(5));
195  Mdouble x;
196  if (max - cur > cur - min)
197  {
198  x = cur + resphi * (max - cur);
199  }
200  else
201  {
202  x = cur - resphi * (cur - min);
203  }
204  if(std::isnan(curVal))
205  curVal = function(cur);
206  Mdouble xVal = function(x);
207  if (xVal < curVal)
208  {
209  if (max - cur > cur - min)
210  {
211  return goldenSectionSearch(function, cur, x, max, endCondition, xVal);
212  }
213  else
214  {
215  return goldenSectionSearch(function, min, x, cur, endCondition, xVal);
216  }
217  }
218  else
219  {
220  if (max - cur > cur - min)
221  {
222  return goldenSectionSearch(function, min, cur, x, endCondition, curVal);
223  }
224  else
225  {
226  return goldenSectionSearch(function, x, cur, max, endCondition, curVal);
227  }
228  }
229 }
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...
double Mdouble
Mdouble mathsFunc::I0 ( Mdouble  x)

Definition at line 341 of file ExtendedMath.cc.

References exp(), and I0_exp().

342 {
343  if (x < 0)
344  x = -x;
345  return exp(x) * I0_exp(x);
346 }
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:78
Mdouble I0_exp(Mdouble x)
Mdouble mathsFunc::I0_exp ( Mdouble  x)

Definition at line 261 of file ExtendedMath.cc.

References A, and chebyshev().

Referenced by I0().

262 {
263  // Cooefficients for [0..8]
264  const Mdouble A[] =
265  {
266  -4.415341646479339379501E-18,
267  3.330794518822238097831E-17,
268  -2.431279846547954693591E-16,
269  1.715391285555133030611E-15,
270  -1.168533287799345168081E-14,
271  7.676185498604935616881E-14,
272  -4.856446783111929460901E-13,
273  2.955052663129639834611E-12,
274  -1.726826291441555707231E-11,
275  9.675809035373236912241E-11,
276  -5.189795601635262906661E-10,
277  2.659823724682386650351E-9,
278  -1.300025009986248042121E-8,
279  6.046995022541918949321E-8,
280  -2.670793853940611733911E-7,
281  1.117387539120103718151E-6,
282  -4.416738358458750563591E-6,
283  1.644844807072889708931E-5,
284  -5.754195010082103703981E-5,
285  1.885028850958416557291E-4,
286  -5.763755745385823658851E-4,
287  1.639475616941335798421E-3,
288  -4.324309995050575944301E-3,
289  1.054646039459499831831E-2,
290  -2.373741480589946881561E-2,
291  4.930528423967070848781E-2,
292  -9.490109704804764442101E-2,
293  1.716209015222087753491E-1,
294  -3.046826723431983986831E-1,
295  6.767952744094760849951E-1
296  };
297 
298  // Cooefficients for [8..infinity]
299  const Mdouble B[] =
300  {
301  -7.233180487874753954561E-18,
302  -4.830504485944182071261E-18,
303  4.465621420296759999011E-17,
304  3.461222867697461093101E-17,
305  -2.827623980516583484941E-16,
306  -3.425485619677219134621E-16,
307  1.772560133056526383601E-15,
308  3.811680669352622420751E-15,
309  -9.554846698828307648701E-15,
310  -4.150569347287222086631E-14,
311  1.540086217521409826911E-14,
312  3.852778382742142701141E-13,
313  7.180124451383666233671E-13,
314  -1.794178531506806117781E-12,
315  -1.321581184044771311881E-11,
316  -3.149916527963241364541E-11,
317  1.188914710784643834241E-11,
318  4.940602388224969589101E-10,
319  3.396232025708386345151E-9,
320  2.266668990498178064591E-8,
321  2.048918589469063741831E-7,
322  2.891370520834756482971E-6,
323  6.889758346916823984261E-5,
324  3.369116478255694089901E-3,
325  8.044904110141088316081E-1
326  };
327 
328  if (x < 0)
329  x = -x;
330 
331  if (x <= 8.0)
332  {
333  Mdouble y = (x / 2.0) - 2.0;
334  return (chebyshev(y, A, 30));
335  }
336 
337  return (chebyshev(32.0 / x - 2.0, B, 25) / sqrt(x));
338 
339 }
double Mdouble
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.
bool mathsFunc::isEqual ( Mdouble  v1,
Mdouble  v2,
Mdouble  absError 
)

Compares the difference of two Mdouble with an absolute error, useful in UnitTests.

Parameters
[in]v1The first Mdouble
[in]v2The second Mdouble
[in]absErrorThe allowed maximum absolute error
Returns
True if the two Mdouble are equal

Definition at line 231 of file ExtendedMath.cc.

Referenced by helpers::check(), ChuteInsertionBoundary::generateParticle(), isEqual(), and MercuryBase::setHGridCellOverSizeRatio().

232 {
233  return std::abs(v1 - v2) <= absError;
234 }
bool mathsFunc::isEqual ( Vec3D  v1,
Vec3D  v2,
Mdouble  absError 
)

Compares the difference of two Vec3D with an absolute error, useful in UnitTests.

Parameters
[in]v1The first Vec3D
[in]v2The second Vec3D
[in]absErrorThe allowed maximum absolute error
Returns
true if the two Vec3D are equal

Definition at line 237 of file ExtendedMath.cc.

References isEqual(), Vec3D::X, Vec3D::Y, and Vec3D::Z.

238 {
239  return isEqual(v1.X,v2.X,absError)&&isEqual(v1.Y,v2.Y,absError)&&isEqual(v1.Z,v2.Z,absError);
240 }
Mdouble X
the vector components
Definition: Vector.h:52
bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError)
Compares the difference of two Mdouble with an absolute error, useful in UnitTests.
Mdouble Y
Definition: Vector.h:52
Mdouble Z
Definition: Vector.h:52
Mdouble mathsFunc::log ( Mdouble  Power)
Todo:
check if this function works

Definition at line 97 of file ExtendedMath.cc.

References A, exp(), and R.

Referenced by CircularPeriodicBoundary::checkBoundaryAfterParticleMoved(), helpers::computeCollisionTimeFromDispAndRestitutionCoefficientAndEffectiveMass(), helpers::computeCollisionTimeFromKAndRestitutionCoefficientAndEffectiveMass(), helpers::computeDispFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(), helpers::computeDispFromKAndRestitutionCoefficientAndEffectiveMass(), helpers::computeDisptFromCollisionTimeAndRestitutionCoefficientAndTangentialRestitutionCoefficientAndEffectiveMass(), helpers::computeKFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(), helpers::computeKFromDispAndRestitutionCoefficientAndEffectiveMass(), CircularPeriodicBoundary::createPeriodicParticles(), SlidingFrictionSpecies::setCollisionTimeAndNormalAndTangentialRestitutionCoefficient(), LinearViscoelasticNormalSpecies::setCollisionTimeAndRestitutionCoefficient(), HertzianViscoelasticNormalSpecies::setElasticModulusAndRestitutionCoefficient(), LinearViscoelasticNormalSpecies::setStiffnessAndRestitutionCoefficient(), LinearPlasticViscoelasticNormalSpecies::setStiffnessAndRestitutionCoefficient(), and SinterNormalSpecies::setStiffnessAndRestitutionCoefficient().

98 {
99  Mdouble N, P, L, R, A, E;
100  E = 2.71828182845905;
101  P = Power;
102  N = 0.0;
103 
104  // This speeds up the convergence by calculating the integral
105  while(P >= E)
106  {
107  P /= E;
108  N++;
109  }
110  N += (P / E);
111  P = Power;
112  do
113  {
114  A = N;
115  L = (P / (exp(N - 1.0)));
116  R = ((N - 1.0) * E);
117  N = ((L + R) / E);
118  } while (N < A);
119  //} while (N != A);
120 
121  return N;
122 }
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:78
double Mdouble
template<typename T >
int mathsFunc::sign ( val)

This is a sign function, it returns -1 for negative numbers, 1 for positive numbers and 0 for 0.

Definition at line 83 of file ExtendedMath.h.

Referenced by Screw::getDistanceAndNormal().

84  {
85  return (T(0) < val) - (val < T(0));
86  }
Mdouble mathsFunc::sin ( Mdouble  x)

Definition at line 42 of file ExtendedMath.cc.

References constants::pi.

Referenced by ChuteWithHopper::addHopper(), HopperInsertionBoundary::generateParticle(), Quarternion::getAxisAndAngle(), Screw::getDistanceAndNormal(), Coil::getDistanceAndNormal(), InfiniteWallWithHole::getDistanceAndNormal(), Vec3D::getFromCylindricalCoordinates(), ChuteWithHopper::getMaximumVelocityInducedByGravity(), Screw::getTriangulation(), Quarternion::integrate(), helpers::objectivenessTest(), Quarternion::Quarternion(), CircularPeriodicBoundary::rotateParticle(), Chute::setChuteAngleAndMagnitudeOfGravity(), tan(), and AxisymmetricIntersectionOfWalls::writeVTK().

42  {
43  Mdouble N = floor(x/(2.0*constants::pi)+0.5);
44  x -= N*2.0*constants::pi;
45  Mdouble Sum = 0;
46  Mdouble Power = x;
47  Mdouble Sign = 1;
48  const Mdouble x2 = x * x;
49  Mdouble Fact = 1.0;
50  for (unsigned int i = 1; i < 25; i += 2) {
51  Sum += Sign * Power / Fact;
52  Power *= x2;
53  Fact *= (i + 1) * (i + 2);
54  Sign *= -1.0;
55  }
56  return Sum;
57 }
double Mdouble
const Mdouble pi
Definition: ExtendedMath.h:42
template<typename T >
T mathsFunc::square ( val)

squares a number

Definition at line 91 of file ExtendedMath.h.

Referenced by DPMBase::areInContact(), DPMBase::checkParticleForInteractionLocal(), helpers::computeCollisionTimeFromKAndDispAndEffectiveMass(), helpers::computeCollisionTimeFromKAndRestitutionCoefficientAndEffectiveMass(), helpers::computeDispFromKAndCollisionTimeAndEffectiveMass(), helpers::computeDispFromKAndRestitutionCoefficientAndEffectiveMass(), helpers::computeDisptFromCollisionTimeAndRestitutionCoefficientAndTangentialRestitutionCoefficientAndEffectiveMass(), SlidingFrictionInteraction::computeFrictionForce(), FrictionInteraction::computeFrictionForce(), MindlinRollingTorsionInteraction::computeFrictionForce(), helpers::computeKFromCollisionTimeAndDispAndEffectiveMass(), helpers::computeKFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(), helpers::computeKFromDispAndRestitutionCoefficientAndEffectiveMass(), ParticleSpecies::computeMass(), SinterInteraction::computeNormalForce(), helpers::computeRestitutionCoefficientFromKAndCollisionTimeAndEffectiveMass(), helpers::computeRestitutionCoefficientFromKAndDispAndEffectiveMass(), HertzianSinterInteraction::computeSinterForce(), LinearPlasticViscoelasticNormalSpecies::computeTimeStep(), SinterNormalSpecies::computeTimeStep(), HertzianViscoelasticNormalSpecies::getCollisionTime(), LinearPlasticViscoelasticNormalSpecies::getCollisionTime(), LinearViscoelasticNormalSpecies::getCollisionTime(), SinterNormalSpecies::getCollisionTime(), StatisticsVector< T >::getCutoff2(), Screw::getDistanceAndNormal(), ChargedBondedInteraction::getElasticEnergy(), HertzianSinterInteraction::getElasticEnergy(), SinterInteraction::getElasticEnergy(), LinearViscoelasticInteraction::getElasticEnergy(), HertzianViscoelasticInteraction::getElasticEnergy(), LinearPlasticViscoelasticInteraction::getElasticEnergy(), IrreversibleAdhesiveInteraction::getElasticEnergy(), HertzianViscoelasticInteraction::getElasticEnergyAtEquilibrium(), helpers::getMaximumVelocity(), StatisticsVector< T >::setCGWidth(), SlidingFrictionSpecies::setCollisionTimeAndNormalAndTangentialRestitutionCoefficient(), SlidingFrictionSpecies::setCollisionTimeAndNormalAndTangentialRestitutionCoefficientNoDispt(), LinearViscoelasticNormalSpecies::setCollisionTimeAndRestitutionCoefficient(), LinearViscoelasticNormalSpecies::setStiffnessAndRestitutionCoefficient(), LinearPlasticViscoelasticNormalSpecies::setStiffnessAndRestitutionCoefficient(), SinterNormalSpecies::setStiffnessAndRestitutionCoefficient(), MatrixSymmetric3D::square(), and Matrix3D::square().

92  {
93  return val * val;
94  }
template<typename T >
T mathsFunc::tan ( x)
Todo:
should be properly computed

Definition at line 146 of file ExtendedMath.h.

References cos(), and sin().

Referenced by ChuteWithHopper::addHopper(), and ChuteWithHopper::setHopper().

146  {
147  return sin(x)/cos(x);
148  }
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:60
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:42