16int roots(
float* a,
int n,
float* wr,
float* wi)
18 float sq, b2, c, disc;
37 wr[m - 2] = fabs(b2) + sq;
38 if (b2 < 0.0) wr[m - 2] = -wr[m - 2];
39 if (wr[m - 2] == 0) wr[m - 1] = 0;
41 wr[m - 1] = c / wr[m - 2];
60void deflate(
float* a,
int n,
float* b,
float* quad,
float* err)
70 for (i = 2; i <= n; i++) {
71 b[i] = a[i] - r * b[i - 1] - s * b[i - 2];
73 *err = fabs(b[n]) + fabs(b[n - 1]);
86void find_quad(
float* a,
int n,
float* b,
float* quad,
float* err,
int* iter)
88 float *c, dn, dr, ds, drn, dsn, eps, r, s;
100 if (((*iter) % 200) == 0) { eps *= 10.0; }
104 for (i = 2; i <= n; i++) {
105 b[i] = a[i] - r * b[i - 1] - s * b[i - 2];
106 c[i] = b[i] - r * c[i - 1] - s * c[i - 2];
108 dn = c[n - 1] * c[n - 3] - c[n - 2] * c[n - 2];
109 drn = b[n] * c[n - 3] - b[n - 1] * c[n - 2];
110 dsn = b[n - 1] * c[n - 1] - b[n] * c[n - 2];
112 if (fabs(dn) < 1e-10) {
113 if (dn < 0.0) dn = -1e-8;
122 }
while ((fabs(dr) + fabs(ds)) > eps);
125 *err = fabs(ds) + fabs(dr);
138 for (i = 1; i < n; i++) {
139 b[i] = a[i] * ((float) (n - i)) / coef;
162void recurse(
float* a,
int n,
float* b,
int m,
float* quad,
float* err,
int* iter)
164 float *c, *
x, rs[2], tst;
166 if (fabs(b[m]) < 1e-16) m--;
174 c =
new float[m + 1];
175 x =
new float[n + 1];
181 tst = fabs(rs[0] - quad[0]) + fabs(rs[1] - quad[1]);
187 if (((*iter > 5) && (tst < 1e-4)) || ((*iter > 20) && (tst < 1e-1))) {
189 recurse(a, n, c, m - 1, rs, err, iter);
203 float *b, *z, err, tmp;
206 if ((tmp = a[0]) != 1.0) {
208 for (i = 1; i <= n; i++) {
222 b =
new float[n + 1];
223 z =
new float[n + 1];
225 for (i = 0; i <= n; i++) {
231 quad[0] = 3.14159e-1;
232 quad[1] = 2.78127e-1;
235 for (i = 0; i < 5; i++) {
237 if ((err > 1e-7) || (iter >
maxiter)) {
239 recurse(z, m, b, m - 1, quad, &err, &iter);
242 if (err < 0.001)
break;
247 printf(
"Error! Convergence failure in quadratic x^2 + r*x + s.\n");
250 }
while (err > 0.01);
254 for (i = 0; i <= m; i++) {
270 float* quad =
new float[2];
271 float*
x =
new float[n];
274 quad[0] = 2.71828e-1;
275 quad[1] = 3.14159e-1;
279 numr =
roots(
x, n, wr, wi);
friend fvec sqrt(const fvec &a)
void polynomComplexRoots(float *wr, float *wi, int n, float *a, int &numr)
int roots(float *a, int n, float *wr, float *wi)
void diff_poly(float *a, int n, float *b)
void find_quad(float *a, int n, float *b, float *quad, float *err, int *iter)
void deflate(float *a, int n, float *b, float *quad, float *err)
void get_quads(float *a, int n, float *quad, float *x)
void recurse(float *a, int n, float *b, int m, float *quad, float *err, int *iter)