-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbbpr.cpp
345 lines (318 loc) · 8.52 KB
/
bbpr.cpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// bbpr.cpp -- Finds all roots of polynomial by first finding quadratic
// factors using Bairstow's method, then extracting roots
// from quadratics. Implements new algorithm for managing
// multiple roots.
//
// (C) 2002, 2003, C. Bond. All rights reserved.
// code added by Nick Ferguson 2016
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include "bbpr.h"
#define maxiter 500
std::vector<double> roots(double *a)
{
double x[21], wr[21], wi[21], quad[2];
std::vector<double> vroots;
int iter, i, numr, n;
n = sizeof(*a) / sizeof(double);
n++;
if ((n < 1) || (n > 20)) {
numr = -1;
vroots.push_back(-1);
return vroots;
}
// get coefficients of polynomial
//"Error! Lowest coefficient (constant term) cannot be 0."
if (a[n] == 0) {
numr = -1;
vroots.push_back(-1);
return vroots;
}
// initialize estimate for 1st root pair
quad[0] = 2.71828e-1;
quad[1] = 3.14159e-1;
// get roots
get_quads(a, n, quad, x);
numr = roots2(x, n, wr, wi);
for (i = 0; i<n; i++) {
if ((wr[i] != 0.0) || (wi[i] != 0.0))
vroots.push_back(wr[i]);
}
return vroots;
}
void rootsX(double *a, int n, double *wr, double *wi, int* numr)
{
double x[21], quad[2], err, t;
int iter, i;
if ((n < 1) || (n > 20)) {
*numr = -1;
return;
}
// get coefficients of polynomial
//"Error! Lowest coefficient (constant term) cannot be 0."
if (a[n] == 0) {
*numr = -1;
return;
}
// initialize estimate for 1st root pair
quad[0] = 2.71828e-1;
quad[1] = 3.14159e-1;
// get roots
get_quads(a, n, quad, x);
*numr = roots2(x, n, wr, wi);
return;
}
//
// Extract individual real or complex roots from list of quadratic factors
//
int roots2(double *a,int n,double *wr,double *wi)
{
double sq,b2,c,disc;
int i,m,numroots;
m = n;
numroots = 0;
while (m > 1) {
b2 = -0.5*a[m-2];
c = a[m-1];
disc = b2*b2-c;
if (disc < 0.0) { // complex roots
sq = sqrt(-disc);
wr[m-2] = b2;
wi[m-2] = sq;
wr[m-1] = b2;
wi[m-1] = -sq;
numroots+=2;
}
else { // real roots
sq = sqrt(disc);
wr[m-2] = fabs(b2)+sq;
if (b2 < 0.0) wr[m-2] = -wr[m-2];
if (wr[m-2] == 0)
wr[m-1] = 0;
else {
wr[m-1] = c/wr[m-2];
numroots+=2;
}
wi[m-2] = 0.0;
wi[m-1] = 0.0;
}
m -= 2;
}
if (m == 1) {
wr[0] = -a[0];
wi[0] = 0.0;
numroots++;
}
return numroots;
}
//
// Deflate polynomial 'a' by dividing out 'quad'. Return quotient
// polynomial in 'b' and error metric based on remainder in 'err'.
//
void deflate(double *a,int n,double *b,double *quad,double *err)
{
double r,s;
int i;
r = quad[1];
s = quad[0];
b[1] = a[1] - r;
for (i=2;i<=n;i++){
b[i] = a[i] - r * b[i-1] - s * b[i-2];
}
*err = fabs(b[n])+fabs(b[n-1]);
}
//
// Find quadratic factor using Bairstow's method (quadratic Newton method).
// A number of ad hoc safeguards are incorporated to prevent stalls due
// to common difficulties, such as zero slope at iteration point, and
// convergence problems.
//
// Bairstow's method is sensitive to the starting estimate. It is possible
// for convergence to fail or for 'wild' values to trigger an overflow.
//
// It is advisable to institute traps for these problems. (To do!)
//
void find_quad(double *a,int n,double *b,double *quad,double *err, int *iter)
{
double *c,dn,dr,ds,drn,dsn,eps,r,s;
int i;
c = new double [n+1];
c[0] = 1.0;
r = quad[1];
s = quad[0];
eps = 1e-15;
*iter = 1;
do {
if (*iter > maxiter) break;
if (((*iter) % 200) == 0) {
eps *= 10.0;
}
b[1] = a[1] - r;
c[1] = b[1] - r;
for (i=2;i<=n;i++){
b[i] = a[i] - r * b[i-1] - s * b[i-2];
c[i] = b[i] - r * c[i-1] - s * c[i-2];
}
dn=c[n-1] * c[n-3] - c[n-2] * c[n-2];
drn=b[n] * c[n-3] - b[n-1] * c[n-2];
dsn=b[n-1] * c[n-1] - b[n] * c[n-2];
if (fabs(dn) < 1e-10) {
if (dn < 0.0) dn = -1e-8;
else dn = 1e-8;
}
dr = drn / dn;
ds = dsn / dn;
r += dr;
s += ds;
(*iter)++;
} while ((fabs(dr)+fabs(ds)) > eps);
quad[0] = s;
quad[1] = r;
*err = fabs(ds)+fabs(dr);
delete [] c;
}
//
// Differentiate polynomial 'a' returning result in 'b'.
//
void diff_poly(double *a,int n,double *b)
{
double coef;
int i;
coef = (double)n;
b[0] = 1.0;
for (i=1;i<n;i++) {
b[i] = a[i]*((double)(n-i))/coef;
}
}
//
// Attempt to find a reliable estimate of a quadratic factor using modified
// Bairstow's method with provisions for 'digging out' factors associated
// with multiple roots.
//
// This resursive routine operates on the principal that differentiation of
// a polynomial reduces the order of all multiple roots by one, and has no
// other roots in common with it. If a root of the differentiated polynomial
// is a root of the original polynomial, there must be multiple roots at
// that location. The differentiated polynomial, however, has lower order
// and is easier to solve.
//
// When the original polynomial exhibits convergence problems in the
// neighborhood of some potential root, a best guess is obtained and tried
// on the differentiated polynomial. The new best guess is applied
// recursively on continually differentiated polynomials until failure
// occurs. At this point, the previous polynomial is accepted as that with
// the least number of roots at this location, and its estimate is
// accepted as the root.
//
void recurse(double *a,int n,double *b,int m,double *quad,
double *err,int *iter)
{
double *c,*x,rs[2],tst,e1,e2;
if (fabs(b[m]) < 1e-16) m--; // this bypasses roots at zero
if (m == 2) {
quad[0] = b[2];
quad[1] = b[1];
*err = 0;
*iter = 0;
return;
}
c = new double [m+1];
x = new double [n+1];
c[0] = x[0] = 1.0;
rs[0] = quad[0];
rs[1] = quad[1];
*iter = 0;
find_quad(b,m,c,rs,err,iter);
tst = fabs(rs[0]-quad[0])+fabs(rs[1]-quad[1]);
if (*err < 1e-12) {
quad[0] = rs[0];
quad[1] = rs[1];
}
// tst will be 'large' if we converge to wrong root
if (((*iter > 5) && (tst < 1e-4)) || ((*iter > 20) && (tst < 1e-1))) {
diff_poly(b,m,c);
recurse(a,n,c,m-1,rs,err,iter);
quad[0] = rs[0];
quad[1] = rs[1];
}
delete [] x;
delete [] c;
}
//
// Top level routine to manage the determination of all roots of the given
// polynomial 'a', returning the quadratic factors (and possibly one linear
// factor) in 'x'.
//
void get_quads(double *a,int n,double *quad,double *x)
{
double *b,*z,err,tmp;
double xr,xs;
int iter,i,m;
if ((tmp = a[0]) != 1.0) {
a[0] = 1.0;
for (i=1;i<=n;i++) {
a[i] /= tmp;
}
}
if (n == 2) {
x[0] = a[1];
x[1] = a[2];
return;
}
else if (n == 1) {
x[0] = a[1];
return;
}
m = n;
b = new double [n+1];
z = new double [n+1];
b[0] = 1.0;
for (i=0;i<=n;i++) {
z[i] = a[i];
x[i] = 0.0;
}
do {
if (n > m) {
quad[0] = 3.14159e-1;
quad[1] = 2.78127e-1;
}
do { // This loop tries to assure convergence
for (i=0;i<5;i++) {
find_quad(z,m,b,quad,&err,&iter);
if ((err > 1e-7) || (iter > maxiter)) {
diff_poly(z,m,b);
recurse(z,m,b,m-1,quad,&err,&iter);
}
deflate(z,m,b,quad,&err);
if (err < 0.001) break;
srand(time(NULL));
quad[0] = rand() % 8 - 4.0;
quad[1] = rand() % 8 - 4.0;
}
if (err > 0.01) {
std::cout << "Error! Convergence failure in quadratic x^2 + r*x + s." << std::endl;
std::cout << "Enter new trial value for 'r': ";
std::cin >> quad[1];
std::cout << "Enter new trial value for 's' ( 0 to exit): ";
std::cin >> quad[0];
if (quad[0] == 0) exit(1);
}
} while (err > 0.01);
x[m-2] = quad[1];
x[m-1] = quad[0];
m -= 2;
for (i=0;i<=m;i++) {
z[i] = b[i];
}
} while (m > 2);
if (m == 2) {
x[0] = b[1];
x[1] = b[2];
}
else x[0] = b[1];
delete [] z;
delete [] b;
}