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
|
/*************************************************************************
* Copyright (C) 2009-2010 Tavian Barnes <tavianator@gmail.com> *
* *
* This file is part of The Dimension Library. *
* *
* The Dimension Library is free software; you can redistribute it and/ *
* or modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
* The Dimension Library is distributed in the hope that it will be *
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
*************************************************************************/
#include "dimension.h"
#include <math.h>
/* Basic solving methods */
static inline size_t
dmnsn_solve_linear(double poly[2], double x[1])
{
x[0] = -poly[0]/poly[1];
return (x[0] >= 0.0) ? 1 : 0;
}
static inline size_t
dmnsn_solve_quadratic(double poly[3], double x[2])
{
double disc = poly[1]*poly[1] - 4.0*poly[0]*poly[2];
if (disc >= 0.0) {
double s = sqrt(disc);
x[0] = (-poly[1] + s)/(2.0*poly[2]);
x[1] = (-poly[1] - s)/(2.0*poly[2]);
return (x[0] >= 0.0) ? ((x[1] >= 0.0) ? 2 : 1) : 0;
} else {
return 0;
}
}
static inline size_t
dmnsn_zero_roots(double poly[], size_t *degree, double x[])
{
size_t i = 0;
while (i <= *degree && poly[i] == 0.0) {
x[i] = 0.0;
++i;
}
if (i > 0) {
for (size_t j = 0; j + i <= *degree; ++j) {
poly[j] = poly[j + i];
}
}
*degree -= i;
return i;
}
/* Get the real degree of a polynomial, ignoring leading zeros */
static inline size_t
dmnsn_real_degree(double poly[], size_t degree)
{
for (ssize_t i = degree; i >= 0; ++i) {
if (poly[i] != 0.0) {
return i;
}
}
return 0;
}
/* Improve a root with Newton's method */
static inline double
dmnsn_improve_root(double poly[], size_t degree, double x)
{
double error;
do {
/* Calculate the value of the polynomial and its derrivative at once */
double p = poly[degree], dp = 0.0;
for (ssize_t i = degree - 1; i >= 0; --i) {
dp = dp*x + p;
p = p*x + poly[i];
}
double dx = p/dp;
error = fabs(dx/x);
x -= dx;
} while (error > dmnsn_epsilon);
return x;
}
/* Use the method of bisection to find a root in a range that contains exactly
one root, counting multiplicity */
static inline double
dmnsn_bisect_root(double poly[], size_t degree, double min, double max)
{
double evmin = dmnsn_evaluate_polynomial(poly, degree, min);
double evmax = dmnsn_evaluate_polynomial(poly, degree, max);
while (max - min > dmnsn_epsilon) {
double mid = (min + max)/2.0;
double evmid = dmnsn_evaluate_polynomial(poly, degree, mid);
if (dmnsn_signbit(evmid) == dmnsn_signbit(evmax)) {
max = mid;
evmax = evmid;
} else {
min = mid;
evmin = evmid;
}
}
return (min + max)/2.0;
}
/* Use synthetic division to eliminate the root `r' from poly[] */
static inline void
dmnsn_eliminate_root(double poly[], size_t *degree, double r)
{
double rem = poly[*degree];
for (ssize_t i = *degree - 1; i >= 0; --i) {
double temp = poly[i];
poly[i] = rem;
rem = temp + r*rem;
}
--*degree;
}
/* Returns the number of sign changes between coefficients of `poly' */
static inline size_t
dmnsn_descartes_rule(double poly[], size_t degree)
{
int lastsign = 0;
size_t i;
for (i = 0; i <= degree; ++i) {
if (poly[i] != 0.0) {
lastsign = dmnsn_signbit(poly[i]);
break;
}
}
size_t changes = 0;
for (++i; i <= degree; ++i) {
int sign = dmnsn_signbit(poly[i]);
if (poly[i] != 0.0 && sign != lastsign) {
lastsign = sign;
++changes;
}
}
return changes;
}
/* Get the (n k) binomial coefficient */
static double
dmnsn_binom(size_t n, size_t k)
{
if (k > n - k) {
k = n - k;
}
double ret = 1.0;
for (size_t i = 0; i < k; ++i) {
ret *= n - i;
ret /= i + 1;
}
return ret;
}
/* Find a range that contains a single root, with Uspensky's algorithm */
static bool
dmnsn_uspensky_bound(double poly[], size_t degree, double *min, double *max)
{
size_t n = dmnsn_descartes_rule(poly, degree);
if (n == 0) {
return false;
} else if (n == 1) {
return true;
} else {
double a[degree];
/* a is the expanded form of poly(x + 1) */
for (size_t i = 0; i <= degree; ++i) {
a[i] = poly[i];
for (size_t j = i + 1; j <= degree; ++j) {
a[i] += dmnsn_binom(j, i)*poly[j];
}
}
if (a[0] == 0.0) {
*max = *min;
return true;
} else if (dmnsn_uspensky_bound(a, degree, min, max)) {
*min += 1.0;
*max += 1.0;
return true;
}
double b[degree];
/* b is the expanded form of (x + 1)^degree * poly(1/(x + 1)) */
for (size_t i = 0; i <= degree; ++i) {
b[i] = poly[degree - i];
for (size_t j = i + 1; j <= degree; ++j) {
b[i] += dmnsn_binom(j, i)*poly[degree - j];
}
}
if (dmnsn_uspensky_bound(b, degree, min, max)) {
double temp = *min;
*min = 1.0/(*max + 1.0);
*max = 1.0/(temp + 1.0);
return true;
}
return false;
}
}
/* Modified Uspensky's algorithm */
size_t
dmnsn_solve_polynomial(double poly[], size_t degree, double x[])
{
/* Copy the polynomial so we can be destructive */
double p[degree + 1];
for (ssize_t i = degree; i >= 0; --i) {
p[i] = poly[i];
}
size_t i = 0; /* Index into x[] */
/* Eliminate simple zero roots */
i += dmnsn_zero_roots(p, °ree, x + i);
/* Account for leading zero coefficients */
degree = dmnsn_real_degree(p, degree);
while (degree > 2) {
/* Get a bound on the range of positive roots */
double min = +0.0, max = INFINITY;
if (dmnsn_uspensky_bound(p, degree, &min, &max)) {
if (isinf(max)) {
/* Replace an infinite upper bound with a finite one due to Cauchy */
max = 0.0;
for (size_t j = 0; j < degree; ++j) {
max = dmnsn_max(max, fabs(p[j]));
}
max /= fabs(p[degree]);
max += 1.0;
}
/* Bisect within the found range */
double r = dmnsn_bisect_root(p, degree, min, max);
r = dmnsn_improve_root(p, degree, r);
/* Use synthetic division to eliminate the root `r' */
dmnsn_eliminate_root(p, °ree, r);
/* Store the found root */
x[i] = r;
++i;
} else {
break;
}
i += dmnsn_zero_roots(p, °ree, x + i);
degree = dmnsn_real_degree(p, degree);
}
switch (degree) {
case 1:
i += dmnsn_solve_linear(p, x + i);
break;
case 2:
i += dmnsn_solve_quadratic(p, x + i);
break;
}
return i;
}
void
dmnsn_print_polynomial(FILE *file, double poly[], size_t degree)
{
fprintf(file, "%g*x^%zu", poly[degree], degree);
for (size_t i = degree - 1; i > 1; --i) {
if (poly[i] > 0.0) {
fprintf(file, " + %g*x^%zu", poly[i], i);
} else if (poly[i] < 0.0) {
fprintf(file, " - %g*x^%zu", -poly[i], i);
}
}
if (poly[1] > 0.0) {
fprintf(file, " + %g*x", poly[1]);
} else if (poly[1] < 0.0) {
fprintf(file, " - %g*x", -poly[1]);
}
if (poly[0] > 0.0) {
fprintf(file, " + %g", poly[0]);
} else if (poly[0] < 0.0) {
fprintf(file, " - %g", -poly[0]);
}
fprintf(file, "\n");
}
|