-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrootfinder.cpp
More file actions
1548 lines (1282 loc) · 52.3 KB
/
rootfinder.cpp
File metadata and controls
1548 lines (1282 loc) · 52.3 KB
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
//=======================================================================
// Copyright (C) 2003-2013 William Hallahan
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//=======================================================================
//**********************************************************************
// File: PolynomialRootFinder.cpp
// Author: Bill Hallahan
// Date: January 30, 2003
//
// Abstract:
//
// This file contains the implementation for class
// PolynomialRootFinder.
//
//**********************************************************************
#include <math.h>
#include <float.h>
#include "rootfinder.h"
//======================================================================
// Local constants.
//======================================================================
namespace
{
//------------------------------------------------------------------
// The following machine constants are used in this method.
//
// f_BASE The base of the floating point number system used.
//
// f_ETA The maximum relative representation error which
// can be described as the smallest positive floating
// point number such that 1.0 + f_ETA is greater than 1.0.
//
// f_MAXIMUM_FLOAT The largest floating point number.
//
// f_MINIMUM_FLOAT The smallest positive floating point number.
//
//------------------------------------------------------------------
const float f_BASE = 2.0;
const float f_ETA = FLT_EPSILON;
const float f_ETA_N = (float)(10.0) * f_ETA;
const float f_ETA_N_SQUARED = (float)(100.0) * f_ETA;
const float f_MAXIMUM_FLOAT = FLT_MAX;
const float f_MINIMUM_FLOAT = FLT_MIN;
const float f_XX_INITIAL_VALUE = (float)(0.70710678);
const float f_COSR_INITIAL_VALUE = (float)(-0.069756474);
const float f_SINR_INITIAL_VALUE = (float)(0.99756405);
};
//======================================================================
// Constructor: PolynomialRootFinder::PolynomialRootFinder
//======================================================================
PolynomialRootFinder::PolynomialRootFinder()
{
}
//======================================================================
// Destructor: PolynomialRootFinder::~PolynomialRootFinder
//======================================================================
PolynomialRootFinder::~PolynomialRootFinder()
{
}
//======================================================================
// Member Function: PolynomialRootFinder::FindRoots
//
// Abstract:
//
// This method determines the roots of a polynomial which
// has real coefficients. This code is based on FORTRAN
// code published in reference [1]. The method is based on
// an algorithm the three-stage algorithm described in
// Jenkins and Traub [2].
//
// 1. "Collected Algorithms from ACM, Volume III", Algorithms 493-545
// 1983. (The root finding algorithms is number 493)
//
// 2. Jenkins, M. A. and Traub, J. F., "A three-stage algorithm for
// real polynomials using quadratic iteration", SIAM Journal of
// Numerical Analysis, 7 (1970), 545-566
//
// 3. Jenkins, M. A. and Traub, J. F., "Principles for testing
// polynomial zerofinding programs", ACM TOMS 1,
// 1 (March 1975), 26-34
//
//
// Input:
//
// All vectors below must be at least a length equal to degree + 1.
//
// coefficicent_ptr A double precision vector that contains
// the polynomial coefficients in order
// of increasing power.
//
// degree The degree of the polynomial.
//
// real_zero_vector_ptr A double precision vector that will
// contain the real parts of the roots
// of the polynomial when this method
// returns.
//
// imaginary_zero_vector_ptr A double precision vector that will
// contain the real parts of the roots
// of the polynomial when this method
// returns.
//
// number_of_roots_found_ptr A pointer to an integer that will
// equal the number of roots found when
// this method returns. If the method
// returns SUCCESS then this value will
// always equal the degree of the
// polynomial.
//
// Return Value:
//
// The function returns an enum value of type
// 'PolynomialRootFinder::RootStatus_T'.
//
//======================================================================
PolynomialRootFinder::RootStatus_T PolynomialRootFinder::FindRoots(
double * coefficient_vector_ptr,
int degree,
double * real_zero_vector_ptr,
double * imaginary_zero_vector_ptr,
int * number_of_roots_found_ptr)
{
//--------------------------------------------------------------
// The algorithm fails if the polynomial is not at least
// degree on or the leading coefficient is zero.
//--------------------------------------------------------------
PolynomialRootFinder::RootStatus_T status;
if (degree == 0)
{
status = PolynomialRootFinder::SCALAR_VALUE_HAS_NO_ROOTS;
}
else if (coefficient_vector_ptr[degree] == 0.0)
{
status = PolynomialRootFinder::LEADING_COEFFICIENT_IS_ZERO;
}
else
{
//--------------------------------------------------------------
// Allocate temporary vectors used to find the roots.
//--------------------------------------------------------------
m_degree = degree;
std::vector<double> temp_vector;
std::vector<PRF_Float_T> pt_vector;
m_p_vector.resize(m_degree + 1);
m_qp_vector.resize(m_degree + 1);
m_k_vector.resize(m_degree + 1);
m_qk_vector.resize(m_degree + 1);
m_svk_vector.resize(m_degree + 1);
temp_vector.resize(m_degree + 1);
pt_vector.resize(m_degree + 1);
m_p_vector_ptr = &m_p_vector[0];
m_qp_vector_ptr = &m_qp_vector[0];
m_k_vector_ptr = &m_k_vector[0];
m_qk_vector_ptr = &m_qk_vector[0];
m_svk_vector_ptr = &m_svk_vector[0];
double * temp_vector_ptr = &temp_vector[0];
PRF_Float_T * pt_vector_ptr = &pt_vector[0];
//--------------------------------------------------------------
// m_are and m_mre refer to the unit error in + and *
// respectively. they are assumed to be the same as
// f_ETA.
//--------------------------------------------------------------
m_are = f_ETA;
m_mre = f_ETA;
PRF_Float_T lo = f_MINIMUM_FLOAT / f_ETA;
//--------------------------------------------------------------
// Initialization of constants for shift rotation.
//--------------------------------------------------------------
PRF_Float_T xx = f_XX_INITIAL_VALUE;
PRF_Float_T yy = - xx;
PRF_Float_T cosr = f_COSR_INITIAL_VALUE;
PRF_Float_T sinr = f_SINR_INITIAL_VALUE;
m_n = m_degree;
m_n_plus_one = m_n + 1;
//--------------------------------------------------------------
// Make a copy of the coefficients in reverse order.
//--------------------------------------------------------------
int ii = 0;
for (ii = 0; ii < m_n_plus_one; ++ii)
{
m_p_vector_ptr[m_n - ii] = coefficient_vector_ptr[ii];
}
//--------------------------------------------------------------
// Assume failure. The status is set to SUCCESS if all
// the roots are found.
//--------------------------------------------------------------
status = PolynomialRootFinder::FAILED_TO_CONVERGE;
//--------------------------------------------------------------
// If there are any zeros at the origin, remove them.
//--------------------------------------------------------------
int jvar = 0;
while (m_p_vector_ptr[m_n] == 0.0)
{
jvar = m_degree - m_n;
real_zero_vector_ptr[jvar] = 0.0;
imaginary_zero_vector_ptr[jvar] = 0.0;
m_n_plus_one = m_n_plus_one - 1;
m_n = m_n - 1;
}
//--------------------------------------------------------------
// Loop and find polynomial zeros. In the original algorithm
// this loop was an infinite loop. Testing revealed that the
// number of main loop iterations to solve a polynomial of a
// particular degree is usually about half the degree.
// We loop twice that to make sure the solution is found.
// (This should be revisited as it might preclude solving
// some large polynomials.)
//--------------------------------------------------------------
int count = 0;
for (count = 0; count < m_degree; ++count)
{
//----------------------------------------------------------
// Check for less than 2 zeros to finish the solution.
//----------------------------------------------------------
if (m_n <= 2)
{
if (m_n > 0)
{
//--------------------------------------------------
// Calculate the final zero or pair of zeros.
//--------------------------------------------------
if (m_n == 1)
{
real_zero_vector_ptr[m_degree - 1] =
- m_p_vector_ptr[1] / m_p_vector_ptr[0];
imaginary_zero_vector_ptr[m_degree - 1] = 0.0;
}
else
{
SolveQuadraticEquation(
m_p_vector_ptr[0],
m_p_vector_ptr[1],
m_p_vector_ptr[2],
real_zero_vector_ptr[m_degree - 2],
imaginary_zero_vector_ptr[m_degree - 2],
real_zero_vector_ptr[m_degree - 1],
imaginary_zero_vector_ptr[m_degree - 1]);
}
}
m_n = 0;
status = PolynomialRootFinder::SUCCESS;
break;
}
else
{
//------------------------------------------------------
// Find largest and smallest moduli of coefficients.
//------------------------------------------------------
PRF_Float_T max = 0.0;
PRF_Float_T min = f_MAXIMUM_FLOAT;
PRF_Float_T xvar;
for (ii = 0; ii < m_n_plus_one; ++ii)
{
xvar = (PRF_Float_T)(::fabs((PRF_Float_T)(m_p_vector_ptr[ii])));
if (xvar > max)
{
max = xvar;
}
if ((xvar != 0.0) && (xvar < min))
{
min = xvar;
}
}
//------------------------------------------------------
// Scale if there are large or very small coefficients.
// Computes a scale factor to multiply the coefficients
// of the polynomial. The scaling is done to avoid
// overflow and to avoid undetected underflow from
// interfering with the convergence criterion.
// The factor is a power of the base.
//------------------------------------------------------
bool do_scaling_flag = false;
PRF_Float_T sc = lo / min;
if (sc <= 1.0)
{
do_scaling_flag = f_MAXIMUM_FLOAT / sc < max;
}
else
{
do_scaling_flag = max < 10.0;
if (! do_scaling_flag)
{
if (sc == 0.0)
{
sc = f_MINIMUM_FLOAT;
}
}
}
//------------------------------------------------------
// Conditionally scale the data.
//------------------------------------------------------
if (do_scaling_flag)
{
int lvar = (int)(::log(sc) / ::log(f_BASE) + 0.5);
double factor = ::pow((double)(f_BASE * 1.0), double(lvar));
if (factor != 1.0)
{
for (ii = 0; ii < m_n_plus_one; ++ii)
{
m_p_vector_ptr[ii] = factor * m_p_vector_ptr[ii];
}
}
}
//------------------------------------------------------
// Compute lower bound on moduli of zeros.
//------------------------------------------------------
for (ii = 0; ii < m_n_plus_one; ++ii)
{
pt_vector_ptr[ii] = (PRF_Float_T)(::fabs((PRF_Float_T)(m_p_vector_ptr[ii])));
}
pt_vector_ptr[m_n] = - pt_vector_ptr[m_n];
//------------------------------------------------------
// Compute upper estimate of bound.
//------------------------------------------------------
xvar = (PRF_Float_T)
(::exp((::log(- pt_vector_ptr[m_n]) - ::log(pt_vector_ptr[0]))
/ (PRF_Float_T)(m_n)));
//------------------------------------------------------
// If newton step at the origin is better, use it.
//------------------------------------------------------
PRF_Float_T xm;
if (pt_vector_ptr[m_n - 1] != 0.0)
{
xm = - pt_vector_ptr[m_n] / pt_vector_ptr[m_n - 1];
if (xm < xvar)
{
xvar = xm;
}
}
//------------------------------------------------------
// Chop the interval (0, xvar) until ff <= 0
//------------------------------------------------------
PRF_Float_T ff;
while (true)
{
xm = (PRF_Float_T)(xvar * 0.1);
ff = pt_vector_ptr[0];
for (ii = 1; ii < m_n_plus_one; ++ii)
{
ff = ff * xm + pt_vector_ptr[ii];
}
if (ff <= 0.0)
{
break;
}
xvar = xm;
}
PRF_Float_T dx = xvar;
//------------------------------------------------------
// Do newton iteration until xvar converges to two
// decimal places.
//------------------------------------------------------
while (true)
{
if ((PRF_Float_T)(::fabs(dx / xvar)) <= 0.005)
{
break;
}
ff = pt_vector_ptr[0];
PRF_Float_T df = ff;
for (ii = 1; ii < m_n; ++ii)
{
ff = ff * xvar + pt_vector_ptr[ii];
df = df * xvar + ff;
}
ff = ff * xvar + pt_vector_ptr[m_n];
dx = ff / df;
xvar = xvar - dx;
}
PRF_Float_T bnd = xvar;
//------------------------------------------------------
// Compute the derivative as the intial m_k_vector_ptr
// polynomial and do 5 steps with no shift.
//------------------------------------------------------
int n_minus_one = m_n - 1;
for (ii = 1; ii < m_n; ++ii)
{
m_k_vector_ptr[ii] =
(PRF_Float_T)(m_n - ii) * m_p_vector_ptr[ii] / (PRF_Float_T)(m_n);
}
m_k_vector_ptr[0] = m_p_vector_ptr[0];
double aa = m_p_vector_ptr[m_n];
double bb = m_p_vector_ptr[m_n - 1];
bool zerok_flag = m_k_vector_ptr[m_n - 1] == 0.0;
int jj = 0;
for (jj = 1; jj <= 5; ++jj)
{
double cc = m_k_vector_ptr[m_n - 1];
if (zerok_flag)
{
//----------------------------------------------
// Use unscaled form of recurrence.
//----------------------------------------------
for (jvar = n_minus_one; jvar > 0; --jvar)
{
m_k_vector_ptr[jvar] = m_k_vector_ptr[jvar - 1];
}
m_k_vector_ptr[0] = 0.0;
zerok_flag = m_k_vector_ptr[m_n - 1] == 0.0;
}
else
{
//----------------------------------------------
// Use scaled form of recurrence if value
// of m_k_vector_ptr at 0 is nonzero.
//----------------------------------------------
double tvar = - aa / cc;
for (jvar = n_minus_one; jvar > 0; --jvar)
{
m_k_vector_ptr[jvar] =
tvar * m_k_vector_ptr[jvar - 1] + m_p_vector_ptr[jvar];
}
m_k_vector_ptr[0] = m_p_vector_ptr[0];
zerok_flag =
::fabs(m_k_vector_ptr[m_n - 1]) <= ::fabs(bb) * f_ETA_N;
}
}
//------------------------------------------------------
// Save m_k_vector_ptr for restarts with new shifts.
//------------------------------------------------------
for (ii = 0; ii < m_n; ++ii)
{
temp_vector_ptr[ii] = m_k_vector_ptr[ii];
}
//------------------------------------------------------
// Loop to select the quadratic corresponding to
// each new shift.
//------------------------------------------------------
int cnt = 0;
for (cnt = 1; cnt <= 20; ++cnt)
{
//--------------------------------------------------
// Quadratic corresponds to a double shift to a
// non-real point and its complex conjugate. The
// point has modulus 'bnd' and amplitude rotated
// by 94 degrees from the previous shift.
//--------------------------------------------------
PRF_Float_T xxx = cosr * xx - sinr * yy;
yy = sinr * xx + cosr * yy;
xx = xxx;
m_real_s = bnd * xx;
m_imag_s = bnd * yy;
m_u = - 2.0 * m_real_s;
m_v = bnd;
//--------------------------------------------------
// Second stage calculation, fixed quadratic.
// Variable nz will contain the number of
// zeros found when function Fxshfr() returns.
//--------------------------------------------------
int nz = Fxshfr(20 * cnt);
if (nz != 0)
{
//----------------------------------------------
// The second stage jumps directly to one of
// the third stage iterations and returns here
// if successful. Deflate the polynomial,
// store the zero or zeros and return to the
// main algorithm.
//----------------------------------------------
jvar = m_degree - m_n;
real_zero_vector_ptr[jvar] = m_real_sz;
imaginary_zero_vector_ptr[jvar] = m_imag_sz;
m_n_plus_one = m_n_plus_one - nz;
m_n = m_n_plus_one - 1;
for (ii = 0; ii < m_n_plus_one; ++ii)
{
m_p_vector_ptr[ii] = m_qp_vector_ptr[ii];
}
if (nz != 1)
{
real_zero_vector_ptr[jvar + 1 ] = m_real_lz;
imaginary_zero_vector_ptr[jvar + 1] = m_imag_lz;
}
break;
//----------------------------------------------
// If the iteration is unsuccessful another
// quadratic is chosen after restoring
// m_k_vector_ptr.
//----------------------------------------------
}
for (ii = 0; ii < m_n; ++ii)
{
m_k_vector_ptr[ii] = temp_vector_ptr[ii];
}
}
}
}
//--------------------------------------------------------------
// If no convergence with 20 shifts then adjust the degree
// for the number of roots found.
//--------------------------------------------------------------
if (number_of_roots_found_ptr != 0)
{
*number_of_roots_found_ptr = m_degree - m_n;
}
}
return status;
}
//======================================================================
// Computes up to l2var fixed shift m_k_vector_ptr polynomials,
// testing for convergence in the linear or quadratic
// case. initiates one of the variable shift
// iterations and returns with the number of zeros
// found.
//
// l2var An integer that is the limit of fixed shift steps.
//
// Return Value:
// nz An integer that is the number of zeros found.
//======================================================================
int PolynomialRootFinder::Fxshfr(int l2var)
{
//------------------------------------------------------------------
// Evaluate polynomial by synthetic division.
//------------------------------------------------------------------
QuadraticSyntheticDivision(m_n_plus_one,
m_u,
m_v,
m_p_vector_ptr,
m_qp_vector_ptr,
m_a,
m_b);
int itype = CalcSc();
int nz = 0;
float betav = 0.25;
float betas = 0.25;
float oss = (float)(m_real_s);
float ovv = (float)(m_v);
float ots;
float otv;
double ui;
double vi;
double svar;
int jvar = 0;
for (jvar = 1; jvar <= l2var; ++jvar)
{
//--------------------------------------------------------------
// Calculate next m_k_vector_ptr polynomial and estimate m_v.
//--------------------------------------------------------------
NextK(itype);
itype = CalcSc();
Newest(itype, ui, vi);
float vv = (float)(vi);
//--------------------------------------------------------------
// Estimate svar
//--------------------------------------------------------------
float ss = 0.0;
if (m_k_vector_ptr[m_n - 1] != 0.0)
{
ss = (float)(- m_p_vector_ptr[m_n] / m_k_vector_ptr[m_n - 1]);
}
float tv = 1.0;
float ts = 1.0;
if ((jvar != 1) && (itype != 3))
{
//----------------------------------------------------------
// Compute relative measures of convergence of
// svar and m_v sequences.
//----------------------------------------------------------
if (vv != 0.0)
{
tv = (float)(::fabs((vv - ovv) / vv));
}
if (ss != 0.0)
{
ts = (float)(::fabs((ss - oss) / ss));
}
//----------------------------------------------------------
// If decreasing, multiply two most recent convergence
// measures.
//----------------------------------------------------------
float tvv = 1.0;
if (tv < otv)
{
tvv = tv * otv;
}
float tss = 1.0;
if (ts < ots)
{
tss = ts * ots;
}
//----------------------------------------------------------
// Compare with convergence criteria.
//----------------------------------------------------------
bool vpass_flag = tvv < betav;
bool spass_flag = tss < betas;
if (spass_flag || vpass_flag)
{
//------------------------------------------------------
// At least one sequence has passed the convergence
// test. Store variables before iterating.
//------------------------------------------------------
double svu = m_u;
double svv = m_v;
int ii = 0;
for (ii = 0; ii < m_n; ++ii)
{
m_svk_vector_ptr[ii] = m_k_vector_ptr[ii];
}
svar = ss;
//------------------------------------------------------
// Choose iteration according to the fastest
// converging sequence.
//------------------------------------------------------
bool vtry_flag = false;
bool stry_flag = false;
bool exit_outer_loop_flag = false;
bool start_with_real_iteration_flag =
(spass_flag && ((! vpass_flag) || (tss < tvv)));
do
{
if (! start_with_real_iteration_flag)
{
nz = QuadraticIteration(ui, vi);
if (nz > 0)
{
exit_outer_loop_flag = true;
break;
}
//----------------------------------------------
// Quadratic iteration has failed. flag
// that it has been tried and decrease
// the convergence criterion.
//----------------------------------------------
vtry_flag = true;
betav = (float)(betav * 0.25);
}
//--------------------------------------------------
// Try linear iteration if it has not been
// tried and the svar sequence is converging.
//--------------------------------------------------
if (((! stry_flag) && spass_flag)
|| start_with_real_iteration_flag)
{
if (! start_with_real_iteration_flag)
{
for (ii = 0; ii < m_n; ++ii)
{
m_k_vector_ptr[ii] = m_svk_vector_ptr[ii];
}
}
else
{
start_with_real_iteration_flag = false;
}
int iflag = 0;
nz = RealIteration(svar, iflag);
if (nz > 0)
{
exit_outer_loop_flag = true;
break;
}
//----------------------------------------------
// Linear iteration has failed. Flag that
// it has been tried and decrease the
// convergence criterion.
//----------------------------------------------
stry_flag = true;
betas = (float)(betas * 0.25);
if (iflag != 0)
{
//------------------------------------------
// If linear iteration signals an almost
// double real zero attempt quadratic
// iteration.
//------------------------------------------
ui = - (svar + svar);
vi = svar * svar;
continue;
}
}
//--------------------------------------------------
// Restore variables
//--------------------------------------------------
m_u = svu;
m_v = svv;
for (ii = 0; ii < m_n; ++ii)
{
m_k_vector_ptr[ii] = m_svk_vector_ptr[ii];
}
//----------------------------------------------
// Try quadratic iteration if it has not been
// tried and the m_v sequence is converging.
//----------------------------------------------
}
while (vpass_flag && (! vtry_flag));
if (exit_outer_loop_flag)
{
break;
}
//------------------------------------------------------
// Recompute m_qp_vector_ptr and scalar values to
// continue the second stage.
//------------------------------------------------------
QuadraticSyntheticDivision(m_n_plus_one,
m_u,
m_v,
m_p_vector_ptr,
m_qp_vector_ptr,
m_a,
m_b);
itype = CalcSc();
}
}
ovv = vv;
oss = ss;
otv = tv;
ots = ts;
}
return nz;
}
//======================================================================
// Variable-shift m_k_vector_ptr-polynomial iteration for
// a quadratic factor converges only if the zeros are
// equimodular or nearly so.
//
// uu Coefficients of starting quadratic
// vv Coefficients of starting quadratic
//
// Return value:
// nz The number of zeros found.
//======================================================================
int PolynomialRootFinder::QuadraticIteration(double uu, double vv)
{
//------------------------------------------------------------------
// Main loop
//------------------------------------------------------------------
double ui = 0.0;
double vi = 0.0;
float omp = 0.0F;
float relstp = 0.0F;
int itype = 0;
bool tried_flag = false;
int jvar = 0;
int nz = 0;
m_u = uu;
m_v = vv;
while (true)
{
SolveQuadraticEquation(1.0,
m_u,
m_v,
m_real_sz,
m_imag_sz,
m_real_lz,
m_imag_lz);
//--------------------------------------------------------------
// Return if roots of the quadratic are real and not close
// to multiple or nearly equal and of opposite sign.
//--------------------------------------------------------------
if (::fabs(::fabs(m_real_sz) - ::fabs(m_real_lz)) > 0.01 * ::fabs(m_real_lz))
{
break;
}
//--------------------------------------------------------------
// Evaluate polynomial by quadratic synthetic division.
//------------------------------------------------------------------
QuadraticSyntheticDivision(m_n_plus_one,
m_u,
m_v,
m_p_vector_ptr,
m_qp_vector_ptr,
m_a,
m_b);
float mp = (float)(::fabs(m_a - m_real_sz * m_b) + ::fabs(m_imag_sz * m_b));
//--------------------------------------------------------------
// Compute a rigorous bound on the rounding error in
// evaluting m_p_vector_ptr.
//--------------------------------------------------------------
float zm = (float)(::sqrt((float)(::fabs((float)(m_v)))));
float ee = (float)(2.0 * (float)(::fabs((float)(m_qp_vector_ptr[0]))));
float tvar = (float)(- m_real_sz * m_b);
int ii = 0;
for (ii = 1; ii < m_n; ++ii)
{
ee = ee * zm + (float)(::fabs((float)(m_qp_vector_ptr[ii])));
}
ee = ee * zm + (float)(::fabs((float)(m_a) + tvar));
ee = (float)((5.0 * m_mre + 4.0 * m_are) * ee
- (5.0 * m_mre + 2.0 * m_are) * ((float)(::fabs((float)(m_a) + tvar)) + (float)(::fabs((float)(m_b))) * zm)
+ 2.0 * m_are * (float)(::fabs(tvar)));
//--------------------------------------------------------------
// Iteration has converged sufficiently if the polynomial
// value is less than 20 times this bound.
//--------------------------------------------------------------
if (mp <= 20.0 * ee)
{
nz = 2;
break;
}
jvar = jvar + 1;
//--------------------------------------------------------------
// Stop iteration after 20 steps.
//--------------------------------------------------------------
if (jvar > 20)
{
break;
}
if ((jvar >= 2) && ((relstp <= 0.01)
&& (mp >= omp) && (! tried_flag)))
{
//----------------------------------------------------------
// A cluster appears to be stalling the convergence.
// Five fixed shift steps are taken with a m_u, m_v
// close to the cluster.
//----------------------------------------------------------
if (relstp < f_ETA)
{
relstp = f_ETA;