39const G4double G4JTPolynomialSolver::base = 2;
48 : sr(0.), si(0.), u(0.),v(0.),
49 a(0.), b(0.), c(0.), d(0.),
50 a1(0.), a2(0.), a3(0.), a6(0.), a7(0.),
51 e(0.), f(0.), g(0.), h(0.),
52 szr(0.), szi(0.), lzr(0.), lzi(0.),
64 G4double t=0.0, aa=0.0, bb=0.0, cc=0.0, factor=1.0;
65 G4double max=0.0, min=infin, xxx=0.0, x=0.0, sc=0.0, bnd=0.0;
66 G4double xm=0.0, ff=0.0, df=0.0, dx=0.0;
67 G4int cnt=0, nz=0, i=0, j=0, jj=0, l=0, nm1=0, zerok=0;
80 if (!(op[0] != 0.0)) {
return -1; }
84 while (!(op[n] != 0.0))
91 if (n < 1) {
return -1; }
95 std::vector<G4double> temp(degr+1) ;
96 std::vector<G4double> pt(degr+1) ;
101 qk.assign(degr+1,0) ;
102 svk.assign(degr+1,0) ;
113 zeror[degr-1] = -p[1]/p[0];
120 Quadratic(p[0],p[1],p[2],&zeror[degr-2],&zeroi[degr-2],
121 &zeror[degr-1],&zeroi[degr-1]);
133 if (x > max) { max = x; }
134 if (x != 0.0 && x < min) { min = x; }
145 if ( ((sc <= 1.0) && (max >= 10.0))
146 || ((sc > 1.0) && (infin/sc >= max))
147 || ((infin/sc >= max) && (max >= 10)) )
151 l = (
G4int)(std::log(sc)/std::log(base) + 0.5);
152 factor = std::pow(base*1.0,l);
156 { p[i] = factor*p[i]; }
164 pt[i] = (std::fabs(p[i]));
170 x = std::exp((std::log(-pt[n])-std::log(pt[0])) / (
G4double)n);
177 if (xm < x) { x = xm; }
187 { ff = ff*xm + pt[i]; }
188 if (ff <= 0.0) {
break; }
195 while (std::fabs(dx/x) > 0.005)
219 zerok = (k[n-1] == 0);
231 k[j] = t*k[j-1]+p[j];
234 zerok = (std::fabs(k[n-1]) <= std::fabs(bb)*eta*10.0);
244 zerok = (!(k[n-1] != 0.0));
255 for (cnt = 0;cnt < 20;cnt++)
262 xxx = cosr*xx - sinr*yy;
263 yy = sinr*xx + cosr*yy;
269 ComputeFixedShiftPolynomial(20*(cnt+1),&nz);
309void G4JTPolynomialSolver::ComputeFixedShiftPolynomial(
G4int l2,
G4int *nz)
315 G4double svu=0.0, svv=0.0, ui=0.0, vi=0.0, xs=0.0;
316 G4double betas=0.25, betav=0.25, oss=sr, ovv=v,
317 ss=0.0, vv=0.0, ts=1.0, tv=1.0;
320 G4int type=0, i=0, j=0, iflag=0, vpass=0, spass=0, vtry=0, stry=0;
326 QuadraticSyntheticDivision(n,&u,&v,p,qp,&a,&b);
327 ComputeScalarFactors(&type);
332 ComputeNextPolynomial(&type);
333 ComputeScalarFactors(&type);
334 ComputeNewEstimate(type,&ui,&vi);
340 if (k[n-1] != 0.0) { ss = -p[n]/k[n-1]; }
343 if (j == 0 || type == 3)
354 if (vv != 0.0) { tv = std::fabs((vv-ovv)/vv); }
355 if (ss != 0.0) { ts = std::fabs((ss-oss)/ss); }
359 if (tv < otv) { tvv = tv*otv; }
361 if (ts < ots) { tss = ts*ots; }
364 vpass = (tvv < betav);
365 spass = (tss < betas);
366 if (!(spass || vpass))
390 if ((spass && (!vpass)) || (tss < tvv))
392 RealPolynomialIteration(&xs,nz,&iflag);
393 if (*nz > 0) {
return; }
400 if (iflag == 0) {
goto _restore_variables; }
413 QuadraticPolynomialIteration(&ui,&vi,nz);
414 if (*nz > 0) {
return; }
425 if (stry || !spass) {
break; }
430 RealPolynomialIteration(&xs,nz,&iflag);
431 if (*nz > 0) {
return; }
438 if (iflag == 0) {
break; }
462 if (vpass && !vtry) {
goto _quadratic_iteration; }
467 QuadraticSyntheticDivision(n,&u,&v,p,qp,&a,&b);
468 ComputeScalarFactors(&type);
477void G4JTPolynomialSolver::
489 G4double mp=0.0, ee=0.0, t=0.0, zm=0.0;
490 G4int type=0, i=1, j=0, tried=0;
501 Quadratic(1.0,u,v,&szr,&szi,&lzr,&lzi);
507 if (std::fabs(std::fabs(szr)-std::fabs(lzr)) > 0.01 * std::fabs(lzr))
512 QuadraticSyntheticDivision(n,&u,&v,p,qp,&a,&b);
513 mp = std::fabs(a-szr*b) + std::fabs(szi*b);
517 zm = std::sqrt(std::fabs(v));
518 ee = 2.0*std::fabs(qp[0]);
522 ee = ee*zm + std::fabs(qp[i]);
524 ee = ee*zm + std::fabs(a+t);
525 ee *= (5.0 *mre + 4.0*are);
526 ee = ee - (5.0*mre+2.0*are)*(std::fabs(a+t)+std::fabs(b)*zm)
527 + 2.0*are*std::fabs(t);
541 if (j > 20) {
return; }
544 if (!(relstp > 0.01 || mp < omp || tried))
549 if (relstp < eta) { relstp = eta; }
550 relstp = std::sqrt(relstp);
553 QuadraticSyntheticDivision(n,&u,&v,p,qp,&a,&b);
556 ComputeScalarFactors(&type);
557 ComputeNextPolynomial(&type);
567 ComputeScalarFactors(&type);
568 ComputeNextPolynomial(&type);
569 ComputeScalarFactors(&type);
570 ComputeNewEstimate(type,&ui,&vi);
574 if (!(vi != 0.0)) {
return; }
575 relstp = std::fabs((vi-v)/vi);
581void G4JTPolynomialSolver::
617 ee = (mre/(are+mre))*std::fabs(qp[0]);
620 ee = ee*mx + std::fabs(qp[i]);
626 if (mp <= 20.0*((are+mre)*ee-mre*mp))
637 if (j > 10) {
return; }
640 if (!(std::fabs(t) > 0.001*std::fabs(xs-t) || mp < omp))
662 if (std::fabs(kv) <= std::fabs(k[n-1])*10.0*eta)
676 k[i] = t*qk[i-1] + qp[i];
685 if (std::fabs(kv) > std::fabs(k[n-1]*10.0*eta)) { t = -pv/kv; }
690void G4JTPolynomialSolver::ComputeScalarFactors(
G4int *type)
700 QuadraticSyntheticDivision(n-1,&u,&v,k,qk,&c,&d);
701 if (std::fabs(c) <= std::fabs(k[n-1]*100.0*eta))
703 if (std::fabs(d) <= std::fabs(k[n-2]*100.0*eta))
710 if (std::fabs(d) < std::fabs(c))
717 a3 = a*e + (h/c+g)*b;
727 a3 = (a+g)*e + h*(b/d);
732void G4JTPolynomialSolver::ComputeNextPolynomial(
G4int *type)
750 if (*type == 1) { temp = b; }
751 if (std::fabs(a1) <= std::fabs(temp)*eta*10.0)
759 k[i] = a3*qk[i-2] - a7*qp[i-1];
769 k[1] = qp[1] - a7*qp[0];
772 k[i] = a3*qk[i-2] - a7*qp[i-1] + qp[i];
776void G4JTPolynomialSolver::
782 G4double a4=0.0, a5=0.0, b1=0.0, b2=0.0,
783 c1=0.0, c2=0.0, c3=0.0, c4=0.0, temp=0.0;
807 b2 = -(k[
n-2]+b1*p[
n-1])/p[n];
812 temp = a5 + b1*a4 - c4;
819 *uu = u - (u*(c3+c2)+v*(b1*a1+b2*a7))/temp;
820 *vv = v*(1.0+c4/temp);
824void G4JTPolynomialSolver::
826 std::vector<G4double> &pp, std::vector<G4double> &qq,
835 *aa =
pp[1] - (*bb)*(*uu);
839 cc =
pp[i] - (*aa)*(*uu) - (*bb)*(*vv);
882 if (std::fabs(bb) < std::fabs(cc))
888 ee = bb*(bb/std::fabs(cc)) - ee;
889 dd = std::sqrt(std::fabs(ee))*std::sqrt(std::fabs(cc));
893 ee = 1.0 - (aa/bb)*(cc/bb);
894 dd = std::sqrt(std::fabs(ee))*std::fabs(bb);
900 *ssi = std::fabs(dd/aa);
910 { *ssr = (cc/ *lr)/aa; }
G4int FindRoots(G4double *op, G4int degree, G4double *zeror, G4double *zeroi)