CMS 3D CMS Logo

VVIObjF.cc
Go to the documentation of this file.
1 //
2 // VVIObjF.cc Version 2.0
3 //
4 // Port of CERNLIB G116 Functions vviden/vvidis
5 //
6 // Created by Morris Swartz on 1/14/2010.
7 // 2010 __TheJohnsHopkinsUniversity__.
8 //
9 // V1.1 - make dzero call both fcns with a switch
10 // V1.2 - remove inappriate initializers and add methods to return non-zero/normalized region
11 // V2.0 - restructuring and speed improvements by V. Innocente
12 // V3.0 - further simplification and speedup by Tamas Vami
13 //
14 
15 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
16 // put CMSSW location of SimpleHelix.h here
18 #else
19 #include "VVIObjF.h"
20 #endif
21 
22 #include <cmath>
23 #include <algorithm>
24 #include "vdt/vdtMath.h"
25 
26 namespace VVIObjFDetails {
27  void sincosint(float x, float& sint, float& cint);
28  float expint(float x);
29 
30  template <typename F>
31  int dzero(float a, float b, float& x0, float& rv, float eps, int mxf, F func);
32 } // namespace VVIObjFDetails
33 
34 // ***************************************************************************************************************************************
40 // ***************************************************************************************************************************************
41 
42 // WARNING: if you change this, dont forget to change VVIObjF::VVIObjF(float kappa) too
43 VVIObjF::VVIObjF(float kappa, float beta2, int mode) : mode_(mode) {
44  const float xp[9] = {9.29, 2.47, .89, .36, .15, .07, .03, .02, 0.0};
45  const float xq[7] = {.012, .03, .08, .26, .87, 3.83, 11.0};
46  float h_[7];
47  float q, u, x, c1, c2, c3, c4, d1, h4, h5, h6, q2, x1, d, ll, ul, xf1, xf2, rv;
48  int lp, lq, k, l, n;
49 
50  // Make sure that the inputs are reasonable
51 
52  if (kappa < 0.01f)
53  kappa = 0.01f;
54  if (kappa > 10.f)
55  kappa = 10.f;
56  if (beta2 < 0.f)
57  beta2 = 0.f;
58  if (beta2 > 1.f)
59  beta2 = 1.f;
60 
61  float invKappa = 1.f / kappa;
62  h_[4] = 1.f - beta2 * 0.42278433999999998f + (7.6f * invKappa);
63  h_[5] = beta2;
64  h_[6] = 1.f - beta2;
65  h4 = -(7.6f * invKappa) - (beta2 * .57721566f + 1.f);
66  h5 = vdt::fast_logf(kappa);
67  h6 = invKappa;
68  t0_ = (h4 - h_[4] * h5 - (h_[4] + beta2) * (vdt::fast_logf(h_[4]) + VVIObjFDetails::expint(h_[4])) +
69  vdt::fast_expf(-h_[4])) /
70  h_[4];
71 
72  // Set up limits for the root search
73 
74  for (lp = 0; lp < 9; ++lp) {
75  if (kappa >= xp[lp])
76  break;
77  }
78  ll = -float(lp) - 1.5f;
79  for (lq = 0; lq < 7; ++lq) {
80  if (kappa <= xq[lq])
81  break;
82  }
83  ul = lq - 6.5f;
84  auto f2 = [h_](float x) {
85  return h_[4] - x + h_[5] * (vdt::fast_logf(std::abs(x)) + VVIObjFDetails::expint(x)) - h_[6] * vdt::fast_expf(-x);
86  };
87  VVIObjFDetails::dzero(ll, ul, u, rv, 1.e-3f, 100, f2);
88  q = 1. / u;
89  t1_ = h4 * q - h5 - (beta2 * q + 1.f) * (vdt::fast_logf((fabs(u))) + VVIObjFDetails::expint(u)) +
90  vdt::fast_expf(-u) * q;
91  t_ = t1_ - t0_;
92  omega_ = 6.2831853000000004f / t_;
93  h_[0] = kappa * (beta2 * .57721566f + 2.f) + 9.9166128600000008f;
94  if (kappa >= .07) {
95  h_[0] += 6.90775527f;
96  }
97  h_[1] = beta2 * kappa;
98  h_[2] = h6 * omega_;
99  h_[3] = omega_ * 1.5707963250000001f;
100  auto f1 = [h_](float x) { return h_[0] + h_[1] * vdt::fast_logf(h_[2] * x) - h_[3] * x; };
101  VVIObjFDetails::dzero(5.f, 155.f, x0_, rv, 1.e-3f, 100, f1);
102  n = x0_ + 1.;
103  d = vdt::fast_expf(kappa * (beta2 * (.57721566f - h5) + 1.f)) * .31830988654751274f;
104  a_[n - 1] = 0.f;
105  if (mode_ == 0) {
106  a_[n - 1] = omega_ * .31830988654751274f;
107  }
108  q = -1.;
109  q2 = 2.;
110  for (k = 1; k < n; ++k) {
111  l = n - k;
112  x = omega_ * k;
113  x1 = h6 * x;
115  c1 = vdt::fast_logf(x) - c1;
116  vdt::fast_sincosf(x1, c3, c4);
117  xf1 = kappa * (beta2 * c1 - c4) - x * c2;
118  xf2 = x * c1 + kappa * (c3 + beta2 * c2) + t0_ * x;
119  float s, c;
120  vdt::fast_sincosf(xf2, s, c);
121  if (mode_ == 0) {
122  d1 = q * d * omega_ * vdt::fast_expf(xf1);
123  a_[l - 1] = d1 * c;
124  b_[l - 1] = -d1 * s;
125  } else {
126  d1 = q * d * vdt::fast_expf(xf1) / k;
127  a_[l - 1] = d1 * s;
128  b_[l - 1] = d1 * c;
129  a_[n - 1] += q2 * a_[l - 1];
130  }
131  q = -q;
132  q2 = -q2;
133  }
134 
135 } // VVIObjF
136 
137 // ***************************************************************************************************************************************
141 // ***************************************************************************************************************************************
142 
143 // WARNING: if you change this, dont forget to change the full constructor too
144 VVIObjF::VVIObjF(float kappa) : mode_(1) {
145  const float xp[9] = {9.29, 2.47, .89, .36, .15, .07, .03, .02, 0.0};
146  const float xq[7] = {.012, .03, .08, .26, .87, 3.83, 11.0};
147  float h_[5];
148  float q, u, x, c1, c2, c3, c4, d1, h4, h5, h6, q2, x1, d, ll, ul, xf1, xf2, rv;
149  int lp, lq, k, l, n;
150 
151  // Make sure that the inputs are reasonable
152 
153  if (kappa < 0.01f)
154  kappa = 0.01f;
155  if (kappa > 10.f)
156  kappa = 10.f;
157 
158  float invKappa = 1.f / kappa;
159  h_[4] = 0.57721566f + (7.6f * invKappa);
160  h4 = -(7.6f * invKappa) - 1.57721566f;
161  h5 = vdt::fast_logf(kappa);
162  h6 = invKappa;
163  t0_ = (h4 - h_[4] * h5 - (h_[4] + 1.f) * (vdt::fast_logf(h_[4]) + VVIObjFDetails::expint(h_[4])) +
164  vdt::fast_expf(-h_[4])) /
165  h_[4];
166 
167  // Set up limits for the root search
168 
169  for (lp = 0; lp < 9; ++lp) {
170  if (kappa >= xp[lp])
171  break;
172  }
173  ll = -float(lp) - 1.5f;
174  for (lq = 0; lq < 7; ++lq) {
175  if (kappa <= xq[lq])
176  break;
177  }
178  ul = lq - 6.5f;
179  auto f2 = [h_](float x) { return h_[4] - x + (vdt::fast_logf(std::abs(x)) + VVIObjFDetails::expint(x)); };
180  VVIObjFDetails::dzero(ll, ul, u, rv, 1.e-3f, 100, f2);
181  q = 1. / u;
182  t1_ = h4 * q - h5 - (q + 1.f) * (vdt::fast_logf((fabs(u))) + VVIObjFDetails::expint(u)) + vdt::fast_expf(-u) * q;
183  t_ = t1_ - t0_;
184  omega_ = 6.2831853000000004f / t_;
185  h_[0] = kappa * 2.57721566f + 9.9166128600000008f;
186  if (kappa >= .07) {
187  h_[0] += 6.90775527f;
188  }
189  h_[1] = kappa;
190  h_[2] = h6 * omega_;
191  h_[3] = omega_ * 1.5707963250000001f;
192  auto f1 = [h_](float x) { return h_[0] + h_[1] * vdt::fast_logf(h_[2] * x) - h_[3] * x; };
193  VVIObjFDetails::dzero(5.f, 155.f, x0_, rv, 1.e-3f, 100, f1);
194  n = x0_ + 1.;
195  d = vdt::fast_expf(kappa * ((0.57721566f - h5) + 1.f)) * .31830988654751274f;
196  a_[n - 1] = 0.f;
197  q = -1.;
198  q2 = 2.;
199  for (k = 1; k < n; ++k) {
200  l = n - k;
201  x = omega_ * k;
202  x1 = h6 * x;
204  c1 = vdt::fast_logf(x) - c1;
205  vdt::fast_sincosf(x1, c3, c4);
206  xf1 = kappa * (c1 - c4) - x * c2;
207  xf2 = x * c1 + kappa * (c3 + c2) + t0_ * x;
208  float s, c;
209  vdt::fast_sincosf(xf2, s, c);
210  d1 = q * d * vdt::fast_expf(xf1) / k;
211  a_[l - 1] = d1 * s;
212  b_[l - 1] = d1 * c;
213  a_[n - 1] += q2 * a_[l - 1];
214  q = -q;
215  q2 = -q2;
216  }
217 
218 } // VVIObjF with kappa only
219 
220 // *************************************************************************************************************************************
224 // *************************************************************************************************************************************
225 
226 float VVIObjF::fcn(float x) const {
227  // Local variables
228 
229  float f, u, y, a0, a1;
230  float a2 = 0.;
231  float b1, b0, b2, cof;
232  int k, n, n1;
233 
234  n = x0_;
235  if (x < t0_) {
236  f = 0.f;
237  } else if (x <= t1_) {
238  y = x - t0_;
239  u = omega_ * y - 3.141592653589793f;
240  float su, cu;
241  vdt::fast_sincosf(u, su, cu);
242  cof = cu * 2.f;
243  a1 = 0.;
244  a0 = a_[0];
245  n1 = n + 1;
246  for (k = 2; k <= n1; ++k) {
247  a2 = a1;
248  a1 = a0;
249  a0 = a_[k - 1] + cof * a1 - a2;
250  }
251  b1 = 0.;
252  b0 = b_[0];
253  for (k = 2; k <= n; ++k) {
254  b2 = b1;
255  b1 = b0;
256  b0 = b_[k - 1] + cof * b1 - b2;
257  }
258  f = (a0 - a2) * .5f + b0 * su;
259  if (mode_ != 0) {
260  f += y / t_;
261  }
262  } else {
263  f = 0.f;
264  if (mode_ != 0) {
265  f = 1.f;
266  }
267  }
268  return f;
269 } // fcn
270 
271 // *************************************************************************************************************************************
275 // *************************************************************************************************************************************
276 
277 void VVIObjF::limits(float& xl, float& xu) const {
278  xl = t0_;
279  xu = t1_;
280  return;
281 } // limits
282 
283 #include "sicif.h"
284 namespace VVIObjFDetails {
285  void sincosint(float x, float& sint, float& cint) { sicif(x, sint, cint); }
286 
287  float expint(float x) {
288  // Initialized data
289 
290  const float zero = 0.;
291  const float q2[7] = {
292  .10340013040487, 3.319092135933, 20.449478501379, 41.280784189142, 32.426421069514, 10.041164382905, 1.};
293  const float p3[6] = {
294  -2.3909964453136, -147.98219500504, -254.3763397689, -119.55761038372, -19.630408535939, -.9999999999036};
295  const float q3[6] = {177.60070940351, 530.68509610812, 462.23027156148, 156.81843364539, 21.630408494238, 1.};
296  const float p4[8] = {-8.6693733995107,
297  -549.14226552109,
298  -4210.0161535707,
299  -249301.39345865,
300  -119623.66934925,
301  -22174462.775885,
302  3892804.213112,
303  -391546073.8091};
304  const float q4[8] = {34.171875,
305  -1607.0892658722,
306  35730.029805851,
307  -483547.43616216,
308  4285596.2461175,
309  -24903337.574054,
310  89192576.757561,
311  -165254299.72521};
312  const float a1[8] = {-2.1808638152072,
313  -21.901023385488,
314  9.3081638566217,
315  25.076281129356,
316  -33.184253199722,
317  60.121799083008,
318  -43.253113287813,
319  1.0044310922808};
320  const float b1[8] = {0.,
321  3.9370770185272,
322  300.89264837292,
323  -6.2504116167188,
324  1003.6743951673,
325  14.325673812194,
326  2736.2411988933,
327  .52746885196291};
328  const float a2[8] = {-3.4833465360285,
329  -18.65454548834,
330  -8.2856199414064,
331  -32.34673303054,
332  17.960168876925,
333  1.7565631546961,
334  -1.9502232128966,
335  .99999429607471};
336  const float b2[8] = {0.,
337  69.500065588743,
338  57.283719383732,
339  25.777638423844,
340  760.76114800773,
341  28.951672792514,
342  -3.4394226689987,
343  1.0008386740264};
344  const float a3[6] = {
345  -27.780928934438, -10.10479081576, -9.1483008216736, -5.0223317461851, -3.0000077799358, 1.0000000000704};
346  const float one = 1.;
347  const float b3[6] = {0., 122.39993926823, 2.7276100778779, -7.1897518395045, -2.9990118065262, 1.999999942826};
348  const float two = 2.;
349  const float three = 3.;
350  const float x0 = .37250741078137;
351  const float xl[6] = {-24., -12., -6., 0., 1., 4.};
352  const float p1[5] = {4.293125234321, 39.894153870321, 292.52518866921, 425.69682638592, -434.98143832952};
353  const float q1[5] = {1., 18.899288395003, 150.95038744251, 568.05252718987, 753.58564359843};
354  const float p2[7] = {.43096783946939,
355  6.9052252278444,
356  23.019255939133,
357  24.378408879132,
358  9.0416155694633,
359  .99997957705159,
360  4.656271079751e-7};
361 
362  // Local variables
363  float v, y, ap, bp, aq, dp, bq, dq;
364 
365  if (x <= xl[0]) {
366  ap = a3[0] - x;
367  for (int i__ = 2; i__ <= 5; ++i__) {
368  ap = a3[i__ - 1] - x + b3[i__ - 1] / ap;
369  }
370  y = vdt::fast_expf(-x) / x * (one - (a3[5] + b3[5] / ap) / x);
371  } else if (x <= xl[1]) {
372  ap = a2[0] - x;
373  for (int i__ = 2; i__ <= 7; ++i__) {
374  ap = a2[i__ - 1] - x + b2[i__ - 1] / ap;
375  }
376  y = vdt::fast_expf(-x) / x * (a2[7] + b2[7] / ap);
377  } else if (x <= xl[2]) {
378  ap = a1[0] - x;
379  for (int i__ = 2; i__ <= 7; ++i__) {
380  ap = a1[i__ - 1] - x + b1[i__ - 1] / ap;
381  }
382  y = vdt::fast_expf(-x) / x * (a1[7] + b1[7] / ap);
383  } else if (x < xl[3]) {
384  v = -two * (x / three + one);
385  bp = zero;
386  dp = p4[0];
387  for (int i__ = 2; i__ <= 8; ++i__) {
388  ap = bp;
389  bp = dp;
390  dp = p4[i__ - 1] - ap + v * bp;
391  }
392  bq = zero;
393  dq = q4[0];
394  for (int i__ = 2; i__ <= 8; ++i__) {
395  aq = bq;
396  bq = dq;
397  dq = q4[i__ - 1] - aq + v * bq;
398  }
399  y = -vdt::fast_logf(-x / x0) + (x + x0) * (dp - ap) / (dq - aq);
400  } else if (x == xl[3]) {
401  return zero;
402  } else if (x < xl[4]) {
403  ap = p1[0];
404  aq = q1[0];
405  for (int i__ = 2; i__ <= 5; ++i__) {
406  ap = p1[i__ - 1] + x * ap;
407  aq = q1[i__ - 1] + x * aq;
408  }
409  y = -vdt::fast_logf(x) + ap / aq;
410  } else if (x <= xl[5]) {
411  y = one / x;
412  ap = p2[0];
413  aq = q2[0];
414  for (int i__ = 2; i__ <= 7; ++i__) {
415  ap = p2[i__ - 1] + y * ap;
416  aq = q2[i__ - 1] + y * aq;
417  }
418  y = vdt::fast_expf(-x) * ap / aq;
419  } else {
420  y = one / x;
421  ap = p3[0];
422  aq = q3[0];
423  for (int i__ = 2; i__ <= 6; ++i__) {
424  ap = p3[i__ - 1] + y * ap;
425  aq = q3[i__ - 1] + y * aq;
426  }
427  y = vdt::fast_expf(-x) * y * (one + y * ap / aq);
428  }
429  return y;
430  } // expint
431 
432  template <typename F>
433  int dzero(float a, float b, float& x0, float& rv, float eps, int mxf, F func) {
434  /* System generated locals */
435  float d__1, d__2, d__3, d__4;
436 
437  // Local variables
438  float f1, f2, f3, u1, u2, x1, x2, u3, u4, x3, ca, cb, cc, fa, fb, ee, ff;
439  int mc;
440  float xa, xb, fx, xx, su4;
441 
442  xa = std::min(a, b);
443  xb = std::max(a, b);
444  fa = func(xa);
445  fb = func(xb);
446  if (fa * fb > 0.f) {
447  rv = (xb - xa) * -2.f;
448  x0 = 0.f;
449  return 1;
450  }
451  mc = 0;
452  L1:
453  x0 = (xa + xb) * 0.5f;
454  rv = x0 - xa;
455  ee = eps * (std::abs(x0) + 1.f);
456  if (rv <= ee) {
457  rv = ee;
458  ff = func(x0);
459  return 0;
460  }
461  f1 = fa;
462  x1 = xa;
463  f2 = fb;
464  x2 = xb;
465  L2:
466  fx = func(x0);
467  ++mc;
468  if (mc > mxf) {
469  rv = (d__1 = xb - xa, fabs(d__1)) * -0.5f;
470  x0 = 0.;
471  return 0;
472  }
473  if (fx * fa > 0.f) {
474  xa = x0;
475  fa = fx;
476  } else {
477  xb = x0;
478  fb = fx;
479  }
480  L3:
481  u1 = f1 - f2;
482  u2 = x1 - x2;
483  u3 = f2 - fx;
484  u4 = x2 - x0;
485  if (u2 == 0.f || u4 == 0.f) {
486  goto L1;
487  }
488  f3 = fx;
489  x3 = x0;
490  u1 /= u2;
491  u2 = u3 / u4;
492  ca = u1 - u2;
493  cb = (x1 + x2) * u2 - (x2 + x0) * u1;
494  cc = (x1 - x0) * f1 - x1 * (ca * x1 + cb);
495  if (ca == 0.f) {
496  if (cb == 0.f) {
497  goto L1;
498  }
499  x0 = -cc / cb;
500  } else {
501  u3 = cb / (ca * 2.f);
502  u4 = u3 * u3 - cc / ca;
503  if (u4 < 0.f) {
504  goto L1;
505  }
506  su4 = std::abs(u4);
507  if (x0 + u3 < 0.f) {
508  su4 = -su4;
509  }
510  x0 = -u3 + su4;
511  }
512  if (x0 < xa || x0 > xb) {
513  goto L1;
514  }
515  // Computing MIN
516  d__3 = (d__1 = x0 - x3, std::abs(d__1));
517  d__4 = (d__2 = x0 - x2, std::abs(d__2));
518  rv = std::min(d__3, d__4);
519  ee = eps * (std::abs(x0) + 1);
520  if (rv > ee) {
521  f1 = f2;
522  x1 = x2;
523  f2 = f3;
524  x2 = x3;
525  goto L2;
526  }
527  fx = func(x0);
528  if (fx == 0.f) {
529  rv = ee;
530  ff = func(x0);
531  return 0;
532  }
533  if (fx * fa < 0.f) {
534  xx = x0 - ee;
535  if (xx <= xa) {
536  rv = ee;
537  ff = func(x0);
538  return 0;
539  }
540  ff = func(xx);
541  fb = ff;
542  xb = xx;
543  } else {
544  xx = x0 + ee;
545  if (xx >= xb) {
546  rv = ee;
547  ff = func(x0);
548  return 0;
549  }
550  ff = func(xx);
551  fa = ff;
552  xa = xx;
553  }
554  if (fx * ff > 0.f) {
555  mc += 2;
556  if (mc > mxf) {
557  rv = (d__1 = xb - xa, std::abs(d__1)) * -0.5f;
558  x0 = 0.f;
559  return 0;
560  }
561  f1 = f3;
562  x1 = x3;
563  f2 = fx;
564  x2 = x0;
565  x0 = xx;
566  fx = ff;
567  goto L3;
568  }
569  /* L4: */
570  rv = ee;
571  ff = func(x0);
572  return 0;
573  } // dzero
574 
575 } // namespace VVIObjFDetails
float omega_
Definition: VVIObjF.h:40
float t1_
Definition: VVIObjF.h:38
const int mode_
returns the limits on the non-zero (mode=0) or normalized region (mode=1)
Definition: VVIObjF.h:36
void sincosint(float x, float &sint, float &cint)
Definition: VVIObjF.cc:285
float x0_
Definition: VVIObjF.h:41
void limits(float &xl, float &xu) const
density (mode=0) or distribution (mode=1) function
Definition: VVIObjF.cc:277
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
int dzero(float a, float b, float &x0, float &rv, float eps, int mxf, F func)
Private version of the exponential integral.
Definition: VVIObjF.cc:433
float t0_
Definition: VVIObjF.h:37
d
Definition: ztail.py:151
float expint(float x)
Private version of the cosine and sine integral.
Definition: VVIObjF.cc:287
float b_[155]
Definition: VVIObjF.h:43
float fcn(float x) const
Definition: VVIObjF.cc:226
static constexpr float a0
double b
Definition: hdecay.h:118
double a
Definition: hdecay.h:119
VVIObjF(float kappa, float beta2, int mode)
Constructor.
Definition: VVIObjF.cc:43
float fast_expf(float x)
float x
static constexpr float b2
static constexpr float b0
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
static constexpr float d1
float fast_logf(float x)
float t_
Definition: VVIObjF.h:39
static constexpr float b1
float a_[155]
Definition: VVIObjF.h:42
int sicif(float xx, float &si, float &ci)
Definition: sicif.h:219