1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
// Copyright Christopher Kormanyos 2013.
// Copyright Paul A. Bristow 2013.
// Copyright John Maddock 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or
// copy at http://www.boost.org/LICENSE_1_0.txt).
#ifdef _MSC_VER
# pragma warning (disable : 4512) // assignment operator could not be generated.
# pragma warning (disable : 4996) // assignment operator could not be generated.
#endif
#include <iostream>
#include <limits>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <iterator>
//[bessel_zeros_iterator_example_1
/*`[h5 Using Output Iterator to sum zeros of Bessel Functions]
This example demonstrates summing zeros of the Bessel functions.
To use the functions for finding zeros of the functions we need
*/
#include <boost/math/special_functions/bessel.hpp>
/*`We use the `cyl_bessel_j_zero` output iterator parameter `out_it`
to create a sum of ['1/zeros[super 2]] by defining a custom output iterator:
*/
template <class T>
struct output_summation_iterator
{
output_summation_iterator(T* p) : p_sum(p)
{}
output_summation_iterator& operator*()
{ return *this; }
output_summation_iterator& operator++()
{ return *this; }
output_summation_iterator& operator++(int)
{ return *this; }
output_summation_iterator& operator = (T const& val)
{
*p_sum += 1./ (val * val); // Summing 1/zero^2.
return *this;
}
private:
T* p_sum;
};
//] [/bessel_zeros_iterator_example_1]
int main()
{
try
{
//[bessel_zeros_iterator_example_2
/*`The sum is calculated for many values, converging on the analytical exact value of `1/8`.
*/
using boost::math::cyl_bessel_j_zero;
double nu = 1.;
double sum = 0;
output_summation_iterator<double> it(&sum); // sum of 1/zeros^2
cyl_bessel_j_zero(nu, 1, 10000, it);
double s = 1/(4 * (nu + 1)); // 0.125 = 1/8 is exact analytical solution.
std::cout << std::setprecision(6) << "nu = " << nu << ", sum = " << sum
<< ", exact = " << s << std::endl;
// nu = 1.00000, sum = 0.124990, exact = 0.125000
//] [/bessel_zeros_iterator_example_2]
}
catch (std::exception const& ex)
{
std::cout << "Thrown exception " << ex.what() << std::endl;
}
return 0;
} // int_main()
/*
Output:
nu = 1, sum = 0.12499, exact = 0.125
*/
|