8 if ( typeof define ===
"function" && define.amd ) {
9 define( [
'JSRootCore'], factory );
10 }
else if (typeof exports ===
'object' && typeof module !==
'undefined') {
11 factory(require(
"./JSRootCore.js"));
13 if (typeof JSROOT ==
'undefined')
14 throw new Error(
"This extension requires JSRootCore.js",
"JSRootMath.js");
15 if (typeof JSROOT.Math ==
"object")
16 throw new Error(
"This JSROOT Math already loaded",
"JSRootMath.js");
24 JSROOT.sources.push(
"math");
29 JSROOT.Math.lgam =
function( x ) {
30 var p, q, u, w, z, i, sgngam = 1;
31 var kMAXLGM = 2.556348e305;
32 var LS2PI = 0.91893853320467274178;
35 8.11614167470508450300E-4,
36 -5.95061904284301438324E-4,
37 7.93650340457716943945E-4,
38 -2.77777777730099687205E-3,
39 8.33333333333331927722E-2
43 -1.37825152569120859100E3,
44 -3.88016315134637840924E4,
45 -3.31612992738871184744E5,
46 -1.16237097492762307383E6,
47 -1.72173700820839662146E6,
48 -8.53555664245765465627E5
53 -3.51815701436523470549E2,
54 -1.70642106651881159223E4,
55 -2.20528590553854454839E5,
56 -1.13933444367982507207E6,
57 -2.53252307177582951285E6,
58 -2.01889141433532773231E6
61 if (x >= Number.POSITIVE_INFINITY)
62 return(Number.POSITIVE_INFINITY);
69 return (Number.POSITIVE_INFINITY);
80 z = q * Math.sin( Math.PI * z );
82 return (Number.POSITIVE_INFINITY);
83 z = Math.log(Math.PI) - Math.log( z ) - w;
97 return (Number.POSITIVE_INFINITY);
109 return( Math.log(z) );
112 p = x * this.Polynomialeval(x, B, 5 ) / this.Polynomial1eval( x, C, 6);
113 return( Math.log(z) + p );
116 return( sgngam * Number.POSITIVE_INFINITY );
118 q = ( x - 0.5 ) * Math.log(x) - x + LS2PI;
124 q += ((7.9365079365079365079365e-4 * p
125 - 2.7777777777777777777778e-3) *p
126 + 0.0833333333333333333333) / x;
128 q += this.Polynomialeval( p, A, 4 ) / x;
138 JSROOT.Math.Polynomialeval =
function(x, a, N) {
139 if (N==0)
return a[0];
142 for (var i=1; i <= N; ++i)
154 JSROOT.Math.Polynomial1eval =
function(x, a, N) {
155 if (N==0)
return a[0];
158 for (var i=1; i < N; ++i)
165 JSROOT.Math.ndtri =
function( y0 ) {
167 return( Number.NEGATIVE_INFINITY );
169 return( Number.POSITIVE_INFINITY );
172 -5.99633501014107895267E1,
173 9.80010754185999661536E1,
174 -5.66762857469070293439E1,
175 1.39312609387279679503E1,
176 -1.23916583867381258016E0
180 1.95448858338141759834E0,
181 4.67627912898881538453E0,
182 8.63602421390890590575E1,
183 -2.25462687854119370527E2,
184 2.00260212380060660359E2,
185 -8.20372256168333339912E1,
186 1.59056225126211695515E1,
187 -1.18331621121330003142E0
191 4.05544892305962419923E0,
192 3.15251094599893866154E1,
193 5.71628192246421288162E1,
194 4.40805073893200834700E1,
195 1.46849561928858024014E1,
196 2.18663306850790267539E0,
197 -1.40256079171354495875E-1,
198 -3.50424626827848203418E-2,
199 -8.57456785154685413611E-4
203 1.57799883256466749731E1,
204 4.53907635128879210584E1,
205 4.13172038254672030440E1,
206 1.50425385692907503408E1,
207 2.50464946208309415979E0,
208 -1.42182922854787788574E-1,
209 -3.80806407691578277194E-2,
210 -9.33259480895457427372E-4
214 3.23774891776946035970E0,
215 6.91522889068984211695E0,
216 3.93881025292474443415E0,
217 1.33303460815807542389E0,
218 2.01485389549179081538E-1,
219 1.23716634817820021358E-2,
220 3.01581553508235416007E-4,
221 2.65806974686737550832E-6,
222 6.23974539184983293730E-9
226 6.02427039364742014255E0,
227 3.67983563856160859403E0,
228 1.37702099489081330271E0,
229 2.16236993594496635890E-1,
230 1.34204006088543189037E-2,
231 3.28014464682127739104E-4,
232 2.89247864745380683936E-6,
233 6.79019408009981274425E-9
236 var s2pi = 2.50662827463100050242e0;
239 var x, z, y2, x0, x1;
241 if ( y > (1.0 - 0.13533528323661269189) ) {
245 if ( y > 0.13533528323661269189 ) {
248 x = y + y * (y2 * this.Polynomialeval( y2, P0, 4)/ this.Polynomial1eval( y2, Q0, 8 ));
252 x = Math.sqrt( -2.0 * Math.log(y) );
253 x0 = x - Math.log(x)/x;
256 x1 = z * this.Polynomialeval( z, P1, 8 )/ this.Polynomial1eval( z, Q1, 8 );
258 x1 = z * this.Polynomialeval( z, P2, 8 )/ this.Polynomial1eval( z, Q2, 8 );
265 JSROOT.Math.normal_quantile =
function(z, sigma) {
266 return sigma * JSROOT.Math.ndtri(z);
270 JSROOT.Math.igam =
function(a,x) {
271 var kMACHEP = 1.11022302462515654042363166809e-16;
272 var kMAXLOG = 709.782712893383973096206318587;
277 if (a <= 0)
return 1.0;
279 if (x <= 0)
return 0.0;
281 if( (x > 1.0) && (x > a ) )
282 return 1.0 - this.igamc(a,x);
285 ax = a * Math.log(x) - x - this.lgam(a);
302 while( c/ans > kMACHEP );
308 JSROOT.Math.igamc =
function(a,x) {
309 var kMACHEP = 1.11022302462515654042363166809e-16;
310 var kMAXLOG = 709.782712893383973096206318587;
312 var kBig = 4.503599627370496e15;
313 var kBiginv = 2.22044604925031308085e-16;
315 var ans, ax, c, yc, r, t, y, z;
316 var pk, pkm1, pkm2, qk, qkm1, qkm2;
320 if (a <= 0)
return 0.0;
322 if (x <= 0)
return 1.0;
324 if( (x < 1.0) || (x < a) )
325 return ( 1.0 - JSROOT.Math.igam(a,x) );
327 ax = a * Math.log(x) - x - JSROOT.Math.lgam(a);
349 pk = pkm1 * z - pkm2 * yc;
350 qk = qkm1 * z - qkm2 * yc;
354 t = Math.abs( (ans - r)/r );
363 if( Math.abs(pk) > kBig )
371 while( t > kMACHEP );
377 JSROOT.Math.igami =
function(a, y0) {
378 var x0, x1, x, yl, yh, y, d, lgm, dithresh, i, dir,
379 kMACHEP = 1.11022302462515654042363166809e-16;
383 alert(
"igami : Wrong domain for parameter a (must be > 0)");
387 return Number.POSITIVE_INFINITY;
393 var kMAXNUM = Number.MAX_VALUE;
398 dithresh = 5.0 * kMACHEP;
402 y = ( 1.0 - d - this.ndtri(y0) * Math.sqrt(d) );
407 for( i=0; i<10; ++i ) {
408 if ( x > x0 || x < x1 )
411 if ( y < yl || y > yh )
422 d = (a - 1.0) * Math.log(x) - x - lgm;
428 if ( Math.abs(d/x) < kMACHEP )
434 if ( x0 == kMAXNUM ) {
437 while ( x0 == kMAXNUM ) {
439 y = this.igamc( a, x );
451 for( i=0; i<400; ++i ) {
452 x = x1 + d * (x0 - x1);
453 y = this.igamc( a, x );
454 lgm = (x0 - x1)/(x1 + x0);
455 if ( Math.abs(lgm) < dithresh )
458 if ( Math.abs(lgm) < dithresh )
472 d = (y0 - yl)/(yh - yl);
485 d = (y0 - yl)/(yh - yl);
493 JSROOT.Math.gamma_quantile_c =
function(z, alpha, theta) {
494 return theta * this.igami( alpha, z);
498 JSROOT.Math.gamma_quantile =
function(z, alpha, theta) {
499 return theta * this.igami( alpha, 1.- z);
503 JSROOT.Math.log10 =
function(n) {
504 return Math.log(n) / Math.log(10);
508 JSROOT.Math.landau_pdf =
function(x, xi, x0) {
511 if (x0===undefined) x0 = 0;
512 if (xi <= 0)
return 0;
514 var u, ue, us, denlan;
515 var p1 =
new Array(0.4259894875,-0.1249762550, 0.03984243700, -0.006298287635, 0.001511162253);
516 var q1 =
new Array(1.0 ,-0.3388260629, 0.09594393323, -0.01608042283, 0.003778942063);
517 var p2 =
new Array(0.1788541609, 0.1173957403, 0.01488850518, -0.001394989411, 0.0001283617211);
518 var q2 =
new Array(1.0 , 0.7428795082, 0.3153932961, 0.06694219548, 0.008790609714);
519 var p3 =
new Array(0.1788544503, 0.09359161662,0.006325387654, 0.00006611667319,-0.000002031049101);
520 var q3 =
new Array(1.0 , 0.6097809921, 0.2560616665, 0.04746722384, 0.006957301675);
521 var p4 =
new Array(0.9874054407, 118.6723273, 849.2794360, -743.7792444, 427.0262186);
522 var q4 =
new Array(1.0 , 106.8615961, 337.6496214, 2016.712389, 1597.063511);
523 var p5 =
new Array(1.003675074, 167.5702434, 4789.711289, 21217.86767, -22324.94910);
524 var q5 =
new Array(1.0 , 156.9424537, 3745.310488, 9834.698876, 66924.28357);
525 var p6 =
new Array(1.000827619, 664.9143136, 62972.92665, 475554.6998, -5743609.109);
526 var q6 =
new Array(1.0 , 651.4101098, 56974.73333, 165917.4725, -2815759.939);
527 var a1 =
new Array(0.04166666667,-0.01996527778, 0.02709538966);
528 var a2 =
new Array(-1.845568670,-4.284640743);
532 if (u < 1e-10)
return 0.0;
535 denlan = 0.3989422803*(ue/us)*(1+(a1[0]+(a1[1]+a1[2]*u)*u)*u);
538 denlan = Math.exp(-u)*Math.sqrt(u)*
539 (p1[0]+(p1[1]+(p1[2]+(p1[3]+p1[4]*v)*v)*v)*v)/
540 (q1[0]+(q1[1]+(q1[2]+(q1[3]+q1[4]*v)*v)*v)*v);
542 denlan = (p2[0]+(p2[1]+(p2[2]+(p2[3]+p2[4]*v)*v)*v)*v)/
543 (q2[0]+(q2[1]+(q2[2]+(q2[3]+q2[4]*v)*v)*v)*v);
545 denlan = (p3[0]+(p3[1]+(p3[2]+(p3[3]+p3[4]*v)*v)*v)*v)/
546 (q3[0]+(q3[1]+(q3[2]+(q3[3]+q3[4]*v)*v)*v)*v);
549 denlan = u*u*(p4[0]+(p4[1]+(p4[2]+(p4[3]+p4[4]*u)*u)*u)*u)/
550 (q4[0]+(q4[1]+(q4[2]+(q4[3]+q4[4]*u)*u)*u)*u);
553 denlan = u*u*(p5[0]+(p5[1]+(p5[2]+(p5[3]+p5[4]*u)*u)*u)*u)/
554 (q5[0]+(q5[1]+(q5[2]+(q5[3]+q5[4]*u)*u)*u)*u);
557 denlan = u*u*(p6[0]+(p6[1]+(p6[2]+(p6[3]+p6[4]*u)*u)*u)*u)/
558 (q6[0]+(q6[1]+(q6[2]+(q6[3]+q6[4]*u)*u)*u)*u);
560 u = 1/(v-v*Math.log(v)/(v+1));
561 denlan = u*u*(1+(a2[0]+a2[1]*u)*u);
567 JSROOT.Math.Landau =
function(x, mpv, sigma, norm) {
568 if (sigma <= 0)
return 0;
569 var den = JSROOT.Math.landau_pdf((x - mpv) / sigma, 1, 0);
570 if (!norm)
return den;
575 JSROOT.Math.inc_gamma_c =
function(a,x) {
576 return JSROOT.Math.igamc(a,x);
580 JSROOT.Math.inc_gamma =
function(a,x) {
581 return JSROOT.Math.igam(a,x);
584 JSROOT.Math.lgamma =
function(z) {
585 return JSROOT.Math.lgam(z);
589 JSROOT.Math.inc_gamma =
function(a,x) {
590 return JSROOT.Math.igam(a,x);
593 JSROOT.Math.lgamma =
function(z) {
594 return JSROOT.Math.lgam(z);
598 JSROOT.Math.chisquared_cdf_c =
function(x,r,x0) {
599 if (x0===undefined) x0 = 0;
600 return JSROOT.Math.inc_gamma_c ( 0.5 * r , 0.5*(x-x0) );
604 JSROOT.Math.chisquared_cdf =
function(x,r,x0) {
605 if (x0===undefined) x0 = 0;
606 return JSROOT.Math.inc_gamma ( 0.5 * r , 0.5*(x-x0) );
610 JSROOT.Math.chisquared_pdf =
function(x,r,x0) {
611 if (x0===undefined) x0 = 0;
612 if ((x-x0) < 0)
return 0.0;
615 if (x == x0 && a == 0)
return 0.5;
617 return Math.exp ((r/2 - 1) * Math.log((x-x0)/2) - (x-x0)/2 - JSROOT.Math.lgamma(r/2))/2;
621 JSROOT.Math.Prob =
function(chi2, ndf) {
622 if (ndf <= 0)
return 0;
625 if (chi2 < 0)
return 0;
629 return JSROOT.Math.chisquared_cdf_c(chi2,ndf,0);
633 JSROOT.Math.Gaus =
function(x, mean, sigma) {
634 return Math.exp(-0.5 * Math.pow((x-mean) / sigma, 2));
638 JSROOT.Math.BreitWigner =
function(x, mean, gamma) {
639 return gamma/((x-mean)*(x-mean) + gamma*gamma/4) / 2 / Math.PI;
643 JSROOT.Math.gaus =
function(f, x, i) {
645 return f.GetParValue(i+0) * Math.exp(-0.5 * Math.pow((x-f.GetParValue(i+1)) / f.GetParValue(i+2), 2));
649 JSROOT.Math.gausn =
function(f, x, i) {
650 return JSROOT.Math.gaus(f, x, i)/(Math.sqrt(2 * Math.PI) * f.GetParValue(i+2));
654 JSROOT.Math.gausxy =
function(f, x, y, i) {
657 return f.GetParValue(i+0) * Math.exp(-0.5 * Math.pow((x-f.GetParValue(i+1)) / f.GetParValue(i+2), 2))
658 * Math.exp(-0.5 * Math.pow((y-f.GetParValue(i+3)) / f.GetParValue(i+4), 2));
663 JSROOT.Math.expo =
function(f, x, i) {
664 return Math.exp(f.GetParValue(i+0) + f.GetParValue(i+1) * x);
668 JSROOT.Math.landau =
function(f, x, i) {
669 return JSROOT.Math.Landau(x, f.GetParValue(i+1),f.GetParValue(i+2),
false);
673 JSROOT.Math.landaun =
function(f, x, i) {
674 return JSROOT.Math.Landau(x, f.GetParValue(i+1),f.GetParValue(i+2),
true);
679 JSROOT.getMoreMethods =
function(m,
typename, obj) {
682 if (
typename.indexOf(
"ROOT::Math::LorentzVector")===0) {
683 m.Px = m.X =
function() {
return this.fCoordinates.Px(); }
684 m.Py = m.Y =
function() {
return this.fCoordinates.Py(); }
685 m.Pz = m.Z =
function() {
return this.fCoordinates.Pz(); }
686 m.E = m.T =
function() {
return this.fCoordinates.E(); }
687 m.M2 =
function() {
return this.fCoordinates.M2(); }
688 m.M =
function() {
return this.fCoordinates.M(); }
689 m.R = m.P =
function() {
return this.fCoordinates.R(); }
690 m.P2 =
function() {
return this.P() * this.P(); }
691 m.Pt = m.pt =
function() {
return Math.sqrt(this.P2()); }
692 m.Phi = m.phi =
function() {
return Math.atan2(this.fCoordinates.Py(), this.fCoordinates.Px()); }
693 m.Eta = m.eta =
function() {
return Math.atanh(this.Pz()/this.P()); }
696 if (
typename.indexOf(
"ROOT::Math::PxPyPzE4D")===0) {
697 m.Px = m.X =
function() {
return this.fX; }
698 m.Py = m.Y =
function() {
return this.fY; }
699 m.Pz = m.Z =
function() {
return this.fZ; }
700 m.E = m.T =
function() {
return this.fT; }
701 m.P2 =
function() {
return this.fX*this.fX + this.fY*this.fY + this.fZ*this.fZ; }
702 m.R = m.P =
function() {
return Math.sqrt(this.P2()); }
703 m.Mag2 = m.M2 =
function() {
return this.fT*this.fT - this.fX*this.fX - this.fY*this.fY - this.fZ*this.fZ; }
704 m.Mag = m.M =
function() {
706 if (mm >= 0)
return Math.sqrt(mm);
707 return -Math.sqrt(-mm);
709 m.Perp2 = m.Pt2 =
function() {
return this.fX*this.fX + this.fY*this.fY;}
710 m.Pt = m.pt =
function() {
return Math.sqrt(this.P2()); }
711 m.Phi = m.phi =
function() {
return Math.atan2(this.fY, this.fX); }
712 m.Eta = m.eta =
function() {
return Math.atanh(this.Pz/this.P()); }