GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSMathAlgorithms.hh
Go to the documentation of this file.
1 #ifndef GUARD_GGEMS_MATHS_GGEMSMATHALGORITHMS_HH
2 #define GUARD_GGEMS_MATHS_GGEMSMATHALGORITHMS_HH
3 
4 // ************************************************************************
5 // * This file is part of GGEMS. *
6 // * *
7 // * GGEMS is free software: you can redistribute it and/or modify *
8 // * it under the terms of the GNU General Public License as published by *
9 // * the Free Software Foundation, either version 3 of the License, or *
10 // * (at your option) any later version. *
11 // * *
12 // * GGEMS is distributed in the hope that it will be useful, *
13 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 // * GNU General Public License for more details. *
16 // * *
17 // * You should have received a copy of the GNU General Public License *
18 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
19 // * *
20 // ************************************************************************
21 
35 
46 #ifdef __OPENCL_C_VERSION__
47 inline GGint BinarySearchLeft(GGfloat const key, global GGfloat const* array, GGint const size, GGint const offset, GGint min)
48 #else
49 inline GGint BinarySearchLeft(GGfloat const key, GGfloat const* array, GGint const size, GGint const offset, GGint min)
50 #endif
51 {
52  GGint max = size - 1, mid = 0; // Max element, and median element
53  GGint min_check = min; // Min element
54 
55  while (min < max) {
56  // Computing median index
57  mid = (min + max) >> 1;
58  if (key == array[mid + offset]) {
59  return mid;
60  }
61  else if (key > array[mid + offset]) {
62  min = mid + 1;
63  }
64  else {
65  max = mid;
66  }
67  }
68 
69  // Checking the min elements
70  if (min > min_check) min--;
71 
72  // Return the min element
73  return min;
74 }
75 
86 inline GGfloat LinearInterpolation(GGfloat const xa, GGfloat const ya, GGfloat const xb, GGfloat const yb, GGfloat const x)
87 {
88  // Taylor young 1st order
89  // if ( xa > x ) return ya;
90  // if ( xb < x ) return yb;
91  if (xa > xb) return yb;
92  if (xa >= x) return ya;
93  if (xb <= x) return yb;
94 
95  return ya + (x - xa)*(yb - ya)/(xb - xa);
96 }
97 
109 {
110  if (x < x0) return y0;
111  if (x > x1) return y1;
112 
113  x0 = 1.0f / x0;
114 
115  #ifdef __OPENCL_C_VERSION__
116  return pow(10.0f, log10(y0) + log10(y1/y0) * (log10(x*x0) / log10(x1*x0)));
117  #else
118  return std::pow(10.0f, std::log10(y0) + std::log10(y1/y0) * (std::log10(x*x0) / std::log10(x1*x0)));
119  #endif
120 }
121 
122 #endif // GUARD_GGEMS_MATHS_GGEMSMATHALGORITHMS_HH
LinearInterpolation
GGfloat LinearInterpolation(GGfloat const xa, GGfloat const ya, GGfloat const xb, GGfloat const yb, GGfloat const x)
interpolate the x value between point A and B
Definition: GGEMSMathAlgorithms.hh:86
BinarySearchLeft
GGint BinarySearchLeft(GGfloat const key, GGfloat const *array, GGint const size, GGint const offset, GGint min)
Find the index of the key value in the p_array buffer.
Definition: GGEMSMathAlgorithms.hh:49
GGint
#define GGint
Definition: GGEMSTypes.hh:224
LogLogInterpolation
GGfloat LogLogInterpolation(GGfloat x, GGfloat x0, GGfloat y0, GGfloat x1, GGfloat y1)
log log interpolation of the x value between point (x0,y0) and (x1,y1)
Definition: GGEMSMathAlgorithms.hh:108
GGEMSSystemOfUnits.hh
Namespace storing all the usefull physical units.
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273