Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / opencl / opinlinefun_statistical.cxx
blob71cfd3df01b73892c039bea821255c877b53bcc9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef SC_OPENCL_OPINLINFUN_statistical
11 #define SC_OPENCL_OPINLINFUN_statistical
12 std::string MinDecl = "#define Min 2.22507e-308\n";
13 std::string F_PIDecl="#define F_PI 3.1415926\n";
14 std::string fBigInvDecl ="#define fBigInv 2.22045e-016\n";
15 std::string fMachEpsDecl ="#define fMachEps 2.22045e-016\n";
16 std::string fLogDblMaxDecl ="#define fLogDblMax log(1.79769e+308)\n";
17 std::string fHalfMachEpsDecl ="#define fHalfMachEps 0.5*2.22045e-016\n";
18 std::string fMaxGammaArgumentDecl=
19 "#define fMaxGammaArgument 171.624376956302\n";
20 std::string GetValueDecl=
21 "double GetValue( double x,double fp,double fDF );\n";
22 std::string GetValue=
23 "double GetValue( double x,double fp,double fDF )\n"
24 "{\n"
25 " return fp - 2 * GetTDist(x, fDF);\n"
26 "}\n";
27 std::string GetGammaSeriesDecl=
28 "double GetGammaSeries( double fA, double fX );\n";
29 std::string GetGammaSeries =
30 "double GetGammaSeries( double fA, double fX )\n"
31 "{\n"
32 " double fDenomfactor = fA;\n"
33 " double fSummand = 1.0/fA;\n"
34 " double fSum = fSummand;\n"
35 " int nCount=1;\n"
36 " do\n"
37 " {\n"
38 " fDenomfactor = fDenomfactor + 1.0;\n"
39 " fSummand = fSummand * fX/fDenomfactor;\n"
40 " fSum = fSum + fSummand;\n"
41 " nCount = nCount+1;\n"
42 " } while ( fSummand/fSum > fHalfMachEps && nCount<=10000);\n"
43 " if (nCount>10000)\n"
44 " {\n"
45 " }\n"
46 " return fSum;\n"
47 "}\n";
48 std::string GetGammaContFractionDecl = "double GetGammaContFraction( double "
49 "fA, double fX );\n";
50 std::string GetGammaContFraction =
51 "double GetGammaContFraction( double fA, double fX )\n"
52 "{\n"
53 " double fBig = 1.0/fBigInv;\n"
54 " double fCount = 0.0;\n"
55 " double fNum = 0.0;\n"
56 " double fY = 1.0 - fA;\n"
57 " double fDenom = fX + 2.0-fA;\n"
58 " double fPk = 0.0;\n"
59 " double fPkm1 = fX + 1.0;\n"
60 " double fPkm2 = 1.0;\n"
61 " double fQk = 1.0;\n"
62 " double fQkm1 = fDenom * fX;\n"
63 " double fQkm2 = fX;\n"
64 " double fApprox = fPkm1/fQkm1;\n"
65 " bool bFinished = false;\n"
66 " double fR = 0.0;\n"
67 " do\n"
68 " {\n"
69 " fCount = fCount +1.0;\n"
70 " fY = fY+ 1.0;\n"
71 " fNum = fY * fCount;\n"
72 " fDenom = fDenom +2.0;\n"
73 " fPk = fPkm1 * fDenom - fPkm2 * fNum;\n"
74 " fQk = fQkm1 * fDenom - fQkm2 * fNum;\n"
75 " if (fQk != 0.0)\n"
76 " {\n"
77 " fR = fPk/fQk;\n"
78 " bFinished = (fabs( (fApprox - fR)/fR ) <= fHalfMachEps);\n"
79 " fApprox = fR;\n"
80 " }\n"
81 " fPkm2 = fPkm1;\n"
82 " fPkm1 = fPk;\n"
83 " fQkm2 = fQkm1;\n"
84 " fQkm1 = fQk;\n"
85 " if (fabs(fPk) > fBig)\n"
86 " {\n"
87 " fPkm2 = fPkm2 * fBigInv;\n"
88 " fPkm1 = fPkm1 * fBigInv;\n"
89 " fQkm2 = fQkm2 * fBigInv;\n"
90 " fQkm1 = fQkm1 * fBigInv;\n"
91 " }\n"
92 " } while (!bFinished && fCount<10000);\n"
93 " if (!bFinished)\n"
94 " {\n"
95 " }\n"
96 " return fApprox;\n"
97 "}\n";
98 std::string GetLowRegIGammaDecl = "double GetLowRegIGamma( double "
99 "fA, double fX );\n";
100 std::string GetLowRegIGamma =
101 "double GetLowRegIGamma( double fA, double fX )\n"
102 "{\n"
103 " double fLnFactor = fA * log(fX) - fX - lgamma(fA);\n"
104 " double fFactor = exp(fLnFactor);\n"
105 " if (fX>fA+1.0) \n"
106 " return 1.0 - fFactor * GetGammaContFraction(fA,fX);\n"
107 " else\n"
108 " return fFactor * GetGammaSeries(fA,fX);\n"
109 "}\n";
110 std::string GetGammaDistDecl = "double GetGammaDist( double fX, double "
111 "fAlpha, double fLambda );\n";
112 std::string GetGammaDist =
113 "double GetGammaDist( double fX, double fAlpha, double fLambda )\n"
114 "{\n"
115 " if (fX <= 0.0)\n"
116 " return 0.0;\n"
117 " else\n"
118 " return GetLowRegIGamma( fAlpha, fX / fLambda);\n"
119 "}\n";
120 std::string GetGammaDistPDFDecl = "double GetGammaDistPDF( double fX"
121 ", double fAlpha, double fLambda );\n";
122 std::string GetGammaDistPDF =
123 "double GetGammaDistPDF( double fX, double fAlpha, double fLambda )\n"
124 "{\n"
125 " if (fX < 0.0)\n"
126 " return 0.0;\n"
127 " else if (fX == 0)\n"
128 " {\n"
129 " if (fAlpha < 1.0)\n"
130 " {\n"
131 " return HUGE_VAL;\n"
132 " }\n"
133 " else if (fAlpha == 1)\n"
134 " {\n"
135 " return (1.0 / fLambda);\n"
136 " }\n"
137 " else\n"
138 " {\n"
139 " return 0.0;\n"
140 " }\n"
141 " }\n"
142 " else\n"
143 " {\n"
144 " double fXr = fX / fLambda;\n"
145 " if (fXr > 1.0)\n"
146 " {\n"
147 " if (log(fXr) * (fAlpha-1.0) < fLogDblMax &&"
148 "fAlpha < fMaxGammaArgument)\n"
149 " {\n"
150 " return pow( fXr, fAlpha-1.0) * exp(-fXr) / "
151 "fLambda / tgamma(fAlpha);\n"
152 " }\n"
153 " else\n"
154 " {\n"
155 " return exp( (fAlpha-1.0) * log(fXr) - fXr - "
156 "log(fLambda) - lgamma(fAlpha));\n"
157 " }\n"
158 " }\n"
159 " else\n"
160 " {\n"
161 " if (fAlpha<fMaxGammaArgument)\n"
162 " {\n"
163 " return pow( fXr, fAlpha-1.0) * exp(-fXr) / "
164 "fLambda / tgamma(fAlpha);\n"
165 " }\n"
166 " else\n"
167 " {\n"
168 " return pow( fXr, fAlpha-1.0) * exp(-fXr) / "
169 "fLambda / exp( lgamma(fAlpha));\n"
170 " }\n"
171 " }\n"
172 " }\n"
173 "}\n";
174 std::string GetBetaDistDecl =
175 "double GetBetaDist(double fXin, double fAlpha, double fBeta);\n";
176 std::string GetBetaDist =
177 "double GetBetaDist(double fXin, double fAlpha, double fBeta)\n"
178 "{\n"
179 " if (fXin <= 0.0)\n"
180 " return 0.0;\n"
181 " if (fXin >= 1.0)\n"
182 " return 1.0;\n"
183 " if (fBeta == 1.0)\n"
184 " return pow(fXin, fAlpha);\n"
185 " if (fAlpha == 1.0)\n"
186 " return -expm1(fBeta * log1p(-fXin));\n"
187 " double fResult;\n"
188 " double fY = (0.5-fXin)+0.5;\n"
189 " double flnY = log1p(-fXin);\n"
190 " double fX = fXin;\n"
191 " double flnX = log(fXin);\n"
192 " double fA = fAlpha;\n"
193 " double fB = fBeta;\n"
194 " bool bReflect = fXin > fAlpha*pow((fAlpha+fBeta),-1.0);\n"
195 " if (bReflect)\n"
196 " {\n"
197 " fA = fBeta;\n"
198 " fB = fAlpha;\n"
199 " fX = fY;\n"
200 " fY = fXin;\n"
201 " flnX = flnY;\n"
202 " flnY = log(fXin);\n"
203 " }\n"
204 " fResult = lcl_GetBetaHelperContFrac(fX,fA,fB)*pow(fA,-1.0);\n"
205 " double fP = fA*pow((fA+fB),-1.0);\n"
206 " double fQ = fB*pow((fA+fB),-1.0);\n"
207 " if (fA > 1.0 && fB > 1.0 && fP < 0.97 && fQ < 0.97)\n"
208 " fResult *= GetBetaDistPDF(fX,fA,fB)*fX*fY;\n"
209 " else\n"
210 " fResult *= pow(exp(1.0),(fA*flnX + fB*flnY - GetLogBeta(fA,fB)));\n"
211 " if (bReflect)\n"
212 " fResult = 0.5 - fResult + 0.5;\n"
213 " if (fResult > 1.0)\n"
214 " fResult = 1.0;\n"
215 " if (fResult < 0.0)\n"
216 " fResult = 0.0;\n"
217 " return fResult;\n"
218 "}\n";
220 std::string GetFDistDecl =
221 "double GetFDist(double x, double fF1, double fF2);\n";
222 std::string GetFDist =
223 "double GetFDist(double x, double fF1, double fF2)\n"
224 "{\n"
225 " double arg = fF2*pow((fF2+fF1*x),-1.0);\n"
226 " double alpha = fF2*pow(2.0,-1.0);\n"
227 " double beta = fF1*pow(2.0,-1.0);\n"
228 " return (GetBetaDist(arg, alpha, beta));\n"
229 "}\n";
230 std::string GetGammaInvValueDecl = "double"
231 " GetGammaInvValue(double fAlpha,double fBeta,double fX1 );\n";
232 std::string GetGammaInvValue =
233 "double GetGammaInvValue(double fAlpha,double fBeta,double fX1 )\n"
234 "{\n"
235 " if (fX1 <= 0.0)\n"
236 " return 0.0;\n"
237 " else\n"
238 " {\n"
239 " double fX=fX1*pow(fBeta,-1.0);\n"
240 " double fLnFactor = fAlpha * log(fX) - fX - lgamma(fAlpha);\n"
241 " double fFactor = exp(fLnFactor);\n"
242 " if (fX>fAlpha+1.0)\n"
243 " return 1.0 - fFactor * GetGammaContFraction(fAlpha,fX);\n"
244 " else\n"
245 " return fFactor * GetGammaSeries(fAlpha,fX);\n"
246 " }\n"
247 "}\n";
248 std::string GetFInvValueDecl = "double GetFInvValue(double fF1,double fF2"
249 ",double fX1 );";
250 std::string GetFInvValue =
251 "double GetFInvValue(double fF1,double fF2,double fX1 )\n"
252 "{\n"
253 " double arg = fF2*pow((fF2+fF1*fX1),-1.0);\n"
254 " double alpha = fF2*pow(2.0,-1.0);\n"
255 " double beta = fF1*pow(2.0,-1.0);\n"
256 " double fXin,fAlpha,fBeta;\n"
257 " fXin=arg;fAlpha=alpha;fBeta=beta;\n"
258 " if (fXin <= 0.0)\n"
259 " return 0.0;\n"
260 " if (fXin >= 1.0)\n"
261 " return 1.0;\n"
262 " if (fBeta == 1.0)\n"
263 " return pow(fXin, fAlpha);\n"
264 " if (fAlpha == 1.0)\n"
265 " return -expm1(fBeta * log1p(-fXin));\n"
266 " double fResult;\n"
267 " double fY = (0.5-fXin)+0.5;\n"
268 " double flnY = log1p(-fXin);\n"
269 " double fX = fXin;\n"
270 " double flnX = log(fXin);\n"
271 " double fA = fAlpha;\n"
272 " double fB = fBeta;\n"
273 " bool bReflect = fXin > fAlpha*pow((fAlpha+fBeta),-1.0);\n"
274 " if (bReflect)\n"
275 " {\n"
276 " fA = fBeta;\n"
277 " fB = fAlpha;\n"
278 " fX = fY;\n"
279 " fY = fXin;\n"
280 " flnX = flnY;\n"
281 " flnY = log(fXin);\n"
282 " }\n"
283 " fResult = lcl_GetBetaHelperContFrac(fX,fA,fB);\n"
284 " fResult = fResult*pow(fA,-1.0);\n"
285 " double fP = fA*pow((fA+fB),-1.0);\n"
286 " double fQ = fB*pow((fA+fB),-1.0);\n"
287 " double fTemp;\n"
288 " if (fA > 1.0 && fB > 1.0 && fP < 0.97 && fQ < 0.97)\n"
289 " fTemp = GetBetaDistPDF(fX,fA,fB)*fX*fY;\n"
290 " else\n"
291 " fTemp = exp(fA*flnX + fB*flnY - GetLogBeta(fA,fB));\n"
292 " fResult *= fTemp;\n"
293 " if (bReflect)\n"
294 " fResult = 0.5 - fResult + 0.5;\n"
295 " if (fResult > 1.0)\n"
296 " fResult = 1.0;\n"
297 " if (fResult < 0.0)\n"
298 " fResult = 0.0;\n"
299 " return fResult;\n"
300 "}\n";
301 std::string GetBinomDistPMFDecl =
302 "double GetBinomDistPMF(double x, double n, double p);";
303 std::string GetBinomDistPMF =
304 "double GetBinomDistPMF(double x, double n, double p)\n"
305 "{\n"
306 " double q = (0.5 - p) + 0.5;\n"
307 " double fFactor = pow(q, n);\n"
308 " if (fFactor <= Min)\n"
309 " {\n"
310 " fFactor = pow(p, n);\n"
311 " if (fFactor <= Min)\n"
312 " return GetBetaDistPDF(p, x + 1.0, n - x + 1.0)*pow((n + 1.0),-1.0);\n"
313 " else\n"
314 " {\n"
315 " uint max = (uint)(n - x);\n"
316 " for (uint i = 0; i < max && fFactor > 0.0; ++i)\n"
317 " fFactor *= (n - i)*pow((i + 1),-1.0)*q*pow(p,-1.0);\n"
318 " return fFactor;\n"
319 " }\n"
320 " }\n"
321 " else\n"
322 " {\n"
323 " uint max = (uint)x;\n"
324 " for (uint i = 0; i < max && fFactor > 0.0; ++i)\n"
325 " fFactor *= (n - i)*pow((i + 1),-1.0)*p*pow(q,-1.0);\n"
326 " return fFactor;\n"
327 " }\n"
328 "}\n";
331 std::string lcl_GetBinomDistRangeDecl =
332 "double lcl_GetBinomDistRange(double n, \n"
333 "double xs, double xe, double fFactor, double p, double q);";
334 std::string lcl_GetBinomDistRange=
335 "double lcl_GetBinomDistRange(double n, double xs, double xe,\n"
336 " double fFactor, double p, double q)\n"
337 "{\n"
338 " uint i;\n"
339 " double fSum;\n"
340 " uint nXs = (uint)xs;\n"
341 " for (i = 1; i <= nXs && fFactor > 0.0; ++i)\n"
342 " fFactor *= (n - i + 1)*pow(i,-1.0)*p*pow(q,-1.0);\n"
343 " fSum = fFactor;\n"
344 " uint nXe =(uint)xe;\n"
345 " for (i = nXs + 1; i <= nXe && fFactor > 0.0; ++i)\n"
346 " {\n"
347 " fFactor *= (n - i + 1)*pow(i,-1.0)*p*pow(q,-1.0);\n"
348 " fSum += fFactor;\n"
349 " }\n"
350 " return (fSum > 1.0) ? 1.0 : fSum;\n"
351 "}\n";
353 std::string GetLogGammaDecl = "double GetLogGamma(double fZ);\n";
354 std::string GetLogGamma =
355 "double GetLogGamma(double fZ)\n"
356 "{\n"
357 " if (fZ >= fMaxGammaArgument)\n"
358 " return lcl_GetLogGammaHelper(fZ);\n"
359 " if (fZ >= 1.0)\n"
360 " return log(lcl_GetGammaHelper(fZ));\n"
361 " if (fZ >= 0.5)\n"
362 " return log( lcl_GetGammaHelper(fZ+1) *pow(fZ,-1.0));\n"
363 " return lcl_GetLogGammaHelper(fZ+2) - log(fZ+1) - log(fZ);\n"
364 "}\n";
367 std::string GetChiDistDecl = "double GetChiDist(double fX, double fDF);\n";
368 std::string GetChiDist =
369 "double GetChiDist(double fX, double fDF)\n"
370 "{\n"
371 " if (fX <= 0.0)\n"
372 " return 1.0;\n"
373 " else\n"
374 " return GetUpRegIGamma( fDF*pow(2.0,-1.0), fX*pow(2.0,-1.0));\n"
375 "}\n";
378 std::string GetChiSqDistCDFDecl =
379 "double GetChiSqDistCDF(double fX, double fDF);\n";
380 std::string GetChiSqDistCDF =
381 "double GetChiSqDistCDF(double fX, double fDF)\n"
382 "{\n"
383 " if (fX <= 0.0)\n"
384 " return 0.0;"
385 " else\n"
386 " return GetLowRegIGamma( fDF*pow(2.0,-1.0), fX*pow(2.0,-1.0));\n"
387 "}\n";
390 std::string GetChiSqDistPDFDecl=
391 "double GetChiSqDistPDF(double fX, double fDF);\n";
392 std::string GetChiSqDistPDF =
393 "double GetChiSqDistPDF(double fX, double fDF)\n"
394 "{\n"
395 " double fValue;\n"
396 " if (fX <= 0.0)\n"
397 " return 0.0;\n"
398 " if (fDF*fX > 1391000.0)\n"
399 " {\n"
400 " fValue = exp((0.5*fDF - 1) * log(fX*0.5) - 0.5 * fX - log(2.0)"
401 " - lgamma(0.5*fDF));\n"
402 " }\n"
403 " else\n"
404 " {\n"
405 " double fCount;\n"
406 " if (fmod(fDF,2.0)<0.5)\n"
407 " {\n"
408 " fValue = 0.5;\n"
409 " fCount = 2.0;\n"
410 " }\n"
411 " else\n"
412 " {\n"
413 " fValue = pow(sqrt(fX*2*F_PI),-1.0);\n"
414 " fCount = 1.0;\n"
415 " }\n"
416 " while ( fCount < fDF)\n"
417 " {\n"
418 " fValue *= (fX *pow(fCount,-1.0));\n"
419 " fCount += 2.0;\n"
420 " }\n"
421 " if (fX>=1425.0)\n"
422 " fValue = exp(log(fValue)-fX*pow(2,-1.0));\n"
423 " else\n"
424 " fValue *= exp(-fX*pow(2,-1.0));\n"
425 " }\n"
426 " return fValue;\n"
427 "}\n";
430 std::string lcl_IterateInverseBetaInvDecl =
431 "static double lcl_IterateInverseBetaInv(double fp, double fAlpha, \n"
432 " double fBeta, double fAx, double fBx, bool *rConvError );\n";
433 std::string lcl_IterateInverseBetaInv =
434 "static double lcl_IterateInverseBetaInv(double fp, double fAlpha, \n"
435 " double fBeta, double fAx, double fBx, bool *rConvError )\n"
436 "{\n"
437 " *rConvError = false;\n"
438 " double fYEps = 1.0E-307;\n"
439 " double fXEps = fMachEps;\n"
440 " if(!(fAx < fBx))\n"
441 " {\n"
442 " //print error\n"
443 " }\n"
444 " double fAy = fp - GetBetaDist(fAx, fAlpha, fBeta);\n"
445 " double fBy = fp - GetBetaDist(fBx, fAlpha, fBeta);\n"
446 " double fTemp;\n"
447 " unsigned short nCount;\n"
448 " for (nCount = 0; nCount < 1000 && !lcl_HasChangeOfSign(fAy,fBy);"
449 " nCount++)\n"
450 " {\n"
451 " if (fabs(fAy) <= fabs(fBy))\n"
452 " {\n"
453 " fTemp = fAx;\n"
454 " fAx += 2.0 * (fAx - fBx);\n"
455 " if (fAx < 0.0)\n"
456 " fAx = 0.0;\n"
457 " fBx = fTemp;\n"
458 " fBy = fAy;\n"
459 " fAy = fp - GetBetaDist(fAx, fAlpha, fBeta);\n"
460 " }\n"
461 " else\n"
462 " {\n"
463 " fTemp = fBx;\n"
464 " fBx += 2.0 * (fBx - fAx);\n"
465 " fAx = fTemp;\n"
466 " fAy = fBy;\n"
467 " fBy = fp - GetBetaDist(fBx, fAlpha, fBeta);\n"
468 " }\n"
469 " }\n"
470 " if (fAy == 0.0)\n"
471 " return fAx;\n"
472 " if (fBy == 0.0)\n"
473 " return fBx;\n"
474 " if (!lcl_HasChangeOfSign( fAy, fBy))\n"
475 " {\n"
476 " *rConvError = true;\n"
477 " return 0.0;\n"
478 " }\n"
479 " double fPx = fAx;\n"
480 " double fPy = fAy;\n"
481 " double fQx = fBx;\n"
482 " double fQy = fBy;\n"
483 " double fRx = fAx;\n"
484 " double fRy = fAy;\n"
485 " double fSx = 0.5 * (fAx + fBx);\n"
486 " bool bHasToInterpolate = true;\n"
487 " nCount = 0;\n"
488 " while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
489 " (fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
490 " {\n"
491 " if (bHasToInterpolate)\n"
492 " {\n"
493 " if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
494 " {\n"
495 " fSx = fPx*fRy*fQy*pow(fRy-fPy,-1.0)*pow(fQy-fPy,-1.0)\n"
496 " + fRx*fQy*fPy*pow(fQy-fRy,-1.0)*pow(fPy-fRy,-1.0)\n"
497 " + fQx*fPy*fRy*pow(fPy-fQy,-1.0)*pow(fRy-fQy,-1.0);\n"
498 " bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
499 " }\n"
500 " else\n"
501 " bHasToInterpolate = false;\n"
502 " }\n"
503 " if(!bHasToInterpolate)\n"
504 " {\n"
505 " fSx = 0.5 * (fAx + fBx);\n"
506 " fPx = fAx; fPy = fAy;\n"
507 " fQx = fBx; fQy = fBy;\n"
508 " bHasToInterpolate = true;\n"
509 " }\n"
510 " fPx = fQx; fQx = fRx; fRx = fSx;\n"
511 " fPy = fQy; fQy = fRy; fRy = fp - GetBetaDist(fSx, fAlpha, fBeta);\n"
512 " if (lcl_HasChangeOfSign( fAy, fRy))\n"
513 " {\n"
514 " fBx = fRx; fBy = fRy;\n"
515 " }\n"
516 " else\n"
517 " {\n"
518 " fAx = fRx; fAy = fRy;\n"
519 " }\n"
520 " bHasToInterpolate = bHasToInterpolate && (fabs(fRy) *"
521 " 2.0 <= fabs(fQy));\n"
522 " ++nCount;\n"
523 " }\n"
524 " return fRx;\n"
525 "}\n";
528 std::string lcl_IterateInverseChiInvDecl =
529 "static double lcl_IterateInverseChiInv"
530 "(double fp, double fdf, double fAx, double fBx, bool *rConvError);\n";
531 std::string lcl_IterateInverseChiInv =
532 "static double lcl_IterateInverseChiInv"
533 "(double fp, double fdf, double fAx, double fBx, bool *rConvError)\n"
534 "{\n"
535 " *rConvError = false;\n"
536 " double fYEps = 1.0E-307;\n"
537 " double fXEps = fMachEps;\n"
538 " if(!(fAx < fBx))\n"
539 " {\n"
540 " //print error\n"
541 " }"
542 " double fAy = fp - GetChiDist(fAx, fdf);\n"
543 " double fBy = fp - GetChiDist(fBx, fdf);\n"
544 " double fTemp;\n"
545 " unsigned short nCount;\n"
546 " for (nCount = 0; nCount < 1000 && "
547 "!lcl_HasChangeOfSign(fAy,fBy); nCount++)\n"
548 " {\n"
549 " if (fabs(fAy) <= fabs(fBy))\n"
550 " {\n"
551 " fTemp = fAx;\n"
552 " fAx += 2.0 * (fAx - fBx);\n"
553 " if (fAx < 0.0)\n"
554 " fAx = 0.0;\n"
555 " fBx = fTemp;\n"
556 " fBy = fAy;\n"
557 " fAy = fp - GetChiDist(fAx, fdf);\n"
558 " }\n"
559 " else\n"
560 " {\n"
561 " fTemp = fBx;\n"
562 " fBx += 2.0 * (fBx - fAx);\n"
563 " fAx = fTemp;\n"
564 " fAy = fBy;\n"
565 " fBy = fp - GetChiDist(fBx, fdf);\n"
566 " }\n"
567 " }\n"
568 " if (fAy == 0.0)\n"
569 " return fAx;\n"
570 " if (fBy == 0.0)\n"
571 " return fBx;\n"
572 " if (!lcl_HasChangeOfSign( fAy, fBy))\n"
573 " {\n"
574 " *rConvError = true;\n"
575 " return 0.0;\n"
576 " }\n"
577 " double fPx = fAx;\n"
578 " double fPy = fAy;\n"
579 " double fQx = fBx;\n"
580 " double fQy = fBy;\n"
581 " double fRx = fAx;\n"
582 " double fRy = fAy;\n"
583 " double fSx = 0.5 * (fAx + fBx);\n"
584 " bool bHasToInterpolate = true;\n"
585 " nCount = 0;\n"
586 " while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
587 " (fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
588 " {\n"
589 " if (bHasToInterpolate)\n"
590 " {\n"
591 " if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
592 " {\n"
593 " fSx = fPx * fRy * fQy*pow(fRy-fPy,-1.0)*pow(fQy-fPy,-1.0)\n"
594 " + fRx * fQy * fPy*pow(fQy-fRy,-1.0)*pow(fPy-fRy,-1.0)\n"
595 " + fQx * fPy * fRy*pow(fPy-fQy,-1.0)*pow(fRy-fQy,-1.0);\n"
596 " bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
597 " }\n"
598 " else\n"
599 " bHasToInterpolate = false;\n"
600 " }\n"
601 " if(!bHasToInterpolate)\n"
602 " {\n"
603 " fSx = 0.5 * (fAx + fBx);\n"
604 " fPx = fAx; fPy = fAy;\n"
605 " fQx = fBx; fQy = fBy;\n"
606 " bHasToInterpolate = true;\n"
607 " }\n"
608 " fPx = fQx; fQx = fRx; fRx = fSx;\n"
609 " fPy = fQy; fQy = fRy; fRy = fp - GetChiDist(fSx, fdf);\n"
610 " if (lcl_HasChangeOfSign( fAy, fRy))\n"
611 " {\n"
612 " fBx = fRx; fBy = fRy;\n"
613 " }\n"
614 " else\n"
615 " {\n"
616 " fAx = fRx; fAy = fRy;\n"
617 " }\n"
618 " bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
619 " * 2.0 <= fabs(fQy));\n"
620 " ++nCount;\n"
621 " }\n"
622 " return fRx;\n"
623 "}\n";
625 std::string lcl_IterateInverseChiSQInvDecl =
626 "static double lcl_IterateInverseChiSQInv( double fp, double fdf, \n"
627 " double fAx, double fBx, bool *rConvError );\n";
628 std::string lcl_IterateInverseChiSQInv =
629 "static double lcl_IterateInverseChiSQInv( double fp, double fdf, \n"
630 " double fAx, double fBx, bool *rConvError )\n"
631 "{\n"
632 " *rConvError = false;\n"
633 " double fYEps = 1.0E-307;\n"
634 " double fXEps = fMachEps;\n"
636 " if(!(fAx < fBx))\n"
637 " {\n"
638 " //print error\n"
639 " }\n"
640 " double fAy = fp - GetChiSqDistCDF(fAx, fdf);\n"
641 " double fBy = fp - GetChiSqDistCDF(fBx, fdf);\n"
642 " double fTemp;\n"
643 " unsigned short nCount;\n"
644 " for (nCount = 0; nCount < 1000 && !lcl_HasChangeOfSign(fAy,fBy);"
645 " nCount++)\n"
646 " {\n"
647 " if (fabs(fAy) <= fabs(fBy))\n"
648 " {\n"
649 " fTemp = fAx;\n"
650 " fAx += 2.0 * (fAx - fBx);\n"
651 " if (fAx < 0.0)\n"
652 " fAx = 0.0;\n"
653 " fBx = fTemp;\n"
654 " fBy = fAy;\n"
655 " fAy = fp - GetChiSqDistCDF(fAx, fdf);\n"
656 " }\n"
657 " else\n"
658 " {\n"
659 " fTemp = fBx;\n"
660 " fBx += 2.0 * (fBx - fAx);\n"
661 " fAx = fTemp;\n"
662 " fAy = fBy;\n"
663 " fBy = fp - GetChiSqDistCDF(fBx, fdf);\n"
664 " }\n"
665 " }\n"
666 " if (fAy == 0.0)\n"
667 " return fAx;\n"
668 " if (fBy == 0.0)\n"
669 " return fBx;\n"
670 " if (!lcl_HasChangeOfSign( fAy, fBy))\n"
671 " {\n"
672 " *rConvError = true;\n"
673 " return 0.0;\n"
674 " }\n"
675 " double fPx = fAx;\n"
676 " double fPy = fAy;\n"
677 " double fQx = fBx;\n"
678 " double fQy = fBy;\n"
679 " double fRx = fAx;\n"
680 " double fRy = fAy;\n"
681 " double fSx = 0.5 * (fAx + fBx);\n"
682 " bool bHasToInterpolate = true;\n"
683 " nCount = 0;\n"
684 " while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
685 " (fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
686 " {\n"
687 " if (bHasToInterpolate)\n"
688 " {\n"
689 " if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
690 " {\n"
691 " fSx = fPx * fRy * fQy / (fRy-fPy) / (fQy-fPy)\n"
692 " + fRx * fQy * fPy / (fQy-fRy) / (fPy-fRy)\n"
693 " + fQx * fPy * fRy / (fPy-fQy) / (fRy-fQy);\n"
694 " bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
695 " }\n"
696 " else\n"
697 " bHasToInterpolate = false;\n"
698 " }\n"
699 " if(!bHasToInterpolate)\n"
700 " {\n"
701 " fSx = 0.5 * (fAx + fBx);\n"
702 " fPx = fAx; fPy = fAy;\n"
703 " fQx = fBx; fQy = fBy;\n"
704 " bHasToInterpolate = true;\n"
705 " }\n"
706 " fPx = fQx; fQx = fRx; fRx = fSx;\n"
707 " fPy = fQy; fQy = fRy; fRy = fp - GetChiSqDistCDF(fSx, fdf);\n"
708 " if (lcl_HasChangeOfSign( fAy, fRy))\n"
709 " {\n"
710 " fBx = fRx; fBy = fRy;\n"
711 " }\n"
712 " else\n"
713 " {\n"
714 " fAx = fRx; fAy = fRy;\n"
715 " }\n"
716 " bHasToInterpolate = bHasToInterpolate && (fabs(fRy) * 2.0"
717 " <= fabs(fQy));\n"
718 " ++nCount;\n"
719 " }\n"
720 " return fRx;\n"
721 "}\n";
723 std::string gaussinvDecl = "double gaussinv(double x);\n";
724 std::string gaussinv =
725 "double gaussinv(double x)\n"
726 "{\n"
727 " double q,t,z;\n"
728 " q=x-0.5;\n"
729 " if(fabs(q)<=.425)\n"
730 " {\n"
731 " t=0.180625-q*q;\n"
732 " z=\n"
733 " q*\n"
734 " (\n"
735 " (\n"
736 " (\n"
737 " (\n"
738 " (\n"
739 " (\n"
740 " (\n"
741 " t*2509.0809287301226727+33430.575583588128105\n"
742 " )\n"
743 " *t+67265.770927008700853\n"
744 " )\n"
745 " *t+45921.953931549871457\n"
746 " )\n"
747 " *t+13731.693765509461125\n"
748 " )\n"
749 " *t+1971.5909503065514427\n"
750 " )\n"
751 " *t+133.14166789178437745\n"
752 " )\n"
753 " *t+3.387132872796366608\n"
754 " )\n"
755 " *pow\n"
756 " (\n"
757 " (\n"
758 " (\n"
759 " (\n"
760 " (\n"
761 " (\n"
762 " (\n"
763 " t*5226.495278852854561+28729.085735721942674\n"
764 " )\n"
765 " *t+39307.89580009271061\n"
766 " )\n"
767 " *t+21213.794301586595867\n"
768 " )\n"
769 " *t+5394.1960214247511077\n"
770 " )\n"
771 " *t+687.1870074920579083\n"
772 " )\n"
773 " *t+42.313330701600911252\n"
774 " )\n"
775 " *t+1.0\n"
776 " , -1.0);\n"
777 " }\n"
778 " else\n"
779 " {\n"
780 " if(q>0) t=1-x;\n"
781 " else t=x;\n"
782 " t=sqrt(-log(t));\n"
783 " if(t<=5.0)\n"
784 " {\n"
785 " t+=-1.6;\n"
786 " z=\n"
787 " (\n"
788 " (\n"
789 " (\n"
790 " (\n"
791 " (\n"
792 " (\n"
793 " (\n"
794 " t*7.7454501427834140764e-4+0.0227238449892691845833\n"
795 " )\n"
796 " *t+0.24178072517745061177\n"
797 " )\n"
798 " *t+1.27045825245236838258\n"
799 " )\n"
800 " *t+3.64784832476320460504\n"
801 " )\n"
802 " *t+5.7694972214606914055\n"
803 " )\n"
804 " *t+4.6303378461565452959\n"
805 " )\n"
806 " *t+1.42343711074968357734\n"
807 " )\n"
808 " *pow\n"
809 " (\n"
810 " (\n"
811 " (\n"
812 " (\n"
813 " (\n"
814 " (\n"
815 " (\n"
816 " t*1.05075007164441684324e-9+5.475938084995344946e-4\n"
817 " )\n"
818 " *t+0.0151986665636164571966\n"
819 " )\n"
820 " *t+0.14810397642748007459\n"
821 " )\n"
822 " *t+0.68976733498510000455\n"
823 " )\n"
824 " *t+1.6763848301838038494\n"
825 " )\n"
826 " *t+2.05319162663775882187\n"
827 " )\n"
828 " *t+1.0\n"
829 " , -1.0);\n"
830 " }\n"
831 " else\n"
832 " {\n"
833 " t+=-5.0;\n"
834 " z=\n"
835 " (\n"
836 " (\n"
837 " (\n"
838 " (\n"
839 " (\n"
840 " (\n"
841 " (\n"
842 " t*2.01033439929228813265e-7+2.71155556874348757815e-5\n"
843 " )\n"
844 " *t+0.0012426609473880784386\n"
845 " )\n"
846 " *t+0.026532189526576123093\n"
847 " )\n"
848 " *t+0.29656057182850489123\n"
849 " )\n"
850 " *t+1.7848265399172913358\n"
851 " )\n"
852 " *t+5.4637849111641143699\n"
853 " )\n"
854 " *t+6.6579046435011037772\n"
855 " )\n"
856 " *pow\n"
857 " (\n"
858 " (\n"
859 " (\n"
860 " (\n"
861 " (\n"
862 " (\n"
863 " (\n"
864 " t*2.04426310338993978564e-15+1.4215117583164458887e-7\n"
865 " )\n"
866 " *t+1.8463183175100546818e-5\n"
867 " )\n"
868 " *t+7.868691311456132591e-4\n"
869 " )\n"
870 " *t+0.0148753612908506148525\n"
871 " )\n"
872 " *t+0.13692988092273580531\n"
873 " )\n"
874 " *t+0.59983220655588793769\n"
875 " )\n"
876 " *t+1.0\n"
877 " , -1.0);\n"
878 " }\n"
879 " if(q<0.0) z=-z;\n"
880 " }\n"
881 " return z;\n"
882 "}\n";
884 std::string lcl_GetLogGammaHelperDecl=
885 "static double lcl_GetLogGammaHelper(double fZ);\n";
886 std::string lcl_GetLogGammaHelper =
887 "static double lcl_GetLogGammaHelper(double fZ)\n"
888 "{\n"
889 " double fg = 6.024680040776729583740234375;\n"
890 " double fZgHelp = fZ + fg - 0.5;\n"
891 " return log( lcl_getLanczosSum(fZ)) + (fZ-0.5) * log(fZgHelp) - fZgHelp;\n"
892 "}\n";
893 std::string lcl_GetGammaHelperDecl=
894 "static double lcl_GetGammaHelper(double fZ);\n";
895 std::string lcl_GetGammaHelper =
896 "static double lcl_GetGammaHelper(double fZ)\n"
897 "{\n"
898 " double fGamma = lcl_getLanczosSum(fZ);\n"
899 " double fg = 6.024680040776729583740234375;\n"
900 " double fZgHelp = fZ + fg - 0.5;\n"
901 " double fHalfpower = pow( fZgHelp, fZ*pow(2,-1.0) - 0.25);\n"
902 " fGamma *= fHalfpower;\n"
903 " fGamma = fGamma*pow(exp(fZgHelp),-1.0);\n"
904 " fGamma *= fHalfpower;\n"
905 " fGamma = 120.4;\n"
906 " if (fZ <= 20.0 && fZ == (int)fZ)\n"
907 " {\n"
908 " fGamma = (int)(fGamma+0.5);\n"
909 " }\n"
910 " return fGamma;\n"
911 "}\n";
912 std::string lcl_getLanczosSumDecl=
913 "static double lcl_getLanczosSum(double fZ);\n";
914 std::string lcl_getLanczosSum =
915 "static double lcl_getLanczosSum(double fZ) \n"
916 "{ \n"
917 " double fNum[13] ={ \n"
918 " 23531376880.41075968857200767445163675473, \n"
919 " 42919803642.64909876895789904700198885093, \n"
920 " 35711959237.35566804944018545154716670596, \n"
921 " 17921034426.03720969991975575445893111267, \n"
922 " 6039542586.35202800506429164430729792107, \n"
923 " 1439720407.311721673663223072794912393972, \n"
924 " 248874557.8620541565114603864132294232163, \n"
925 " 31426415.58540019438061423162831820536287, \n"
926 " 2876370.628935372441225409051620849613599, \n"
927 " 186056.2653952234950402949897160456992822, \n"
928 " 8071.672002365816210638002902272250613822, \n"
929 " 210.8242777515793458725097339207133627117, \n"
930 " 2.506628274631000270164908177133837338626 \n"
931 " }; \n"
932 " double fDenom[13] = { \n"
933 " 0,\n"
934 " 39916800,\n"
935 " 120543840,\n"
936 " 150917976,\n"
937 " 105258076,\n"
938 " 45995730,\n"
939 " 13339535,\n"
940 " 2637558,\n"
941 " 357423,\n"
942 " 32670,\n"
943 " 1925,\n"
944 " 66,\n"
945 " 1\n"
946 " };\n"
947 " double fSumNum;\n"
948 " double fSumDenom;\n"
949 " int nI;\n"
950 " if (fZ<=1.0)\n"
951 " {\n"
952 " fSumNum = fNum[12];\n"
953 " fSumDenom = fDenom[12];\n"
954 " nI = 11;\n"
955 " fSumNum = fSumNum*fZ+fNum[nI];\n"
956 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
957 " nI = 10;\n"
958 " fSumNum = fSumNum*fZ+fNum[nI];\n"
959 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
960 " nI = 9;\n"
961 " fSumNum = fSumNum*fZ+fNum[nI];\n"
962 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
963 " nI = 8;\n"
964 " fSumNum = fSumNum*fZ+fNum[nI];\n"
965 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
966 " nI = 7;\n"
967 " fSumNum = fSumNum*fZ+fNum[nI];\n"
968 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
969 " nI = 6;\n"
970 " fSumNum = fSumNum*fZ+fNum[nI];\n"
971 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
972 " nI = 5;\n"
973 " fSumNum = fSumNum*fZ+fNum[nI];\n"
974 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
975 " nI = 4;\n"
976 " fSumNum = fSumNum*fZ+fNum[nI];\n"
977 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
978 " nI = 3;\n"
979 " fSumNum = fSumNum*fZ+fNum[nI];\n"
980 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
981 " nI = 2;\n"
982 " fSumNum = fSumNum*fZ+fNum[nI];\n"
983 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
984 " nI = 1;\n"
985 " fSumNum = fSumNum*fZ+fNum[nI];\n"
986 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
987 " nI = 0;\n"
988 " fSumNum = fSumNum*fZ+fNum[nI];\n"
989 " fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
990 " }\n"
991 " if (fZ>1.0)\n"
992 " {\n"
993 " double fZInv = pow(fZ,-1.0);\n"
994 " fSumNum = fNum[0];\n"
995 " fSumDenom = fDenom[0];\n"
996 " nI = 1;\n"
997 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
998 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
999 " nI = 2;\n"
1000 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1001 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1002 " nI = 3;\n"
1003 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1004 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1005 " nI = 4;\n"
1006 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1007 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1008 " nI = 5;\n"
1009 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1010 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1011 " nI = 6;\n"
1012 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1013 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1014 " nI = 7;\n"
1015 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1016 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1017 " nI = 8;\n"
1018 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1019 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1020 " nI = 9;\n"
1021 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1022 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1023 " nI = 10;\n"
1024 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1025 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1026 " nI = 11;\n"
1027 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1028 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1029 " nI = 12;\n"
1030 " fSumNum = fSumNum*fZInv+fNum[nI];\n"
1031 " fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1032 " }\n"
1033 " return fSumNum*pow(fSumDenom,-1.0);\n"
1034 "}\n";
1036 std::string GetUpRegIGammaDecl=
1037 " double GetUpRegIGamma( double fA, double fX ) ;\n";
1038 std::string GetUpRegIGamma =
1039 "double GetUpRegIGamma( double fA, double fX )\n"
1040 "{\n"
1041 " double fLnFactor= fA*log(fX)-fX-lgamma(fA);\n"
1042 " double fFactor = exp(fLnFactor); \n"
1043 " if (fX>fA+1.0) \n"
1044 " return fFactor * GetGammaContFraction(fA,fX);\n"
1045 " else \n"
1046 " return 1.0 -fFactor * GetGammaSeries(fA,fX);\n"
1047 "}\n";
1049 std::string lcl_HasChangeOfSignDecl=
1050 "static inline bool lcl_HasChangeOfSign( double u, double w );\n";
1051 std::string lcl_HasChangeOfSign =
1052 "static inline bool lcl_HasChangeOfSign( double u, double w )\n"
1053 "{\n"
1054 " return (u < 0.0 && w > 0.0) || (u > 0.0 && w < 0.0);\n"
1055 "}\n";
1057 std::string GetTDistDecl=" double GetTDist(double T, double fDF);\n";
1058 std::string GetTDist =
1059 "double GetTDist(double T, double fDF)\n"
1060 "{\n"
1061 " return 0.5 * GetBetaDist(fDF*pow(fDF+T*T,-1.0),fDF*pow(2.0,-1.0), 0.5);\n"
1062 "}\n";
1064 std::string GetBetaDecl=" double GetBeta(double fAlpha, double fBeta);\n";
1065 std::string GetBeta =
1066 "double GetBeta(double fAlpha, double fBeta)\n"
1067 "{\n"
1068 " double fA;\n"
1069 " double fB;\n"
1070 " fAlpha>fBeta?(fA = fAlpha,fB = fBeta):(fA = fBeta,fB = fAlpha);\n"
1071 " double fAB = fA + fB;\n"
1073 " if (fAB < fMaxGammaArgument)\n"
1074 " return tgamma(fA)*pow(tgamma(fAB),-1.0)*tgamma(fB);\n"
1075 " double fgm = 5.524680040776729583740234375;\n"
1076 " double fLanczos = lcl_getLanczosSum(fA)*lcl_getLanczosSum(fB)\n"
1077 " *pow(lcl_getLanczosSum(fAB),-1.0);\n"
1078 " fLanczos *= sqrt(((fAB + fgm)*pow(fA + fgm,-1.0))*pow(fB + fgm,-1.0));\n"
1079 " return fLanczos * pow(exp(1.0),(-fA*log1p(fB*pow(fA + fgm,-1.0)))"
1080 " - fB*log1p(fA*pow(fB + fgm,-1.0)) - fgm);\n"
1081 "}\n";
1083 std::string GetLogBetaDecl=
1084 " double GetLogBeta(double fAlpha, double fBeta);\n";
1085 std::string GetLogBeta =
1086 "double GetLogBeta(double fAlpha, double fBeta)\n"
1087 "{\n"
1088 " double fA;\n"
1089 " double fB;\n"
1090 " fAlpha>fBeta?(fA = fAlpha,fB = fBeta):(fA = fBeta,fB = fAlpha);\n"
1091 " double fgm = 5.524680040776729583740234375;\n"
1093 " double fLanczos = lcl_getLanczosSum(fA)*lcl_getLanczosSum(fB)*\n"
1094 " pow(lcl_getLanczosSum(fA + fB),-1.0);\n"
1095 " double fResult= -fA *log1p(fB*pow(fA + fgm,-1.0))"
1096 "-fB *log1p(fA*pow(fB + fgm,-1.0))-fgm;\n"
1097 " fResult += log(fLanczos)+0.5*(log(fA + fB + fgm) - log(fA + fgm)\n"
1098 " - log(fB + fgm));\n"
1099 " return fResult;\n"
1100 "}\n";
1102 std::string GetBetaDistPDFDecl=
1103 "double GetBetaDistPDF(double fX, double fA, double fB);\n";
1104 std::string GetBetaDistPDF =
1105 "double GetBetaDistPDF(double fX, double fA, double fB)\n"
1106 "{\n"
1107 " if (fA == 1.0) \n"
1108 " {\n"
1109 " if (fB == 1.0)\n"
1110 " return 1.0;\n"
1111 " if (fB == 2.0)\n"
1112 " return -2.0*fX + 2.0;\n"
1113 " if (fX == 1.0 && fB < 1.0)\n"
1114 " {\n"
1115 " return HUGE_VAL;\n"
1116 " }\n"
1117 " if (fX <= 0.01)\n"
1118 " return fB + fB * expm1((fB-1.0) * log1p(-fX));\n"
1119 " else \n"
1120 " return fB * pow(0.5-fX+0.5,fB-1.0);\n"
1121 " }\n"
1122 " if (fB == 1.0) \n"
1123 " {\n"
1124 " if (fA == 2.0)\n"
1125 " return fA * fX;\n"
1126 " if (fX == 0.0 && fA < 1.0)\n"
1127 " {\n"
1128 " return HUGE_VAL;\n"
1129 " }\n"
1130 " return fA * pow(fX,fA-1);\n"
1131 " }\n"
1132 " if (fX <= 0.0)\n"
1133 " {\n"
1134 " if (fA < 1.0 && fX == 0.0)\n"
1135 " {\n"
1136 " return HUGE_VAL;\n"
1137 " }\n"
1138 " else\n"
1139 " return 0.0;\n"
1140 " }\n"
1141 " if (fX >= 1.0)\n"
1142 " {\n"
1143 " if (fB < 1.0 && fX == 1.0)\n"
1144 " {\n"
1145 " return HUGE_VAL;\n"
1146 " }\n"
1147 " else \n"
1148 " return 0.0;\n"
1149 " }\n"
1150 " double fLogDblMax = log( 1.79769e+308 );\n"
1151 " double fLogDblMin = log( 2.22507e-308 );\n"
1152 " double fLogY = (fX < 0.1) ? log1p(-fX) : log(0.5-fX+0.5);\n"
1153 " double fLogX = log(fX);\n"
1154 " double fAm1LogX = (fA-1.0) * fLogX;\n"
1155 " double fBm1LogY = (fB-1.0) * fLogY;\n"
1156 " double fLogBeta = GetLogBeta(fA,fB);\n"
1157 " if ( fAm1LogX < fLogDblMax && fAm1LogX > fLogDblMin\n"
1158 " && fBm1LogY < fLogDblMax && fBm1LogY > fLogDblMin\n"
1159 " && fLogBeta < fLogDblMax && fLogBeta > fLogDblMin\n"
1160 " && fAm1LogX + fBm1LogY < fLogDblMax && fAm1LogX + fBm1LogY > \n"
1161 " fLogDblMin)\n"
1162 " return pow(fX,fA-1.0)*pow(0.5-fX+0.5,fB-1.0)"
1163 "*pow(GetBeta(fA,fB),-1.0);\n"
1164 " else \n"
1165 " return exp( fAm1LogX + fBm1LogY - fLogBeta);\n"
1166 "}\n";
1168 std::string lcl_GetBetaHelperContFracDecl=
1169 "double lcl_GetBetaHelperContFrac(double fX, double fA, double fB);\n";
1170 std::string lcl_GetBetaHelperContFrac =
1171 "double lcl_GetBetaHelperContFrac(double fX, double fA, double fB)\n"
1172 "{ \n"
1174 " double a1, b1, a2, b2, fnorm, apl2m, d2m, d2m1, cfnew, cf;\n"
1175 " a1 = 1.0; b1 = 1.0;\n"
1176 " b2 = 1.0 - (fA+fB)*pow(fA+1.0,-1.0)*fX;\n"
1177 " b2==0.0?(a2 = 0.0,fnorm = 1.0,cf = 1.0):\n"
1178 " (a2 = 1.0,fnorm = pow(b2,-1.0),cf = a2*fnorm);\n"
1179 " cfnew = 1.0;\n"
1180 " double rm = 1.0;\n"
1181 " double fMaxIter = 50000.0;\n"
1182 " bool bfinished = false;\n"
1183 " do\n"
1184 " {\n"
1185 " apl2m = fA + 2.0*rm;\n"
1186 " d2m = (rm*(fB-rm))*fX*pow(apl2m*(apl2m-1.0),-1.0);\n"
1187 " d2m1 = -((fA+rm)*(fA+rm+fB))*fX*pow(apl2m*(apl2m+1.0),-1.0);\n"
1188 " a1 = (a2+d2m*a1)*fnorm;\n"
1189 " b1 = (b2+d2m*b1)*fnorm;\n"
1190 " a2 = a1 + d2m1*a2*fnorm;\n"
1191 " b2 = b1 + d2m1*b2*fnorm;\n"
1192 " if (b2 != 0.0) \n"
1193 " {\n"
1194 " fnorm = pow(b2,-1.0);\n"
1195 " cfnew = a2*fnorm;\n"
1196 " bfinished = (fabs(cf-cfnew) < fabs(cf)*fMachEps);\n"
1197 " }\n"
1198 " cf = cfnew;\n"
1199 " rm += 1.0;\n"
1200 " }\n"
1201 " while (rm < fMaxIter && !bfinished);\n"
1202 " return cf;\n"
1203 "}\n";
1205 std::string lcl_IterateInverseDecl=
1206 "double lcl_IterateInverse("
1207 "double fAx, double fBx, bool* rConvError,double fp,double fDF );\n";
1208 std::string lcl_IterateInverse =
1209 "double lcl_IterateInverse( "
1210 "double fAx, double fBx, bool* rConvError,double fp,double fDF )\n"
1211 "{\n"
1212 " *rConvError = false;\n"
1213 " double fYEps = 1.0E-307;\n"
1214 " double fXEps =DBL_EPSILON;\n"
1215 " if(fAx>fBx)\n"
1216 " return DBL_MAX;\n"
1217 " double fAy = GetValue(fAx,fp,fDF);\n"
1218 " double fBy = GetValue(fBx,fp,fDF);\n"
1219 " double fTemp;\n"
1220 " unsigned short nCount;\n"
1221 " double inter;\n"
1222 " bool sign;\n"
1223 " for (nCount =0;nCount<1000&&!lcl_HasChangeOfSign(fAy,fBy);nCount++)\n"
1224 " {\n"
1225 " inter = 2.0 * (fAx - fBx);\n"
1226 " if (fabs(fAy) <= fabs(fBy)) \n"
1227 " {\n"
1228 " sign = true;\n"
1229 " fTemp = fAx;\n"
1230 " fAx += inter;\n"
1231 " if (fAx < 0.0)\n"
1232 " fAx = 0.0;\n"
1233 " fBx = fTemp;\n"
1234 " fBy = fAy;\n"
1235 " fTemp = fAx;\n"
1236 " }\n"
1237 " else\n"
1238 " {\n"
1239 " sign = false;\n"
1240 " fTemp = fBx;\n"
1241 " fBx -= inter;\n"
1242 " fAx = fTemp;\n"
1243 " fAy = fBy;\n"
1244 " fTemp = fBx;\n"
1245 " }\n"
1246 " fTemp = GetValue(fTemp,fp,fDF);\n"
1247 " sign?(fAy = fTemp):(fBy = fTemp);\n"
1248 " }\n"
1249 " if (fAy == 0.0)\n"
1250 " return fAx;\n"
1251 " if (fBy == 0.0)\n"
1252 " return fBx;\n"
1253 " if (!lcl_HasChangeOfSign( fAy, fBy))\n"
1254 " {\n"
1255 " *rConvError = true;\n"
1256 " return 0.0;\n"
1257 " }\n"
1258 " double fPx = fAx;\n"
1259 " double fPy = fAy;\n"
1260 " double fQx = fBx;\n"
1261 " double fQy = fBy;\n"
1262 " double fRx = fAx;\n"
1263 " double fRy = fAy;\n"
1264 " double fSx = 0.5 * (fAx + fBx); \n"
1265 " bool bHasToInterpolate = true;\n"
1266 " nCount = 0;\n"
1267 " while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
1268 " (fBx-fAx) > max( fabs(fAx), fabs(fBx)) * fXEps )\n"
1269 " {\n"
1270 " if (bHasToInterpolate)\n"
1271 " {\n"
1272 " if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
1273 " {\n"
1274 " fSx = fPx * fRy * fQy * pow(fRy-fPy,-1.0)*pow(fQy-fPy,-1.0)\n"
1275 " + fRx * fQy * fPy * pow(fQy-fRy,-1.0)*pow(fPy-fRy,-1.0)\n"
1276 " + fQx * fPy * fRy * pow(fPy-fQy,-1.0)*pow(fRy-fQy,-1.0);\n"
1277 " bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
1278 " }\n"
1279 " else\n"
1280 " bHasToInterpolate = false;\n"
1281 " }\n"
1282 " if(!bHasToInterpolate)\n"
1283 " {\n"
1284 " fSx = 0.5 * (fAx + fBx);\n"
1285 " \n"
1286 " fPx = fAx; fPy = fAy;\n"
1287 " fQx = fBx; fQy = fBy;\n"
1288 " bHasToInterpolate = true;\n"
1289 " }\n"
1290 " fPx = fQx; fQx = fRx; fRx = fSx;\n"
1291 " fPy = fQy; fQy = fRy; fRy = GetValue(fSx,fp,fDF);\n"
1292 " lcl_HasChangeOfSign( fAy, fRy)?(fBx = fRx,fBy = fRy):\n"
1293 " (fAx = fRx,fAy = fRy);\n"
1294 " bHasToInterpolate =\n"
1295 " bHasToInterpolate && (fabs(fRy) * 2.0 <= fabs(fQy));\n"
1296 " ++nCount;\n"
1297 " }\n"
1298 " return fRx;\n"
1299 "}\n";
1300 std::string phiDecl=
1301 "double phi(double x);\n";
1302 std::string phi =
1303 "double phi(double x)\n"
1304 "{\n"
1305 " return 0.39894228040143268 * exp(-(x * x) / 2.0);\n"
1306 "}\n";
1307 std::string taylorDecl =
1308 "double taylor(double* pPolynom, uint nMax, double x);\n";
1309 std::string taylor =
1310 "double taylor(double* pPolynom, uint nMax, double x)\n"
1311 "{\n"
1312 " double nVal = pPolynom[nMax];\n"
1313 " for (short i = nMax-1; i >= 0; i--)\n"
1314 " {\n"
1315 " nVal = pPolynom[i] + (nVal * x);\n"
1316 " }\n"
1317 " return nVal;\n"
1318 "}";
1319 std::string gaussDecl = "double gauss(double x);\n";
1320 std::string gauss =
1321 "double gauss(double x)\n"
1322 "{\n"
1323 " double xAbs = fabs(x);\n"
1324 " uint xShort = (uint)(floor(xAbs));\n"
1325 " double nVal = 0.0;\n"
1326 " if (xShort == 0)\n"
1327 " {\n"
1328 " double t0[] =\n"
1329 " { 0.39894228040143268, -0.06649038006690545, 0.00997355701003582,\n"
1330 " -0.00118732821548045, 0.00011543468761616, -0.00000944465625950,\n"
1331 " 0.00000066596935163, -0.00000004122667415, 0.00000000227352982,\n"
1332 " 0.00000000011301172, 0.00000000000511243, -0.00000000000021218 };\n"
1333 " nVal = taylor(t0, 11, (xAbs * xAbs)) * xAbs;\n"
1334 " }\n"
1335 " else if ((xShort >= 1) && (xShort <= 2))\n"
1336 " {\n"
1337 " double t2[] =\n"
1338 " { 0.47724986805182079, 0.05399096651318805, -0.05399096651318805,\n"
1339 " 0.02699548325659403, -0.00449924720943234, -0.00224962360471617,\n"
1340 " 0.00134977416282970, -0.00011783742691370, -0.00011515930357476,\n"
1341 " 0.00003704737285544, 0.00000282690796889, -0.00000354513195524,\n"
1342 " 0.00000037669563126, 0.00000019202407921, -0.00000005226908590,\n"
1343 " -0.00000000491799345, 0.00000000366377919, -0.00000000015981997,\n"
1344 " -0.00000000017381238, 0.00000000002624031, 0.00000000000560919,\n"
1345 " -0.00000000000172127, -0.00000000000008634, 0.00000000000007894 };\n"
1346 " nVal = taylor(t2, 23, (xAbs - 2.0));\n"
1347 " }\n"
1348 " else if ((xShort >= 3) && (xShort <= 4))\n"
1349 " {\n"
1350 " double t4[] =\n"
1351 " { 0.49996832875816688, 0.00013383022576489, -0.00026766045152977,\n"
1352 " 0.00033457556441221, -0.00028996548915725, 0.00018178605666397,\n"
1353 " -0.00008252863922168, 0.00002551802519049, -0.00000391665839292,\n"
1354 " -0.00000074018205222, 0.00000064422023359, -0.00000017370155340,\n"
1355 " 0.00000000909595465, 0.00000000944943118, -0.00000000329957075,\n"
1356 " 0.00000000029492075, 0.00000000011874477, -0.00000000004420396,\n"
1357 " 0.00000000000361422, 0.00000000000143638, -0.00000000000045848 };\n"
1358 " nVal = taylor(t4, 20, (xAbs - 4.0));\n"
1359 " }\n"
1360 " else\n"
1361 " {\n"
1362 " double asympt[] = { -1.0, 1.0, -3.0, 15.0, -105.0 };\n"
1363 " nVal = 0.5 + phi(xAbs) * taylor(asympt, 4, 1.0/(xAbs * xAbs))/xAbs;\n"
1364 " }\n"
1365 " if (x < 0.0)\n"
1366 " return -nVal;\n"
1367 " else\n"
1368 " return nVal;\n"
1369 "}\n";
1370 std::string lcl_Erfc0600Decl=
1371 "void lcl_Erfc0600( double x, double *fVal );\n";
1372 std::string lcl_Erfc0600 =
1373 "void lcl_Erfc0600( double x, double *fVal )\n"
1374 "{\n"
1375 " double fPSum = 0.0;\n"
1376 " double fQSum = 0.0;\n"
1377 " double fXPow = 1.0;\n"
1378 " double *pn;\n"
1379 " double *qn;\n"
1380 " if ( x < 2.2 )\n"
1381 " {\n"
1382 " double pn22[] = { \n"
1383 " 9.99999992049799098E-1, \n"
1384 " 1.33154163936765307, \n"
1385 " 8.78115804155881782E-1, \n"
1386 " 3.31899559578213215E-1, \n"
1387 " 7.14193832506776067E-2, \n"
1388 " 7.06940843763253131E-3 \n"
1389 " }; \n"
1390 " double qn22[] = { \n"
1391 " 1.00000000000000000, \n"
1392 " 2.45992070144245533, \n"
1393 " 2.65383972869775752, \n"
1394 " 1.61876655543871376, \n"
1395 " 5.94651311286481502E-1, \n"
1396 " 1.26579413030177940E-1, \n"
1397 " 1.25304936549413393E-2 \n"
1398 " }; \n"
1399 " pn = pn22; \n"
1400 " qn = qn22; \n"
1401 " } \n"
1402 " else \n"
1403 " \n"
1404 " { \n"
1405 " double pn60[] = {\n"
1406 " 9.99921140009714409E-1,\n"
1407 " 1.62356584489366647,\n"
1408 " 1.26739901455873222,\n"
1409 " 5.81528574177741135E-1,\n"
1410 " 1.57289620742838702E-1,\n"
1411 " 2.25716982919217555E-2\n"
1412 " };\n"
1413 " double qn60[] = {\n"
1414 " 1.00000000000000000,\n"
1415 " 2.75143870676376208,\n"
1416 " 3.37367334657284535,\n"
1417 " 2.38574194785344389,\n"
1418 " 1.05074004614827206,\n"
1419 " 2.78788439273628983E-1,\n"
1420 " 4.00072964526861362E-2\n"
1421 " };\n"
1422 " pn = pn60;\n"
1423 " qn = qn60;\n"
1424 " }\n"
1425 " for ( unsigned int i = 0; i < 6; ++i ) \n"
1426 " {\n"
1427 " fPSum += pn[i]*fXPow;\n"
1428 " fQSum += qn[i]*fXPow;\n"
1429 " fXPow *= x;\n"
1430 " }\n"
1431 " fQSum += qn[6]*fXPow;\n"
1432 " *fVal = exp((-1.0)*x*x)*fPSum*pow(fQSum, -1.0);\n"
1433 " }\n";
1434 std::string lcl_Erfc2654Decl=
1435 "void lcl_Erfc2654( double x, double *fVal );\n";
1436 std::string lcl_Erfc2654 =
1437 "void lcl_Erfc2654( double x, double *fVal )\n"
1438 "{\n"
1439 " double pn[] = {\n"
1440 " 5.64189583547756078E-1,\n"
1441 " 8.80253746105525775,\n"
1442 " 3.84683103716117320E1,\n"
1443 " 4.77209965874436377E1,\n"
1444 " 8.08040729052301677\n"
1445 " };\n"
1446 " double qn[] = {\n"
1447 " 1.00000000000000000,\n"
1448 " 1.61020914205869003E1,\n"
1449 " 7.54843505665954743E1,\n"
1450 " 1.12123870801026015E2,\n"
1451 " 3.73997570145040850E1\n"
1452 " };\n"
1453 "\n"
1454 " double fPSum = 0.0;\n"
1455 " double fQSum = 0.0;\n"
1456 " double fXPow = 1.0;\n"
1457 "\n"
1458 " for ( unsigned int i = 0; i <= 4; ++i )\n"
1459 " {\n"
1460 " fPSum += pn[i]*fXPow; \n"
1461 " fQSum += qn[i]*fXPow;\n"
1462 " fXPow *= pow(x*x, -1.0);\n"
1463 " }\n"
1464 " *fVal = exp((-1.0)*x*x)*fPSum*pow(x*fQSum, -1.0);\n"
1465 "}\n";
1466 std::string lcl_Erf0065Decl=
1467 "void lcl_Erf0065( double x, double *fVal );\n";
1468 std::string lcl_Erf0065 =
1469 "void lcl_Erf0065( double x, double *fVal )\n"
1470 " {\n"
1471 " double pn[] = {\n"
1472 " 1.12837916709551256,\n"
1473 " 1.35894887627277916E-1,\n"
1474 " 4.03259488531795274E-2,\n"
1475 " 1.20339380863079457E-3,\n"
1476 " 6.49254556481904354E-5\n"
1477 " };\n"
1478 " double qn[] = {\n"
1479 " 1.00000000000000000,\n"
1480 " 4.53767041780002545E-1,\n"
1481 " 8.69936222615385890E-2,\n"
1482 " 8.49717371168693357E-3,\n"
1483 " 3.64915280629351082E-4\n"
1484 " };\n"
1485 " double fPSum = 0.0;\n"
1486 " double fQSum = 0.0;\n"
1487 " double fXPow = 1.0;\n"
1488 " for ( unsigned int i = 0; i <= 4; ++i )\n"
1489 " {\n"
1490 " fPSum += pn[i]*fXPow;\n"
1491 " fQSum += qn[i]*fXPow;\n"
1492 " fXPow *= x*x;\n"
1493 " }\n"
1494 " *fVal = x * fPSum * pow(fQSum, -1.0);\n"
1495 " }\n";
1496 std::string rtl_math_erf_rdDecl=
1497 "double rtl_math_erf_rd( double x );\n";
1498 std::string rtl_math_erf_rd =
1499 " double rtl_math_erf_rd( double x )\n"
1500 " {\n"
1501 " if( x == 0.0 )\n"
1502 " return 0.0;\n"
1503 " bool bNegative = false;\n"
1504 " if ( x < 0.0 )\n"
1505 " {\n"
1506 " x = fabs( x );\n"
1507 " bNegative = true;\n"
1508 " }\n"
1509 " double fErf = 1.0;\n"
1510 " if ( x < 1.0e-10 )\n"
1511 " fErf = (double) (x*1.1283791670955125738961589031215452);\n"
1512 " else if ( x < 0.65 )\n"
1513 " lcl_Erf0065( x, &fErf );\n"
1514 " if ( bNegative )\n"
1515 " fErf *= (-1.0);\n"
1516 " return fErf;\n"
1517 " }\n";
1518 std::string rtl_math_erfc_rdDecl=
1519 "double rtl_math_erfc_rd( double x );\n";
1520 std::string rtl_math_erfc_rd =
1521 " double rtl_math_erfc_rd( double x )\n"
1522 " {\n"
1523 " if ( x == 0.0 )\n"
1524 " return 1.0;\n"
1525 " bool bNegative = false;\n"
1526 " if ( x < 0.0 )\n"
1527 " {\n"
1528 " x = fabs( x );\n"
1529 " bNegative = true;\n"
1530 " }\n"
1531 " double fErfc = 0.0;\n"
1532 " if ( x >= 0.65 )\n"
1533 " {\n"
1534 " if ( x < 6.0 )\n"
1535 " lcl_Erfc0600( x, &fErfc );\n"
1536 " else\n"
1537 " lcl_Erfc2654( x, &fErfc );\n"
1538 " }\n"
1539 " else\n"
1540 " fErfc = 1.0 - rtl_math_erf_rd( x );\n"
1541 " if ( bNegative )\n"
1542 " fErfc = 2.0 - fErfc;\n"
1543 " return fErfc;\n"
1544 " }\n";
1545 #endif
1547 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */