#include "poly-dk-solve.h" #include /*** implementation of the Durand-Kerner method. seems buggy*/ std::complex evalu(Poly const & p, std::complex x) { std::complex result = 0; std::complex xx = 1; for(unsigned i = 0; i < p.size(); i++) { result += p[i]*xx; xx *= x; } return result; } std::vector > DK(Poly const & ply, const double tol) { std::vector > roots; const int N = ply.degree(); std::complex b(0.4, 0.9); std::complex p = 1; for(int i = 0; i < N; i++) { roots.push_back(p); p *= b; } assert(roots.size() == ply.degree()); double error = 0; int i; for( i = 0; i < 30; i++) { error = 0; for(int r_i = 0; r_i < N; r_i++) { std::complex denom = 1; std::complex R = roots[r_i]; for(int d_i = 0; d_i < N; d_i++) { if(r_i != d_i) denom *= R-roots[d_i]; } assert(norm(denom) != 0); std::complex dr = evalu(ply, R)/denom; error += norm(dr); roots[r_i] = R - dr; } /*std::copy(roots.begin(), roots.end(), std::ostream_iterator >(std::cout, ",\t")); std::cout << std::endl;*/ if(error < tol) break; } //std::cout << error << ", " << i<< std::endl; return roots; } /* Local Variables: mode:c++ c-file-style:"stroustrup" c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) indent-tabs-mode:nil fill-column:99 End: */ // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :