1 // Copyright Nick Thompson 2017.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #define BOOST_TEST_MAIN
8 
9 #include <boost/test/unit_test.hpp>
10 #include <boost/math/special_functions/legendre.hpp>
11 #include <boost/math/special_functions/legendre_stieltjes.hpp>
12 #include <boost/math/constants/constants.hpp>
13 #include <boost/multiprecision/cpp_bin_float.hpp>
14 
15 
16 using boost::math::legendre_stieltjes;
17 using boost::math::legendre_p;
18 using boost::multiprecision::cpp_bin_float_quad;
19 
20 
21 template<class Real>
test_legendre_stieltjes()22 void test_legendre_stieltjes()
23 {
24     std::cout << std::setprecision(std::numeric_limits<Real>::digits10);
25     using std::sqrt;
26     using std::abs;
27     using boost::math::constants::third;
28     using boost::math::constants::half;
29 
30     Real tol = std::numeric_limits<Real>::epsilon();
31     legendre_stieltjes<Real> ls1(1);
32     legendre_stieltjes<Real> ls2(2);
33     legendre_stieltjes<Real> ls3(3);
34     legendre_stieltjes<Real> ls4(4);
35     legendre_stieltjes<Real> ls5(5);
36     legendre_stieltjes<Real> ls8(8);
37     Real x = -1;
38     while(x <= 1)
39     {
40         BOOST_CHECK_CLOSE_FRACTION(ls1(x), x, tol);
41         BOOST_CHECK_CLOSE_FRACTION(ls1.prime(x), 1, tol);
42 
43         Real p2 = legendre_p(2, x);
44         BOOST_CHECK_CLOSE_FRACTION(ls2(x), p2 - 2/static_cast<Real>(5), tol);
45         BOOST_CHECK_CLOSE_FRACTION(ls2.prime(x), 3*x, tol);
46 
47         Real p3 = legendre_p(3, x);
48         BOOST_CHECK_CLOSE_FRACTION(ls3(x), p3 - 9*x/static_cast<Real>(14), 600*tol);
49         BOOST_CHECK_CLOSE_FRACTION(ls3.prime(x), 15*x*x*half<Real>() -3*half<Real>()-9/static_cast<Real>(14), 100*tol);
50 
51         Real p4 = legendre_p(4, x);
52         //-20P_2(x)/27 + 14P_0(x)/891
53         Real E4 = p4 - 20*p2/static_cast<Real>(27) + 14/static_cast<Real>(891);
54         BOOST_CHECK_CLOSE_FRACTION(ls4(x), E4, 250*tol);
55         BOOST_CHECK_CLOSE_FRACTION(ls4.prime(x), 35*x*(9*x*x -5)/static_cast<Real>(18), 250*tol);
56 
57         Real p5 = legendre_p(5, x);
58         Real E5 = p5 - 35*p3/static_cast<Real>(44) + 135*x/static_cast<Real>(12584);
59         BOOST_CHECK_CLOSE_FRACTION(ls5(x), E5, 29000*tol);
60         Real E5prime = (315*(123 + 143*x*x*(11*x*x-9)))/static_cast<Real>(12584);
61         BOOST_CHECK_CLOSE_FRACTION(ls5.prime(x), E5prime, 29000*tol);
62         x += 1/static_cast<Real>(1 << 9);
63     }
64 
65     // Test norm:
66     // E_1 = x
67     Real expected_norm_sq = 2*third<Real>();
68     BOOST_CHECK_CLOSE_FRACTION(expected_norm_sq, ls1.norm_sq(), tol);
69 
70     // E_2 = P[sub 2](x) - 2P[sup 0](x)/5
71     expected_norm_sq = 2/static_cast<Real>(5) + 8/static_cast<Real>(25);
72     BOOST_CHECK_CLOSE_FRACTION(expected_norm_sq, ls2.norm_sq(), tol);
73 
74     // E_3 = P[sub 3](x) - 9P[sub 1]/14
75     expected_norm_sq = 2/static_cast<Real>(7) + 9*9*2*third<Real>()/static_cast<Real>(14*14);
76     BOOST_CHECK_CLOSE_FRACTION(expected_norm_sq, ls3.norm_sq(), tol);
77 
78     // E_4 = P[sub 4](x) -20P[sub 2](x)/27 + 14P[sub 0](x)/891
79     expected_norm_sq = static_cast<Real>(2)/static_cast<Real>(9) + static_cast<Real>(20*20*2)/static_cast<Real>(27*27*5) + 14*14*2/static_cast<Real>(891*891);
80     BOOST_CHECK_CLOSE_FRACTION(expected_norm_sq, ls4.norm_sq(), tol);
81 
82     // E_5 = P[sub 5](x) - 35P[sub 3](x)/44 + 135P[sub 1](x)/12584
83     expected_norm_sq = 2/static_cast<Real>(11) + (35*35/static_cast<Real>(44*44))*(2/static_cast<Real>(7)) + (135*135/static_cast<Real>(12584*12584))*2*third<Real>();
84     BOOST_CHECK_CLOSE_FRACTION(expected_norm_sq, ls5.norm_sq(), tol);
85 
86     // Only zero of E1 is 0:
87     std::vector<Real> zeros = ls1.zeros();
88     BOOST_CHECK(zeros.size() == 1);
89     BOOST_CHECK_SMALL(zeros[0], tol);
90     BOOST_CHECK_SMALL(ls1(zeros[0]), tol);
91 
92     zeros = ls2.zeros();
93     BOOST_CHECK(zeros.size() == 1);
94     BOOST_CHECK_CLOSE_FRACTION(zeros[0], sqrt(3/static_cast<Real>(5)), tol);
95     BOOST_CHECK_SMALL(ls2(zeros[0]), tol);
96 
97     zeros = ls3.zeros();
98     BOOST_CHECK(zeros.size() == 2);
99     BOOST_CHECK_SMALL(zeros[0], tol);
100     BOOST_CHECK_CLOSE_FRACTION(zeros[1], sqrt(6/static_cast<Real>(7)), tol);
101 
102 
103     zeros = ls4.zeros();
104     BOOST_CHECK(zeros.size() == 2);
105     Real expected = sqrt( (55 - 2*sqrt(static_cast<Real>(330)))/static_cast<Real>(11) )/static_cast<Real>(3);
106     BOOST_CHECK_CLOSE_FRACTION(zeros[0], expected, tol);
107 
108     expected = sqrt( (55 + 2*sqrt(static_cast<Real>(330)))/static_cast<Real>(11) )/static_cast<Real>(3);
109     BOOST_CHECK_CLOSE_FRACTION(zeros[1], expected, 10*tol);
110 
111 
112     zeros = ls5.zeros();
113     BOOST_CHECK(zeros.size() == 3);
114     BOOST_CHECK_SMALL(zeros[0], tol);
115 
116     expected = sqrt( ( 195 - sqrt(static_cast<Real>(6045)) )/static_cast<Real>(286));
117     BOOST_CHECK_CLOSE_FRACTION(zeros[1], expected, tol);
118 
119     expected = sqrt( ( 195 + sqrt(static_cast<Real>(6045)) )/static_cast<Real>(286));
120     BOOST_CHECK_CLOSE_FRACTION(zeros[2], expected, tol);
121 
122 
123     for (size_t i = 6; i < 50; ++i)
124     {
125         legendre_stieltjes<Real> En(i);
126         zeros = En.zeros();
127         for(auto const & zero : zeros)
128         {
129             BOOST_CHECK_SMALL(En(zero), 50*tol);
130         }
131     }
132 }
133 
134 
BOOST_AUTO_TEST_CASE(LegendreStieltjesZeros)135 BOOST_AUTO_TEST_CASE(LegendreStieltjesZeros)
136 {
137     test_legendre_stieltjes<double>();
138     test_legendre_stieltjes<long double>();
139     test_legendre_stieltjes<cpp_bin_float_quad>();
140     //test_legendre_stieltjes<boost::multiprecision::cpp_bin_float_100>();
141 }
142