1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
| #include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define TwoPi 6.28318530717958648
const double eps=1e-14;
// solve cubic equation x^3 + a*x^2 + b*x + c = 0
int SolveP3(double *x,double a,double b,double c);
// solve equation x^4 + b*x^2 + d = 0
int SolveP4Bi(double *x, double b, double d);
// solve equation x^4 + b*x^2 + c*x + d = 0
int SolveP4De(double *x, double b, double c, double d);
// solve equation x^4 + a*x^3 + b*x^2 + c*x + d = 0 by Dekart-Euler method
// x - array of size 4
// return 4: 4 real roots x[0], x[1], x[2], x[3], possible multiple roots
// return 2: 2 real roots x[0], x[1] and complex x[2]±i*x[3],
// return 0: two pair of complex roots: x[0]±i*x[1], x[2]±i*x[3],
int SolveP4(double *x,double a,double b,double c,double d);
// returns as a+i*b, sqrt(x+i*y)
void CSqrt(double x, double y, double &a, double &b);
// one Newton step for x^4 + a*x^3 + b*x^2 + c*x + d
double N4Step(double x, double a,double b,double c,double d);
//---------------------------------------------------------------------------
// solve cubic equation x^3 + a*x^2 + b*x + c
// x - array of size 3
// In case 3 real roots: => x[0], x[1], x[2], return 3
// 2 real roots: x[0], x[1], return 2
// 1 real root : x[0], x[1] ± i*x[2], return 1
int SolveP3(double *x,double a,double b,double c)
{
double a2 = a*a;
double q = (a2 - 3*b)/9;
double r = (a*(2*a2-9*b) + 27*c)/54;
double r2 = r*r;
double q3 = q*q*q;
double A,B;
if(r2<q3)
{
double t=r/sqrt(q3);
if( t<-1) t=-1;
if( t> 1) t= 1;
t=acos(t);
a/=3; q=-2*sqrt(q);
x[0]=q*cos(t/3)-a;
x[1]=q*cos((t+TwoPi)/3)-a;
x[2]=q*cos((t-TwoPi)/3)-a;
return(3);
}
else
{
A =-pow(fabs(r)+sqrt(r2-q3),1./3);
if( r<0 ) A=-A;
B = A==0? 0 : B=q/A;
a/=3;
x[0] =(A+B)-a;
x[1] =-0.5*(A+B)-a;
x[2] = 0.5*sqrt(3.)*(A-B);
if(fabs(x[2])<eps)
{
x[2]=x[1];
return(2);
}
return(1);
}
}// SolveP3(double *x,double a,double b,double c)
//---------------------------------------------------------------------------
// a>=0!
void CSqrt(double x, double y, double &a, double &b) // returns: a+i*b = sqrt(x+i*y)
{
double r = sqrt(x*x+y*y);
if( y==0 )
{
r = sqrt(r);
if(x>=0)
{
a=r;
b=0;
}
else
{
a=0;
b=r;
}
}
else
{ // y != 0
a = sqrt(0.5*(x+r));
b = 0.5*y/a;
}
}
//---------------------------------------------------------------------------
int SolveP4Bi(double *x, double b, double d) // solve equation x^4 + b*x^2 + d = 0
{
double D = b*b-4*d;
if( D>=0 )
{
double sD = sqrt(D);
double x1 = (-b+sD)/2;
double x2 = (-b-sD)/2; // x2 <= x1
if( x2>=0 ) // 0 <= x2 <= x1, 4 real roots
{
double sx1 = sqrt(x1);
double sx2 = sqrt(x2);
x[0] = -sx1;
x[1] = sx1;
x[2] = -sx2;
x[3] = sx2;
return 4;
}
if( x1 < 0 ) // x2 <= x1 < 0, two pair of imaginary roots
{
double sx1 = sqrt(-x1);
double sx2 = sqrt(-x2);
x[0] = 0;
x[1] = sx1;
x[2] = 0;
x[3] = sx2;
return 0;
}
// now x2 < 0 <= x1 , two real roots and one pair of imginary root
double sx1 = sqrt( x1);
double sx2 = sqrt(-x2);
x[0] = -sx1;
x[1] = sx1;
x[2] = 0;
x[3] = sx2;
return 2;
} else { // if( D < 0 ), two pair of compex roots
double sD2 = 0.5*sqrt(-D);
CSqrt(-0.5*b, sD2, x[0],x[1]);
CSqrt(-0.5*b,-sD2, x[2],x[3]);
return 0;
} // if( D>=0 )
} // SolveP4Bi(double *x, double b, double d) // solve equation x^4 + b*x^2 d
//---------------------------------------------------------------------------
#define SWAP(a,b) {t=b; b=a; a=t;}
static void dblSort3( double &a, double &b, double &c) // make: a <= b <= c
{
double t;
if( a>b )
SWAP(a,b); // now a<=b
if( c<b )
{
SWAP(b,c); // now a<=b, b<=c
if( a>b )
SWAP(a,b);// now a<=b
}
}
//---------------------------------------------------------------------------
// solve equation x^4 + b*x^2 + c*x + d
int SolveP4De(double *x, double b, double c, double d)
{
if( fabs(c)<1e-14*(fabs(b)+fabs(d)) )
return SolveP4Bi(x,b,d); // After that, c!=0
int res3 = SolveP3(x, 2*b, b*b-4*d, -c*c); // solve resolvent
// by Viet theorem: x1*x2*x3=-c*c not equals to 0, so x1!=0, x2!=0, x3!=0
if( res3>1 ) // 3 real roots,
{
dblSort3(x[0], x[1], x[2]); // sort roots to x[0] <= x[1] <= x[2]
// Note: x[0]*x[1]*x[2]= c*c > 0
if( x[0] > 0) // all roots are positive
{
double sz1 = sqrt(x[0]);
double sz2 = sqrt(x[1]);
double sz3 = sqrt(x[2]);
// Note: sz1*sz2*sz3= -c (and not equal to 0)
if( c>0 )
{
x[0] = (-sz1 -sz2 -sz3)/2;
x[1] = (-sz1 +sz2 +sz3)/2;
x[2] = (+sz1 -sz2 +sz3)/2;
x[3] = (+sz1 +sz2 -sz3)/2;
return 4;
}
// now: c<0
x[0] = (-sz1 -sz2 +sz3)/2;
x[1] = (-sz1 +sz2 -sz3)/2;
x[2] = (+sz1 -sz2 -sz3)/2;
x[3] = (+sz1 +sz2 +sz3)/2;
return 4;
} // if( x[0] > 0) // all roots are positive
// now x[0] <= x[1] < 0, x[2] > 0
// two pair of comlex roots
double sz1 = sqrt(-x[0]);
double sz2 = sqrt(-x[1]);
double sz3 = sqrt( x[2]);
if( c>0 ) // sign = -1
{
x[0] = -sz3/2;
x[1] = ( sz1 -sz2)/2; // x[0]±i*x[1]
x[2] = sz3/2;
x[3] = (-sz1 -sz2)/2; // x[2]±i*x[3]
return 0;
}
// now: c<0 , sign = +1
x[0] = sz3/2;
x[1] = (-sz1 +sz2)/2;
x[2] = -sz3/2;
x[3] = ( sz1 +sz2)/2;
return 0;
} // if( res3>1 ) // 3 real roots,
// now resoventa have 1 real and pair of compex roots
// x[0] - real root, and x[0]>0,
// x[1]±i*x[2] - complex roots,
double sz1 = sqrt(x[0]);
double szr, szi;
CSqrt(x[1], x[2], szr, szi); // (szr+i*szi)^2 = x[1]+i*x[2]
if( c>0 ) // sign = -1
{
x[0] = -sz1/2-szr; // 1st real root
x[1] = -sz1/2+szr; // 2nd real root
x[2] = sz1/2;
x[3] = szi;
return 2;
}
// now: c<0 , sign = +1
x[0] = sz1/2-szr; // 1st real root
x[1] = sz1/2+szr; // 2nd real root
x[2] = -sz1/2;
x[3] = szi;
return 2;
} // SolveP4De(), solve equation x^4 + b*x^2 + c*x + d
//-----------------------------------------------------------------------------
// one Newton step for x^4 + a*x^3 + b*x^2 + c*x + d
double N4Step(double x, double a,double b,double c,double d)
{
double fxs= ((4*x+3*a)*x+2*b)*x+c; // f'(x)
if( fxs==0 )
return 1e99;
double fx = (((x+a)*x+b)*x+c)*x+d; // f(x)
return x - fx/fxs;
}
//-----------------------------------------------------------------------------
// solve equation x^4 + a*x^3 + b*x^2 + c*x + d by Dekart-Euler method
// x - array of size 4
// return 4: 4 real roots x[0], x[1], x[2], x[3], possible multiple roots
// return 2: 2 real roots x[0], x[1] and complex x[2]±i*x[3],
// return 0: two pair of complex roots: x[0]±i*x[1], x[2]±i*x[3],
int SolveP4(double *x,double a,double b,double c,double d)
{
// move to a=0:
double d1 = d + 0.25*a*( 0.25*b*a - 3./64*a*a*a - c);
double c1 = c + 0.5*a*(0.25*a*a - b);
double b1 = b - 0.375*a*a;
int res = SolveP4De( x, b1, c1, d1);
if( res==4)
{
x[0]-= a/4;
x[1]-= a/4;
x[2]-= a/4;
x[3]-= a/4;
}
else if (res==2)
{
x[0]-= a/4;
x[1]-= a/4;
x[2]-= a/4;
}
else
{
x[0]-= a/4;
x[2]-= a/4;
}
// one Newton step for each real root:
if( res>0 )
{
x[0] = N4Step(x[0], a,b,c,d);
x[1] = N4Step(x[1], a,b,c,d);
}
if( res>2 )
{
x[2] = N4Step(x[2], a,b,c,d);
x[3] = N4Step(x[3], a,b,c,d);
}
return res;
}
//-----------------------------------------------------------------------------
//f(x)=6x^4+19x^3-7x^2-26x+12
int main()
{
double x[4];
switch(SolveP4(x, 19./6, -7./6, -26./6, 12./6))
{
case 4:
printf("x[0] = %g\nx[1] = %g\nx[2] = %g\nx[3] = %g\n",
x[0],x[1],x[2],x[3]);
break;
case 2:
printf("x[0] = %g\nx[1] = %g\nx[2] = %g+i*%g\nx[3] = %g-i*%g\n",
x[0],x[1],x[2],fabs(x[3]),x[2],fabs(x[3]));
break;
case 0:
printf("x[0] = %g+i*%g\nx[1] = %g-i*%g\nx[2] = %g+i*%g\nx[3] = %g-i*%g\n",
x[0],fabs(x[1]),x[0],fabs(x[1]),x[2],fabs(x[3]),x[2],fabs(x[3]));
}
_getch();
return 0;
} |