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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
This file is part of LilyPond, the GNU music typesetter.
Copyright (C) 1993--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
LilyPond is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LilyPond 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
*/
#include "polynomial.hh"
#include "warn.hh"
#include <cmath>
using namespace std;
/*
Een beter milieu begint bij uzelf. Hergebruik!
This was ripped from Rayce, a raytracer I once wrote.
*/
Real
Polynomial::eval (Real x) const
{
Real p = 0.0;
// horner's scheme
for (vsize i = coefs_.size (); i--;)
p = x * p + coefs_[i];
return p;
}
Polynomial
Polynomial::multiply (const Polynomial &p1, const Polynomial &p2)
{
Polynomial dest;
ssize_t deg = p1.degree () + p2.degree ();
for (ssize_t i = 0; i <= deg; i++)
{
dest.coefs_.push_back (0);
for (ssize_t j = 0; j <= i; j++)
if (i - j <= p2.degree () && j <= p1.degree ())
dest.coefs_.back () += p1.coefs_[j] * p2.coefs_[i - j];
}
return dest;
}
Real
Polynomial::minmax (Real l, Real r, bool ret_max) const
{
vector<Real> sols;
if (l > r)
{
programming_error ("left bound greater than right bound for polynomial minmax. flipping bounds.");
l = l + r;
r = l - r;
l = l - r;
}
sols.push_back (eval (l));
sols.push_back (eval (r));
Polynomial deriv (*this);
deriv.differentiate ();
vector<Real> maxmins = deriv.solve ();
for (vsize i = 0; i < maxmins.size (); i++)
if (maxmins[i] >= l && maxmins[i] <= r)
sols.push_back (eval (maxmins[i]));
vector_sort (sols, less<Real> ());
return ret_max ? sols.back () : sols[0];
}
void
Polynomial::differentiate ()
{
for (int i = 1; i <= degree (); i++)
coefs_[i - 1] = coefs_[i] * i;
coefs_.pop_back ();
}
Polynomial
Polynomial::power (int exponent, const Polynomial &src)
{
int e = exponent;
Polynomial dest (1), base (src);
/*
classic int power. invariant: src^exponent = dest * src ^ e
greetings go out to Lex Bijlsma & Jaap vd Woude */
while (e > 0)
{
if (e % 2)
{
dest = multiply (dest, base);
e--;
}
else
{
base = multiply (base, base);
e /= 2;
}
}
return dest;
}
static Real const FUDGE = 1e-8;
void
Polynomial::clean ()
{
/*
We only do relative comparisons. Absolute comparisons break down in
degenerate cases. */
while (degree () > 0
&& (fabs (coefs_.back ()) < FUDGE * fabs (back (coefs_, 1))
|| !coefs_.back ()))
coefs_.pop_back ();
}
void
Polynomial::operator += (Polynomial const &p)
{
while (degree () < p.degree ())
coefs_.push_back (0.0);
for (int i = 0; i <= p.degree (); i++)
coefs_[i] += p.coefs_[i];
}
void
Polynomial::operator -= (Polynomial const &p)
{
while (degree () < p.degree ())
coefs_.push_back (0.0);
for (int i = 0; i <= p.degree (); i++)
coefs_[i] -= p.coefs_[i];
}
void
Polynomial::scalarmultiply (Real fact)
{
for (int i = 0; i <= degree (); i++)
coefs_[i] *= fact;
}
void
Polynomial::set_negate (const Polynomial &src)
{
for (int i = 0; i <= src.degree (); i++)
coefs_[i] = -src.coefs_[i];
}
/// mod of #u/v#
int
Polynomial::set_mod (const Polynomial &u, const Polynomial &v)
{
(*this) = u;
if (v.lc () < 0.0)
{
for (ssize_t k = u.degree () - v.degree () - 1; k >= 0; k -= 2)
coefs_[k] = -coefs_[k];
for (ssize_t k = u.degree () - v.degree (); k >= 0; k--)
for (ssize_t j = v.degree () + k - 1; j >= k; j--)
coefs_[j] = -coefs_[j] - coefs_[v.degree () + k] * v.coefs_[j - k];
}
else
{
for (ssize_t k = u.degree () - v.degree (); k >= 0; k--)
for (ssize_t j = v.degree () + k - 1; j >= k; j--)
coefs_[j] -= coefs_[v.degree () + k] * v.coefs_[j - k];
}
ssize_t k = v.degree () - 1;
while (k >= 0 && coefs_[k] == 0.0)
k--;
coefs_.resize (1 + ((k < 0) ? 0 : k));
return degree ();
}
void
Polynomial::check_sol (Real x) const
{
Real f = eval (x);
Polynomial p (*this);
p.differentiate ();
Real d = p.eval (x);
if (abs (f) > abs (d) * FUDGE)
programming_error ("not a root of polynomial\n");
}
void
Polynomial::check_sols (vector<Real> roots) const
{
for (vsize i = 0; i < roots.size (); i++)
check_sol (roots[i]);
}
Polynomial::Polynomial (Real a, Real b)
{
coefs_.push_back (a);
if (b)
coefs_.push_back (b);
}
/* cubic root. */
inline Real cubic_root (Real x)
{
if (x > 0.0)
return pow (x, 1.0 / 3.0);
else if (x < 0.0)
return -pow (-x, 1.0 / 3.0);
return 0.0;
}
static bool
iszero (Real r)
{
return !r;
}
vector<Real>
Polynomial::solve_cubic ()const
{
vector<Real> sol;
/* normal form: x^3 + Ax^2 + Bx + C = 0 */
Real A = coefs_[2] / coefs_[3];
Real B = coefs_[1] / coefs_[3];
Real C = coefs_[0] / coefs_[3];
/*
* substitute x = y - A/3 to eliminate quadric term: x^3 +px + q = 0
*/
Real sq_A = A * A;
Real p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
Real q = 1.0 / 2 * (2.0 / 27 * A * sq_A - 1.0 / 3 * A * B + C);
/* use Cardano's formula */
Real cb = p * p * p;
Real D = q * q + cb;
if (iszero (D))
{
if (iszero (q)) /* one triple solution */
{
sol.push_back (0);
sol.push_back (0);
sol.push_back (0);
}
else /* one single and one double solution */
{
Real u = cubic_root (-q);
sol.push_back (2 * u);
sol.push_back (-u);
}
}
else if (D < 0)
{
/* Casus irreducibilis: three real solutions */
Real phi = 1.0 / 3 * acos (-q / sqrt (-cb));
Real t = 2 * sqrt (-p);
sol.push_back (t * cos (phi));
sol.push_back (-t * cos (phi + M_PI / 3));
sol.push_back (-t * cos (phi - M_PI / 3));
}
else
{
/* one real solution */
Real sqrt_D = sqrt (D);
Real u = cubic_root (sqrt_D - q);
Real v = -cubic_root (sqrt_D + q);
sol.push_back (u + v);
}
/* resubstitute */
Real sub = 1.0 / 3 * A;
for (vsize i = sol.size (); i--;)
{
sol[i] -= sub;
#ifdef PARANOID
assert (fabs (eval (sol[i])) < 1e-8);
#endif
}
return sol;
}
Real
Polynomial::lc () const
{
return coefs_.back ();
}
Real &
Polynomial::lc ()
{
return coefs_.back ();
}
ssize_t
Polynomial::degree ()const
{
return coefs_.size () - 1;
}
/*
all roots of quadratic eqn.
*/
vector<Real>
Polynomial::solve_quadric ()const
{
vector<Real> sol;
/* normal form: x^2 + px + q = 0 */
Real p = coefs_[1] / (2 * coefs_[2]);
Real q = coefs_[0] / coefs_[2];
Real D = p * p - q;
if (D > 0)
{
D = sqrt (D);
sol.push_back (D - p);
sol.push_back (-D - p);
}
return sol;
}
/* solve linear equation */
vector<Real>
Polynomial::solve_linear ()const
{
vector<Real> s;
if (coefs_[1])
s.push_back (-coefs_[0] / coefs_[1]);
return s;
}
vector<Real>
Polynomial::solve () const
{
Polynomial *me = (Polynomial *) this;
me->clean ();
switch (degree ())
{
case 1:
return solve_linear ();
case 2:
return solve_quadric ();
case 3:
return solve_cubic ();
}
vector<Real> s;
return s;
}
void
Polynomial::operator *= (Polynomial const &p2)
{
*this = multiply (*this, p2);
}
|