6#ifndef CHEBYSHEV_ESTIMATOR
7#define CHEBYSHEV_ESTIMATOR
29 template<
typename FloatType = real_t>
37 if(options.domain.size() != 1) {
38 throw std::runtime_error(
39 "estimator::quadrature1D only works on mono-dimensional domains"
50 const FloatType length = domain.
length();
51 const FloatType dx = length / options.iterations;
55 FloatType diff = std::abs(funcApprox(domain.
a) - funcExpected(domain.
a));
58 sumSqr += diff * diff;
59 sumAbs += std::abs(funcExpected(domain.
a));
62 for (
unsigned int i = 1; i < options.iterations; ++i) {
64 x = domain.
a + i * dx;
65 diff = std::abs(funcApprox(x) - funcExpected(x));
76 sumSqr += coeff * diff * diff;
77 sumAbs += coeff * funcExpected(x);
80 diff = std::abs(funcApprox(domain.
b) - funcExpected(domain.
b));
83 sumSqr += diff * diff;
84 sumAbs += std::abs(funcExpected(domain.
b));
92 res.
meanErr = (sum * dx / 3.0) / length;
93 res.
rmsErr = std::sqrt((sumSqr * dx / 3.0) / length);
94 res.
relErr = std::abs((sum * dx / 3.0) / (sumAbs * dx / 3.0));
110 typename IntType = int,
111 typename ReturnType = IntType
117 std::function<IntType(ReturnType)> funcApprox,
118 std::function<IntType(ReturnType)> funcExpected,
121 if(options.domain.size() != 1) {
122 throw std::runtime_error(
123 "estimator::discrete1D only works on mono-dimensional domains"
127 IntType extreme1 = IntType(std::ceil(options.domain[0].a));
128 IntType extreme2 = IntType(std::floor(options.domain[0].b));
130 IntType lower = extreme1 < extreme2 ? extreme1 : extreme2;
131 IntType upper = extreme1 > extreme2 ? extreme1 : extreme2;
137 uint64_t totalPoints = 0;
139 for (IntType n = lower; n <= upper; ++n) {
141 const ReturnType resExpected = funcExpected(n);
142 const ReturnType resApprox = funcApprox(n);
145 (resExpected - resApprox) : (resApprox - resExpected);
147 maxErr = std::max(maxErr, diff);
149 sumSqr += diff * diff;
150 sumAbs += std::abs((
prec_t) funcExpected(n));
157 res.
meanErr = totalPoints > 0 ? (sumDiff / totalPoints) : 0;
158 res.
rmsErr = totalPoints > 0 ? (std::sqrt(sumSqr) / totalPoints) : 0;
159 res.
relErr = sumDiff / sumAbs;
171 template<
typename FloatType = real_t>
172 inline auto montecarlo1D(std::shared_ptr<random::random_context> rnd_ctx) {
182 if(options.domain.size() != 1) {
183 throw std::runtime_error(
184 "estimator::montecarlo1D only works on mono-dimensional domains"
189 FloatType sumSqr = 0;
190 FloatType sumAbs = 0;
192 const FloatType length = options.domain[0].length();
194 for (
int i = 0; i < options.iterations; ++i) {
196 FloatType x = rnd.
uniform(options.domain[0].a, options.domain[0].b);
197 const FloatType diff = std::abs(funcApprox(x) - funcExpected(x));
199 max = std::max(max, diff);
201 sumSqr += diff * diff;
202 sumAbs += std::abs(funcExpected(x));
207 res.
meanErr = sum / options.iterations;
208 res.
absErr = sum * (length / options.iterations);
209 res.
rmsErr = std::sqrt(sumSqr * (length / options.iterations));
210 res.
relErr = sum / sumAbs;
230 typename FloatType =
real_t,
231 typename Vector = std::vector<FloatType>
234 std::shared_ptr<random::random_context> rnd_ctx,
unsigned int dimensions) {
237 return [rnd_ctx, dimensions](
238 std::function<FloatType(Vector)> funcApprox,
239 std::function<FloatType(Vector)> funcExpected,
244 if(options.domain.size() != dimensions) {
245 throw std::runtime_error(
246 "The estimation domain's dimension does not match "
247 "the instantiated number of dimensions "
248 "in estimator::montecarlo"
253 FloatType sumSqr = 0;
254 FloatType sumAbs = 0;
258 FloatType volume = 1;
262 Vector x (dimensions);
264 for (
unsigned int i = 0; i < options.iterations; ++i) {
266 rnd.
uniform(x, options.domain);
268 const FloatType diff = std::abs(funcApprox(x) - funcExpected(x));
274 sumSqr += diff * diff;
275 sumAbs += std::abs(funcExpected(x));
280 res.
meanErr = sum / options.iterations;
281 res.
absErr = sum * (volume / options.iterations);
282 res.
rmsErr = std::sqrt(sumSqr * (volume / options.iterations));
283 res.
relErr = sum / sumAbs;
Common definitions for the framework.
long double prec_t
Floating-point type of higher precision, used in computations, such as error estimation.
Definition common.h:42
double real_t
Floating-point type, used as default for function arguments.
Definition common.h:37
auto quadrature1D()
Use Simpson's quadrature scheme to approximate error integrals for univariate real functions (endofun...
Definition estimator.h:30
auto montecarlo(std::shared_ptr< random::random_context > rnd_ctx, unsigned int dimensions)
Use crude Monte Carlo integration to approximate error integrals for multivariate real functions.
Definition estimator.h:233
auto montecarlo1D(std::shared_ptr< random::random_context > rnd_ctx)
Use crude Monte Carlo integration to approximate error integrals for univariate real functions.
Definition estimator.h:172
auto discrete1D()
Use a discrete estimator over a lattice of points, here implemented in one dimension,...
Definition estimator.h:113
General namespace of the framework.
Definition benchmark.h:22
std::function< Type(Type)> EndoFunction
An endofunction is a function which has the same type of input and output, such as a real function of...
Definition common.h:60
Structures for precision testing.
The pseudorandom number generation and sampling module.
A structure holding the options for precision estimation.
Definition prec_structures.h:77
A structure holding the result of precision estimation.
Definition prec_structures.h:25
prec_t maxErr
Estimated maximum absolute error on interval.
Definition prec_structures.h:37
prec_t meanErr
Estimated mean error on interval.
Definition prec_structures.h:40
prec_t absErr
Estimated absolute error on interval.
Definition prec_structures.h:49
prec_t relErr
Estimated relative error on interval.
Definition prec_structures.h:46
prec_t rmsErr
Estimated RMS error on interval.
Definition prec_structures.h:43
An interval on the real numbers.
Definition interval.h:16
real_t length() const
Returns the length of the interval.
Definition interval.h:33
double a
Lower extreme of the interval.
Definition interval.h:19
double b
Upper extreme of the interval.
Definition interval.h:22
A source of pseudorandom numbers.
Definition random.h:39
real_t uniform(real_t a, real_t b)
Generate a uniformly distributed random number.
Definition random.h:76