5 namespace integration {
7 t_real
integrate(
const Vector<t_real> &xmin,
const Vector<t_real> &xmax,
8 const std::function<t_real(Vector<t_real>)> &func,
const norm_type norm,
9 const t_real required_abs_error,
const t_real required_rel_error,
10 const t_uint max_evaluations,
const method methodtype) {
11 assert(xmin.size() == xmax.size());
14 auto wrap_integrand = [](
unsigned ndim,
const t_real *x,
void *fdata,
unsigned fdim,
15 double *fval) ->
int {
16 fval[0] = (*(
static_cast<std::function<t_real(Vector<t_real>)
> *>(fdata)))(
17 Vector<t_real>::Map(x, ndim));
22 if (pcubature(1, wrap_integrand,
const_cast<std::function<t_real(Vector<t_real>)
> *>(&func),
23 xmin.size(), xmin.data(), xmax.data(), max_evaluations, required_abs_error,
24 required_rel_error,
norm_error(norm), &val, &err) != 0)
25 throw std::runtime_error(
"Error in calculating p adaptive integral with cubature.");
28 if (hcubature(1, wrap_integrand,
const_cast<std::function<t_real(Vector<t_real>)
> *>(&func),
29 xmin.size(), xmin.data(), xmax.data(), max_evaluations, required_abs_error,
30 required_rel_error,
norm_error(norm), &val, &err) != 0)
31 throw std::runtime_error(
"Error in calculating h adaptive integral with cubature.");
34 throw std::runtime_error(
"Method not possibe with cubature.");
40 t_complex
integrate(
const Vector<t_real> &xmin,
const Vector<t_real> &xmax,
41 const std::function<t_complex(Vector<t_real>)> &func,
const norm_type norm,
42 const t_real required_abs_error,
const t_real required_rel_error,
43 const t_uint max_evaluations,
const method methodtype) {
44 assert(xmin.size() == xmax.size());
45 std::array<t_real, 2> val{0., 0.};
46 std::array<t_real, 2> err{0., 0.};
47 auto wrap_integrand = [](
unsigned ndim,
const t_real *x,
void *fdata,
unsigned fdim,
48 double *fval) ->
int {
50 const t_complex output = (*(
static_cast<std::function<t_complex(Vector<t_real>)
> *>(fdata)))(
51 Vector<t_real>::Map(x, ndim));
52 fval[0] = output.real();
53 fval[1] = output.imag();
58 if (pcubature(val.size(), wrap_integrand,
59 const_cast<std::function<t_complex(Vector<t_real>)
> *>(&func), xmin.size(),
60 xmin.data(), xmax.data(), max_evaluations, required_abs_error, required_rel_error,
61 norm_error(norm), val.data(), err.data()) != 0)
62 throw std::runtime_error(
"Error in calculating p adaptive integral with cubature.");
65 if (hcubature(val.size(), wrap_integrand,
66 const_cast<std::function<t_complex(Vector<t_real>)
> *>(&func), xmin.size(),
67 xmin.data(), xmax.data(), max_evaluations, required_abs_error, required_rel_error,
68 norm_error(norm), val.data(), err.data()) != 0)
69 throw std::runtime_error(
"Error in calculating h adaptive integral with cubature.");
72 throw std::runtime_error(
"Integration method not known with cubature.");
76 return t_complex(val.at(0), val.at(1));
78 Vector<t_real>
integrate(
const t_uint fdim,
const Vector<t_real> &xmin,
const Vector<t_real> &xmax,
79 const std::function<Vector<t_real>(Vector<t_real>)> &func,
80 const norm_type norm,
const t_real required_abs_error,
81 const t_real required_rel_error,
const t_uint max_evaluations,
83 assert(xmin.size() == xmax.size());
84 Vector<t_real> val = Vector<t_real>::Zero(fdim);
85 Vector<t_real> err = Vector<t_real>::Zero(fdim);
86 auto wrap_integrand = [](
unsigned ndim,
const t_real *x,
void *fdata,
unsigned fdim,
87 double *fval) ->
int {
88 Vector<t_real>::Map(fval, fdim) =
89 (*(
static_cast<std::function<Vector<t_real>(Vector<t_real>)
> *>(fdata)))(
90 Vector<t_real>::Map(x, ndim));
95 if (pcubature(val.size(), wrap_integrand,
96 const_cast<std::function<Vector<t_real>(Vector<t_real>)
> *>(&func), xmin.size(),
97 xmin.data(), xmax.data(), max_evaluations, required_abs_error, required_rel_error,
98 norm_error(norm), val.data(), err.data()) != 0)
99 throw std::runtime_error(
"Error in calculating p adaptive integral with cubature.");
102 if (hcubature(val.size(), wrap_integrand,
103 const_cast<std::function<Vector<t_real>(Vector<t_real>)
> *>(&func), xmin.size(),
104 xmin.data(), xmax.data(), max_evaluations, required_abs_error, required_rel_error,
105 norm_error(norm), val.data(), err.data()) != 0)
106 throw std::runtime_error(
"Error in calculating h adaptive integral with cubature.");
109 throw std::runtime_error(
"Method not possible with cubature.");
115 Vector<t_complex>
integrate_v(
const t_uint fdim,
const t_uint npts,
const Vector<t_real> &xmin,
116 const Vector<t_real> &xmax,
117 const std::vector<std::function<t_complex(Vector<t_real>)>> &func,
118 const norm_type norm,
const t_real required_abs_error,
119 const t_real required_rel_error,
const t_uint max_evaluations,
120 const method methodtype) {
121 assert(xmin.size() == xmax.size());
122 Vector<t_real> val = Vector<t_real>::Zero(2 * fdim);
123 Vector<t_real> err = Vector<t_real>::Zero(2 * fdim);
124 auto wrap_integrand = [](
unsigned ndim,
unsigned long npts,
const t_real *x,
void *fdata,
125 unsigned fdim,
double *fval) ->
int {
126 for (
int i = 0; i < npts; i = i + 2) {
127 const t_complex val =
128 (*(
static_cast<std::vector<std::function<t_complex(Vector<t_real>)
>> *>(fdata)))
129 .at(i)(Vector<t_real>::Map(x, ndim));
130 fval[i] = val.real();
131 fval[i + 1] = val.imag();
135 switch (methodtype) {
137 if (pcubature_v(val.size(), wrap_integrand,
138 const_cast<std::vector<std::function<t_complex(Vector<t_real>)
>> *>(&func),
139 xmin.size(), xmin.data(), xmax.data(), max_evaluations, required_abs_error,
140 required_rel_error,
norm_error(norm), val.data(), err.data()) != 0)
141 throw std::runtime_error(
"Error in calculating p adaptive integral with cubature.");
144 if (hcubature_v(val.size(), wrap_integrand,
145 const_cast<std::vector<std::function<t_complex(Vector<t_real>)
>> *>(&func),
146 xmin.size(), xmin.data(), xmax.data(), max_evaluations, required_abs_error,
147 required_rel_error,
norm_error(norm), val.data(), err.data()) != 0)
148 throw std::runtime_error(
"Error in calculating h adaptive integral with cubature.");
151 throw std::runtime_error(
"Method not possible with cubature.");
154 Vector<t_complex> output = Vector<t_complex>::Zero(fdim);
155 output.real() = Vector<t_real>::Map(val.data(), fdim, 1, Eigen::Stride<Eigen::Dynamic, 1>(2, 0));
157 Vector<t_real>::Map(val.data() + 1, fdim, 1, Eigen::Stride<Eigen::Dynamic, 1>(2, 0));
173 return ERROR_INDIVIDUAL;
183 t_complex
convolution(
const Vector<t_real> &x0,
const Vector<t_real> &xmin,
184 const Vector<t_real> &xmax,
185 const std::function<t_complex(Vector<t_real>)> &func1,
186 const std::function<t_complex(Vector<t_real>)> &func2,
const norm_type norm,
187 const t_real required_abs_error,
const t_real required_rel_error,
188 const t_uint max_evaluations,
const method methodtype) {
190 xmin, xmax, [=](
const Vector<t_real> &x) -> t_complex {
return func1(x0 - x) * func2(x); },
191 norm, required_abs_error, required_rel_error, max_evaluations, methodtype);
Vector< t_complex > integrate_v(const t_uint fdim, const t_uint npts, const Vector< t_real > &xmin, const Vector< t_real > &xmax, const std::vector< std::function< t_complex(Vector< t_real >)>> &func, const norm_type norm, const t_real required_abs_error, const t_real required_rel_error, const t_uint max_evaluations, const method methodtype)
t_complex convolution(const Vector< t_real > &x0, const Vector< t_real > &xmin, const Vector< t_real > &xmax, const std::function< t_complex(Vector< t_real >)> &func1, const std::function< t_complex(Vector< t_real >)> &func2, const norm_type norm, const t_real required_abs_error, const t_real required_rel_error, const t_uint max_evaluations, const method methodtype)
use adaptive integration to calculate convolution at x0 of func1(x0 - x) * func2(x)
error_norm norm_error(norm_type norm)
return norm used for error
t_real integrate(const Vector< t_real > &xmin, const Vector< t_real > &xmax, const std::function< t_real(Vector< t_real >)> &func, const norm_type norm, const t_real required_abs_error, const t_real required_rel_error, const t_uint max_evaluations, const method methodtype)
adaptive integration with cubature for real scalar to vector