8 #define TwoPi 6.28318530717958648 9 const double eps=1e-14;
15 int SolveP3(
double *x,
double a,
double b,
double c) {
17 double q = (a2 - 3*b)/9;
18 double r = (a*(2*a2-9*b) + 27*c)/54;
29 x[1]=q*cos((t+
TwoPi)/3)-a;
30 x[2]=q*cos((t-
TwoPi)/3)-a;
33 A =-pow(fabs(r)+sqrt(r2-q3),1./3);
40 x[2] = 0.5*sqrt(3.)*(A-B);
41 if(fabs(x[2])<
eps) { x[2]=x[1];
return(2); }
47 void CSqrt(
double x,
double y,
double &a,
double &b)
49 double r = sqrt(x*x+y*y);
52 if(x>=0) { a=r; b=0; }
else { a=0; b=r; }
65 double x1 = (-b+sD)/2;
66 double x2 = (-b-sD)/2;
69 double sx1 = sqrt(x1);
70 double sx2 = sqrt(x2);
79 double sx1 = sqrt(-x1);
80 double sx2 = sqrt(-x2);
88 double sx1 = sqrt( x1);
89 double sx2 = sqrt(-x2);
96 double sD2 = 0.5*sqrt(-D);
97 CSqrt(-0.5*b, sD2, x[0],x[1]);
98 CSqrt(-0.5*b,-sD2, x[2],x[3]);
103 #define SWAP(a,b) { t=b; b=a; a=t; } 104 static void dblSort3(
double &a,
double &b,
double &c)
117 if( fabs(c)<1e-14*(fabs(b)+fabs(d)) )
return SolveP4Bi(x,b,d);
119 int res3 =
SolveP3( x, 2*b, b*b-4*d, -c*c);
127 double sz1 = sqrt(x[0]);
128 double sz2 = sqrt(x[1]);
129 double sz3 = sqrt(x[2]);
133 x[0] = (-sz1 -sz2 -sz3)/2;
134 x[1] = (-sz1 +sz2 +sz3)/2;
135 x[2] = (+sz1 -sz2 +sz3)/2;
136 x[3] = (+sz1 +sz2 -sz3)/2;
140 x[0] = (-sz1 -sz2 +sz3)/2;
141 x[1] = (-sz1 +sz2 -sz3)/2;
142 x[2] = (+sz1 -sz2 -sz3)/2;
143 x[3] = (+sz1 +sz2 +sz3)/2;
148 double sz1 = sqrt(-x[0]);
149 double sz2 = sqrt(-x[1]);
150 double sz3 = sqrt( x[2]);
155 x[1] = ( sz1 -sz2)/2;
157 x[3] = (-sz1 -sz2)/2;
162 x[1] = (-sz1 +sz2)/2;
164 x[3] = ( sz1 +sz2)/2;
170 double sz1 = sqrt(x[0]);
172 CSqrt(x[1], x[2], szr, szi);
189 double N4Step(
double x,
double a,
double b,
double c,
double d)
191 double fxs= ((4*x+3*a)*x+2*b)*x+c;
192 if( fxs==0 )
return 1e99;
193 double fx = (((x+a)*x+b)*x+c)*x+d;
201 int SolveP4(
double *x,
double a,
double b,
double c,
double d) {
203 double d1 = d + 0.25*a*( 0.25*b*a - 3./64*a*a*a - c);
204 double c1 = c + 0.5*a*(0.25*a*a - b);
205 double b1 = b - 0.375*a*a;
207 if( res==4) { x[0]-= a/4; x[1]-= a/4; x[2]-= a/4; x[3]-= a/4; }
208 else if (res==2) { x[0]-= a/4; x[1]-= a/4; x[2]-= a/4; }
209 else { x[0]-= a/4; x[2]-= a/4; }
213 x[0] =
N4Step(x[0], a,b,c,d);
214 x[1] =
N4Step(x[1], a,b,c,d);
218 x[2] =
N4Step(x[2], a,b,c,d);
219 x[3] =
N4Step(x[3], a,b,c,d);
224 #define F5(t) (((((t+a)*t+b)*t+c)*t+d)*t+e) 226 double SolveP5_1(
double a,
double b,
double c,
double d,
double e)
229 if( fabs(e)<
eps )
return 0;
231 double brd = fabs(a);
232 if( fabs(b)>brd ) brd = fabs(b);
233 if( fabs(c)>brd ) brd = fabs(c);
234 if( fabs(d)>brd ) brd = fabs(d);
235 if( fabs(e)>brd ) brd = fabs(e);
243 if( e<0 ) { x0 = 0; x1 = brd; f0=e; f1=
F5(x1); x2 = 0.01*brd; }
244 else { x0 =-brd; x1 = 0; f0=
F5(x0); f1=e; x2 =-0.01*brd; }
246 if( fabs(f0)<
eps )
return x0;
247 if( fabs(f1)<
eps )
return x1;
251 for( cnt=0; cnt<5; cnt++)
255 if( fabs(f2)<
eps )
return x2;
256 if( f2>0 ) { x1=x2; f1=f2; }
257 else { x0=x2; f0=f2; }
266 if( x2<=x0 || x2>= x1 ) x2 = (x0 + x1)/2;
268 if( fabs(f2)<
eps )
return x2;
269 if( f2>0 ) { x1=x2; f1=f2; }
270 else { x0=x2; f0=f2; }
271 f2s= (((5*x2+4*a)*x2+3*b)*x2+2*c)*x2+d;
272 if( fabs(f2s)<
eps ) { x2=1e99;
continue; }
275 }
while(fabs(dx)>
eps);
279 int SolveP5(
double *x,
double a,
double b,
double c,
double d,
double e)
282 double a1 = a+r, b1=b+r*a1, c1=c+r*b1, d1=d+r*c1;
283 return 1+
SolveP4(x+1, a1,b1,c1,d1);
292 int Solve2(
double x0,
double x1,
double x2,
double f0,
double f1,
double f2,
double &r1,
double &r2)
294 double w0 = f0*(x1-x2);
295 double w1 = f1*(x2-x0);
296 double w2 = f2*(x0-x1);
297 double a1 = w0 + w1 + w2;
298 double b1 = -w0*(x1+x2) - w1*(x2+x0) - w2*(x0+x1);
299 double c1 = w0*x1*x2 + w1*x2*x0 + w2*x0*x1;
300 double Di = b1*b1 - 4*a1*c1;
301 if( Di<0 ) { r1=r2=1e99;
return 0; }
303 r1 = (-b1 + Di)/2/a1;
304 r2 = (-b1 - Di)/2/a1;
double SolveP5_1(double a, double b, double c, double d, double e)
void CSqrt(double x, double y, double &a, double &b)
int SolveP4Bi(double *x, double b, double d)
int SolveP4(double *x, double a, double b, double c, double d)
int SolveP3(double *x, double a, double b, double c)
int SolveP4De(double *x, double b, double c, double d)
double N4Step(double x, double a, double b, double c, double d)
int Solve2(double x0, double x1, double x2, double f0, double f1, double f2, double &r1, double &r2)
int SolveP5(double *x, double a, double b, double c, double d, double e)
static void dblSort3(double &a, double &b, double &c)