21 #define TwoPi 6.28318530717958648 22 const double eps=1e-14;
31 double q = (
a2 - 3*
b)/9;
32 double r = (
a*(2*
a2-9*
b) + 27*
c)/54;
47 A =-pow(std::abs(
r)+sqrt(r2-q3),1./3);
54 x[2] = 0.5*sqrt(3.)*(A-B);
55 if(std::abs(
x[2])<
eps) {
x[2]=
x[1];
return(2); }
61 void CSqrt(
double x,
double y,
double &
a,
double &
b)
63 double r = sqrt(
x*
x+
y*
y);
66 if(
x>=0) {
a=
r;
b=0; }
else {
a=0;
b=
r; }
79 double x1 = (-
b+sD)/2;
80 double x2 = (-
b-sD)/2;
83 double sx1 = sqrt(x1);
84 double sx2 = sqrt(x2);
93 double sx1 = sqrt(-x1);
94 double sx2 = sqrt(-x2);
102 double sx1 = sqrt( x1);
103 double sx2 = sqrt(-x2);
110 double sD2 = 0.5*sqrt(-D);
117 #define SWAP(a,b) { t=b; b=a; a=t; } 131 if( std::abs(
c)<1e-14*(std::abs(
b)+std::abs(d)) )
return SolveP4Bi(
x,
b,d);
141 double sz1 = sqrt(
x[0]);
142 double sz2 = sqrt(
x[1]);
143 double sz3 = sqrt(
x[2]);
147 x[0] = (-sz1 -sz2 -sz3)/2;
148 x[1] = (-sz1 +sz2 +sz3)/2;
149 x[2] = (+sz1 -sz2 +sz3)/2;
150 x[3] = (+sz1 +sz2 -sz3)/2;
154 x[0] = (-sz1 -sz2 +sz3)/2;
155 x[1] = (-sz1 +sz2 -sz3)/2;
156 x[2] = (+sz1 -sz2 -sz3)/2;
157 x[3] = (+sz1 +sz2 +sz3)/2;
162 double sz1 = sqrt(-
x[0]);
163 double sz2 = sqrt(-
x[1]);
164 double sz3 = sqrt(
x[2]);
169 x[1] = ( sz1 -sz2)/2;
171 x[3] = (-sz1 -sz2)/2;
176 x[1] = (-sz1 +sz2)/2;
178 x[3] = ( sz1 +sz2)/2;
184 double sz1 = sqrt(
x[0]);
205 double fxs= ((4*
x+3*
a)*
x+2*
b)*
x+
c;
206 if( fxs==0 )
return 1e99;
207 double fx = (((
x+
a)*
x+
b)*
x+
c)*
x+d;
217 double d1 = d + 0.25*
a*( 0.25*
b*
a - 3./64*
a*
a*
a -
c);
218 double c1 =
c + 0.5*
a*(0.25*
a*
a -
b);
219 double b1 =
b - 0.375*
a*
a;
221 if(
res==4) {
x[0]-=
a/4;
x[1]-=
a/4;
x[2]-=
a/4;
x[3]-=
a/4; }
222 else if (
res==2) {
x[0]-=
a/4;
x[1]-=
a/4;
x[2]-=
a/4; }
223 else {
x[0]-=
a/4;
x[2]-=
a/4; }
238 #define F5(t) (((((t+a)*t+b)*t+c)*t+d)*t+e) 243 if( std::abs(e)<
eps )
return 0;
245 double brd = std::abs(
a);
246 if( std::abs(
b)>brd ) brd = std::abs(
b);
247 if( std::abs(
c)>brd ) brd = std::abs(
c);
248 if( std::abs(d)>brd ) brd = std::abs(d);
249 if( std::abs(e)>brd ) brd = std::abs(e);
257 if( e<0 ) { x0 = 0; x1 = brd; f0=e; f1=
F5(x1); x2 = 0.01*brd; }
258 else { x0 =-brd; x1 = 0; f0=
F5(x0); f1=e; x2 =-0.01*brd; }
260 if( std::abs(f0)<
eps )
return x0;
261 if( std::abs(f1)<
eps )
return x1;
265 for( cnt=0; cnt<5; cnt++)
269 if( std::abs(f2)<
eps )
return x2;
270 if( f2>0 ) { x1=x2; f1=f2; }
271 else { x0=x2; f0=f2; }
280 if( x2<=x0 || x2>= x1 ) x2 = (x0 + x1)/2;
282 if( std::abs(f2)<
eps )
return x2;
283 if( f2>0 ) { x1=x2; f1=f2; }
284 else { x0=x2; f0=f2; }
285 f2s= (((5*x2+4*
a)*x2+3*
b)*x2+2*
c)*x2+d;
286 if( std::abs(f2s)<
eps ) { x2=1e99;
continue; }
289 }
while(std::abs(dx)>
eps);
315 double Di =
b*
b - 4*
a*
c;
316 if( Di<0 ) { r1=r2=1e99;
return 0; }
318 r1 = (-
b + Di)/(2*
a);
319 r2 = (-
b - Di)/(2*
a);
322 if (r2<r1)
SWAP(r1,r2);
static double SolveP5_1(double a, double b, double c, double d, double e)
int BASE_IMPEXP solve_poly3(double *x, double a, double b, double c) MRPT_NO_THROWS
Solves cubic equation x^3 + a*x^2 + b*x + c = 0.
GLdouble GLdouble GLdouble GLdouble q
int BASE_IMPEXP solve_poly4(double *x, double a, double b, double c, double d) MRPT_NO_THROWS
Solves quartic equation x^4 + a*x^3 + b*x^2 + c*x + d = 0 by Dekart-Euler method. ...
#define MRPT_NO_THROWS
C++11 noexcept: Used after member declarations.
int SolveP4Bi(double *x, double b, double d)
double N4Step(double x, double a, double b, double c, double d)
static void dblSort3(double &a, double &b, double &c)
GLdouble GLdouble GLdouble r
void CSqrt(double x, double y, double &a, double &b)
int SolveP4De(double *x, double b, double c, double d)
int BASE_IMPEXP solve_poly2(double a, double b, double c, double &r1, double &r2) MRPT_NO_THROWS
Solves equation a*x^2 + b*x + c = 0.
GLubyte GLubyte GLubyte a
int BASE_IMPEXP solve_poly5(double *x, double a, double b, double c, double d, double e) MRPT_NO_THROWS
Solves equation x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0.