-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpolyvp.hpp
359 lines (345 loc) · 9.82 KB
/
cpolyvp.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
#ifndef _CPOLYVP_
#define _CPOLYVP_
//#define BINI_CONV_CRIT
/*
* 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 "./cpoly.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
using namespace std;
template <class cmplx, class ntype>
class azero
{
public:
cmplx z;
ntype r;
vector<int> bonds;
};
template <class cmplx, class ntype, class dcmplx=complex<long double>, class dntype=long double>
class cpolyvp: public numeric_limits<ntype> {
const ntype pigr=acos(ntype(-1.0));
const cmplx I = cmplx(0.0,1.0);
cpoly<cmplx,-1,ntype> pol;
ntype eps05, meps, maxf, maxf2, maxf3, minf, scalfact, cubic_rescal_fact;
unsigned input_precision, output_precision;
int maxdigits, n;
unsigned current_precision;
ntype goaleps;
ntype Kconv;
bool gpolish;
bool use_dbl_iniguess;
bool guess_provided, calc_err_bound;
pvector<cmplx> coeff, roots;
bool *found;
void deallocate(void)
{
coeff.deallocate();
delete[] found;
}
void allocate(int nc)
{
n=nc;
coeff.allocate(n+1);
found = new bool[n];
}
vector<cmplx> rg;
vector<int> k;
unsigned initial_precision;
double prec_fact;
struct scoped_precision
{
unsigned p;
scoped_precision(unsigned new_p) : p(ntype::default_precision())
{
ntype::default_precision(new_p);
cmplx::default_precision(new_p);
}
~scoped_precision()
{
ntype::default_precision(p);
cmplx::default_precision(p);
}
};
void set_precision(unsigned p)
{
current_precision=p;
ntype::default_precision(p);
cmplx::default_precision(p);
}
#if 0
vector<azero<cmplx,ntype>> particles;
vector<ntype> errbarr;
void set_particles(pvector<cmplx>& ro)
{
particles.resize(n);
for (int i=0; i < n; i++)
{
particles[i].z = ro[i];
particles[i].r = errbarr[i];
}
}
void find_bonds(void)
{
int i, j;
for (i=0; i < n; i++)
particles[i].bonds.clear();
/* we need to employ linked cell lists to find bonds
* otherwise can become very slow for large n */
for (i=0; i < n; i++)
{
for (j=i+1; j < n; j++)
{
auto sig = particles[i].r + particles[j].r;
if (abs(particles[i].z - particles[j].z) <= sig)
{
particles[i].bonds.add(j);
particles[j].bonds.add(i);
}
}
}
}
void find_clusters(pvector<cmplx>& ro)
{
}
void cluster_analysis(pvector<cmplx>& ro)
{
set_particles(ro);
find_bonds();
find_clusters(ro);
}
#endif
public:
void show(void)
{
show(NULL);
}
void show(const char* str)
{
set_precision(input_precision);
cpoly<cmplx,-1,ntype> rp(n);
rp.set_show_digits(input_precision);
rp.set_coeff(coeff);
rp.show(str);
}
void set_coeff(pvector<ntype>& v)
{
set_input_precision(v[0].precision());
for (int i=0; i <= n; i++)
coeff[i].assign(cmplx(v[i],0.0),input_precision);
}
void set_coeff(pvector<cmplx>& v)
{
set_input_precision(v[0].precision());
for (int i=0; i <= n; i++)
coeff[i].assign(v[i],input_precision);
}
void set_initial_precision(unsigned p)
{
initial_precision=p;
}
void set_input_precision(unsigned p)
{
input_precision=p;
}
int degree()
{
return n;
}
unsigned auto_precision(void)
{
return output_precision+12;
}
// find roots by default uses aberth method which is faster than laguerre implicit method
void find_roots(pvector<cmplx>& roots)
{
set_precision(output_precision+5);
ntype errb, maxerr=0, EPS=pow(ntype(2.0),-ntype(output_precision)*log(10.0)/log(2.0));
//ntype maxrelerr=0, relerr;
cmplx roo;
unsigned prec=initial_precision<=0?auto_precision():initial_precision;
set_precision(prec);
pvector<dcmplx> cvp(n+1);
cpoly<dcmplx,-1,dntype,dcmplx,dntype> rp(n);
pvector<dcmplx> roini(n);
int j;
if constexpr (!(is_same<dcmplx,complex<float>>::value &&
is_same<dntype,float>::value) &&
!(is_same<dcmplx,complex<double>>::value &&
is_same<dntype, double>::value) &&
!(is_same<dcmplx,complex<long double>>::value &&
is_same<dntype,long double>::value))
{
cout << "dcmplx and dntype must be either float, double or long double\n";
exit(1);
}
for (j=0; j < n+1; j++)
cvp[j]=dcmplx(coeff[j]);
rp.iniguess_slow();
rp.set_coeff(cvp);
rp.find_roots(roini);
int nf=0;
#if 0
errbarr.resize(n);
particles.resize(n);
#endif
//cout << setprecision(200) << "EPS=" << EPS << "\n";
for (j=0; j < n; j++)
{
errb.assign(ntype(rp.calcerrb(roini[j])), errb.precision());
//errbarr[j].assign(errb,current_precision);
#if 0
if (roinid[j]==dcmplx(0,0))
relerr = errb;
else
relerr = errb/ntype(abs(roinid[j]));
#endif
if (j==0 || errb > maxerr)
maxerr = errb;
//if (j==0 || relerr> maxrelerr)
//maxrelerr = relerr;
roo.assign(cmplx(roini[j]), roo.precision());
if (errb <= EPS*abs(roo))
{
nf++;
found[j] = true;
}
else
{
//cout << "1)root #" << j << " not accurate enough (errb=" << errb << ")\n";
found[j] = false;
}
}
//cout << setprecision(200) << "maxerr= " << maxerr << "\n";
//cout << "nf=" << nf << " over " << n << "\n";
if (nf == n)
{
for (j=0; j < n; j++)
roots[j].assign(cmplx(roini[j]), roots[j].precision());
return;
}
else
{
for (j=0; j < n; j++)
roots[j].assign(cmplx(roini[j]), roots[j].precision());
}
//prec = (unsigned)(double(prec)*1.1*abs(log10(EPS)/log10(maxrelerr)));
//cout << "INIPREC=" << prec << "\n";
for (int ip=0; ip < 8; ip++)
{
set_precision(prec);
pvector<cmplx> cvp(n+1);
cpoly<cmplx,-1,ntype,cmplx,ntype> rp(n);
ntype errb;
for (j=0; j < n+1; j++)
cvp[j].assign(coeff[j], cvp[j].precision());
pvector<cmplx> ro(n);
for (j=0; j < n; j++)
ro[j].assign(roots[j], ro[j].precision());
rp.use_this_guess(ro);
rp.set_prec_reached(found);
rp.set_coeff(cvp);
rp.find_roots(ro);
//rp.aberth(roots);
ntype maxerr;
nf=0;
for (j=0; j < n; j++)
{
if (found[j])
{
nf++;
continue;
}
errb.assign(rp.calcerrb(ro[j]),errb.precision());
roo.assign(ro[j],roo.precision());
if (j==0 || errb > maxerr)
maxerr = errb;
if (errb <= EPS*abs(roo))
{
nf++;
found[j] = true;
}
else
{
//cout << "2)root #" << j << " not accurate enough (errb=" << errb << ")\n";
found[j] = false;
}
}
//cout << setprecision(200) << "2) maxerr= " << maxerr << "nf=" << nf << "\n";
prec = (int)(double(prec)*prec_fact);
if (nf == n)
{
for (j=0; j < n; j++)
roots[j].assign(ro[j], output_precision);
break;
}
else
{
for (j=0; j < n; j++)
roots[j].assign(ro[j], roots[j].precision());
}
//cout << "newprec2=" << prec << " iter=" << ip << "\n";
}
}
void set_output_precision(unsigned op)
{
output_precision = op;
}
void init_const(void)
{
//cout << "numeric digits=" << maxdigits << " meps=" << meps << "\n";
input_precision=16;
initial_precision=0; // 0 means "auto"
prec_fact=2.0;
}
cpolyvp(int nc): coeff(nc+1), roots(nc)
{
init_const();
found = new bool[nc];
n=nc;
}
~cpolyvp()
{
delete[] found;
}
cpolyvp()=delete;
};
#endif