32 #ifndef VSMC_INTEGRATE_NINTEGRATE_NEWTON_COTES_HPP
33 #define VSMC_INTEGRATE_NINTEGRATE_NEWTON_COTES_HPP
37 #define VSMC_STATIC_ASSERT_INTEGRATE_NINTEGRATE_NEWTON_COTES_DEGREE(degree) \
38 VSMC_STATIC_ASSERT((degree >= 1 && degree <= max_degree_), \
39 USE_NIntegrateNewtonCotes_WITH_A_DEGREE_LARGER_THAN_max_degree)
45 template <
unsigned Index,
typename EvalType>
48 static double result (
const double *coeff,
double a,
double h,
51 return coeff[Index] * eval(a + (Index - 1) * h) +
57 template <
typename EvalType>
60 static double result (
const double *coeff,
double a,
double,
61 const EvalType &eval) {
return coeff[1] * eval(a);}
64 template <
unsigned Degree>
76 const double *
coeff()
const {
return coeff_;}
80 double coeff_[Degree + 2];
85 NIntegrateNewtonCotesCoeff (
86 const NIntegrateNewtonCotesCoeff<Degree> &);
88 NIntegrateNewtonCotesCoeff<Degree> &operator= (
89 const NIntegrateNewtonCotesCoeff<Degree> &);
98 void coeff_init (cxx11::integral_constant<unsigned, 2>)
100 coeff_[0] = 1.0 / 6.0;
106 void coeff_init (cxx11::integral_constant<unsigned, 3>)
115 void coeff_init (cxx11::integral_constant<unsigned, 4>)
117 coeff_[0] = 1.0 / 90.0;
125 void coeff_init (cxx11::integral_constant<unsigned, 5>)
127 coeff_[0] = 1.0 / 288.0;
136 void coeff_init (cxx11::integral_constant<unsigned, 6>)
138 coeff_[0] = 1.0 / 840.0;
148 void coeff_init (cxx11::integral_constant<unsigned, 7>)
150 coeff_[0] = 1.0 / 17280.0;
161 void coeff_init (cxx11::integral_constant<unsigned, 8>)
163 coeff_[0] = 1.0 / 28350.0;
175 void coeff_init (cxx11::integral_constant<unsigned, 9>)
177 coeff_[0] = 1.0 / 89600.0;
190 void coeff_init (cxx11::integral_constant<unsigned, 10>)
192 coeff_[0] = 1.0 / 598752.0;
211 template <
unsigned Degree>
226 const double *
const coeff =
229 double h = (b - a) / Degree;
231 return coeff[0] * (b - a) * (
233 result(coeff, a, h, eval) + coeff[Degree + 1] * eval(b));
245 #endif // VSMC_INTEGRATE_NINTEGRATE_NEWTON_COTES_HPP
static double result(const double *coeff, double a, double h, const EvalType &eval)
#define VSMC_CONSTEXPR
constexpr
Numerical integration with the (closed) Newton-Cotes formulae.
static double integrate_segment(double a, double b, const eval_type &eval)
static constexpr unsigned max_degree()
NIntegrateBase< NIntegrateNewtonCotes< Degree > >::eval_type eval_type
static double result(const double *coeff, double a, double, const EvalType &eval)
NIntegrateBase< NIntegrateNewtonCotes< Degree > >::size_type size_type
const double * coeff() const
#define VSMC_STATIC_ASSERT_INTEGRATE_NINTEGRATE_NEWTON_COTES_DEGREE(degree)
static NIntegrateNewtonCotesCoeff< Degree > & instance()
Numerical integration base dispatch class.