Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / jacobi_test.cpp
1 /*
2  * Copyright Nick Thompson, 2019
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
8 #include "math_unit_test.hpp"
9 #include <numeric>
10 #include <utility>
11 #include <random>
12 #include <cmath>
13 #include <boost/math/special_functions/jacobi.hpp>
14 #ifdef BOOST_HAS_FLOAT128
15 #include <boost/multiprecision/float128.hpp>
16 using boost::multiprecision::float128;
17 #endif
18
19 using std::abs;
20 using boost::math::jacobi;
21 using boost::math::jacobi_derivative;
22
23 template<typename Real>
24 void test_to_quadratic()
25 {
26     Real h = 1/Real(8);
27     for (Real alpha = -1 + h; alpha < 2; alpha += h) {
28         for (Real beta = -1 + h; beta < 2; beta += h) {
29             for (Real x = -1; x < 1; x += h) {
30                 Real expected = 1;
31                 Real computed = jacobi(0, alpha, beta, x);
32                 CHECK_ULP_CLOSE(expected, computed, 0);
33
34                 expected = (alpha + 1) + (alpha + beta +2)*(x-1)/2;
35                 computed = jacobi(1, alpha, beta, x);
36                 CHECK_ULP_CLOSE(expected, computed, 0);
37
38                 expected = (alpha + 1)*(alpha+2)/2 + (alpha + 2)*(alpha + beta + 3)*(x-1)/2 + (alpha + beta + 3)*(alpha + beta + 4)*(x-1)*(x-1)/8;
39                 computed = jacobi(2, alpha, beta, x);
40                 CHECK_ULP_CLOSE(expected, computed, 1);
41
42             }
43         }
44     }
45 }
46
47 template<typename Real>
48 void test_symmetry()
49 {
50     Real h = 1/Real(4);
51     for (Real alpha = -1 + h; alpha < 2; alpha += h) {
52         for (Real beta = -1 + h; beta < 2; beta += h) {
53             for (Real x = -1; x < 1; x += h) {
54                 for (size_t n = 0; n < 20; n += 2)
55                 {
56                     Real expected = jacobi(n, beta, alpha , -x);
57                     Real computed = jacobi(n, alpha, beta, x);
58                     CHECK_ULP_CLOSE(expected, computed, 0);
59
60                     expected = jacobi(n+1, beta, alpha, -x);
61                     computed = -jacobi(n+1, alpha, beta, x);
62                     CHECK_ULP_CLOSE(expected, computed, 0);
63                 }
64             }
65         }
66     }
67 }
68
69 template<typename Real>
70 void test_derivative()
71 {
72     Real h = 1/Real(4);
73     for (Real alpha = -1 + h; alpha < 2; alpha += h) {
74         for (Real beta = -1 + h; beta < 2; beta += h) {
75             for (Real x = -1; x < 1; x += h) {
76                 Real expected = 0;
77                 Real computed = jacobi_derivative(0, alpha, beta, x, 1);
78                 CHECK_ULP_CLOSE(expected, computed, 0);
79
80                 expected = (alpha + beta + 2)/2;
81                 computed = jacobi_derivative(1, alpha, beta, x, 1);
82                 CHECK_ULP_CLOSE(expected, computed, 0);
83
84                 expected = (alpha + 2)*(alpha + beta + 3)/2 + (alpha + beta + 3)*(alpha + beta + 4)*(x-1)/4;
85                 computed = jacobi_derivative(2, alpha, beta, x, 1);
86                 CHECK_ULP_CLOSE(expected, computed, 0);
87
88                 expected = (alpha + beta + 3)*(alpha + beta + 4)/4;
89                 computed = jacobi_derivative(2, alpha, beta, x, 2);
90                 CHECK_ULP_CLOSE(expected, computed, 0);
91             }
92         }
93     }
94 }
95
96 int main()
97 {
98     test_to_quadratic<double>();
99     test_to_quadratic<long double>();
100
101     test_symmetry<float>();
102     test_symmetry<double>();
103     test_symmetry<long double>();
104
105     test_derivative<float>();
106     test_derivative<double>();
107     test_derivative<long double>();
108
109 #ifdef BOOST_HAS_FLOAT128
110     test_to_quadratic<boost::multiprecision::float128>();
111     test_symmetry<boost::multiprecision::float128>();
112     test_derivative<boost::multiprecision::float128>();
113 #endif
114
115     return boost::math::test::report_errors();
116 }