source: trunk/MagicSoft/Mars/mbase/MMath.cc@ 9157

Last change on this file since 9157 was 8989, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 24.5 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MMath.cc,v 1.41 2008-07-01 14:03:58 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Thomas Bretz 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
21!
22! Copyright: MAGIC Software Development, 2000-2005
23!
24!
25\* ======================================================================== */
26
27/////////////////////////////////////////////////////////////////////////////
28//
29// MMath
30//
31// Mars - Math package (eg Significances, etc)
32//
33/////////////////////////////////////////////////////////////////////////////
34#include "MMath.h"
35
36#ifndef ROOT_TVector2
37#include <TVector2.h>
38#endif
39
40#ifndef ROOT_TVector3
41#include <TVector3.h>
42#endif
43
44#ifndef ROOT_TArrayD
45#include <TArrayD.h>
46#endif
47
48#ifndef ROOT_TComplex
49#include <TComplex.h>
50#endif
51
52//NamespaceImp(MMath);
53
54// --------------------------------------------------------------------------
55//
56// Calculate Significance as
57// significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
58//
59// s: total number of events in signal region
60// b: number of background events in signal region
61//
62Double_t MMath::Significance(Double_t s, Double_t b)
63{
64 const Double_t k = b==0 ? 0 : s/b;
65 const Double_t f = s+k*k*b;
66
67 return f==0 ? 0 : (s-b)/TMath::Sqrt(f);
68}
69
70// --------------------------------------------------------------------------
71//
72// Symmetrized significance - this is somehow analog to
73// SignificanceLiMaSigned
74//
75// Returns Significance(s,b) if s>b otherwise -Significance(b, s);
76//
77Double_t MMath::SignificanceSym(Double_t s, Double_t b)
78{
79 return s>b ? Significance(s, b) : -Significance(b, s);
80}
81
82// --------------------------------------------------------------------------
83//
84// calculates the significance according to Li & Ma
85// ApJ 272 (1983) 317, Formula 17
86//
87// s // s: number of on events
88// b // b: number of off events
89// alpha = t_on/t_off; // t: observation time
90//
91// The significance has the same (positive!) value for s>b and b>s.
92//
93// Returns -1 if s<0 or b<0 or alpha<0 or the argument of sqrt<0
94//
95// Here is some eMail written by Daniel Mazin about the meaning of the arguments:
96//
97// > Ok. Here is my understanding:
98// > According to Li&Ma paper (correctly cited in MMath.cc) alpha is the
99// > scaling factor. The mathematics behind the formula 17 (and/or 9) implies
100// > exactly this. If you scale OFF to ON first (using time or using any other
101// > method), then you cannot use formula 17 (9) anymore. You can just try
102// > the formula before scaling (alpha!=1) and after scaling (alpha=1), you
103// > will see the result will be different.
104//
105// > Here are less mathematical arguments:
106//
107// > 1) the better background determination you have (smaller alpha) the more
108// > significant is your excess, thus your analysis is more sensitive. If you
109// > normalize OFF to ON first, you loose this sensitivity.
110//
111// > 2) the normalization OFF to ON has an error, which naturally depends on
112// > the OFF and ON. This error is propagating to the significance of your
113// > excess if you use the Li&Ma formula 17 correctly. But if you normalize
114// > first and use then alpha=1, the error gets lost completely, you loose
115// > somehow the criteria of goodness of the normalization.
116//
117Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
118{
119 const Double_t sum = s+b;
120
121 if (s<0 || b<0 || alpha<=0)
122 return -1;
123
124 const Double_t l = s==0 ? 0 : s*TMath::Log(s/sum*(alpha+1)/alpha);
125 const Double_t m = b==0 ? 0 : b*TMath::Log(b/sum*(alpha+1) );
126
127 return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
128}
129
130/*
131Double_t MMath::SignificanceLiMaErr(Double_t s, Double_t b, Double_t alpha)
132{
133 Double_t S = SignificanceLiMa(s, b, alpha);
134 if (S<0)
135 return -1;
136
137 const Double_t sum = s+b;
138
139
140 Double_t l = TMath::Log(s/sum*(alpha+1)/alpha)/TMath::Sqrt(2*S);
141 Double_t m = TMath::Log(s/sum*(alpha+1)/alpha)/TMath::Sqrt(2*S);
142
143
144 const Double_t sum = s+b;
145
146 if (s<0 || b<0 || alpha<=0)
147 return -1;
148
149 const Double_t l = s==0 ? 0 : s*TMath::Log(s/sum*(alpha+1)/alpha);
150 const Double_t m = b==0 ? 0 : b*TMath::Log(b/sum*(alpha+1) );
151
152 return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
153}
154*/
155
156// --------------------------------------------------------------------------
157//
158// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
159// calculation has failed. Otherwise the Li/Ma significance which was
160// calculated. If s<b a negative value is returned.
161//
162Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
163{
164 const Double_t sig = SignificanceLiMa(s, b, alpha);
165 if (sig<=0)
166 return 0;
167
168 return TMath::Sign(sig, s-alpha*b);
169}
170
171// --------------------------------------------------------------------------
172//
173// Return Li/Ma (5) for the error of the excess, under the assumption that
174// the existance of a signal is already known. (basically signal/error
175// calculated by error propagation)
176//
177Double_t MMath::SignificanceExc(Double_t s, Double_t b, Double_t alpha)
178{
179 const Double_t error = ErrorExc(s, b, alpha);
180 if (error==0)
181 return 0;
182
183 const Double_t Ns = s - alpha*b;
184
185 return Ns/error;
186}
187
188// --------------------------------------------------------------------------
189//
190// Calculate the error of s-alpha*b by error propagation
191//
192Double_t MMath::ErrorExc(Double_t s, Double_t b, Double_t alpha)
193{
194 const Double_t sN = s + alpha*alpha*b;
195 return sN<0 ? 0 : TMath::Sqrt(sN);
196}
197
198// --------------------------------------------------------------------------
199//
200// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
201//
202Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
203{
204 if (x<mean)
205 return 0;
206
207 static const Double_t sqrt2 = TMath::Sqrt(2.);
208
209 const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
210
211 if (rc<0)
212 return 0;
213 if (rc>1)
214 return 1;
215
216 return rc;
217}
218
219// ------------------------------------------------------------------------
220//
221// Return the "median" (at 68.3%) value of the distribution of
222// abs(a[i]-Median)
223//
224template <class Size, class Element>
225Double_t MMath::MedianDevImp(Size n, const Element *a, Double_t &med)
226{
227 static const Double_t prob = 0.682689477208650697; //MMath::GaussProb(1.0);
228
229 // Sanity check
230 if (n <= 0 || !a)
231 return 0;
232
233 // Get median of distribution
234 med = TMath::Median(n, a);
235
236 // Create the abs(a[i]-med) distribution
237 Double_t arr[n];
238 for (int i=0; i<n; i++)
239 arr[i] = TMath::Abs(a[i]-med);
240
241 //return TMath::Median(n, arr)/0.67449896936; //MMath::GaussProb(x)=0.5
242
243 // Define where to divide (floor because the highest possible is n-1)
244 const Size div = TMath::FloorNint(n*prob);
245
246 // Calculate result
247 Double_t dev = TMath::KOrdStat(n, arr, div);
248 if (n%2 == 0)
249 {
250 dev += TMath::KOrdStat(n, arr, div-1);
251 dev /= 2;
252 }
253
254 return dev;
255}
256
257// ------------------------------------------------------------------------
258//
259// Return the "median" (at 68.3%) value of the distribution of
260// abs(a[i]-Median)
261//
262Double_t MMath::MedianDev(Long64_t n, const Short_t *a, Double_t &med)
263{
264 return MedianDevImp(n, a, med);
265}
266
267// ------------------------------------------------------------------------
268//
269// Return the "median" (at 68.3%) value of the distribution of
270// abs(a[i]-Median)
271//
272Double_t MMath::MedianDev(Long64_t n, const Int_t *a, Double_t &med)
273{
274 return MedianDevImp(n, a, med);
275}
276
277// ------------------------------------------------------------------------
278//
279// Return the "median" (at 68.3%) value of the distribution of
280// abs(a[i]-Median)
281//
282Double_t MMath::MedianDev(Long64_t n, const Float_t *a, Double_t &med)
283{
284 return MedianDevImp(n, a, med);
285}
286
287// ------------------------------------------------------------------------
288//
289// Return the "median" (at 68.3%) value of the distribution of
290// abs(a[i]-Median)
291//
292Double_t MMath::MedianDev(Long64_t n, const Double_t *a, Double_t &med)
293{
294 return MedianDevImp(n, a, med);
295}
296
297// ------------------------------------------------------------------------
298//
299// Return the "median" (at 68.3%) value of the distribution of
300// abs(a[i]-Median)
301//
302Double_t MMath::MedianDev(Long64_t n, const Long_t *a, Double_t &med)
303{
304 return MedianDevImp(n, a, med);
305}
306
307// ------------------------------------------------------------------------
308//
309// Return the "median" (at 68.3%) value of the distribution of
310// abs(a[i]-Median)
311//
312Double_t MMath::MedianDev(Long64_t n, const Long64_t *a, Double_t &med)
313{
314 return MedianDevImp(n, a, med);
315}
316
317Double_t MMath::MedianDev(Long64_t n, const Short_t *a) { Double_t med; return MedianDevImp(n, a, med); }
318Double_t MMath::MedianDev(Long64_t n, const Int_t *a) { Double_t med; return MedianDevImp(n, a, med); }
319Double_t MMath::MedianDev(Long64_t n, const Float_t *a) { Double_t med; return MedianDevImp(n, a, med); }
320Double_t MMath::MedianDev(Long64_t n, const Double_t *a) { Double_t med; return MedianDevImp(n, a, med); }
321Double_t MMath::MedianDev(Long64_t n, const Long_t *a) { Double_t med; return MedianDevImp(n, a, med); }
322Double_t MMath::MedianDev(Long64_t n, const Long64_t *a) { Double_t med; return MedianDevImp(n, a, med); }
323
324// --------------------------------------------------------------------------
325//
326// This function reduces the precision to roughly 0.5% of a Float_t by
327// changing its bit-pattern (Be carefull, in rare cases this function must
328// be adapted to different machines!). This is usefull to enforce better
329// compression by eg. gzip.
330//
331void MMath::ReducePrecision(Float_t &val)
332{
333 UInt_t &f = (UInt_t&)val;
334
335 f += 0x00004000;
336 f &= 0xffff8000;
337}
338
339// -------------------------------------------------------------------------
340//
341// Quadratic interpolation
342//
343// calculate the parameters of a parabula such that
344// y(i) = a + b*x(i) + c*x(i)^2
345//
346// If the determinant==0 an empty TVector3 is returned.
347//
348TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
349{
350 const Double_t x1 = x(0);
351 const Double_t x2 = x(1);
352 const Double_t x3 = x(2);
353
354 const Double_t y1 = y(0);
355 const Double_t y2 = y(1);
356 const Double_t y3 = y(2);
357
358 const double det =
359 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
360 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
361
362
363 if (det==0)
364 return TVector3();
365
366 const double det1 = 1.0/det;
367
368 const double ai11 = x2*x3*x3 - x3*x2*x2;
369 const double ai12 = x3*x1*x1 - x1*x3*x3;
370 const double ai13 = x1*x2*x2 - x2*x1*x1;
371
372 const double ai21 = x2*x2 - x3*x3;
373 const double ai22 = x3*x3 - x1*x1;
374 const double ai23 = x1*x1 - x2*x2;
375
376 const double ai31 = x3 - x2;
377 const double ai32 = x1 - x3;
378 const double ai33 = x2 - x1;
379
380 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
381 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
382 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
383}
384
385// --------------------------------------------------------------------------
386//
387// Interpolate the points with x-coordinates vx and y-coordinates vy
388// by a parabola (second order polynomial) and return the value at x.
389//
390Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
391{
392 const TVector3 c = GetParab(vx, vy);
393 return c(0) + c(1)*x + c(2)*x*x;
394}
395
396// --------------------------------------------------------------------------
397//
398// Interpolate the points with x-coordinates vx=(-1,0,1) and
399// y-coordinates vy by a parabola (second order polynomial) and return
400// the value at x.
401//
402Double_t MMath::InterpolParabLin(const TVector3 &vy, Double_t x)
403{
404 const TVector3 c(vy(1), (vy(2)-vy(0))/2, vy(0)/2 - vy(1) + vy(2)/2);
405 return c(0) + c(1)*x + c(2)*x*x;
406}
407
408Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
409{
410 const Double_t l0 = TMath::Log10(vx(0));
411 const Double_t l1 = TMath::Log10(vx(1));
412 const Double_t l2 = TMath::Log10(vx(2));
413
414 const TVector3 vx0(l0, l1, l2);
415 return InterpolParabLin(vx0, vy, TMath::Log10(x));
416}
417
418Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
419{
420 const Double_t l0 = TMath::Cos(vx(0));
421 const Double_t l1 = TMath::Cos(vx(1));
422 const Double_t l2 = TMath::Cos(vx(2));
423
424 const TVector3 vx0(l0, l1, l2);
425 return InterpolParabLin(vx0, vy, TMath::Cos(x));
426}
427
428// --------------------------------------------------------------------------
429//
430// Analytically calculated result of a least square fit of:
431// y = A*e^(B*x)
432// Equal weights
433//
434// It returns TArrayD(2) = { A, B };
435//
436// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
437//
438TArrayD MMath::LeastSqFitExpW1(Int_t n, Double_t *x, Double_t *y)
439{
440 Double_t sumxsqy = 0;
441 Double_t sumylny = 0;
442 Double_t sumxy = 0;
443 Double_t sumy = 0;
444 Double_t sumxylny = 0;
445 for (int i=0; i<n; i++)
446 {
447 sumylny += y[i]*TMath::Log(y[i]);
448 sumxy += x[i]*y[i];
449 sumxsqy += x[i]*x[i]*y[i];
450 sumxylny += x[i]*y[i]*TMath::Log(y[i]);
451 sumy += y[i];
452 }
453
454 const Double_t dev = sumy*sumxsqy - sumxy*sumxy;
455
456 const Double_t a = (sumxsqy*sumylny - sumxy*sumxylny)/dev;
457 const Double_t b = (sumy*sumxylny - sumxy*sumylny)/dev;
458
459 TArrayD rc(2);
460 rc[0] = TMath::Exp(a);
461 rc[1] = b;
462 return rc;
463}
464
465// --------------------------------------------------------------------------
466//
467// Analytically calculated result of a least square fit of:
468// y = A*e^(B*x)
469// Greater weights to smaller values
470//
471// It returns TArrayD(2) = { A, B };
472//
473// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
474//
475TArrayD MMath::LeastSqFitExp(Int_t n, Double_t *x, Double_t *y)
476{
477 // -------- Greater weights to smaller values ---------
478 Double_t sumlny = 0;
479 Double_t sumxlny = 0;
480 Double_t sumxsq = 0;
481 Double_t sumx = 0;
482 for (int i=0; i<n; i++)
483 {
484 sumlny += TMath::Log(y[i]);
485 sumxlny += x[i]*TMath::Log(y[i]);
486
487 sumxsq += x[i]*x[i];
488 sumx += x[i];
489 }
490
491 const Double_t dev = n*sumxsq-sumx*sumx;
492
493 const Double_t a = (sumlny*sumxsq - sumx*sumxlny)/dev;
494 const Double_t b = (n*sumxlny - sumx*sumlny)/dev;
495
496 TArrayD rc(2);
497 rc[0] = TMath::Exp(a);
498 rc[1] = b;
499 return rc;
500}
501
502// --------------------------------------------------------------------------
503//
504// Analytically calculated result of a least square fit of:
505// y = A+B*ln(x)
506//
507// It returns TArrayD(2) = { A, B };
508//
509// see: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
510//
511TArrayD MMath::LeastSqFitLog(Int_t n, Double_t *x, Double_t *y)
512{
513 Double_t sumylnx = 0;
514 Double_t sumy = 0;
515 Double_t sumlnx = 0;
516 Double_t sumlnxsq = 0;
517 for (int i=0; i<n; i++)
518 {
519 sumylnx += y[i]*TMath::Log(x[i]);
520 sumy += y[i];
521 sumlnx += TMath::Log(x[i]);
522 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
523 }
524
525 const Double_t b = (n*sumylnx-sumy*sumlnx)/(n*sumlnxsq-sumlnx*sumlnx);
526 const Double_t a = (sumy-b*sumlnx)/n;
527
528 TArrayD rc(2);
529 rc[0] = a;
530 rc[1] = b;
531 return rc;
532}
533
534// --------------------------------------------------------------------------
535//
536// Analytically calculated result of a least square fit of:
537// y = A*x^B
538//
539// It returns TArrayD(2) = { A, B };
540//
541// see: http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
542//
543TArrayD MMath::LeastSqFitPowerLaw(Int_t n, Double_t *x, Double_t *y)
544{
545 Double_t sumlnxlny = 0;
546 Double_t sumlnx = 0;
547 Double_t sumlny = 0;
548 Double_t sumlnxsq = 0;
549 for (int i=0; i<n; i++)
550 {
551 sumlnxlny += TMath::Log(x[i])*TMath::Log(y[i]);
552 sumlnx += TMath::Log(x[i]);
553 sumlny += TMath::Log(y[i]);
554 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
555 }
556
557 const Double_t b = (n*sumlnxlny-sumlnx*sumlny)/(n*sumlnxsq-sumlnx*sumlnx);
558 const Double_t a = (sumlny-b*sumlnx)/n;
559
560 TArrayD rc(2);
561 rc[0] = TMath::Exp(a);
562 rc[1] = b;
563 return rc;
564}
565
566// --------------------------------------------------------------------------
567//
568// Calculate the intersection of two lines defined by (x1;y1) and (x2;x2)
569// Returns the intersection point.
570//
571// It is assumed that the lines intersect. If there is no intersection
572// TVector2() is returned (which is not destinguishable from
573// TVector2(0,0) if the intersection is at the coordinate source)
574//
575// Formula from: http://mathworld.wolfram.com/Line-LineIntersection.html
576//
577TVector2 MMath::GetIntersectionPoint(const TVector2 &x1, const TVector2 &y1, const TVector2 &x2, const TVector2 &y2)
578{
579 TMatrix d(2,2);
580 d[0][0] = x1.X()-y1.X();
581 d[0][1] = x2.X()-y2.X();
582 d[1][0] = x1.Y()-y1.Y();
583 d[1][1] = x2.Y()-y2.Y();
584
585 const Double_t denom = d.Determinant();
586 if (denom==0)
587 return TVector2();
588
589 TMatrix l1(2,2);
590 TMatrix l2(2,2);
591
592 l1[0][0] = x1.X();
593 l1[0][1] = y1.X();
594 l2[0][0] = x2.X();
595 l2[0][1] = y2.X();
596
597 l1[1][0] = x1.Y();
598 l1[1][1] = y1.Y();
599 l2[1][0] = x2.Y();
600 l2[1][1] = y2.Y();
601
602 TMatrix a(2,2);
603 a[0][0] = l1.Determinant();
604 a[0][1] = l2.Determinant();
605 a[1][0] = x1.X()-y1.X();
606 a[1][1] = x2.X()-y2.X();
607
608 const Double_t X = a.Determinant()/denom;
609
610 a[1][0] = x1.Y()-y1.Y();
611 a[1][1] = x2.Y()-y2.Y();
612
613 const Double_t Y = a.Determinant()/denom;
614
615 return TVector2(X, Y);
616}
617
618// --------------------------------------------------------------------------
619//
620// Solves: x^2 + ax + b = 0;
621// Return number of solutions returned as x1, x2
622//
623Int_t MMath::SolvePol2(Double_t a, Double_t b, Double_t &x1, Double_t &x2)
624{
625 const Double_t r = a*a - 4*b;
626 if (r<0)
627 return 0;
628
629 if (r==0)
630 {
631 x1 = x2 = -a/2;
632 return 1;
633 }
634
635 const Double_t s = TMath::Sqrt(r);
636
637 x1 = (-a+s)/2;
638 x2 = (-a-s)/2;
639
640 return 2;
641}
642
643// --------------------------------------------------------------------------
644//
645// This is a helper function making the execution of SolverPol3 a bit faster
646//
647static inline Double_t ReMul(const TComplex &c1, const TComplex &th)
648{
649 const TComplex c2 = TComplex::Cos(th/3.);
650 return c1.Re() * c2.Re() - c1.Im() * c2.Im();
651}
652
653// --------------------------------------------------------------------------
654//
655// Solves: x^3 + ax^2 + bx + c = 0;
656// Return number of the real solutions, returned as z1, z2, z3
657//
658// Algorithm adapted from http://home.att.net/~srschmitt/cubizen.heml
659// Which is based on the solution given in
660// http://mathworld.wolfram.com/CubicEquation.html
661//
662// -------------------------------------------------------------------------
663//
664// Exact solutions of cubic polynomial equations
665// by Stephen R. Schmitt Algorithm
666//
667// An exact solution of the cubic polynomial equation:
668//
669// x^3 + a*x^2 + b*x + c = 0
670//
671// was first published by Gerolamo Cardano (1501-1576) in his treatise,
672// Ars Magna. He did not discoverer of the solution; a professor of
673// mathematics at the University of Bologna named Scipione del Ferro (ca.
674// 1465-1526) is credited as the first to find an exact solution. In the
675// years since, several improvements to the original solution have been
676// discovered. Zeno source code
677//
678// http://home.att.net/~srschmitt/cubizen.html
679//
680// % compute real or complex roots of cubic polynomial
681// function cubic( var z1, z2, z3 : real, a, b, c : real ) : real
682//
683// var Q, R, D, S, T : real
684// var im, th : real
685//
686// Q := (3*b - a^2)/9
687// R := (9*b*a - 27*c - 2*a^3)/54
688// D := Q^3 + R^2 % polynomial discriminant
689//
690// if (D >= 0) then % complex or duplicate roots
691//
692// S := sgn(R + sqrt(D))*abs(R + sqrt(D))^(1/3)
693// T := sgn(R - sqrt(D))*abs(R - sqrt(D))^(1/3)
694//
695// z1 := -a/3 + (S + T) % real root
696// z2 := -a/3 - (S + T)/2 % real part of complex root
697// z3 := -a/3 - (S + T)/2 % real part of complex root
698// im := abs(sqrt(3)*(S - T)/2) % complex part of root pair
699//
700// else % distinct real roots
701//
702// th := arccos(R/sqrt( -Q^3))
703//
704// z1 := 2*sqrt(-Q)*cos(th/3) - a/3
705// z2 := 2*sqrt(-Q)*cos((th + 2*pi)/3) - a/3
706// z3 := 2*sqrt(-Q)*cos((th + 4*pi)/3) - a/3
707// im := 0
708//
709// end if
710//
711// return im % imaginary part
712//
713// end function
714//
715// see also http://en.wikipedia.org/wiki/Cubic_equation
716//
717Int_t MMath::SolvePol3(Double_t a, Double_t b, Double_t c,
718 Double_t &x1, Double_t &x2, Double_t &x3)
719{
720 // Double_t coeff[4] = { 1, a, b, c };
721 // return TMath::RootsCubic(coeff, x1, x2, x3) ? 1 : 3;
722
723 const Double_t Q = (a*a - 3*b)/9;
724 const Double_t R = (9*b*a - 27*c - 2*a*a*a)/54;
725 const Double_t D = R*R - Q*Q*Q; // polynomial discriminant
726
727 // ----- The single-real / duplicate-roots solution -----
728
729 // D<0: three real roots
730 // D>0: one real root
731 // D==0: maximum two real roots (two identical roots)
732
733 // R==0: only one unique root
734 // R!=0: two roots
735
736 if (D==0)
737 {
738 const Double_t r = MMath::Sqrt3(R);
739
740 x1 = r - a/3.; // real root
741 if (R==0)
742 return 1;
743
744 x2 = 2*r - a/3.; // real root
745 return 2;
746 }
747
748 if (D>0) // complex or duplicate roots
749 {
750 const Double_t sqrtd = TMath::Sqrt(D);
751
752 const Double_t A = TMath::Sign(1., R)*MMath::Sqrt3(TMath::Abs(R)+sqrtd);
753
754 // The case A==0 cannot happen. This would imply D==0
755 // if (A==0)
756 // {
757 // x1 = -a/3;
758 // return 1;
759 // }
760
761 x1 = (A+Q/A)-a/3;
762
763 //const Double_t S = MMath::Sqrt3(R + sqrtd);
764 //const Double_t T = MMath::Sqrt3(R - sqrtd);
765 //x1 = (S+T) - a/3.; // real root
766
767 return 1;
768
769 //z2 = (S + T)/2 - a/3.; // real part of complex root
770 //z3 = (S + T)/2 - a/3.; // real part of complex root
771 //im = fabs(sqrt(3)*(S - T)/2) // complex part of root pair
772 }
773
774 // ----- The general solution with three roots ---
775
776 if (Q==0)
777 return 0;
778
779 if (Q>0) // This is here for speed reasons
780 {
781 const Double_t sqrtq = TMath::Sqrt(Q);
782 const Double_t rq = R/TMath::Abs(Q);
783
784 const Double_t t = TMath::ACos(rq/sqrtq)/3;
785
786 static const Double_t sqrt3 = TMath::Sqrt(3.);
787
788 const Double_t sn = TMath::Sin(t)*sqrt3;
789 const Double_t cs = TMath::Cos(t);
790
791 x1 = 2*sqrtq * cs - a/3;
792 x2 = -sqrtq * (sn + cs) - a/3;
793 x3 = sqrtq * (sn - cs) - a/3;
794
795 /* --- Easier to understand but slower ---
796 const Double_t th1 = TMath::ACos(rq/sqrtq);
797 const Double_t th2 = th1 + TMath::TwoPi();
798 const Double_t th3 = th2 + TMath::TwoPi();
799
800 x1 = 2.*sqrtq * TMath::Cos(th1/3.) - a/3.;
801 x2 = 2.*sqrtq * TMath::Cos(th2/3.) - a/3.;
802 x3 = 2.*sqrtq * TMath::Cos(th3/3.) - a/3.;
803 */
804 return 3;
805 }
806
807 const TComplex sqrtq = TComplex::Sqrt(Q);
808 const Double_t rq = R/TMath::Abs(Q);
809
810 const TComplex th1 = TComplex::ACos(rq/sqrtq);
811 const TComplex th2 = th1 + TMath::TwoPi();
812 const TComplex th3 = th2 + TMath::TwoPi();
813
814 // For ReMul, see bove
815 x1 = ReMul(2.*sqrtq, th1) - a/3.;
816 x2 = ReMul(2.*sqrtq, th2) - a/3.;
817 x3 = ReMul(2.*sqrtq, th3) - a/3.;
818
819 return 3;
820}
821
822// --------------------------------------------------------------------------
823//
824// Format a value and its error corresponding to the rules (note
825// this won't work if the error is more then eight orders smaller than
826// the value)
827//
828void MMath::Format(Double_t &v, Double_t &e)
829{
830 // Valid digits
831 Int_t i = TMath::FloorNint(TMath::Log10(v))-TMath::FloorNint(TMath::Log10(e));
832
833 // Check if error starts with 1 or 2. In this case use one
834 // more valid digit
835 TString error = Form("%.0e", e);
836 if (error[0]=='1' || error[0]=='2')
837 {
838 i++;
839 error = Form("%.1e", e);
840 }
841
842 const char *fmt = Form("%%.%de", i);
843
844 v = atof(Form(fmt, v));
845 e = error.Atof();
846}
Note: See TracBrowser for help on using the repository browser.