forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPolynomialSolversUnivariate.cxx
2228 lines (1995 loc) · 56.2 KB
/
vtkPolynomialSolversUnivariate.cxx
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPolynomialSolversUnivariate.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================
Copyright 2007 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
license for use of this work by or on behalf of the
U.S. Government. Redistribution and use in source and binary forms, with
or without modification, are permitted provided that this Notice and any
statement of authorship are reproduced on all copies.
Contact: pppebay@sandia.gov,dcthomp@sandia.gov
=========================================================================*/
#include "vtkPolynomialSolversUnivariate.h"
#include "vtkDataArray.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include <cmath>
#define VTK_SIGN(x) (((x) < 0) ? (-1) : (1))
vtkStandardNewMacro(vtkPolynomialSolversUnivariate);
static const double sqrt3 = sqrt(static_cast<double>(3.));
static const double inv3 = 1 / 3.;
static const double absolute0 = 10. * VTK_DBL_MIN;
double vtkPolynomialSolversUnivariate::DivisionTolerance = 1e-8; // sqrt( VTK_DBL_EPSILON );
//------------------------------------------------------------------------------
void vtkPolynomialSolversUnivariate::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent
<< "(s) DivisionTolerance: " << vtkPolynomialSolversUnivariate::GetDivisionTolerance() << "\n";
}
//------------------------------------------------------------------------------
// Print the polynomial for debuggery
ostream& vtkPolynomialSolversUnivariate::PrintPolynomial(ostream& os, double* P, int degP)
{
os << "\n";
os << "The polynomial has degree " << degP << "\n";
if (degP < 0)
{
os << "0\n";
return os;
}
if (degP == 0)
{
os << P[0] << "\n";
return os;
}
unsigned int degPm1 = degP - 1;
for (unsigned int i = 0; i < degPm1; ++i)
{
if (P[i] > 0)
{
if (i)
{
os << "+";
}
if (P[i] != 1.)
{
os << P[i] << "*";
}
os << "x**" << degP - i;
}
else if (P[i] < 0)
{
os << P[i] << "*x**" << degP - i;
}
}
if (degP > 0)
{
if (P[degPm1] > 0)
{
os << "+" << P[degPm1] << "*x";
}
else if (P[degPm1] < 0)
{
os << P[degPm1] << "*x";
}
}
if (P[degP] > 0)
{
os << "+" << P[degP];
}
else if (P[degP] < 0)
{
os << P[degP];
}
os << "\n";
return os;
}
//------------------------------------------------------------------------------
// Double precision comparison with 0
inline bool IsZero(double x)
{
return (fabs(x) < absolute0) ? true : false;
}
//------------------------------------------------------------------------------
// Double precision comparison
inline bool AreEqual(double x, double y, double rTol)
{
double delta = fabs(x - y);
// First, handle "absolute" zeros. This is to eliminate the
// case (x+t - -x ) / x = 2 + t/x even when both x and t are small.
if (delta < absolute0)
{
return true;
}
// Second, handle "relative" equalities.
double absx = fabs(x);
double absy = fabs(y);
if (absx > absy)
{
return delta > rTol * absx ? false : true;
}
else
{
return delta > rTol * absy ? false : true;
}
}
//------------------------------------------------------------------------------
// Polynomial Euclidean division of A (deg m) by B (deg n).
static int polynomialEucliDiv(double* A, int m, double* B, int n, double* Q, double* R, double rtol)
{
// Note: for execution speed, no sanity checks are performed on A and B.
// You must know what you are doing.
int mMn = m - n;
int i;
if (mMn < 0)
{
Q[0] = 0.;
for (i = 0; i <= m; ++i)
{
R[i] = A[i];
}
return m;
}
double iB0 = 1. / B[0];
if (!n)
{
for (i = 0; i <= m; ++i)
{
Q[i] = A[i] * iB0;
}
return -1;
}
int nj;
for (i = 0; i <= mMn; ++i)
{
nj = i > n ? n : i;
Q[i] = A[i];
for (int j = 1; j <= nj; ++j)
{
Q[i] -= B[j] * Q[i - j];
}
Q[i] *= iB0;
}
bool nullCoeff = false;
int r = 0;
for (i = 1; i <= n; ++i)
{
double sum = 0;
nj = mMn + 1 > i ? i : mMn + 1;
for (int j = 0; j < nj; ++j)
{
sum += B[n - i + 1 + j] * Q[mMn - j];
}
if (!AreEqual(A[m - i + 1], sum, rtol))
{
R[n - i] = A[m - i + 1] - sum;
r = i - 1;
}
else
{
R[n - i] = 0.;
if (n == i)
{
nullCoeff = true;
}
}
}
if (!r && nullCoeff)
{
return -1;
}
return r;
}
//------------------------------------------------------------------------------
// Polynomial Euclidean division of A (deg m) by B (deg n).
// Does not store Q and stores -R instead of R
static int polynomialEucliDivOppositeR(double* A, int m, double* B, int n, double* mR, double rtol)
{
// Note: for execution speed, no sanity checks are performed on A and B.
// You must know what you are doing.
int mMn = m - n;
int i;
if (mMn < 0)
{
for (i = 0; i <= m; ++i)
{
mR[i] = A[i];
}
return m;
}
if (!n)
{
return -1;
}
int nj;
double iB0 = 1. / B[0];
double* Q = new double[mMn + 1];
for (i = 0; i <= mMn; ++i)
{
nj = i > n ? n : i;
Q[i] = A[i];
for (int j = 1; j <= nj; ++j)
{
Q[i] -= B[j] * Q[i - j];
}
Q[i] *= iB0;
}
bool nullCoeff = false;
int r = 0;
for (i = 1; i <= n; ++i)
{
double sum = 0;
nj = mMn + 1 > i ? i : mMn + 1;
for (int j = 0; j < nj; ++j)
{
sum += B[n - i + 1 + j] * Q[mMn - j];
}
if (!AreEqual(A[m - i + 1], sum, rtol))
{
mR[n - i] = sum - A[m - i + 1];
r = i - 1;
}
else
{
mR[n - i] = 0.;
if (n == i)
{
nullCoeff = true;
}
}
}
delete[] Q;
if (!r && nullCoeff)
{
r = -1;
}
return r;
}
//------------------------------------------------------------------------------
inline double vtkNormalizePolyCoeff(double d, double* div = nullptr)
{
static const double high = 18446744073709551616.; // 2^64
static const double reallyBig = 1.e300;
static const double reallyBigInv = 1 / reallyBig;
static const double notThatBig = 1.e30;
static const double notThatBigInv = 1.e-30;
if (fabs(d) < reallyBig)
{
while (fabs(d) > notThatBig)
{
d /= high;
if (div)
{
*div /= high;
}
}
}
if (fabs(d) > reallyBigInv)
{
while (fabs(d) < notThatBigInv)
{
d *= high;
if (div)
{
*div *= high;
}
}
}
return d;
}
//------------------------------------------------------------------------------
// Polynomial Euclidean division of A (deg m) by B (deg n).
// Does not store Q and stores -R instead of R. This premultiplies Ai by mul and
// then divides mR by div. mR MUST have at least the size of m+1, because it is
// used as temporary storage besides as the return value.
// This shouldn't be too much slower than the version without mul and div. It
// has to copy mul*Ai into mR at the beginning. So it has m+1 extra
// multiplications and assignments. When it assigns final values into mR it
// divides by div. So it has deg(mR) extra divisions.
static int polynomialEucliDivOppositeR(
double mul, double* Ai, int m, double* B, int n, double div, double* mR, double rtol)
{
// Note: for execution speed, no sanity checks are performed on A and B.
// You must know what you are doing.
int mMn = m - n;
int i;
// To save space we will use mR *instead* of Ai. Just have to be sure that
// when we write to mR we are writing to a spot that will not be used again.
for (i = 0; i <= m; ++i)
{
mR[i] = mul * Ai[i];
}
// Remember that mMn >= 0 implies m-n >= 0 implies m >= n and so
// m + 1 > n.
if (mMn < 0)
{
return m;
}
if (!n)
{
return -1;
}
div = 1 / div;
int nj;
double iB0 = 1. / B[0];
double* Q = new double[mMn + 1];
for (i = 0; i <= mMn; ++i)
{
nj = i > n ? n : i;
Q[i] = mR[i];
for (int j = 1; j <= nj; ++j)
{
Q[i] -= B[j] * Q[i - j];
}
Q[i] *= iB0;
}
bool nullCoeff = false;
int r = 0;
for (i = n; i >= 1; --i)
{
double sum = 0;
nj = mMn + 1 > i ? i : mMn + 1;
for (int j = 0; j < nj; ++j)
{
sum += B[n - i + 1 + j] * Q[mMn - j];
}
if (!AreEqual(mR[m - i + 1], sum, rtol))
{
// Now we have m + 1 > n implies n - i < m - i + 1. Thus since i is
// decreasing (hence m - i + 1 increasing) we will never use n - i again,
// so we can safely write over it.
mR[n - i] = (sum - mR[m - i + 1]) * div;
// We want the non-zero term with the largest index. So we only write to r
// once.
if (r == 0)
{
mR[n - i] = vtkNormalizePolyCoeff(mR[n - i], &div);
r = i - 1;
}
}
else
{
// See previous comment.
mR[n - i] = 0.;
if (n == i)
{
nullCoeff = true;
}
}
}
delete[] Q;
if (!r && nullCoeff)
{
r = -1;
}
return r;
}
//------------------------------------------------------------------------------
// Evaluate the value of the degree d univariate polynomial P at x
// using Horner's algorithm.
inline double evaluateHorner(double* P, int d, double x)
{
if (d == -1)
{
return 0.;
}
double val = P[0];
for (int i = 1; i <= d; ++i)
{
val = val * x + P[i];
}
return val;
}
static int vtkGetSignChanges(
double* P, int* degP, int* offsets, int count, double val, int* fsign = nullptr)
{
int oldVal = 0;
double v;
int changes = 0;
for (int i = 0; i < count; ++i)
{
v = evaluateHorner(P + offsets[i], degP[i], val);
if (fsign && i == 0)
{
if (IsZero(v))
{
*fsign = 0;
}
else if (v > 0)
{
*fsign = 1;
}
else
{
*fsign = -1;
}
}
if (v == 0)
{
continue;
}
if (v * oldVal < 0)
{
++changes;
oldVal = -oldVal;
}
if (oldVal == 0)
{
oldVal = (v < 0 ? -1 : 1);
}
}
return changes;
}
// ----------------------------------------------------------
// Gets the Habicht sequence. SSS and degrees and ofsets are expected to be
// large enough and the number of non-zero items is returned. P is expected to
// have degree at least 1.
//
// This algorithm is modified from
// BPR, Algorithms in Real Algebraic Geometry, page 318
static int vtkGetHabichtSequence(
double* P, int d, double* SSS, int* degrees, int* offsets, double rtol)
{
// const static double high = pow(2.,256);
degrees[0] = d;
offsets[0] = 0;
int dp1 = d + 1;
double* t = new double[dp1];
double* s = new double[dp1];
degrees[1] = d - 1;
offsets[1] = dp1;
int offset = dp1;
// Set the first two elements SSS = {P, P'}.
for (int m = 0; m < d; ++m)
{
SSS[m] = P[m];
SSS[m + offset] = static_cast<double>(d - m) * SSS[m];
}
SSS[d] = P[d];
t[0] = s[0] = (P[0] > 0. ? 1. : -1.);
t[1] = s[1] = SSS[offset];
int j = 0;
int deg = d;
int degree = d - 1;
int jp1 = 1;
int ip1 = 0;
while (degree > 0 && j < d - 1)
{
int k = deg - degree;
if (k == jp1)
{
s[jp1] = t[jp1];
degrees[k + 1] = polynomialEucliDivOppositeR(s[jp1] * s[jp1], SSS + offsets[ip1],
degrees[ip1], SSS + offset, degree, s[j] * t[ip1], SSS + offsets[k] + degree + 1, rtol);
offsets[k + 1] = offset + 2 * degree - degrees[k + 1];
}
else
{
s[jp1] = 0;
for (int delta = 1; delta < k - j; ++delta)
{
t[jp1 + delta] = (t[jp1] * t[j + delta]) / s[j];
t[jp1 + delta] = vtkNormalizePolyCoeff(t[jp1 + delta]);
if (delta % 2)
{
t[jp1 + delta] *= -1;
}
}
s[k] = t[k];
offsets[k] = offsets[jp1] + degrees[jp1] + 1;
degrees[k] = degrees[jp1];
for (int dg = 0; dg <= degree; ++dg)
{
SSS[offsets[k] + dg] = (s[k] * SSS[offset + dg]) / t[jp1];
}
for (int l = j + 2; l < k; ++l)
{
degrees[l] = -1;
offsets[l] = offsets[k];
s[l] = 0;
}
degrees[k + 1] = polynomialEucliDivOppositeR(t[jp1] * s[k], SSS + offsets[ip1], degrees[ip1],
SSS + offset, degree, s[j] * t[ip1], SSS + offsets[k] + degrees[k] + 1, rtol);
offsets[k + 1] = offsets[k] + 2 * degrees[k] - degrees[k + 1];
}
t[k + 1] = SSS[offsets[k + 1]];
ip1 = jp1;
j = k;
jp1 = j + 1;
degree = degrees[jp1];
offset = offsets[jp1];
}
delete[] s;
delete[] t;
if (degree == 0)
{
return jp1 + 1;
}
else
{
while (degrees[jp1] < 0)
{
--jp1;
}
return jp1 + 1;
}
}
// ----------------------------------------------------------
// Gets the sturm sequence. SSS and degrees and offsets are expected to
// be large enough and the number of non-zero items
// is returned. P is expected to have degree at least 1.
static int vtkGetSturmSequence(
double* P, int d, double* SSS, int* degrees, int* offsets, double rtol)
{
degrees[0] = d;
offsets[0] = 0;
int dp1 = d + 1;
int dm1 = d - 1;
degrees[1] = dm1;
offsets[1] = dp1;
int offset = dp1;
// nSSS will keep track of the number of the index of
// the last item in our list.
int nSSS = 1;
// Set the first two elements SSS = {P, P'}.
for (int k = 0; k < d; ++k)
{
SSS[k] = P[k];
SSS[k + offset] = static_cast<double>(d - k) * P[k];
}
SSS[d] = P[d];
int degree = dm1;
while (degrees[nSSS] > 0)
{
nSSS++;
degrees[nSSS] = polynomialEucliDivOppositeR(SSS + offsets[nSSS - 2], degrees[nSSS - 2],
SSS + offset, degree, SSS + offset + degree + 1, rtol);
offsets[nSSS] = offset + 2 * degree - degrees[nSSS];
offset = offsets[nSSS];
degree = degrees[nSSS];
}
// If the last element is zero then we ignore it.
if (degrees[nSSS] < 0)
{
return nSSS;
}
// Otherwise we include it in our count, because it
// is a constant. (We are returning the number of
// nonzero items, so 1 plus the index of the last).
return nSSS + 1;
}
extern "C"
{
static int vtkPolynomialSolversUnivariateCompareRoots(const void* a, const void* b)
{
double aa = *static_cast<const double*>(a);
double bb = *static_cast<const double*>(b);
if (aa < bb)
{
return -1;
}
if (aa > bb)
{
return 1;
}
return 0;
}
} // extern "C"
// ------------------------------------------------------------
// upperBnds is expected to be large enough.
// intervalType finds roots as follows
// 0 = ]a,b[
// 1 = [a,b[
// 2 = ]a,a]
// 3 = [a,b]
//
// divideGCD uses an integer in case in the future someone wants to add logic to
// whether the gcd is divided. For example something like
// divideGCD == 0 -> never divide
// divideGCD == 1 -> use logic
// divideGCD == 2 -> divide as long as non-constant (ignore the logic).
//
// It probably would have been better to originally have had the tolerance be a
// relative tolerance rather than an absolute tolerance.
static int vtkHabichtOrSturmBisectionSolve(double* P, int d, double* a, double* upperBnds,
double tol, int intervalType, int divideGCD, int method)
{
// Pretend to be one solver or the other (for error reporting)
static const char title1[] = "vtkPolynomialSolversUnivariate::SturmBisectionSolve";
static const char title2[] = "vtkPolynomialSolversUnivariate::HabichtBisectionSolve";
const char* title = (method == 0 ? title1 : title2);
// 0. Stupidity checks
if (tol <= 0)
{
vtkGenericWarningMacro(<< title << ": Tolerance must be positive");
return -1;
}
if (IsZero(P[0]))
{
vtkGenericWarningMacro(<< title << ": Zero leading coefficient");
return -1;
}
if (d < 1)
{
vtkGenericWarningMacro(<< title << ": Degree (" << d << ") < 1");
return -1;
}
if (a[1] < a[0] + tol)
{
vtkGenericWarningMacro(<< title << ": Erroneous interval endpoints and/or tolerance");
return -1;
}
// Check for 0 as a root and reduce the degree if so.
bool zeroroot = false;
if (IsZero(P[d]))
{
zeroroot = true;
while (IsZero(P[d]))
{
--d;
}
}
// Take care of constant polynomials and polynomials of the form a*x^d.
if (d == 0)
{
if (zeroroot)
{
upperBnds[0] = 0.;
return 1;
}
else
{
return 0;
}
}
// Create one large array to hold all the
// polynomials.
//
// We need two extra spaces because the habicht division uses R to temporarily
// hold mul*A. So there must be at least size(A) space when dividing, which can be
// 2 floats larger than R.
double* SSS = new double[((d + 1) * (d + 2)) / 2 + 2];
int* degrees = new int[d + 1];
int* offsets = new int[d + 1];
double bounds[] = { a[0], a[1] };
int nSSS;
if (method == 0)
{
nSSS = vtkGetSturmSequence(
P, d, SSS, degrees, offsets, vtkPolynomialSolversUnivariate::GetDivisionTolerance());
}
else
{
nSSS = vtkGetHabichtSequence(
P, d, SSS, degrees, offsets, vtkPolynomialSolversUnivariate::GetDivisionTolerance());
}
// If degrees[count-1] > 0 then we have degenerate roots.
// We could possibly then find the degenerate roots.
// Maybe we could more or less remove the degenerate roots.
// Lets do some testwork to see.
if (degrees[nSSS - 1] > 0 && divideGCD == 1)
{
double* R = new double[d + 1];
double* Q = new double[d + 1];
// Get the quotient and call this function again using the
// quotient.
int deg = polynomialEucliDiv(SSS, d, SSS + offsets[nSSS - 1], degrees[nSSS - 1], Q, R,
vtkPolynomialSolversUnivariate::GetDivisionTolerance());
(void)deg; // above result not really needed, but useful in debugging
deg = d - degrees[nSSS - 1];
delete[] R;
// The Habicht sequence will occasionally get infinite coeffs and cause
// unpleasant events to happen with the sequence. In that case Q[0] == 0, thus
// the division is not used.
if (!IsZero(Q[0]))
{
delete[] SSS;
delete[] degrees;
delete[] offsets;
int rval =
vtkHabichtOrSturmBisectionSolve(Q, deg, a, upperBnds, tol, intervalType, 0, method);
delete[] Q;
if (zeroroot)
{
upperBnds[rval] = 0;
return rval + 1;
}
return rval;
}
else
{
delete[] Q;
}
}
// Move away from zeros on the edges. We can also slightly speed up
// computation by keeping the fact that these are roots and
// continuing on.
// If perturbation is too small compared to the bounds then we won't move when
// we perturb.
double perturbation =
fmax(fmax(fabs(bounds[0]) * 1e-12, fabs(bounds[1]) * 1e-12), .5 * tol / static_cast<double>(d));
int varSgn[] = { 0, 0 };
varSgn[0] = vtkGetSignChanges(SSS, degrees, offsets, nSSS, bounds[0]);
varSgn[1] = vtkGetSignChanges(SSS, degrees, offsets, nSSS, bounds[1]);
for (int k = 0; k <= 1; ++k)
{
if (IsZero(evaluateHorner(SSS, d, bounds[k])))
{
int leftVarSgn = varSgn[k];
int rightVarSgn = varSgn[k];
double leftx = bounds[k];
double rightx = bounds[k];
// Make sure we move far enough away that everything still works. That is
// we needs to be non-zero and have the sequence realize that we've
// got a zero in the interval. It is possible that this causes our code
// to slow down. Should probably play around with how large the
// perturbation should be. Also it is possible (but unlikely) that our inexact
// sturm sequence doesn't realize there is a zero here.
//
// JUST AS WITH THE BISECTING, NEED TO MAKE SURE WE DON'T HAVE AN
// INFINITE LOOP.
while (IsZero(evaluateHorner(SSS, d, leftx)) || IsZero(evaluateHorner(SSS, d, rightx)) ||
leftVarSgn <= rightVarSgn ||
((leftVarSgn == varSgn[k] || rightVarSgn == varSgn[k]) && leftVarSgn - rightVarSgn != 1))
{
leftx -= perturbation;
rightx += perturbation;
leftVarSgn = vtkGetSignChanges(SSS, degrees, offsets, nSSS, leftx);
rightVarSgn = vtkGetSignChanges(SSS, degrees, offsets, nSSS, rightx);
}
// Move properly according to what kind of sequence we are searching.
if ((!(intervalType & 2) && k == 1) || ((intervalType & 1) && k == 0))
{
bounds[k] = leftx;
varSgn[k] = leftVarSgn;
}
else
{
bounds[k] = rightx;
varSgn[k] = rightVarSgn;
}
}
}
// If we don't have roots then leave here.
int nRoots = varSgn[0] - varSgn[1];
if (nRoots < 1)
{
upperBnds[0] = 0;
delete[] SSS;
delete[] degrees;
delete[] offsets;
if (zeroroot)
return 1;
return 0;
}
// 2. Root bracketing
// Initialize the bounds for the root intervals. The interval
// ]lowerBnds[i], upperBnds[i][ contains the i+1 root. We will
// see if we can completely separate the roots. Of course
// the interval ]bounds[0], bounds[1][ contains all the roots.
// Afterwards if some intervals are the same all but
// one will be removed.
int i;
double* lowerBnds = new double[nRoots];
for (i = 0; i < nRoots; ++i)
{
upperBnds[i] = bounds[1];
lowerBnds[i] = bounds[0];
}
int leftVarSgn, rightVarSgn, tempSgn;
double leftx, rightx;
bool hitroot;
int nloc = nRoots - 1;
while (nloc >= 1)
{
// Only one root according to Sturm or the interval is
// small enough to consider the same root.
if (upperBnds[nloc] - lowerBnds[nloc] <= tol ||
((nloc < 1 || (upperBnds[nloc - 1] < lowerBnds[nloc] - tol)) &&
((nloc >= nRoots - 1) || (upperBnds[nloc]) < lowerBnds[nloc + 1] - tol)))
{
--nloc;
continue;
}
// We begin with leftx and rightx being equal and change them only if
// leftx (rightx) is a root. Then we can bracket the root. We do this
// because roots can cause problems (our sequence is inexact so
// even single roots can cause problems. Furthermore if we hit a
// root we may as well bracket it within tol so that we don't have to
// worry about it later.
leftx = (upperBnds[nloc] + lowerBnds[nloc]) / 2;
// If we are going nowhere then quit.
if (leftx >= upperBnds[nloc] || leftx <= lowerBnds[nloc])
{
nloc--;
continue;
}
rightx = leftx;
hitroot = false;
leftVarSgn = rightVarSgn = tempSgn = vtkGetSignChanges(SSS, degrees, offsets, nSSS, rightx);
// Move away if we have a root, just like we did with the initial endpoints.
// Maybe could write a function to do this, but it may be slower.
// After moving away we want the following things to be handled:
// 1 Not zero at the endpoints (leftx or rightx)
// 2 Sign changes at left != sign changes at right.
// 3 No "crazy" values:
// a. sign[left] > sign[0]
// b. sign[right] < sign[1]
// c. sign[right] > sign[left].
// 4 Does not take too long.
//
// It would be convenient if we could get sign[left]-sign[right] = 1, but
// in reality we may be too close for our values to even be worthwhile.
//
// The question remains, "What happens if we move away too far?" There must
// be some distance after which we quit perturbing and deal with the fact
// that our sequence is not perfect. This will take care of 4, but we still
// need to handle what has just happened.
//
// Option 2: In this step we are only worried about bracketing the
// roots. If the midpoint is a root, we could try this. Suppose a=0, b=1.
// Then if we get 1/2 is a root, we check whether 1/4 and 3/4 work. If
// one of them does then call that our "mid". Then in the next step we
// will be moved away from 1/2. If neither of them work then we could
// do what we are doing here, or try 1/8, 3/8, 5/8, and 7/8. In this latter
// case we should make sure we don't come back to that messy point later.
// This option removes the use of one of leftx or rightx.
// So we need to have the following not happen.
//
// 1. Not zero at mid.
// 2. No "crazy" valus:
// a. sign[mid] > sign[0]
// b. sign[mid] < sign[1].
// 3. Does no take too long.
//
// This method is better because the "perturbation" is always relative to
// the endpoints. Unlike the other method, because the perturbation
// variable is relative to the larger endpoing.
if (IsZero(leftx) || IsZero(evaluateHorner(SSS, d, leftx)) || (tempSgn > varSgn[0]) ||
(tempSgn < nloc))
{
int step = 2;
int pos = 1;
double p2 = 4.;
double mid = upperBnds[nloc] / p2 + (p2 - pos) * lowerBnds[nloc] / p2;
bool found = false;
leftVarSgn = vtkGetSignChanges(SSS, degrees, offsets, nSSS, lowerBnds[nloc]);
rightVarSgn = vtkGetSignChanges(SSS, degrees, offsets, nSSS, upperBnds[nloc]);
tempSgn = vtkGetSignChanges(SSS, degrees, offsets, nSSS, mid);
while (
// 3.
step < 10 &&
// 2a.
((tempSgn > leftVarSgn) ||
// 2b.
(tempSgn < rightVarSgn) ||
// 1
IsZero(evaluateHorner(SSS, d, mid)) || IsZero(mid)))
{
pos += 2;
if (pos > p2)
{
pos = 1;
++step;
p2 *= 2.;
}