6 #ifndef CHEBYSHEV_ESTIMATOR
7 #define CHEBYSHEV_ESTIMATOR
13 #include "../core/common.h"
14 #include "../core/random.h"
29 template<
typename FloatType =
double>
37 if(options.domain.size() != 1)
38 throw std::runtime_error(
39 "estimator::quadrature1D only works on mono-dimensional domains");
48 const FloatType length = domain.
length();
49 const FloatType dx = length / options.iterations;
53 FloatType diff = std::abs(funcApprox(domain.
a) - funcExpected(domain.
a));
56 sumSqr += diff * diff;
57 sumAbs += std::abs(funcExpected(domain.
a));
60 for (
unsigned int i = 1; i < options.iterations; ++i) {
62 x = domain.
a + i * dx;
63 diff = std::abs(funcApprox(x) - funcExpected(x));
74 sumSqr += coeff * diff * diff;
75 sumAbs += coeff * funcExpected(x);
78 diff = std::abs(funcApprox(domain.
b) - funcExpected(domain.
b));
81 sumSqr += diff * diff;
82 sumAbs += std::abs(funcExpected(domain.
b));
90 res.meanErr = (sum * dx / 3.0) / length;
91 res.rmsErr = std::sqrt((sumSqr * dx / 3.0) / length);
92 res.relErr = std::abs((sum * dx / 3.0) / (sumAbs * dx / 3.0));
107 template<
typename IntType =
int,
typename ReturnType = IntType>
113 std::function<IntType(ReturnType)> funcApprox,
114 std::function<IntType(ReturnType)> funcExpected,
117 if(options.domain.size() != 1)
118 throw std::runtime_error(
119 "estimator::discrete1D only works on mono-dimensional domains");
121 IntType extreme1 = IntType(std::ceil(options.domain[0].a));
122 IntType extreme2 = IntType(std::floor(options.domain[0].b));
124 IntType lower = extreme1 < extreme2 ? extreme1 : extreme2;
125 IntType upper = extreme1 > extreme2 ? extreme1 : extreme2;
127 long double maxErr = 0;
128 long double sumDiff = 0;
129 long double sumSqr = 0;
130 long double sumAbs = 0;
131 uint64_t totalPoints = 0;
133 for (IntType n = lower; n <= upper; ++n) {
135 const ReturnType resExpected = funcExpected(n);
136 const ReturnType resApprox = funcApprox(n);
138 const long double diff = (
long double) resExpected > resApprox ?
139 (resExpected - resApprox) : (resApprox - resExpected);
141 maxErr = std::max(maxErr, diff);
143 sumSqr += diff * diff;
144 sumAbs += std::abs((
long double) funcExpected(n));
151 res.meanErr = totalPoints > 0 ? (sumDiff / totalPoints) : 0;
152 res.rmsErr = totalPoints > 0 ? (std::sqrt(sumSqr) / totalPoints) : 0;
153 res.relErr = sumDiff / sumAbs;
162 template<
typename FloatType =
double>
172 if(options.domain.size() != 1)
173 throw std::runtime_error(
174 "estimator::montecarlo1D only works on mono-dimensional domains");
177 FloatType sumSqr = 0;
178 FloatType sumAbs = 0;
180 const FloatType length = options.domain[0].length();
182 for (
int i = 0; i < options.iterations; ++i) {
184 FloatType x =
random::uniform(options.domain[0].a, options.domain[0].b);
185 const FloatType diff = std::abs(funcApprox(x) - funcExpected(x));
187 max = std::max(max, diff);
189 sumSqr += diff * diff;
190 sumAbs += std::abs(funcExpected(x));
195 res.meanErr = sum / options.iterations;
196 res.absErr = sum * (length / options.iterations);
197 res.rmsErr = sumSqr * (length / options.iterations);
198 res.relErr = sum / sumAbs;
211 template<
typename FloatType =
double,
typename Vector = std::vector<FloatType>>
217 std::function<FloatType(Vector)> funcApprox,
218 std::function<FloatType(Vector)> fExpected,
221 if(options.domain.size() != dimensions)
222 throw std::runtime_error(
223 "The estimation domain's dimension does not match "
224 "the instantiated number of dimensions "
225 "in estimator::montecarlo");
228 FloatType sumSqr = 0;
229 FloatType sumAbs = 0;
233 FloatType volume = 1;
237 Vector x (dimensions);
239 for (
int i = 0; i < options.iterations; ++i) {
243 const FloatType diff = std::abs(funcApprox(x) - funcExpected(x));
249 sumSqr += diff * diff;
250 sumAbs += std::abs(funcExpected(x));
255 res.meanErr = sum / options.iterations;
256 res.absErr = sum * (volume / options.iterations);
257 res.rmsErr = sumSqr * (volume / options.iterations);
258 res.relErr = sum / sumAbs;
auto quadrature1D()
Use Simpson's quadrature scheme to approximate error integrals for univariate real functions (endofun...
Definition: estimator.h:30
auto discrete1D()
Use a discrete estimator over a lattice of points, here implemented in one dimension,...
Definition: estimator.h:108
auto montecarlo1D()
Use crude Monte Carlo integration to approximate error integrals for univariate real functions.
Definition: estimator.h:163
auto montecarlo(unsigned int dimensions)
Use crude Monte Carlo integration to approximate error integrals for multivariate real functions.
Definition: estimator.h:212
long double uniform(long double a, long double b)
Generate a uniformly distributed random number.
Definition: random.h:54
Vector & sample_uniform(Vector &x, const std::vector< prec::interval > intervals)
Fill an already allocated vector with uniformly distributed numbers over different intervals.
Definition: random.h:66
General namespace of the framework.
Definition: benchmark_structures.h:16
std::function< Type(Type)> EndoFunction
An endofunction is a function which has the same type of input and output (e.g.
Definition: common.h:50
Structures for precision testing.
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
long double maxErr
Estimated maximum absolute error on interval.
Definition: prec_structures.h:37
long double absErr
Estimated absolute error on interval.
Definition: prec_structures.h:49
An interval on the real numbers.
Definition: interval.h:16
long double length() const
Returns the length of the interval.
Definition: interval.h:33
long double a
Lower extreme of the interval.
Definition: interval.h:19
long double b
Upper extreme of the interval.
Definition: interval.h:22