| 
 | 1 | +/*  | 
 | 2 | + * Copyright Nick Thompson, 2024  | 
 | 3 | + * Use, modification and distribution are subject to the  | 
 | 4 | + * Boost Software License, Version 1.0. (See accompanying file  | 
 | 5 | + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)  | 
 | 6 | + */  | 
 | 7 | +#include <vector>  | 
 | 8 | +#include <iostream>  | 
 | 9 | +#include <list>  | 
 | 10 | +#include <random>  | 
 | 11 | +#include <cmath>  | 
 | 12 | +#include <complex>  | 
 | 13 | +#include <utility>  | 
 | 14 | +#include <limits>  | 
 | 15 | +#include <algorithm>  | 
 | 16 | +#include <boost/math/tools/polynomial.hpp>  | 
 | 17 | +using boost::math::tools::polynomial;  | 
 | 18 | +#ifdef BOOST_HAS_FLOAT128  | 
 | 19 | +#include <boost/multiprecision/float128.hpp>  | 
 | 20 | +using boost::multiprecision::float128;  | 
 | 21 | +#endif  | 
 | 22 | +#include <boost/multiprecision/cpp_bin_float.hpp>  | 
 | 23 | +#include "math_unit_test.hpp"  | 
 | 24 | + | 
 | 25 | +#if __has_include(<Eigen/Eigenvalues>)  | 
 | 26 | + | 
 | 27 | +void test_random_coefficients() {  | 
 | 28 | +    std::random_device rd;  | 
 | 29 | +    uint32_t seed = rd();   | 
 | 30 | +    std::mt19937_64 mt(seed);  | 
 | 31 | +    std::uniform_real_distribution<double> unif(-1, 1);  | 
 | 32 | +    std::size_t n = seed % 3 + 3;  | 
 | 33 | +    std::vector<double> coeffs(n, std::numeric_limits<double>::quiet_NaN());  | 
 | 34 | +    for (std::size_t i = 0; i < coeffs.size(); ++i) {  | 
 | 35 | +        coeffs[i] = unif(mt);  | 
 | 36 | +    }  | 
 | 37 | +    coeffs[coeffs.size() -1] = 1.0;  | 
 | 38 | +    auto p = polynomial(std::move(coeffs));  | 
 | 39 | +    auto roots = p.roots();  | 
 | 40 | +    CHECK_EQUAL(roots.size(), p.size() - 1);  | 
 | 41 | +    std::complex<double> root_product = -1;  | 
 | 42 | +    std::complex<double> root_sum = 0.0;  | 
 | 43 | +    for (auto const & root : roots) {  | 
 | 44 | +        root_product *= static_cast<std::complex<double>>(root);  | 
 | 45 | +        root_sum += static_cast<std::complex<double>>(root);  | 
 | 46 | +    }  | 
 | 47 | +    if (p.size() & 1) {  | 
 | 48 | +       root_product *= -1;  | 
 | 49 | +    }  | 
 | 50 | +    CHECK_ULP_CLOSE(root_product.real(), p[0], 1000);  | 
 | 51 | +    CHECK_LE(root_product.imag(), 1e-6);  | 
 | 52 | + | 
 | 53 | +    CHECK_LE(root_sum.imag(), 1e-7);  | 
 | 54 | +    CHECK_ULP_CLOSE(root_sum.real(), -p[p.size() - 2], 1000);  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | + | 
 | 58 | + | 
 | 59 | +void test_wilkinson_polynomial() {  | 
 | 60 | +    // CoefficientList[Expand[(x - 1)*(x - 2)*(x - 3)*(x - 4)*(x - 5)*(x - 6)*(x - 7)*(x - 8)*(x - 9)*(x - 10)], x]  | 
 | 61 | +    std::vector<float> coeffs{3628800.0, -10628640.0, 12753576.0, -8409500.0, 3416930.0, -902055.0, 157773.0, -18150.0, 1320.0, -55.0 ,1.0};  | 
 | 62 | +    auto p = polynomial(std::move(coeffs));  | 
 | 63 | +    auto roots = p.roots();  | 
 | 64 | +    CHECK_EQUAL(roots.size(), p.size() - 1);  | 
 | 65 | +    std::complex<double> root_product = -1;  | 
 | 66 | +    std::complex<double> root_sum = 0.0;  | 
 | 67 | +    for (auto const & root : roots) {  | 
 | 68 | +        root_product *= static_cast<std::complex<double>>(root);  | 
 | 69 | +        root_sum += static_cast<std::complex<double>>(root);  | 
 | 70 | +    }  | 
 | 71 | +    if (p.size() & 1) {  | 
 | 72 | +       root_product *= -1;  | 
 | 73 | +    }  | 
 | 74 | +    CHECK_ABSOLUTE_ERROR(root_product.real(), double(p[0]), double(1e-3*p[0]));  | 
 | 75 | +    CHECK_LE(root_product.imag(), 1e-8);  | 
 | 76 | + | 
 | 77 | +    CHECK_LE(root_sum.imag(), 1e-8);  | 
 | 78 | +    CHECK_ABSOLUTE_ERROR(root_sum.real(), -double(p[p.size() - 2]), 1e-5);  | 
 | 79 | + | 
 | 80 | +    std::complex<double> c = 0.0;  | 
 | 81 | +    for (std::size_t i = 0; i < roots.size(); ++i) {  | 
 | 82 | +        auto ri = static_cast<std::complex<double>>(roots[i]);  | 
 | 83 | +        for (std::size_t j = i + 1; j < roots.size(); ++j) {  | 
 | 84 | +            c += ri*static_cast<std::complex<double>>(roots[j]);  | 
 | 85 | +        }  | 
 | 86 | +    }  | 
 | 87 | +    CHECK_ULP_CLOSE(p[p.size()-3], static_cast<float>(c.real()), 10);  | 
 | 88 | +    CHECK_ABSOLUTE_ERROR(0.0, c.imag(), 1e-8);  | 
 | 89 | + | 
 | 90 | +}  | 
 | 91 | + | 
 | 92 | +template<typename T>  | 
 | 93 | +void test_singular_companion()  | 
 | 94 | +{  | 
 | 95 | +    std::vector<T> coeffs{0.0, 0.0, 1.0};   | 
 | 96 | +    auto p = polynomial(std::move(coeffs));  | 
 | 97 | +    auto roots = p.roots();  | 
 | 98 | +    CHECK_EQUAL(roots.size(), p.size() - 1);  | 
 | 99 | +    for (std::size_t i = 0; i < roots.size() - 1; ++i) {  | 
 | 100 | +        CHECK_ABSOLUTE_ERROR(T(0), roots[i].real(), std::numeric_limits<T>::epsilon());  | 
 | 101 | +        CHECK_ABSOLUTE_ERROR(T(0), roots[i].imag(), std::numeric_limits<T>::epsilon());  | 
 | 102 | +    }  | 
 | 103 | +}  | 
 | 104 | + | 
 | 105 | + | 
 | 106 | +int main()  | 
 | 107 | +{  | 
 | 108 | +    test_random_coefficients();  | 
 | 109 | +    test_singular_companion<float>();  | 
 | 110 | +    test_singular_companion<double>();  | 
 | 111 | +#if BOOST_HAS_FLOAT128  | 
 | 112 | +    test_singular_companion<float128>();  | 
 | 113 | +#endif  | 
 | 114 | +    test_singular_companion<boost::multiprecision::cpp_bin_float_100>();  | 
 | 115 | +    test_wilkinson_polynomial();  | 
 | 116 | +    return boost::math::test::report_errors();  | 
 | 117 | +}  | 
 | 118 | +#endif  | 
0 commit comments