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

Last change on this file since 8563 was 8563, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 21.8 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MMath.cc,v 1.33 2007-06-16 21:50:26 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// --------------------------------------------------------------------------
131//
132// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
133// calculation has failed. Otherwise the Li/Ma significance which was
134// calculated. If s<b a negative value is returned.
135//
136Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
137{
138 const Double_t sig = SignificanceLiMa(s, b, alpha);
139 if (sig<=0)
140 return 0;
141
142 return TMath::Sign(sig, s-alpha*b);
143}
144
145// --------------------------------------------------------------------------
146//
147// Return Li/Ma (5) for the error of the excess, under the assumption that
148// the existance of a signal is already known.
149//
150Double_t MMath::SignificanceLiMaExc(Double_t s, Double_t b, Double_t alpha)
151{
152 Double_t Ns = s - alpha*b;
153 Double_t sN = s + alpha*alpha*b;
154
155 return Ns<0 || sN<0 ? 0 : Ns/TMath::Sqrt(sN);
156}
157
158// --------------------------------------------------------------------------
159//
160// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
161//
162Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
163{
164 if (x<mean)
165 return 0;
166
167 static const Double_t sqrt2 = TMath::Sqrt(2.);
168
169 const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
170
171 if (rc<0)
172 return 0;
173 if (rc>1)
174 return 1;
175
176 return rc;
177}
178
179// ------------------------------------------------------------------------
180//
181// Return the "median" (at 68.3%) value of the distribution of
182// abs(a[i]-Median)
183//
184template <class Size, class Element>
185Double_t MMath::MedianDevImp(Size n, const Element *a, Double_t &med)
186{
187 static const Double_t prob = 0.682689477208650697; //MMath::GaussProb(1.0);
188
189 // Sanity check
190 if (n <= 0 || !a)
191 return 0;
192
193 // Get median of distribution
194 med = TMath::Median(n, a);
195
196 // Create the abs(a[i]-med) distribution
197 Double_t arr[n];
198 for (int i=0; i<n; i++)
199 arr[i] = TMath::Abs(a[i]-med);
200
201 // Define where to divide (floor because the highest possible is n-1)
202 const Int_t div = TMath::FloorNint(n*prob);
203
204 // Calculate result
205 Double_t dev = TMath::KOrdStat(n, arr, div);
206 if (n%2 == 0)
207 {
208 dev += TMath::KOrdStat(n, arr, div-1);
209 dev /= 2;
210 }
211
212 return dev;
213}
214
215// ------------------------------------------------------------------------
216//
217// Return the "median" (at 68.3%) value of the distribution of
218// abs(a[i]-Median)
219//
220Double_t MMath::MedianDev(Long64_t n, const Short_t *a, Double_t &med)
221{
222 return MedianDevImp(n, a, med);
223}
224
225// ------------------------------------------------------------------------
226//
227// Return the "median" (at 68.3%) value of the distribution of
228// abs(a[i]-Median)
229//
230Double_t MMath::MedianDev(Long64_t n, const Int_t *a, Double_t &med)
231{
232 return MedianDevImp(n, a, med);
233}
234
235// ------------------------------------------------------------------------
236//
237// Return the "median" (at 68.3%) value of the distribution of
238// abs(a[i]-Median)
239//
240Double_t MMath::MedianDev(Long64_t n, const Float_t *a, Double_t &med)
241{
242 return MedianDevImp(n, a, med);
243}
244
245// ------------------------------------------------------------------------
246//
247// Return the "median" (at 68.3%) value of the distribution of
248// abs(a[i]-Median)
249//
250Double_t MMath::MedianDev(Long64_t n, const Double_t *a, Double_t &med)
251{
252 return MedianDevImp(n, a, med);
253}
254
255// ------------------------------------------------------------------------
256//
257// Return the "median" (at 68.3%) value of the distribution of
258// abs(a[i]-Median)
259//
260Double_t MMath::MedianDev(Long64_t n, const Long_t *a, Double_t &med)
261{
262 return MedianDevImp(n, a, med);
263}
264
265// ------------------------------------------------------------------------
266//
267// Return the "median" (at 68.3%) value of the distribution of
268// abs(a[i]-Median)
269//
270Double_t MMath::MedianDev(Long64_t n, const Long64_t *a, Double_t &med)
271{
272 return MedianDevImp(n, a, med);
273}
274
275Double_t MMath::MedianDev(Long64_t n, const Short_t *a) { Double_t med; return MedianDevImp(n, a, med); }
276Double_t MMath::MedianDev(Long64_t n, const Int_t *a) { Double_t med; return MedianDevImp(n, a, med); }
277Double_t MMath::MedianDev(Long64_t n, const Float_t *a) { Double_t med; return MedianDevImp(n, a, med); }
278Double_t MMath::MedianDev(Long64_t n, const Double_t *a) { Double_t med; return MedianDevImp(n, a, med); }
279Double_t MMath::MedianDev(Long64_t n, const Long_t *a) { Double_t med; return MedianDevImp(n, a, med); }
280Double_t MMath::MedianDev(Long64_t n, const Long64_t *a) { Double_t med; return MedianDevImp(n, a, med); }
281
282// --------------------------------------------------------------------------
283//
284// This function reduces the precision to roughly 0.5% of a Float_t by
285// changing its bit-pattern (Be carefull, in rare cases this function must
286// be adapted to different machines!). This is usefull to enforce better
287// compression by eg. gzip.
288//
289void MMath::ReducePrecision(Float_t &val)
290{
291 UInt_t &f = (UInt_t&)val;
292
293 f += 0x00004000;
294 f &= 0xffff8000;
295}
296
297// -------------------------------------------------------------------------
298//
299// Quadratic interpolation
300//
301// calculate the parameters of a parabula such that
302// y(i) = a + b*x(i) + c*x(i)^2
303//
304// If the determinant==0 an empty TVector3 is returned.
305//
306TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
307{
308 Double_t x1 = x(0);
309 Double_t x2 = x(1);
310 Double_t x3 = x(2);
311
312 Double_t y1 = y(0);
313 Double_t y2 = y(1);
314 Double_t y3 = y(2);
315
316 const double det =
317 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
318 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
319
320
321 if (det==0)
322 return TVector3();
323
324 const double det1 = 1.0/det;
325
326 const double ai11 = x2*x3*x3 - x3*x2*x2;
327 const double ai12 = x3*x1*x1 - x1*x3*x3;
328 const double ai13 = x1*x2*x2 - x2*x1*x1;
329
330 const double ai21 = x2*x2 - x3*x3;
331 const double ai22 = x3*x3 - x1*x1;
332 const double ai23 = x1*x1 - x2*x2;
333
334 const double ai31 = x3 - x2;
335 const double ai32 = x1 - x3;
336 const double ai33 = x2 - x1;
337
338 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
339 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
340 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
341}
342
343Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
344{
345 const TVector3 c = GetParab(vx, vy);
346 return c(0) + c(1)*x + c(2)*x*x;
347}
348
349Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
350{
351 const Double_t l0 = TMath::Log10(vx(0));
352 const Double_t l1 = TMath::Log10(vx(1));
353 const Double_t l2 = TMath::Log10(vx(2));
354
355 const TVector3 vx0(l0, l1, l2);
356 return InterpolParabLin(vx0, vy, TMath::Log10(x));
357}
358
359Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
360{
361 const Double_t l0 = TMath::Cos(vx(0));
362 const Double_t l1 = TMath::Cos(vx(1));
363 const Double_t l2 = TMath::Cos(vx(2));
364
365 const TVector3 vx0(l0, l1, l2);
366 return InterpolParabLin(vx0, vy, TMath::Cos(x));
367}
368
369// --------------------------------------------------------------------------
370//
371// Analytically calculated result of a least square fit of:
372// y = A*e^(B*x)
373// Equal weights
374//
375// It returns TArrayD(2) = { A, B };
376//
377// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
378//
379TArrayD MMath::LeastSqFitExpW1(Int_t n, Double_t *x, Double_t *y)
380{
381 Double_t sumxsqy = 0;
382 Double_t sumylny = 0;
383 Double_t sumxy = 0;
384 Double_t sumy = 0;
385 Double_t sumxylny = 0;
386 for (int i=0; i<n; i++)
387 {
388 sumylny += y[i]*TMath::Log(y[i]);
389 sumxy += x[i]*y[i];
390 sumxsqy += x[i]*x[i]*y[i];
391 sumxylny += x[i]*y[i]*TMath::Log(y[i]);
392 sumy += y[i];
393 }
394
395 const Double_t dev = sumy*sumxsqy - sumxy*sumxy;
396
397 const Double_t a = (sumxsqy*sumylny - sumxy*sumxylny)/dev;
398 const Double_t b = (sumy*sumxylny - sumxy*sumylny)/dev;
399
400 TArrayD rc(2);
401 rc[0] = TMath::Exp(a);
402 rc[1] = b;
403 return rc;
404}
405
406// --------------------------------------------------------------------------
407//
408// Analytically calculated result of a least square fit of:
409// y = A*e^(B*x)
410// Greater weights to smaller values
411//
412// It returns TArrayD(2) = { A, B };
413//
414// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
415//
416TArrayD MMath::LeastSqFitExp(Int_t n, Double_t *x, Double_t *y)
417{
418 // -------- Greater weights to smaller values ---------
419 Double_t sumlny = 0;
420 Double_t sumxlny = 0;
421 Double_t sumxsq = 0;
422 Double_t sumx = 0;
423 for (int i=0; i<n; i++)
424 {
425 sumlny += TMath::Log(y[i]);
426 sumxlny += x[i]*TMath::Log(y[i]);
427
428 sumxsq += x[i]*x[i];
429 sumx += x[i];
430 }
431
432 const Double_t dev = n*sumxsq-sumx*sumx;
433
434 const Double_t a = (sumlny*sumxsq - sumx*sumxlny)/dev;
435 const Double_t b = (n*sumxlny - sumx*sumlny)/dev;
436
437 TArrayD rc(2);
438 rc[0] = TMath::Exp(a);
439 rc[1] = b;
440 return rc;
441}
442
443// --------------------------------------------------------------------------
444//
445// Analytically calculated result of a least square fit of:
446// y = A+B*ln(x)
447//
448// It returns TArrayD(2) = { A, B };
449//
450// see: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
451//
452TArrayD MMath::LeastSqFitLog(Int_t n, Double_t *x, Double_t *y)
453{
454 Double_t sumylnx = 0;
455 Double_t sumy = 0;
456 Double_t sumlnx = 0;
457 Double_t sumlnxsq = 0;
458 for (int i=0; i<n; i++)
459 {
460 sumylnx += y[i]*TMath::Log(x[i]);
461 sumy += y[i];
462 sumlnx += TMath::Log(x[i]);
463 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
464 }
465
466 const Double_t b = (n*sumylnx-sumy*sumlnx)/(n*sumlnxsq-sumlnx*sumlnx);
467 const Double_t a = (sumy-b*sumlnx)/n;
468
469 TArrayD rc(2);
470 rc[0] = a;
471 rc[1] = b;
472 return rc;
473}
474
475// --------------------------------------------------------------------------
476//
477// Analytically calculated result of a least square fit of:
478// y = A*x^B
479//
480// It returns TArrayD(2) = { A, B };
481//
482// see: http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
483//
484TArrayD MMath::LeastSqFitPowerLaw(Int_t n, Double_t *x, Double_t *y)
485{
486 Double_t sumlnxlny = 0;
487 Double_t sumlnx = 0;
488 Double_t sumlny = 0;
489 Double_t sumlnxsq = 0;
490 for (int i=0; i<n; i++)
491 {
492 sumlnxlny += TMath::Log(x[i])*TMath::Log(y[i]);
493 sumlnx += TMath::Log(x[i]);
494 sumlny += TMath::Log(y[i]);
495 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
496 }
497
498 const Double_t b = (n*sumlnxlny-sumlnx*sumlny)/(n*sumlnxsq-sumlnx*sumlnx);
499 const Double_t a = (sumlny-b*sumlnx)/n;
500
501 TArrayD rc(2);
502 rc[0] = TMath::Exp(a);
503 rc[1] = b;
504 return rc;
505}
506
507// --------------------------------------------------------------------------
508//
509// Calculate the intersection of two lines defined by (x1;y1) and (x2;x2)
510// Returns the intersection point.
511//
512// It is assumed that the lines intersect. If there is no intersection
513// TVector2() is returned (which is not destinguishable from
514// TVector2(0,0) if the intersection is at the coordinate source)
515//
516// Formula from: http://mathworld.wolfram.com/Line-LineIntersection.html
517//
518TVector2 MMath::GetIntersectionPoint(const TVector2 &x1, const TVector2 &y1, const TVector2 &x2, const TVector2 &y2)
519{
520 TMatrix d(2,2);
521 d[0][0] = x1.X()-y1.X();
522 d[0][1] = x2.X()-y2.X();
523 d[1][0] = x1.Y()-y1.Y();
524 d[1][1] = x2.Y()-y2.Y();
525
526 const Double_t denom = d.Determinant();
527 if (denom==0)
528 return TVector2();
529
530 TMatrix l1(2,2);
531 TMatrix l2(2,2);
532
533 l1[0][0] = x1.X();
534 l1[0][1] = y1.X();
535 l2[0][0] = x2.X();
536 l2[0][1] = y2.X();
537
538 l1[1][0] = x1.Y();
539 l1[1][1] = y1.Y();
540 l2[1][0] = x2.Y();
541 l2[1][1] = y2.Y();
542
543 TMatrix a(2,2);
544 a[0][0] = l1.Determinant();
545 a[0][1] = l2.Determinant();
546 a[1][0] = x1.X()-y1.X();
547 a[1][1] = x2.X()-y2.X();
548
549 const Double_t X = a.Determinant()/denom;
550
551 a[1][0] = x1.Y()-y1.Y();
552 a[1][1] = x2.Y()-y2.Y();
553
554 const Double_t Y = a.Determinant()/denom;
555
556 return TVector2(X, Y);
557}
558
559// --------------------------------------------------------------------------
560//
561// Solves: x^2 + ax + b = 0;
562// Return number of solutions returned as x1, x2
563//
564Int_t MMath::SolvePol2(Double_t a, Double_t b, Double_t &x1, Double_t &x2)
565{
566 const Double_t r = a*a - 4*b;
567 if (r<0)
568 return 0;
569
570 if (r==0)
571 {
572 x1 = x2 = -a/2;
573 return 1;
574 }
575
576 const Double_t s = TMath::Sqrt(r);
577
578 x1 = (-a+s)/2;
579 x2 = (-a-s)/2;
580
581 return 2;
582}
583
584// --------------------------------------------------------------------------
585//
586// This is a helper function making the execution of SolverPol3 a bit faster
587//
588static inline Double_t ReMul(const TComplex &c1, const TComplex &th)
589{
590 const TComplex c2 = TComplex::Cos(th/3.);
591 return c1.Re() * c2.Re() - c1.Im() * c2.Im();
592}
593
594// --------------------------------------------------------------------------
595//
596// Solves: x^3 + ax^2 + bx + c = 0;
597// Return number of the real solutions, returned as z1, z2, z3
598//
599// Algorithm adapted from http://home.att.net/~srschmitt/cubizen.heml
600// Which is based on the solution given in
601// http://mathworld.wolfram.com/CubicEquation.html
602//
603// -------------------------------------------------------------------------
604//
605// Exact solutions of cubic polynomial equations
606// by Stephen R. Schmitt Algorithm
607//
608// An exact solution of the cubic polynomial equation:
609//
610// x^3 + a*x^2 + b*x + c = 0
611//
612// was first published by Gerolamo Cardano (1501-1576) in his treatise,
613// Ars Magna. He did not discoverer of the solution; a professor of
614// mathematics at the University of Bologna named Scipione del Ferro (ca.
615// 1465-1526) is credited as the first to find an exact solution. In the
616// years since, several improvements to the original solution have been
617// discovered. Zeno source code
618//
619// http://home.att.net/~srschmitt/cubizen.html
620//
621// % compute real or complex roots of cubic polynomial
622// function cubic( var z1, z2, z3 : real, a, b, c : real ) : real
623//
624// var Q, R, D, S, T : real
625// var im, th : real
626//
627// Q := (3*b - a^2)/9
628// R := (9*b*a - 27*c - 2*a^3)/54
629// D := Q^3 + R^2 % polynomial discriminant
630//
631// if (D >= 0) then % complex or duplicate roots
632//
633// S := sgn(R + sqrt(D))*abs(R + sqrt(D))^(1/3)
634// T := sgn(R - sqrt(D))*abs(R - sqrt(D))^(1/3)
635//
636// z1 := -a/3 + (S + T) % real root
637// z2 := -a/3 - (S + T)/2 % real part of complex root
638// z3 := -a/3 - (S + T)/2 % real part of complex root
639// im := abs(sqrt(3)*(S - T)/2) % complex part of root pair
640//
641// else % distinct real roots
642//
643// th := arccos(R/sqrt( -Q^3))
644//
645// z1 := 2*sqrt(-Q)*cos(th/3) - a/3
646// z2 := 2*sqrt(-Q)*cos((th + 2*pi)/3) - a/3
647// z3 := 2*sqrt(-Q)*cos((th + 4*pi)/3) - a/3
648// im := 0
649//
650// end if
651//
652// return im % imaginary part
653//
654// end function
655//
656// see also http://en.wikipedia.org/wiki/Cubic_equation
657//
658Int_t MMath::SolvePol3(Double_t a, Double_t b, Double_t c,
659 Double_t &x1, Double_t &x2, Double_t &x3)
660{
661 // Double_t coeff[4] = { 1, a, b, c };
662 // return TMath::RootsCubic(coeff, x1, x2, x3) ? 1 : 3;
663
664 const Double_t Q = (a*a - 3*b)/9;
665 const Double_t R = (9*b*a - 27*c - 2*a*a*a)/54;
666 const Double_t D = R*R - Q*Q*Q; // polynomial discriminant
667
668 // ----- The single-real / duplicate-roots solution -----
669
670 // D<0: three real roots
671 // D>0: one real root
672 // D==0: maximum two real roots (two identical roots)
673
674 // R==0: only one unique root
675 // R!=0: two roots
676
677 if (D==0)
678 {
679 const Double_t r = MMath::Sqrt3(R);
680
681 x1 = r - a/3.; // real root
682 if (R==0)
683 return 1;
684
685 x2 = 2*r - a/3.; // real root
686 return 2;
687 }
688
689 if (D>0) // complex or duplicate roots
690 {
691 const Double_t sqrtd = TMath::Sqrt(D);
692
693 const Double_t S = MMath::Sqrt3(R + sqrtd);
694 const Double_t T = MMath::Sqrt3(R - sqrtd);
695
696 x1 = (S+T) - a/3.; // real root
697
698 return 1;
699
700 //z2 = (S + T)/2 - a/3.; // real part of complex root
701 //z3 = (S + T)/2 - a/3.; // real part of complex root
702 //im = fabs(sqrt(3)*(S - T)/2) // complex part of root pair
703 }
704
705 // ----- The general solution with three roots ---
706
707 if (Q==0)
708 return 0;
709
710 if (Q>0) // This is here for speed reasons
711 {
712 const Double_t sqrtq = TMath::Sqrt(Q);
713 const Double_t rq = R/TMath::Abs(Q);
714
715 const Double_t t = TMath::ACos(rq/sqrtq)/3;
716
717 static const Double_t sqrt3 = TMath::Sqrt(3.);
718
719 const Double_t s = TMath::Sin(t)*sqrt3;
720 const Double_t c = TMath::Cos(t);
721
722 x1 = 2*sqrtq * c - a/3;
723 x2 = -sqrtq * (s + c) - a/3;
724 x3 = sqrtq * (s - c) - a/3;
725
726 /* --- Easier to understand but slower ---
727 const Double_t th1 = TMath::ACos(rq/sqrtq);
728 const Double_t th2 = th1 + TMath::TwoPi();
729 const Double_t th3 = th2 + TMath::TwoPi();
730
731 x1 = 2.*sqrtq * TMath::Cos(th1/3.) - a/3.;
732 x2 = 2.*sqrtq * TMath::Cos(th2/3.) - a/3.;
733 x3 = 2.*sqrtq * TMath::Cos(th3/3.) - a/3.;
734 */
735 return 3;
736 }
737
738 const TComplex sqrtq = TComplex::Sqrt(Q);
739 const Double_t rq = R/TMath::Abs(Q);
740
741 const TComplex th1 = TComplex::ACos(rq/sqrtq);
742 const TComplex th2 = th1 + TMath::TwoPi();
743 const TComplex th3 = th2 + TMath::TwoPi();
744
745 // For ReMul, see bove
746 x1 = ReMul(2.*sqrtq, th1) - a/3.;
747 x2 = ReMul(2.*sqrtq, th2) - a/3.;
748 x3 = ReMul(2.*sqrtq, th3) - a/3.;
749
750 return 3;
751}
Note: See TracBrowser for help on using the repository browser.