template<class T = double>
struct Simpson {
function<T(T)> f;
Simpson(function<T(T)> f) : f(f) {}
T simpson(T L, T R) {
T mid = (L + R) / 2;
return (R - L) * (f(L) + 4 * f(mid) + f(R)) / 6;
}
T asr(T L, T R, T eps, T ans) {
T mid = (L + R) / 2;
T fL = simpson(L, mid), fR = simpson(mid, R);
if (fabs(fL + fR - ans) <= 15 * eps) {
return fL + fR + (fL + fR - ans) / 15;
}
return asr(L, mid, eps / 2, fL) + asr(mid, R, eps / 2, fR);
}
T operator()(T L, T R) {
return (*this)(L, R, 1e-12);
}
T operator()(T L, T R, T eps) {
return asr(L, R, eps, simpson(L, R));
}
}; // Simpson