|
Algorithms_in_C++
1.0.0
Set of algorithms implemented in C++.
|
Compute all possible approximate roots of any given polynomial using Durand Kerner algorithm More...
#include <algorithm>#include <cassert>#include <cmath>#include <complex>#include <cstdlib>#include <ctime>#include <fstream>#include <iostream>#include <valarray>Macros | |
| #define | ACCURACY 1e-10 |
| #define | MAX_BUFF_SIZE 50 |
Functions | |
| std::complex< double > | poly_function (const std::valarray< double > &coeffs, std::complex< double > x) |
| const char * | complex_str (const std::complex< double > &x) |
| bool | check_termination (long double delta) |
| std::pair< uint32_t, double > | durand_kerner_algo (const std::valarray< double > &coeffs, std::valarray< std::complex< double >> *roots, bool write_log=false) |
| void | test1 () |
| void | test2 () |
| int | main (int argc, char **argv) |
Compute all possible approximate roots of any given polynomial using Durand Kerner algorithm
Test the algorithm online: https://gist.github.com/kvedala/27f1b0b6502af935f6917673ec43bcd7
Try the highly unstable Wilkinson's polynomial:
Sample implementation results to compute approximate roots of the equation \(x^4-1=0\):
| #define ACCURACY 1e-10 |
maximum accuracy limit
| bool check_termination | ( | long double | delta | ) |
check for termination condition
| [in] | delta | point at which to evaluate the polynomial |
false if termination not reached true if termination reached | const char* complex_str | ( | const std::complex< double > & | x | ) |
create a textual form of complex number
| [in] | x | point at which to evaluate the polynomial |
| std::pair<uint32_t, double> durand_kerner_algo | ( | const std::valarray< double > & | coeffs, |
| std::valarray< std::complex< double >> * | roots, | ||
| bool | write_log = false |
||
| ) |
Implements Durand Kerner iterative algorithm to compute all roots of a polynomial.
| [in] | coeffs | coefficients of the polynomial |
| [out] | roots | the computed roots of the polynomial |
| [in] | write_log | flag whether to save the log file (default = false) |
| std::complex<double> poly_function | ( | const std::valarray< double > & | coeffs, |
| std::complex< double > | x | ||
| ) |
Evaluate the value of a polynomial with given coefficients
| [in] | coeffs | coefficients of the polynomial |
| [in] | x | point at which to evaluate the polynomial |
| void test1 | ( | ) |
Self test the algorithm by checking the roots for \(x^2+4=0\) to which the roots are \(0 \pm 2i\)
| void test2 | ( | ) |
Self test the algorithm by checking the roots for \(0.015625x^3-1=0\) to which the roots are \((4+0i),\,(-2\pm3.464i)\)