-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpoly.hpp
1491 lines (1439 loc) · 37.9 KB
/
cpoly.hpp
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
#ifndef _CPOLY_
#define _CPOLY_
/*
* NOTES:
*
* Find_roots uses by default aberth method (implicit deflation based on newton-raphson method),
* as discussed in [1] and [2]
*
* Aberth method can be parallelized without issues
*
* stopping criterion and accurate calculation of correction term in Abrth method have been implemented as suggested in [4]
* By using the method set_polish(true), polishing by maehly's method is enabled (by default is disabled since it is slower)
*
*
* set_output_prec() set the precision to use for checking convergence
*
* This class can be used also using multiprecision types from boost library (e.g. mpfr and mpc )
*
* References
* [1] D. A. Bini, Numerical Algorithms 13, 179-200 (1996).
* [2] D. A. Bini and G. Fiorentino, Numerical Algorithms 23, 127–173 (2000).
* [3] D. A. Bini et al. Numerical Algorithms 34, 217–227 (2003).
* [4] T. R. Cameron, Numerical Algorithms, 82, 1065–1084 (2019), doi: https://doi.org/10.1007/s11075-018-0641-9
* */
#include "pvector.hpp"
#include "quartic.hpp"
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<cmath>
#include <algorithm>
#include <limits>
#include <cstdlib>
#include <vector>
#include <array>
// To enable parallelizazion use gnu g++ and use the flag -fopenmp, i.e.
// g++ -fopenmp ...
#if defined(_OPENMP)
#include<omp.h>
#endif
#define Sqr(x) ((x)*(x))
//#define BINI_CONV_CRIT // Bini stopping criterion is slightly less accurate and slightly slower than Cameron one.
#define USE_CONVEX_HULL // <- faster (from T. R. Cameron, Numerical Algorithms, 82, 1065–1084 (2019)
#define USE_ABERTH_REAL //<--- faster
#if defined(_OPENMP)
#define USE_ROLD
#endif
/* NOTE: I have added a method to check whether roots are newton-isolated, nevertheless it is not
* more efficient to switch to a simple newton-raphson (NR) if a root is newton isolated since convergence
* of NR is quadratic while that of Aberth method is cubic (I made also timing tests to verify this). */
using namespace std;
template <class cmplx, class ntype, class dcmplx, int N>
class cpoly_base_static
{
public:
int n;
constexpr static int dynamic = false;
pvector<cmplx, N+1> coeff;
pvector<ntype, N+1> acmon;
pvector<cmplx, N+1> cmon;
pvector<cmplx, N-1> abscmon;
pvector<ntype, N+1> alpha;
quartic<ntype,cmplx,false> quar;
bool found[N], newt_isol_arr[N];
bool prec_reached[N];
#ifdef USE_ROLD
pvector<cmplx, N> rold;
#endif
pvector<ntype, N> errb;
pvector<dcmplx,N> droots;
//int conv_crit; /* 0 = Cameron 1 = Bini*/
void set_coeff(pvector<ntype,N+1> v)
{
for (int i=0; i <= N; i++)
coeff[i] = cmplx(v[i],0.0);
cmon[n]=1.0;
for (int i=n-1; i >=0; i--)
{
cmon[i]=coeff[i]/coeff[n];
}
}
void set_coeff(pvector<cmplx,N+1> v)
{
coeff = v;
cmon[n]=1.0;
for (int i=n-1; i >=0; i--)
{
cmon[i]=coeff[i]/coeff[n];
}
}
cpoly_base_static()
{
n=N;
}
cpoly_base_static(int nc): cpoly_base_static()
{
n=nc;
}
};
template <class cmplx, class ntype, class dcmplx, int N>
class cpoly_base_dynamic
{
public:
int n;
constexpr static int dynamic = true;
pvector<cmplx> coeff;
pvector<cmplx> cmon;
pvector<ntype> acmon;
pvector<ntype> alpha;
#ifdef USE_ROLD
pvector<cmplx> rold;
#endif
pvector<dcmplx> droots;
quartic<ntype,cmplx,true> quar;
bool *found, *newt_isol_arr;
bool *prec_reached;
pvector<ntype> errb;
cpoly_base_dynamic()
{
n=0;
}
void set_coeff(pvector<ntype,-1> v)
{
for (int i=0; i <= n; i++)
coeff[i] = cmplx(v[i],0.0);
cmon[n]=1.0;
for (int i=n-1; i >=0; i--)
{
cmon[i]=coeff[i]/coeff[n];
}
}
void set_coeff(pvector<cmplx,-1> v)
{
coeff = v;
cmon[n]=1.0;
for (int i=n-1; i >=0; i--)
{
cmon[i]=coeff[i]/coeff[n];
}
}
void use_vec(int nc, cmplx* coeffv, cmplx* cmonv,
ntype* acmonv, ntype *alphav)
{
coeff.use_vec(nc+1,coeffv);
cmon.use_vec(nc+1,cmonv);
acmon.use_vec(nc+1,acmonv);
alpha.use_vec(nc+1,alphav);
n=nc;
}
cpoly_base_dynamic(int nc): coeff(nc+1), cmon(nc+1), acmon(nc+1), alpha(nc+1), droots(nc), errb(nc)
{
#ifdef USE_ROLD
rold.allocate(nc);
#endif
found = new bool[nc];
newt_isol_arr= new bool[nc];
prec_reached = new bool[nc];
n=nc;
}
~cpoly_base_dynamic()
{
delete[] found;
delete[] newt_isol_arr;
delete[] prec_reached;
}
void deallocate(void)
{
coeff.deallocate();
cmon.deallocate();
acmon.deallocate();
alpha.deallocate();
#ifdef USE_ROLD
rold.deallocate();
#endif
droots.deallocate();
errb.deallocate();
delete[] found;
delete[] prec_reached;
delete newt_isol_arr;
}
void allocate(int nc)
{
n=nc;
coeff.allocate(n+1);
cmon.allocate(n+1);
acmon.allocate(n+1);
alpha.allocate(n+1);
#ifdef USE_ROLD
rold.allocate(n);
#endif
errb.allocate(n);
droots.allocate(n);
found = new bool[n];
newt_isol_arr = new bool[n];
prec_reached = new bool[n];
for (int i=0; i < n; i++)
{
prec_reached[i] = false;
}
}
};
template <class cmplx, class ntype, class dcmplx, int N> using cpolybase =
typename std::conditional<(N>0), cpoly_base_static <cmplx, ntype, dcmplx, N>,
cpoly_base_dynamic <cmplx, ntype, dcmplx, N>>::type;
template <class cmplx, int N=-1, class ntype=double, class dcmplx=complex<long double>, class dntype=long double>
class cpoly: public numeric_limits<ntype>, public cpolybase<cmplx,ntype,dcmplx,N>
{
using cpolybase<cmplx,ntype,dcmplx,N>::n;
using cpolybase<cmplx,ntype,dcmplx,N>::coeff;
using cpolybase<cmplx,ntype,dcmplx,N>::cmon;
using cpolybase<cmplx,ntype,dcmplx,N>::acmon;
using cpolybase<cmplx,ntype,dcmplx,N>::alpha;
using cpolybase<cmplx,ntype,dcmplx,N>::droots;
using cpolybase<cmplx,ntype,dcmplx,N>::quar;
using cpolybase<cmplx,ntype,dcmplx,N>::found;
using cpolybase<cmplx,ntype,dcmplx,N>::newt_isol_arr;
using cpolybase<cmplx,ntype,dcmplx,N>::prec_reached;
using cpolybase<cmplx,ntype,dcmplx,N>::errb;
const ntype pigr=acos(ntype(-1.0));
const cmplx I = cmplx(0.0,1.0);
#ifdef USE_ROLD
using cpolybase<cmplx,ntype,dcmplx,N>::rold;
#endif
template <class vtype>
using pvecNm1 = typename std::conditional<(N>0), pvector<vtype, N-1>,
pvector<vtype, -1>>::type;
template <class vtype>
using pvecNp1 = typename std::conditional<(N>0), pvector<vtype, N+1>,
pvector<vtype, -1>>::type;
using cpolyNm1 = typename std::conditional<(N>0), cpoly<cmplx, N-1,ntype>,
cpoly<cmplx, -1,ntype>>::type;
template <class vtype>
using pdvecNp1 = typename std::conditional<(N>0), pvector<vtype, N+1>,
pvector<vtype, -1>>::type;
const int maxiter_polish=8;
int imaxarg1,imaxarg2;
ntype eps05, meps, maxf, maxf2, maxf3, minf, scalfact, cubic_rescal_fact;
int maxdigits;
ntype goaleps;
ntype Kconv;
bool gpolish;
bool use_dbl_iniguess;
bool guess_provided, calc_err_bound;
#ifdef USE_CONVEX_HULL
using point=pair<ntype,ntype>;
typedef ntype coord_t; // coordinate type
typedef ntype coord2_t; // must be big enough to hold 2*max(|coordinate|)^2
struct Point {
coord_t x;
coord_t y;
bool operator <(const Point &p) const {
return x < p.x || (x == p.x && y < p.y);
}
};
// 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product.
// Returns a positive value, if OAB makes a clockwise turn,
// negative for counter-clockwise turn, and zero if the points are collinear.
vector<Point> pts, convh;
coord2_t cross(const Point &O, const Point &A, const Point &B)
{
return ((A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x));
}
// Returns a list of points on the convex hull in counter-clockwise order.
// Note: the last point in the returned list is the same as the first one.
// Method: Andrew’s monotone chain algorithm O(n) in this case since we do not need sorting
vector<Point> upper_convex_hull(vector<Point> P)
{
int n = P.size(), k = 0;
if (n <= 3)
return P;
vector<Point> H(n+1);
// points are already ordered in the present case!
//sort(P.begin(), P.end());
// we need just upper hull for Bini's algorithm
// Build upper hull
for (int i = n-1; i >= 0; i--) {
while (k >= 2 && cross(H[k-1], H[k], P[i]) < 0)
{
k--;
}
k++;
H[k] = P[i];
}
H.resize(k+1);
return H;
}
#else
vector<ntype> vk, uk;
#endif
vector<cmplx> rg;
vector<int> k;
/* check if the roots is newton-isolated, i.e if a simple newton-raphson
* iteration can be applied (without aberth correction) */
bool is_newt_isol(int i, pvector<cmplx,N>& roots, pvector <ntype, N>& radius)
{
ntype theta, v, minv=0.0;
int primo=1;
for (int j=0; j < n; j++)
{
v = abs(roots[j]-roots[i])-radius[i]-radius[j];
if (j!=i && (primo || v < minv))
{
primo=0;
minv=v;
}
}
theta = radius[i]*(n-1)/minv;
//cout << "root["<< i << "] theta=" << theta << " minv=" << minv << " radius=" << radius[i] << "\n";
if (theta < ntype(1.0)/ntype(3.0))
{
return true;
}
else
{
return false;
}
}
public:
void print_error_bounds(void)
{
int i=0;
for (auto& eb: errb)
{
cout << setprecision(maxdigits) << "errbound[" << i << "]=" << eb << "\n";
i++;
}
}
ntype get_error_bound(int i)
{
return errb[i];
}
void set_calc_errb(bool v)
{
calc_err_bound=v;
}
void use_this_guess(pvector<dcmplx,N>& rg)
{
droots=rg;
guess_provided=true;
}
void no_guess(void)
{
guess_provided=false;
}
void show(void)
{
show(NULL);
}
void show(const char* str)
{
int i;
bool re, im;
if (str!=NULL)
cout << str;
if (maxdigits <=0)
maxdigits=30;
for (i=n; i >= 0; i--)
{
re=false;
im=false;
if (real(coeff[i]) > 0.0)
{
if (i < n)
cout << "+";
if (imag(coeff[i])!=0)
cout << "(";
if ( i==0 || abs(real(coeff[i]))!=1.0 || imag(coeff[i])!=0 )
{
cout << setprecision(maxdigits) << abs(real(coeff[i]));
}
re=true;
}
else if (real(coeff[i]) < 0)
{
cout << "-";
cout << setprecision(maxdigits) << abs(real(coeff[i]));
re=true;
}
else
re=false;
if (imag(coeff[i]) > 0.0)
{
if (i < n && re==true)
cout << "+" << abs(imag(coeff[i]));
im=true;
}
else if (imag(coeff[i]) < 0)
{
cout << "-" << abs(imag(coeff[i]));
im=true;
}
else
im=false;
if (im==true && re==true)
cout << ")";
if (im==true || re==true)
{
if ((im==true || abs(real(coeff[i]))!=1.0) && i > 0)
cout << "*";
if ( i > 1)
{
cout << "x^" << i;
}
else if (i==1)
cout << "x";
}
}
cout << "\n";
}
cmplx evalpoly(cmplx x)
{
// evaluate polynomail via Horner's formula
cmplx bn=cmplx(0.0);
for (int i=n; i >= 0; i--)
{
bn = cmon[i] + bn*x;
}
return bn;
}
cmplx evaldpoly(cmplx x)
{
// evaluate polynomail via Horner's formula
cmplx bn=0.0;
for (int i=n-1; i >= 0; i--)
{
bn = cmplx(i+1)*cmon[i+1] + bn*x;
}
return bn;
}
cmplx evalddpoly(cmplx x)
{
// evaluate second derivative of polynomail via Horner's formula
cmplx bn=0.0;
if (n == 1)
return 0;
for (int i=n-2; i >= 0; i--)
{
bn = (i+2)*(i+1)*cmon[i+2] + bn*x;
}
return bn;
}
// evaluate polynomail via Horner's formula
ntype calcerrb(cmplx r0)
{
ntype s, sp=0.0, abx;
cmplx p, p1=cmplx(0,0);
int j;
s=acmon[n];
p = cmon[n];
abx = abs(r0);
for (j=n-1; j >=0; j--)
{
sp = sp*abx + s;
s=abx*s+acmon[j];
p1 = p1*r0 + p;
p = p*r0 + cmon[j];
}
return ntype(n)*(abs(p)+meps*s)/abs(abs(p1)-meps*sp);
//return ntype(n)*(abs(evalpoly(r0))+meps*s)/abs(evaldpoly(r0));
}
// quadratic equation
void solve_quadratic(pvector<cmplx,N>&sol)
{
cmplx acx,bcx,zx1,zx2,cdiskr,zxmax,zxmin;
acx = coeff[1]/coeff[2];
bcx = coeff[0]/coeff[2];
cdiskr=sqrt(acx*acx-ntype(4.0)*bcx);
zx1 = -ntype(0.5)*(acx+cdiskr);
zx2 = -ntype(0.5)*(acx-cdiskr);
if (abs(zx1) > abs(zx2))
zxmax = zx1;
else
zxmax = zx2;
if (zxmax==cmplx(0.0,0.0))
zxmin=0;
else
zxmin = bcx/zxmax;
sol[0] = zxmin;
sol[1] = zxmax;
}
bool nr_aberth_real_rev(cmplx &r0, pvector<cmplx,N> &roots, int iac)
{
int j;
#ifndef BINI_CONV_CRIT
ntype err;
#endif
cmplx p1pc;
const ntype EPS=goaleps;
// root polishing by NR
ntype tt[2], xcR, xcI, aR, aI, p1p[2];
#ifdef BINI_CONV_CRIT
ntype pa[2], s;
#else
ntype absp;
#endif
ntype abx, p10, p0, x[2], p[2], p1[2], dx[2], invden;
//cout << "xc=" << xc << "\n";
xcR=real(r0);
xcI=imag(r0);
invden = 1.0/(xcR*xcR+xcI*xcI);
x[0] = xcR*invden;// 1/x
x[1] = -xcI*invden;
p[0]=real(cmon[0]);
p[1]=imag(cmon[0]);
p1[0]=0.0;
p1[1]=0.0;
for (j=1;j<=n;j++) {
p10 = p1[0];
p1[0] = x[0]*p1[0] - x[1]*p1[1] + p[0];
p1[1] = x[0]*p1[1] + x[1]*p10 + p[1];
p0 = p[0];
p[0] = x[0]*p[0] - x[1]*p[1] + real(cmon[j]);
p[1] = x[0]*p[1] + x[1]*p0 + imag(cmon[j]);
}
#ifdef BINI_CONV_CRIT
s=acmon[n];
abx = sqrt(xcR*xcR+xcI*xcI);
pa[0]=real(cmon[n]);
pa[1]=imag(cmon[n]);
for (j=n-1; j >=0; j--)
{
s=abx*s+acmon[j];
p0 = pa[0];
pa[0] = xcR*pa[0] - xcI*pa[1] + real(cmon[j]);
pa[1] = xcR*pa[1] + xcI*p0 + imag(cmon[j]);
}
if (abs(cmplx(pa[0],pa[1])) <= 2.0*EPS*(4.0*ntype(n)+1)*s) // stopping criterion of bini
{
return true;
}
#else
err=alpha[0];//abs(cmon[0])*(Kconv*m+1);
abx=1.0/sqrt(xcR*xcR+xcI*xcI);
for (j=1;j<=n;j++) {
err=abx*err+alpha[j];//abs(cmon[j])*(Kconv*j+1);
}
absp=abs(cmplx(p[0],p[1]));
if (absp <= EPS*err || (EPS*err <= minf && absp < minf))
{
return true;
}
#endif
p1pc=cmplx(p1[0],p1[1])/cmplx(p[0],p[1]);
p1p[0]=real(p1pc);
p1p[1]=imag(p1pc);
tt[0] = ntype(n)-x[0]*p1p[0]+x[1]*p1p[1];
tt[1] = -x[0]*p1p[1]-x[1]*p1p[0];
p1p[0] = x[0]*tt[0] - x[1]*tt[1];
p1p[1] = x[1]*tt[0] + x[0]*tt[1];
for (j=0; j < n; j++)
{
if (j==iac)
continue;
aR = xcR-real(roots[j]);
aI = xcI-imag(roots[j]);
invden = 1.0/(aR*aR+aI*aI);
p1p[0] -= aR*invden;
p1p[1] -= -aI*invden;
}
invden=1.0/(p1p[0]*p1p[0]+p1p[1]*p1p[1]);
dx[0]=p1p[0]*invden;
dx[1]=-p1p[1]*invden;
r0 = cmplx(xcR-dx[0],xcI-dx[1]);
return false;
}
bool nr_aberth_real(cmplx &r0, pvector<cmplx,N> &roots, int iac)
{
int j;
#ifndef BINI_CONV_CRIT
ntype err;
#endif
cmplx p1pc;
const ntype EPS=goaleps;
// root polishing by NR
ntype aR, aI, p1p[2];
ntype abx, p10, p0, x[2], p[2], p1[2], dx[2], invden;
#ifdef BINI_CONV_CRIT
ntype s;
#else
ntype absp;
#endif
x[0] = r0.real();
x[1] = r0.imag();
//its=iter;
p[0]=real(cmon[n]);
p[1]=imag(cmon[n]);
#ifndef BINI_CONV_CRIT
err=alpha[n];
#endif
p1[0]=0.0;
p1[1]=0.0;
abx=sqrt(x[0]*x[0]+x[1]*x[1]);
for (j=n-1;j>=0;j--) {
p10 = p1[0];
p1[0] = x[0]*p1[0] - x[1]*p1[1] + p[0];
p1[1] = x[0]*p1[1] + x[1]*p10 + p[1];
p0 = p[0];
p[0] = x[0]*p[0] - x[1]*p[1] + real(cmon[j]);
p[1] = x[0]*p[1] + x[1]*p0 + imag(cmon[j]);
#ifndef BINI_CONV_CRIT
err=abx*err+alpha[j];
#endif
}
#ifdef BINI_CONV_CRIT
s=acmon[n];
for (j=n-1; j >=0; j--)
{
s=abx*s+acmon[j];
}
if (abs(cmplx(p[0],p[1])) <= 2.0*EPS*(4.0*ntype(n)+1)*s) // stopping criterion of bini
{
return true;
}
#else
absp=abs(cmplx(p[0],p[1]));
if (absp <= EPS*err || (EPS*err <= minf && absp < minf))
{
return true;
}
#endif
p1pc=cmplx(p1[0],p1[1])/cmplx(p[0],p[1]);
p1p[0]=real(p1pc);
p1p[1]=imag(p1pc);
for (j=0; j < n; j++)
{
if (j==iac)
continue;
aR = x[0]-real(roots[j]);
aI = x[1]-imag(roots[j]);
invden = 1.0/(aR*aR+aI*aI);
p1p[0] -= aR*invden;
p1p[1] -= -aI*invden;
}
invden=1.0/(p1p[0]*p1p[0]+p1p[1]*p1p[1]);
dx[0]=p1p[0]*invden;
dx[1]=-p1p[1]*invden;
x[0] -= dx[0];
x[1] -= dx[1];
r0 = cmplx(x[0],x[1]);
return false;
}
bool nr_aberth_cmplx(cmplx &r0, pvector<cmplx,N> &roots, int iac)
{
int i;
#ifdef POLISH_NR_REAL
cmplx povp1;
#endif
const ntype EPS=goaleps;
// root polishing by NR
cmplx p, p1, p1p;
ntype absp, abx;
#ifndef BINI_CONV_CRIT
ntype err;
#else
ntype K=2.0*EPS*(4.0*ntype(n)+1.0);
ntype s;
#endif
abx=abs(r0);
#ifndef BINI_CONV_CRIT
err=alpha[n];
#endif
p=cmon[n];
p1=0;
#ifndef BINI_CONV_CRIT
err=alpha[n];//abs(cmon[m])*(Kconv*m+1);
#endif
for(i=n-1;i>=0;i--) {
p1=p1*r0+p;
p=p*r0+cmon[i];
#ifndef BINI_CONV_CRIT
err=abx*err+alpha[i];
#endif
}
#ifdef BINI_CONV_CRIT
s=acmon[n];
for (i=n-1; i >=0; i--)
{
s=abx*s+acmon[i];
}
absp=abs(p);
if (absp <= K*s) // stopping criterion of bini
{
return true;
}
#else
absp=abs(p);
if (absp <= EPS*err || (EPS*err <= minf && absp < minf))
{
return true;
}
#endif
p1p=p1/p;
for (i=0; i < n; i++)
{
if (i==iac)
continue;
p1p -= ntype(1.0)/(r0-roots[i]);
}
r0 -= ntype(1.0)/p1p;
return false;
}
bool nr_aberth_cmplx_rev(cmplx &r0, pvector<cmplx,N> &roots, int iac)
{
int i;
const ntype EPS=goaleps;
// root polishing by NR
cmplx pa, p, p1, p1p, x;
ntype absp, abx;
#ifndef BINI_CONV_CRIT
ntype err;
#else
ntype K=2.0*EPS*(4.0*ntype(n)+1.0);
ntype s;
#endif
x=ntype(1.0)/r0;
p=cmon[0];
p1=0;
for(i=1;i<=n;i++) {
p1=p1*x+p;
p=p*x+cmon[i];
}
#ifdef BINI_CONV_CRIT
s=acmon[n];
pa=cmon[n];
abx=abs(r0);
for (i=n-1; i >=0; i--)
{
s=abx*s+acmon[i];
pa=r0*pa+cmon[i];
}
absp=abs(pa);
if (absp <= K*s) // stopping criterion of bini
{
return true;
}
#else
err=alpha[0];//abs(cmon[0])*(Kconv*m+1);
abx=1.0/abs(r0);
for (i=1;i<=n;i++) {
err=abx*err+alpha[i];//abs(cmon[j])*(Kconv*j+1);
}
absp=abs(p);
if (absp <= EPS*err|| (EPS*err<=minf && absp<=minf))
{
return true;
}
#endif
p1p=p1/p;
p1p=x*(ntype(n)-x*p1p);
for (i=0; i < n; i++)
{
if (i==iac)
continue;
p1p -= ntype(1.0)/(r0-roots[i]);
}
r0 -= ntype(1.0)/p1p;
return false;
}
void refine_root_maehly(cmplx &r0, pvector<cmplx,N> &roots, int iac)
{
int iter,i;
ntype err;
#ifdef POLISH_NR_REAL
cmplx povp1;
#endif
// root polishing by NR
cmplx r0old, p, sum;
cmplx p1, p1c;
ntype errold;
for (iter=0; ; iter++)
{
p=cmon[n]*r0+cmon[n-1];
p1=cmon[n];
for(i=n-2;i>=0;i--) {
p1=p+p1*r0;
p=cmon[i]+p*r0;
}
if (iter > 0)
errold=err;
err = abs(p);//abs(p.real())+abs(p.imag());
if (err==0)
break;
if (iter > 0 && err >= errold)
{
r0=r0old;
break;
}
if (p1==cmplx(0,0))
{
break;
}
if (iter==maxiter_polish)
break;
r0old=r0;
sum=0;
for (i=0; i < iac; i++)
sum += ntype(1.0)/(r0-roots[i]);
sum *= p;
p1c = p1 - sum;
r0 -= p/p1c;
if (isnan(abs(r0)) || isinf(abs(r0)))
{
r0=r0old;
break;
}
}
}
void refine_root(cmplx &r0)
{
int iter,i;
ntype err;
// root polishing by NR
#ifdef POLISH_NR_REAL
// root polishing by NR
ntype r0old[2], p[2], r0new[2];
ntype p1[2], p0, p10;
ntype errold, invnp1;
cmplx povp1;
r0new[0]=r0.real();
r0new[1]=r0.imag();
for (iter=0; ;iter++)
{
p[0]=real(cmon[n])*r0new[0]-imag(cmon[n])*r0new[1] + real(cmon[n-1]);
p[1]=real(cmon[n])*r0new[1]+imag(cmon[n])*r0new[0] + imag(cmon[n-1]);
p1[0]=real(cmon[n]);
p1[1]=imag(cmon[n]);
for(i=n-2;i>=0;i--) {
p10=p1[0];
p1[0]=p[0]+p1[0]*r0new[0]-p1[1]*r0new[1];
p1[1]=p[1]+p10*r0new[1]+p1[1]*r0new[0];
p0=p[0];
p[0]=real(cmon[i])+p[0]*r0new[0]-p[1]*r0new[1];
p[1]=imag(cmon[i])+p0*r0new[1]+p[1]*r0new[0];
}
if (iter > 0)
errold=err;
err = abs(cmplx(p[0],p[1]));// abs(p[0])+abs(p[1]);
if (err==0)
{
break;
}
if (iter > 0 && err >= errold)
{
r0new[0]=r0old[0];
r0new[1]=r0old[1];
break;
}
if (p1[0]==0 && p1[1]==0)
{
break;
}
if (iter == maxiter_polish)
break;
r0old[0]=r0new[0];
r0old[1]=r0new[1];
povp1 = cmplx(p[0],p[1])/cmplx(p1[0],p1[1]);
r0new[0] -= povp1.real();
r0new[1] -= povp1.imag();
if (isnan(r0new[0]) || isnan(r0new[1]) ||isinf(r0new[0]) || isinf(r0new[1]))
{
r0new[0]=r0old[0];
r0new[1]=r0old[1];
break;
}
}
r0=cmplx(r0new[0], r0new[1]);
#else
cmplx r0old, p;
cmplx p1;
ntype errold;
for (iter=0; ; iter++)
{
p=cmon[n]*r0+cmon[n-1];
p1=cmon[n];
for(i=n-2;i>=0;i--) {
p1=p+p1*r0;
p=cmon[i]+p*r0;
}
if (iter > 0)
errold=err;
err = abs(p);//abs(p.real())+abs(p.imag());
if (err==0)
break;
if (iter > 0 && err >= errold)
{
r0=r0old;
break;
}
if (p1==cmplx(0,0))
{
break;
}
if (iter==maxiter_polish)
break;
r0old=r0;
r0 -= p/p1;
if (isnan(abs(r0)) || isinf(abs(r0)))
{
r0=r0old;
break;
}
}
#endif
}
ntype calc_upper_bound_kal(void)
{
// Kalantari's formula as found in McNamee Pan Vol. 1
int k;
static const ntype K= 1.0/0.682338;
ntype ximax, xi;
cmplx cnsq, term;
cnsq=cmon[n-1]*cmon[n-1];
for (k=4; k <= n+3; k++)
{
term=0;
if (n-k+3 >= 0)
term += cnsq*cmon[n-k+3]-cmon[n-2]*cmon[n-k+3];
if (n-k+2 >= 0)
term +=-cmon[n-1]*cmon[n-k+2];
if (n-k+1 >= 0)
term += cmon[n-k+1];
xi = pow(abs(term),1.0/(k-1));
if (k==4 || xi > ximax)
ximax=xi;
}
ximax=K*ximax;
if (isinf(ximax)||isnan(ximax))
{
//cout << "I cannot calculate the upper bound...\n";
//cmon.show("coeff");
//exit(-1);
return pow(maxf/1.618034,1.0/n);
}
return ximax;
}
void initial_guess(pvector<cmplx,N>& roots)
{
ntype sigma;
#ifndef USE_CONVEX_HULL
ntype ukc, vkc, maxt=0.0, t;
#endif
int q, i, j;
#ifdef USE_CONVEX_HULL
Point p;
pts.resize(n+1);
for (i = 0; i <= n; i++)
{
p.x=ntype(i);
if (abs(cmon[i])!=0)
{
p.y=log(abs(cmon[i]));
}
else
{
p.y=-maxf/1.618034;
}
pts[i] = p;
}
convh=upper_convex_hull(pts);
q=convh.size()-1;