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)
7 #define BOOST_TEST_MODULE sinh_sinh_quadrature_test
9 #include <boost/multiprecision/cpp_complex.hpp>
10 #include <boost/math/concepts/real_concept.hpp>
11 #include <boost/test/included/unit_test.hpp>
12 #include <boost/test/tools/floating_point_comparison.hpp>
13 #include <boost/math/quadrature/sinh_sinh.hpp>
14 #include <boost/math/special_functions/sinc.hpp>
15 #include <boost/multiprecision/cpp_bin_float.hpp>
17 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
18 // MSVC-12 has problems if we include 2 different multiprecision types in the same program,
19 // basically random things stop compiling, even though they work fine in isolation :(
20 #include <boost/multiprecision/cpp_dec_float.hpp>
41 using boost::multiprecision::cpp_bin_float_quad;
42 using boost::math::quadrature::sinh_sinh;
43 using boost::math::constants::pi;
44 using boost::math::constants::pi_sqr;
45 using boost::math::constants::half_pi;
46 using boost::math::constants::two_div_pi;
47 using boost::math::constants::half;
48 using boost::math::constants::third;
49 using boost::math::constants::half;
50 using boost::math::constants::third;
51 using boost::math::constants::catalan;
52 using boost::math::constants::ln_two;
53 using boost::math::constants::root_two;
54 using boost::math::constants::root_two_pi;
55 using boost::math::constants::root_pi;
57 // Code for generating the coefficients:
60 void print_levels(const T& v, const char* suffix)
63 for (unsigned i = 0; i < v.size(); ++i)
66 for (unsigned j = 0; j < v[i].size(); ++j)
68 std::cout << v[i][j] << suffix << ", ";
76 void print_levels(const std::pair<T, T>& p, const char* suffix = "")
78 std::cout << " static const std::vector<std::vector<Real> > abscissa = ";
79 print_levels(p.first, suffix);
80 std::cout << " static const std::vector<std::vector<Real> > weights = ";
81 print_levels(p.second, suffix);
84 template <class Real, class TargetType>
85 std::pair<std::vector<std::vector<Real>>, std::vector<std::vector<Real>> > generate_constants(unsigned max_rows)
87 using boost::math::constants::half_pi;
88 using boost::math::constants::two_div_pi;
89 using boost::math::constants::pi;
90 auto g = [](Real t) { return sinh(half_pi<Real>()*sinh(t)); };
91 auto w = [](Real t) { return cosh(t)*half_pi<Real>()*cosh(half_pi<Real>()*sinh(t)); };
93 std::vector<std::vector<Real>> abscissa, weights;
95 std::vector<Real> temp;
97 Real t_max = log(2 * two_div_pi<Real>()*log(2 * two_div_pi<Real>()*sqrt(boost::math::tools::max_value<TargetType>())));
99 std::cout << "m_t_max = " << t_max << ";\n";
102 for (Real t = 1; t < t_max; t += h)
104 temp.push_back(g(t));
106 abscissa.push_back(temp);
109 for (Real t = 1; t < t_max; t += h)
111 temp.push_back(w(t * h));
113 weights.push_back(temp);
116 for (unsigned row = 1; row < max_rows; ++row)
119 for (Real t = h; t < t_max; t += 2 * h)
120 temp.push_back(g(t));
121 abscissa.push_back(temp);
125 for (unsigned row = 1; row < max_rows; ++row)
128 for (Real t = h; t < t_max; t += 2 * h)
129 temp.push_back(w(t));
130 weights.push_back(temp);
134 return std::make_pair(abscissa, weights);
138 void test_nr_examples()
140 std::cout << "Testing type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
141 Real integration_limit = sqrt(boost::math::tools::epsilon<Real>());
142 Real tol = 10 * boost::math::tools::epsilon<Real>();
143 std::cout << std::setprecision(std::numeric_limits<Real>::digits10);
148 sinh_sinh<Real> integrator(10);
150 auto f0 = [](Real)->Real { return (Real) 0; };
151 Q = integrator.integrate(f0, integration_limit, &error, &L1);
153 BOOST_CHECK_SMALL(Q, tol);
154 BOOST_CHECK_SMALL(L1, tol);
156 // In spite of the poles at \pm i, we still get a doubling of the correct digits at each level of refinement.
157 auto f1 = [](const Real& t)->Real { return 1/(1+t*t); };
158 Q = integrator.integrate(f1, integration_limit, &error, &L1);
159 Q_expected = pi<Real>();
160 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
161 BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
162 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1900)
163 auto f2 = [](const Real& x)->Real { return fabs(x) > boost::math::tools::log_max_value<Real>() ? 0 : exp(-x*x); };
165 auto f2 = [](const Real& x)->Real { return exp(-x*x); };
167 Q = integrator.integrate(f2, integration_limit, &error, &L1);
168 Q_expected = root_pi<Real>();
169 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
170 BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
172 auto f5 = [](const Real& t)->Real { return 1/cosh(t);};
173 Q = integrator.integrate(f5, integration_limit, &error, &L1);
174 Q_expected = pi<Real>();
175 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
176 BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
178 // This oscillatory integral has rapid convergence because the oscillations get swamped by the exponential growth of the denominator,
179 // none the less the error is slightly higher than for the other cases:
181 auto f8 = [](const Real& t)->Real { return cos(t)/cosh(t);};
182 Q = integrator.integrate(f8, integration_limit, &error, &L1);
183 Q_expected = pi<Real>()/cosh(half_pi<Real>());
184 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
185 // Try again with progressively fewer arguments:
186 Q = integrator.integrate(f8, integration_limit);
187 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
188 Q = integrator.integrate(f8);
189 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
192 // Test formulas for in the CRC Handbook of Mathematical functions, 32nd edition.
196 std::cout << "Testing CRC formulas on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
197 Real integration_limit = sqrt(boost::math::tools::epsilon<Real>());
198 Real tol = 10 * boost::math::tools::epsilon<Real>();
199 std::cout << std::setprecision(std::numeric_limits<Real>::digits10);
204 sinh_sinh<Real> integrator(10);
206 // CRC Definite integral 698:
207 auto f0 = [](Real x)->Real {
214 Q = integrator.integrate(f0, integration_limit, &error, &L1);
215 Q_expected = pi_sqr<Real>()/2;
216 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
217 BOOST_CHECK_CLOSE_FRACTION(L1, Q_expected, tol);
220 // CRC Definite integral 695:
221 auto f1 = [](Real x)->Real {
222 using std::sin; using std::sinh;
226 return (Real) sin(x)/sinh(x);
228 Q = integrator.integrate(f1, integration_limit, &error, &L1);
229 Q_expected = pi<Real>()*tanh(half_pi<Real>());
230 BOOST_CHECK_CLOSE_FRACTION(Q, Q_expected, tol);
233 template<class Complex>
234 void test_dirichlet_eta()
236 typedef typename Complex::value_type Real;
237 std::cout << "Testing Dirichlet eta function on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
238 Real tol = 10 * boost::math::tools::epsilon<Real>();
240 sinh_sinh<Real> integrator(10);
242 //https://en.wikipedia.org/wiki/Dirichlet_eta_function, integral representations:
244 auto eta = [&z](Real t)->Complex {
247 using boost::math::constants::pi;
249 Complex num = pow((Real)1/ (Real)2 + i*t, -z);
250 Real denom = exp(pi<Real>()*t) + exp(-pi<Real>()*t);
251 Complex res = num/denom;
254 Q = integrator.integrate(eta);
255 // N[DirichletEta[1 + I], 150]
256 Complex Q_expected = {boost::lexical_cast<Real>("0.726559775062463263201495728547241386311129502735725787103568290594808442332084045617744978600192784188182345866652233650512117834307254514480657408096"),
257 boost::lexical_cast<Real>("0.158095863901207324355426285544321998253687969756843115763682522207208309489794631247865357375538028170751576870244296106203144195376645765556607038775")};
259 BOOST_CHECK_CLOSE_FRACTION(Q.real(), Q_expected.real(), tol);
260 BOOST_CHECK_CLOSE_FRACTION(Q.imag(), Q_expected.imag(), tol);
264 BOOST_AUTO_TEST_CASE(sinh_sinh_quadrature_test)
267 // Uncomment the following to print out the coefficients:
270 std::cout << std::scientific << std::setprecision(8);
271 print_levels(generate_constants<cpp_bin_float_100, float>(8), "f");
272 std::cout << std::setprecision(18);
273 print_levels(generate_constants<cpp_bin_float_100, double>(8), "");
274 std::cout << std::setprecision(35);
275 print_levels(generate_constants<cpp_bin_float_100, cpp_bin_float_quad>(8), "L");
277 test_nr_examples<float>();
278 test_nr_examples<double>();
279 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
280 test_nr_examples<long double>();
282 test_nr_examples<cpp_bin_float_quad>();
283 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
284 test_nr_examples<boost::math::concepts::real_concept>();
286 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
287 test_nr_examples<boost::multiprecision::cpp_dec_float_50>();
292 test_dirichlet_eta<std::complex<double>>();
293 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
294 test_crc<long double>();
295 test_dirichlet_eta<std::complex<long double>>();
297 test_crc<cpp_bin_float_quad>();
298 test_dirichlet_eta<boost::multiprecision::cpp_complex_quad>();
299 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
300 test_crc<boost::math::concepts::real_concept>();
302 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
303 test_crc<boost::multiprecision::cpp_dec_float_50>();