Skip to content

Commit 5e6586b

Browse files
committed
Merge remote-tracking branch 'upstream/pr/162/head' into development
* upstream/pr/162/head: poly: Use a fixed number of DKW iterations for roots. Signed-off-by: Torbjörn Klatt <[email protected]>
2 parents 3022436 + 2aa8e72 commit 5e6586b

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

include/pfasst/quadrature/polynomial.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace pfasst
3333
}
3434

3535
Polynomial<CoeffT> normalize() const;
36-
vector<CoeffT> roots() const;
36+
vector<CoeffT> roots(size_t num_iterations=20, CoeffT ztol=1.0e-20) const;
3737
static Polynomial<CoeffT> legendre(const size_t order);
3838
};
3939
} // ::pfasst

src/pfasst/quadrature/polynomial_impl.hpp

Lines changed: 5 additions & 12 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

@@ -58,21 +57,21 @@ namespace pfasst
5857
}
5958

6059
template<typename CoeffT>
61-
vector<CoeffT> Polynomial<CoeffT>::roots() const
60+
vector<CoeffT> Polynomial<CoeffT>::roots(size_t num_iterations, CoeffT ztol) const
6261
{
6362
assert(c.size() >= 1);
6463
size_t n = c.size() - 1;
6564

6665
// initial guess
67-
Polynomial<complex<CoeffT>> z0(n), z1(n);
66+
vector<complex<CoeffT>> z0(n), z1(n);
6867
for (size_t j = 0; j < n; j++) {
6968
z0[j] = pow(complex<double>(0.4, 0.9), j);
7069
z1[j] = z0[j];
7170
}
7271

7372
// durand-kerner-weierstrass iterations
74-
Polynomial<CoeffT> p = normalize();
75-
for (size_t k = 0; k < 100; k++) {
73+
Polynomial<CoeffT> p = this->normalize();
74+
for (size_t k = 0; k < num_iterations; k++) {
7675
complex<CoeffT> num, den;
7776
for (size_t i = 0; i < n; i++) {
7877
num = p.evaluate(z0[i]);
@@ -83,18 +82,12 @@ namespace pfasst
8382
}
8483
z0[i] = z0[i] - num / den;
8584
}
86-
87-
// converged?
88-
CoeffT acc = 0.0;
89-
for (size_t j = 0; j < n; j++) { acc += abs(z0[j] - z1[j]); }
90-
if (acc < 2 * numeric_limits<CoeffT>::epsilon()) { break; }
91-
9285
z1 = z0;
9386
}
9487

9588
vector<CoeffT> roots(n);
9689
for (size_t j = 0; j < n; j++) {
97-
roots[j] = (abs(z0[j]) < 4 * numeric_limits<CoeffT>::epsilon()) ? 0.0 : real(z0[j]);
90+
roots[j] = abs(z0[j]) < ztol ? 0.0 : real(z0[j]);
9891
}
9992

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

0 commit comments

Comments
 (0)