jacobi_test.cpp 3.6 KB

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