Skip to content

Commit f3159ca

Browse files
committed
backmerge branch 'development'
* development: poly: Use a fixed number of DKW iterations for roots.
2 parents f858fe8 + 5e6586b commit f3159ca

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

include/pfasst/quadrature/polynomial.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace pfasst
114114
*
115115
* @returns roots sorted with respect to their value
116116
*/
117-
vector<CoeffT> roots() const;
117+
vector<CoeffT> roots(size_t num_iterations=20, CoeffT ztol=1.0e-20) const;
118118
//! @}
119119

120120
//! @{

src/pfasst/quadrature/polynomial_impl.hpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <cassert>
55
#include <cmath>
66
#include <complex>
7-
#include <limits>
87
using namespace std;
98

109

@@ -68,21 +67,21 @@ namespace pfasst
6867
* @endinternals
6968
*/
7069
template<typename CoeffT>
71-
vector<CoeffT> Polynomial<CoeffT>::roots() const
70+
vector<CoeffT> Polynomial<CoeffT>::roots(size_t num_iterations, CoeffT ztol) const
7271
{
7372
assert(c.size() >= 1);
74-
size_t n = this->order();
73+
size_t n = c.size() - 1;
7574

7675
// initial guess
77-
Polynomial<complex<CoeffT>> z0(n), z1(n);
76+
vector<complex<CoeffT>> z0(n), z1(n);
7877
for (size_t j = 0; j < n; j++) {
7978
z0[j] = pow(complex<double>(0.4, 0.9), j);
8079
z1[j] = z0[j];
8180
}
8281

8382
// durand-kerner-weierstrass iterations
84-
Polynomial<CoeffT> p = normalize();
85-
for (size_t k = 0; k < 100; k++) {
83+
Polynomial<CoeffT> p = this->normalize();
84+
for (size_t k = 0; k < num_iterations; k++) {
8685
complex<CoeffT> num, den;
8786
for (size_t i = 0; i < n; i++) {
8887
num = p.evaluate(z0[i]);
@@ -93,18 +92,12 @@ namespace pfasst
9392
}
9493
z0[i] = z0[i] - num / den;
9594
}
96-
97-
// converged?
98-
CoeffT acc = 0.0;
99-
for (size_t j = 0; j < n; j++) { acc += abs(z0[j] - z1[j]); }
100-
if (acc < 2 * numeric_limits<CoeffT>::epsilon()) { break; }
101-
10295
z1 = z0;
10396
}
10497

10598
vector<CoeffT> roots(n);
10699
for (size_t j = 0; j < n; j++) {
107-
roots[j] = (abs(z0[j]) < 4 * numeric_limits<CoeffT>::epsilon()) ? 0.0 : real(z0[j]);
100+
roots[j] = abs(z0[j]) < ztol ? 0.0 : real(z0[j]);
108101
}
109102

110103
sort(roots.begin(), roots.end());

0 commit comments

Comments
 (0)