CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/RecoLocalTracker/SiPixelRecHits/src/VVIObj.cc

Go to the documentation of this file.
00001 //
00002 //  VVIObj.cc  Version 2.0 
00003 //
00004 //  Port of CERNLIB G116 Functions vviden/vvidis
00005 //
00006 // Created by Morris Swartz on 1/14/2010.
00007 // Copyright 2010 __TheJohnsHopkinsUniversity__. All rights reserved.
00008 //
00009 // V1.1 - make dzero call both fcns with a switch
00010 // V1.2 - remove inappriate initializers and add methods to return non-zero/normalized region
00011 // V2.0 - restructuring and speed improvements by V. Innocente
00012 //
00013 
00014 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
00015 // put CMSSW location of SimpleHelix.h here
00016 #include "RecoLocalTracker/SiPixelRecHits/interface/VVIObj.h"
00017 #else
00018 #include "VVIObj.h"
00019 #endif
00020 
00021 
00022 #include <cmath>
00023 #include <algorithm>
00024 #include<boost/bind.hpp>
00025 
00026 
00027 namespace VVIObjDetails {
00028   void sincosint(double x, double & sint, double & cint);  
00029   double cosint(double x);    
00030   double sinint(double x);    
00031   double expint(double x);    
00032   
00033   template<typename F>
00034   int dzero(double a, double b, double& x0, 
00035             double& rv, double eps, int mxf, F func);
00036 }
00037 
00038 
00039 
00040 // ***************************************************************************************************************************************
00046 // *************************************************************************************************************************************** 
00047 
00048 VVIObj::VVIObj(double kappa, double beta2, int mode) : mode_(mode) {
00049   
00050   const double xp[9] = { 9.29,2.47,.89,.36,.15,.07,.03,.02,0.0 };
00051   const double xq[7] = { .012,.03,.08,.26,.87,3.83,11.0 };
00052   double h_[7];
00053   double  q, u, x, c1, c2, c3, c4, d1, h4, h5, h6, q2, x1, d, ll, ul, xf1, xf2, rv;
00054   int lp, lq, k, l, n;
00055   
00056   // Make sure that the inputs are reasonable
00057   
00058   if(kappa < 0.01) kappa = 0.01;
00059   if(kappa > 10.) kappa = 10.;
00060   if(beta2 < 0.) beta2 = 0.;
00061   if(beta2 > 1.) beta2 = 1.;
00062   
00063   h_[4] = 1. - beta2*0.42278433999999998 + 7.6/kappa;
00064   h_[5] = beta2;
00065   h_[6] = 1. - beta2;
00066   h4 = -7.6/kappa - (beta2 * .57721566 + 1);
00067   h5 = log(kappa);
00068   h6 = 1./kappa;
00069   t0_ = (h4 - h_[4]*h5 - (h_[4] + beta2)*(log(h_[4]) + VVIObjDetails::expint(h_[4])) + exp(-h_[4]))/h_[4];
00070   
00071   // Set up limits for the root search
00072   
00073   for (lp = 0; lp < 9; ++lp) {
00074     if (kappa >= xp[lp]) break;
00075   }
00076   ll = -lp - 1.5;
00077   for (lq = 0; lq < 7; ++lq) {
00078     if (kappa <= xq[lq]) break;
00079   }
00080   ul = lq - 6.5;
00081   auto f2 = [h_](double x) { return h_[4]-x+h_[5]*(std::log(std::abs(x))+VVIObjDetails::expint(x))-h_[6]*std::exp(-x);};
00082   VVIObjDetails::dzero(ll, ul, u, rv, 1.e-5, 1000, f2);
00083   q = 1./u;
00084   t1_ = h4 * q - h5 - (beta2 * q + 1) * (log((fabs(u))) + VVIObjDetails::expint(u)) + exp(-u) * q;
00085   t_ = t1_ - t0_;
00086   omega_ = 6.2831853000000004/t_;
00087   h_[0] = kappa * (beta2 * .57721566 + 2.) + 9.9166128600000008;
00088   if (kappa >= .07) {h_[0] += 6.90775527;}
00089   h_[1] = beta2 * kappa;
00090   h_[2] = h6 * omega_;
00091   h_[3] = omega_ * 1.5707963250000001;
00092   auto f1 = [h_](double x){ return h_[0]+h_[1]*std::log(h_[2]*x)-h_[3]*x;};
00093   VVIObjDetails::dzero(5., 155., x0_, rv, 1.e-5, 1000, f1);
00094   n = x0_ + 1.;
00095   d = exp(kappa * (beta2 * (.57721566 - h5) + 1.)) * .31830988654751274;
00096   a_[n - 1] = 0.;
00097   if (mode_ == 0) {
00098     a_[n - 1] = omega_ * .31830988654751274;
00099   }
00100   q = -1.;
00101   q2 = 2.;
00102   for (k = 1; k < n; ++k) {
00103     l = n - k;
00104     x = omega_ * k;
00105     x1 = h6 * x;
00106     VVIObjDetails::sincosint(x1,c2,c1);
00107     c1 = log(x) - c1;
00108     c3 = sin(x1);
00109     c4 = cos(x1);
00110     xf1 = kappa * (beta2 * c1 - c4) - x * c2;
00111     xf2 = x * c1 + kappa * (c3 + beta2 * c2) + t0_ * x;
00112     if (mode_ == 0) {
00113       d1 = q * d * omega_ * exp(xf1);
00114       a_[l - 1] = d1 * cos(xf2);
00115       b_[l - 1] = -d1 * sin(xf2);
00116     } else {
00117       d1 = q * d * exp(xf1)/k;
00118       a_[l - 1] = d1 * sin(xf2);
00119       b_[l - 1] = d1 * cos(xf2);
00120       a_[n - 1] += q2 * a_[l - 1];
00121     }
00122     q = -q;
00123     q2 = -q2;
00124   }
00125   
00126 } // VVIObj
00127 
00128 // *************************************************************************************************************************************
00132 // ************************************************************************************************************************************* 
00133 
00134 
00135 double VVIObj::fcn(double x) const {
00136         
00137         // Local variables
00138         
00139         double f, u, y, a0, a1;
00140         double a2 = 0.;
00141         double b1, b0, b2, cof;
00142         int k, n, n1;
00143         
00144         n = x0_;
00145         if (x < t0_) {
00146                 f = 0.;
00147         } else if (x <= t1_) {
00148           y = x - t0_;
00149           u = omega_ * y - 3.141592653589793;
00150           cof = cos(u) * 2.;
00151           a1 = 0.;
00152           a0 = a_[0];
00153           n1=n+1;
00154           for (k = 2; k <= n1; ++k) {
00155             a2 = a1;
00156             a1 = a0;
00157             a0 = a_[k - 1] + cof * a1 - a2;
00158           }
00159           b1 = 0.;
00160           b0 = b_[0];
00161           for (k = 2; k <= n; ++k) {
00162             b2 = b1;
00163             b1 = b0;
00164             b0 = b_[k - 1] + cof * b1 - b2;
00165           }
00166           f = (a0 - a2) * .5 + b0 * sin(u);
00167           if (mode_ != 0) {f += y / t_;}
00168         } else {
00169           f = 0.;
00170           if (mode_ != 0) {f = 1.;}
00171         }
00172         return f;
00173 } // fcn
00174 
00175 
00176 
00177 // *************************************************************************************************************************************
00181 // ************************************************************************************************************************************* 
00182 
00183 
00184 void VVIObj::limits(double& xl, double& xu) const {
00185         
00186    xl = t0_;
00187    xu = t1_;
00188         return;
00189 } // limits
00190 
00191 
00192 namespace VVIObjDetails {
00193   double cosint(double x) {
00194     // Initialized data
00195     
00196     const double zero = 0.;
00197     const double one = 1.;
00198     const double two = 2.;
00199     const double eight = 8.;
00200     const double ce = .57721566490153;
00201     const double c__[14] = { 1.9405491464836,.9413409132865,
00202                              -.579845034293,.3091572011159,-.0916101792208,.0164437407515,
00203                              -.0019713091952,1.692538851e-4,-1.09393296e-5,5.522386e-7,
00204                              -2.23995e-8,7.465e-10,-2.08e-11,5e-13 };
00205     const double p[23] = { .96074783975204,-.0371138962124,
00206                            .00194143988899,-1.7165988425e-4,2.112637753e-5,-3.27163257e-6,
00207                            6.0069212e-7,-1.2586794e-7,2.932563e-8,-7.45696e-9,2.04105e-9,
00208                            -5.9502e-10,1.8323e-10,-5.921e-11,1.997e-11,-7e-12,2.54e-12,
00209                            -9.5e-13,3.7e-13,-1.4e-13,6e-14,-2e-14,1e-14 };
00210     const double q[20] = { .98604065696238,-.0134717382083,
00211                            4.5329284117e-4,-3.067288652e-5,3.13199198e-6,-4.2110196e-7,
00212                            6.907245e-8,-1.318321e-8,2.83697e-9,-6.7329e-10,1.734e-10,
00213                            -4.787e-11,1.403e-11,-4.33e-12,1.4e-12,-4.7e-13,1.7e-13,-6e-14,
00214                            2e-14,-1e-14 };
00215     
00216     // System generated locals
00217     double d__1;
00218     
00219     // Local variables
00220     double h__;
00221     int i__;
00222     double r__, y, b0, b1, b2, pp, qq, alfa;
00223     
00224     // If x==0, return same
00225     
00226     if (x == zero) {
00227       return zero;
00228     }
00229     if (fabs(x) <= eight) {
00230       y = x / eight;
00231       // Computing 2nd power
00232       d__1 = y;
00233       h__ = two * (d__1 * d__1) - one;
00234       alfa = -two * h__;
00235       b1 = zero;
00236       b2 = zero;
00237       for (i__ = 13; i__ >= 0; --i__) {
00238         b0 = c__[i__] - alfa * b1 - b2;
00239         b2 = b1;
00240         b1 = b0;
00241       }
00242       b1 = ce + log((fabs(x))) - b0 + h__ * b2;
00243     } else {
00244       r__ = one / x;
00245       y = eight * r__;
00246       // Computing 2nd power
00247       d__1 = y;
00248       h__ = two * (d__1 * d__1) - one;
00249       alfa = -two * h__;
00250       b1 = zero;
00251       b2 = zero;
00252       for (i__ = 22; i__ >= 0; --i__) {
00253         b0 = p[i__] - alfa * b1 - b2;
00254         b2 = b1;
00255         b1 = b0;
00256       }
00257       pp = b0 - h__ * b2;
00258       b1 = zero;
00259       b2 = zero;
00260       for (i__ = 19; i__ >= 0; --i__) {
00261         b0 = q[i__] - alfa * b1 - b2;
00262         b2 = b1;
00263         b1 = b0;
00264       }
00265       qq = b0 - h__ * b2;
00266       b1 = r__ * (qq * sin(x) - r__ * pp * cos(x));
00267     }
00268     return b1;
00269   } // cosint
00270   
00271   double sinint(double x) {
00272     // Initialized data
00273     
00274     const double zero = 0.;
00275     const double one = 1.;
00276     const double two = 2.;
00277     const double eight = 8.;
00278     const double pih = 1.5707963267949;
00279     const double s[14] = { 1.9522209759531,-.6884042321257,
00280                            .4551855132256,-.1804571236838,.0410422133759,-.0059586169556,
00281                            6.001427414e-4,-4.44708329e-5,2.5300782e-6,-1.141308e-7,4.1858e-9,
00282                            -1.273e-10,3.3e-12,-1e-13 };
00283     const double p[23] = { .96074783975204,-.0371138962124,
00284                            .00194143988899,-1.7165988425e-4,2.112637753e-5,-3.27163257e-6,
00285                            6.0069212e-7,-1.2586794e-7,2.932563e-8,-7.45696e-9,2.04105e-9,
00286                            -5.9502e-10,1.8323e-10,-5.921e-11,1.997e-11,-7e-12,2.54e-12,
00287                            -9.5e-13,3.7e-13,-1.4e-13,6e-14,-2e-14,1e-14 };
00288     const double q[20] = { .98604065696238,-.0134717382083,
00289                            4.5329284117e-4,-3.067288652e-5,3.13199198e-6,-4.2110196e-7,
00290                            6.907245e-8,-1.318321e-8,2.83697e-9,-6.7329e-10,1.734e-10,
00291                            -4.787e-11,1.403e-11,-4.33e-12,1.4e-12,-4.7e-13,1.7e-13,-6e-14,
00292                            2e-14,-1e-14 };
00293     
00294     // System generated locals
00295     double d__1;
00296     
00297     // Local variables
00298     double h__;
00299     int i__;
00300     double r__, y, b0, b1, b2, pp, qq, alfa;
00301     
00302     if (fabs(x) <= eight) {
00303       y = x / eight;
00304       d__1 = y;
00305       h__ = two * (d__1 * d__1) - one;
00306       alfa = -two * h__;
00307       b1 = zero;
00308       b2 = zero;
00309       for (i__ = 13; i__ >= 0; --i__) {
00310         b0 = s[i__] - alfa * b1 - b2;
00311         b2 = b1;
00312         b1 = b0;
00313       }
00314       b1 = y * (b0 - b2);
00315     } else {
00316       r__ = one / x;
00317       y = eight * r__;
00318       d__1 = y;
00319       h__ = two * (d__1 * d__1) - one;
00320       alfa = -two * h__;
00321       b1 = zero;
00322       b2 = zero;
00323       for (i__ = 22; i__ >= 0; --i__) {
00324         b0 = p[i__] - alfa * b1 - b2;
00325         b2 = b1;
00326         b1 = b0;
00327       }
00328       pp = b0 - h__ * b2;
00329       b1 = zero;
00330       b2 = zero;
00331       for (i__ = 19; i__ >= 0; --i__) {
00332         b0 = q[i__] - alfa * b1 - b2;
00333         b2 = b1;
00334         b1 = b0;
00335       }
00336       qq = b0 - h__ * b2;
00337       d__1 = fabs(pih);
00338       if(x < 0.) d__1 = -d__1;
00339       b1 = d__1 - r__ * (r__ * pp * sin(x) + qq * cos(x));
00340     }
00341     
00342     return b1;
00343   } // sinint
00344   
00345   void sincosint(double x, double & sint, double & cint) {
00346     // Initialized data
00347     
00348     const double zero = 0.;
00349     const double one = 1.;
00350     const double two = 2.;
00351     const double eight = 8.;
00352     const double ce = .57721566490153;
00353     const double pih = 1.5707963267949;
00354     const double s__[14] = { 1.9522209759531,-.6884042321257,
00355                              .4551855132256,-.1804571236838,.0410422133759,-.0059586169556,
00356                              6.001427414e-4,-4.44708329e-5,2.5300782e-6,-1.141308e-7,4.1858e-9,
00357                              -1.273e-10,3.3e-12,-1e-13 };
00358     
00359     const double c__[14] = { 1.9405491464836,.9413409132865,
00360                              -.579845034293,.3091572011159,-.0916101792208,.0164437407515,
00361                              -.0019713091952,1.692538851e-4,-1.09393296e-5,5.522386e-7,
00362                              -2.23995e-8,7.465e-10,-2.08e-11,5e-13 };
00363     
00364     const double p[23] = { .96074783975204,-.0371138962124,
00365                            .00194143988899,-1.7165988425e-4,2.112637753e-5,-3.27163257e-6,
00366                            6.0069212e-7,-1.2586794e-7,2.932563e-8,-7.45696e-9,2.04105e-9,
00367                            -5.9502e-10,1.8323e-10,-5.921e-11,1.997e-11,-7e-12,2.54e-12,
00368                            -9.5e-13,3.7e-13,-1.4e-13,6e-14,-2e-14,1e-14 };
00369     const double q[20] = { .98604065696238,-.0134717382083,
00370                            4.5329284117e-4,-3.067288652e-5,3.13199198e-6,-4.2110196e-7,
00371                            6.907245e-8,-1.318321e-8,2.83697e-9,-6.7329e-10,1.734e-10,
00372                            -4.787e-11,1.403e-11,-4.33e-12,1.4e-12,-4.7e-13,1.7e-13,-6e-14,
00373                            2e-14,-1e-14 };
00374     
00375     // System generated locals
00376     double d__1;
00377     
00378     // Local variables
00379     double h__;
00380     int i__;
00381     double r__, y, b0, b1, b2, pp, qq, alfa;
00382     
00383     sint=0; 
00384     cint=0;
00385     
00386     
00387     if (fabs(x) <= eight) {
00388       y = x / eight;
00389       // Computing 2nd power
00390       d__1 = y;
00391       h__ = two * (d__1 * d__1) - one;
00392       alfa = -two * h__;
00393       
00394       // cos
00395       if (x!=0) {
00396         b1 = zero;
00397         b2 = zero;
00398         for (i__ = 13; i__ >= 0; --i__) {
00399           b0 = c__[i__] - alfa * b1 - b2;
00400           b2 = b1;
00401           b1 = b0;
00402         }
00403         cint = ce + log((fabs(x))) - b0 + h__ * b2;
00404       }
00405       // sin
00406       b1 = zero;
00407       b2 = zero;
00408       for (i__ = 13; i__ >= 0; --i__) {
00409         b0 = s__[i__] - alfa * b1 - b2;
00410         b2 = b1;
00411         b1 = b0;
00412       }
00413       sint = y * (b0 - b2);
00414       
00415     } else {
00416       r__ = one / x;
00417       y = eight * r__;
00418       // Computing 2nd power
00419       d__1 = y;
00420       h__ = two * (d__1 * d__1) - one;
00421       alfa = -two * h__;
00422       b1 = zero;
00423       b2 = zero;
00424       for (i__ = 22; i__ >= 0; --i__) {
00425         b0 = p[i__] - alfa * b1 - b2;
00426         b2 = b1;
00427         b1 = b0;
00428       }
00429       pp = b0 - h__ * b2;
00430       b1 = zero;
00431       b2 = zero;
00432       for (i__ = 19; i__ >= 0; --i__) {
00433         b0 = q[i__] - alfa * b1 - b2;
00434         b2 = b1;
00435         b1 = b0;
00436       }
00437       qq = b0 - h__ * b2;
00438       // cos
00439       cint = r__ * (qq * sin(x) - r__ * pp * cos(x));
00440       // sin
00441       d__1 = pih;
00442       if(x < 0.) d__1 = -d__1;
00443       sint = d__1 - r__ * (r__ * pp * sin(x) + qq * cos(x));
00444     }
00445   }
00446 
00447 
00448 double expint(double x) {
00449   
00450   // Initialized data
00451   
00452   const double zero = 0.;
00453   const double q2[7] = { .10340013040487,3.319092135933,
00454                          20.449478501379,41.280784189142,32.426421069514,10.041164382905,
00455                          1. };
00456   const double p3[6] = { -2.3909964453136,-147.98219500504,
00457                          -254.3763397689,-119.55761038372,-19.630408535939,-.9999999999036 
00458   };
00459   const double q3[6] = { 177.60070940351,530.68509610812,
00460                          462.23027156148,156.81843364539,21.630408494238,1. };
00461   const double p4[8] = { -8.6693733995107,-549.14226552109,
00462                          -4210.0161535707,-249301.39345865,-119623.66934925,
00463                          -22174462.775885,3892804.213112,-391546073.8091 };
00464   const double q4[8] = { 34.171875,-1607.0892658722,35730.029805851,
00465                          -483547.43616216,4285596.2461175,-24903337.574054,89192576.757561,
00466                          -165254299.72521 };
00467   const double a1[8] = { -2.1808638152072,-21.901023385488,
00468                          9.3081638566217,25.076281129356,-33.184253199722,60.121799083008,
00469                          -43.253113287813,1.0044310922808 };
00470   const double b1[8] = { 0.,3.9370770185272,300.89264837292,
00471                          -6.2504116167188,1003.6743951673,14.325673812194,2736.2411988933,
00472                          .52746885196291 };
00473   const double a2[8] = { -3.4833465360285,-18.65454548834,
00474                          -8.2856199414064,-32.34673303054,17.960168876925,1.7565631546961,
00475                          -1.9502232128966,.99999429607471 };
00476   const double b2[8] = { 0.,69.500065588743,57.283719383732,
00477                          25.777638423844,760.76114800773,28.951672792514,-3.4394226689987,
00478                          1.0008386740264 };
00479   const double a3[6] = { -27.780928934438,-10.10479081576,
00480                          -9.1483008216736,-5.0223317461851,-3.0000077799358,
00481                          1.0000000000704 };
00482   const double one = 1.;
00483   const double b3[6] = { 0.,122.39993926823,2.7276100778779,
00484                          -7.1897518395045,-2.9990118065262,1.999999942826 };
00485   const double two = 2.;
00486   const double three = 3.;
00487   const double x0 = .37250741078137;
00488   const double xl[6] = { -24.,-12.,-6.,0.,1.,4. };
00489   const double p1[5] = { 4.293125234321,39.894153870321,
00490                          292.52518866921,425.69682638592,-434.98143832952 };
00491   const double q1[5] = { 1.,18.899288395003,150.95038744251,
00492                          568.05252718987,753.58564359843 };
00493   const double p2[7] = { .43096783946939,6.9052252278444,
00494                          23.019255939133,24.378408879132,9.0416155694633,.99997957705159,
00495                          4.656271079751e-7 };
00496   
00497   /* Local variables */
00498    double v, y, ap, bp, aq, dp, bq, dq;
00499   
00500   if (x <= xl[0]) {
00501     ap = a3[0] - x;
00502     for ( int i__ = 2; i__ <= 5; ++i__) {
00503       /* L1: */
00504       ap = a3[i__ - 1] - x + b3[i__ - 1] / ap;
00505     }
00506     y = exp(-x) / x * (one - (a3[5] + b3[5] / ap) / x);
00507   } else if (x <= xl[1]) {
00508     ap = a2[0] - x;
00509     for ( int i__ = 2; i__ <= 7; ++i__) {
00510       ap = a2[i__ - 1] - x + b2[i__ - 1] / ap;
00511     }
00512     y = exp(-x) / x * (a2[7] + b2[7] / ap);
00513   } else if (x <= xl[2]) {
00514     ap = a1[0] - x;
00515     for ( int i__ = 2; i__ <= 7; ++i__) {
00516       ap = a1[i__ - 1] - x + b1[i__ - 1] / ap;
00517     }
00518     y = exp(-x) / x * (a1[7] + b1[7] / ap);
00519   } else if (x < xl[3]) {
00520     v = -two * (x / three + one);
00521     bp = zero;
00522     dp = p4[0];
00523     for ( int i__ = 2; i__ <= 8; ++i__) {
00524       ap = bp;
00525       bp = dp;
00526       dp = p4[i__ - 1] - ap + v * bp;
00527     }
00528     bq = zero;
00529     dq = q4[0];
00530     for ( int i__ = 2; i__ <= 8; ++i__) {
00531       aq = bq;
00532       bq = dq;
00533       dq = q4[i__ - 1] - aq + v * bq;
00534     }
00535     y = -log(-x / x0) + (x + x0) * (dp - ap) / (dq - aq);
00536   } else if (x == xl[3]) {
00537     return zero;
00538   } else if (x < xl[4]) {
00539     ap = p1[0];
00540     aq = q1[0];
00541     for ( int i__ = 2; i__ <= 5; ++i__) {
00542       ap = p1[i__ - 1] + x * ap;
00543       aq = q1[i__ - 1] + x * aq;
00544     }
00545     y = -log(x) + ap / aq;
00546   } else if (x <= xl[5]) {
00547     y = one / x;
00548     ap = p2[0];
00549     aq = q2[0];
00550     for ( int i__ = 2; i__ <= 7; ++i__) {
00551       ap = p2[i__ - 1] + y * ap;
00552       aq = q2[i__ - 1] + y * aq;
00553     }
00554     y = exp(-x) * ap / aq;
00555   } else {
00556     y = one / x;
00557     ap = p3[0];
00558     aq = q3[0];
00559     for ( int i__ = 2; i__ <= 6; ++i__) {
00560       ap = p3[i__ - 1] + y * ap;
00561       aq = q3[i__ - 1] + y * aq;
00562     }
00563     y = exp(-x) * y * (one + y * ap / aq);
00564   }
00565   return y;
00566 } // expint
00567   
00568 
00569   template<typename F>
00570   int dzero(double a, double b, double& x0, 
00571             double& rv, double eps, int mxf, F func) {
00572     /* System generated locals */
00573     double d__1, d__2, d__3, d__4;
00574     
00575     // Local variables
00576     double f1, f2, f3, u1, u2, x1, x2, u3, u4, x3, ca, cb, cc, fa, fb, ee, ff;
00577     int mc;
00578     double xa, xb, fx, xx, su4;
00579     
00580     xa = std::min(a,b);
00581     xb = std::max(a,b);
00582     fa = func(xa);
00583     fb = func(xb);
00584     if (fa * fb > 0.) {
00585       rv = (xb - xa) * -2;
00586       x0 = 0.;
00587       return 1;
00588     }
00589     mc = 0;
00590   L1:
00591     x0 = (xa + xb) * .5;
00592     rv = x0 - xa;
00593     ee = eps * (fabs(x0) + 1);
00594     if (rv <= ee) {
00595       rv = ee;
00596       ff = func(x0);
00597       return 0;
00598     }
00599     f1 = fa;
00600     x1 = xa;
00601     f2 = fb;
00602     x2 = xb;
00603   L2:
00604     fx = func(x0);
00605     ++mc;
00606     if (mc > mxf) {
00607       rv = (d__1 = xb - xa, fabs(d__1)) * -.5;
00608       x0 = 0.;
00609       return 0;
00610     }
00611     if (fx * fa > 0.) {
00612       xa = x0;
00613       fa = fx;
00614     } else {
00615       xb = x0;
00616       fb = fx;
00617     }
00618   L3:
00619     u1 = f1 - f2;
00620     u2 = x1 - x2;
00621     u3 = f2 - fx;
00622     u4 = x2 - x0;
00623     if (u2 == 0. || u4 == 0.) {goto L1;}
00624     f3 = fx;
00625     x3 = x0;
00626     u1 /= u2;
00627     u2 = u3 / u4;
00628     ca = u1 - u2;
00629     cb = (x1 + x2) * u2 - (x2 + x0) * u1;
00630     cc = (x1 - x0) * f1 - x1 * (ca * x1 + cb);
00631     if (ca == 0.) {
00632       if (cb == 0.) {goto L1;}
00633       x0 = -cc / cb;
00634     } else {
00635       u3 = cb / (ca * 2);
00636       u4 = u3 * u3 - cc / ca;
00637       if (u4 < 0.) {goto L1;}
00638       su4 = fabs(u4);
00639       if (x0 + u3 < 0.f) {su4 = -su4;}
00640       x0 = -u3 + su4;
00641     }
00642     if (x0 < xa || x0 > xb) {goto L1;}
00643     // Computing MIN
00644     d__3 = (d__1 = x0 - x3, fabs(d__1)), d__4 = (d__2 = x0 - x2, fabs(d__2));
00645     rv = std::min(d__3,d__4);
00646     ee = eps * (fabs(x0) + 1);
00647     if (rv > ee) {
00648       f1 = f2;
00649       x1 = x2;
00650       f2 = f3;
00651       x2 = x3;
00652       goto L2;
00653     }
00654     fx = func(x0);
00655     if (fx == 0.) {
00656       rv = ee;
00657       ff = func(x0);
00658       return 0;
00659     }
00660     if (fx * fa < 0.) {
00661       xx = x0 - ee;
00662       if (xx <= xa) {
00663         rv = ee;
00664         ff = func(x0);
00665         return 0;
00666       }
00667       ff = func(xx);
00668       fb = ff;
00669       xb = xx;
00670     } else {
00671       xx = x0 + ee;
00672       if (xx >= xb) {
00673         rv = ee;
00674         ff = func(x0);
00675         return 0;
00676       }
00677       ff = func(xx);
00678       fa = ff;
00679       xa = xx;
00680     }
00681     if (fx * ff > 0.) {
00682       mc += 2;
00683       if (mc > mxf) {
00684         rv = (d__1 = xb - xa, fabs(d__1)) * -.5;
00685         x0 = 0.;
00686         return 0;
00687       }
00688       f1 = f3;
00689       x1 = x3;
00690       f2 = fx;
00691       x2 = x0;
00692       x0 = xx;
00693       fx = ff;
00694       goto L3;
00695     }
00696     /* L4: */
00697     rv = ee;
00698     ff = func(x0);
00699     return 0;
00700   } // dzero
00701   
00702 }