6#ifndef CHEBYSHEV_PREC_H
7#define CHEBYSHEV_PREC_H
51 "name",
"meanErr",
"rmsErr",
"maxErr",
"failed"
60 "name",
"difference",
"tolerance",
"failed"
88 std::map<std::string, std::vector<estimate_result>> estimateResults {};
91 std::mutex estimateMutex;
94 std::vector<std::thread> estimateThreads;
97 std::map<std::string, std::vector<equation_result>> equationResults {};
100 bool wasTerminated {
false};
109 std::shared_ptr<output::output_context>
output;
113 std::shared_ptr<random::random_context>
random;
122 const std::string& moduleName,
123 int argc = 0,
const char** argv =
nullptr) {
127 output = std::make_shared<output::output_context>();
128 random = std::make_shared<random::random_context>();
132 for (
int i = 1; i < argc; ++i)
133 settings.pickedTests[argv[i]] =
true;
135 output->info(
"Starting precision testing of the " + moduleName +
" module ...");
138 wasTerminated =
false;
151 unsigned int totalTests = 0;
152 unsigned int failedTests = 0;
154 for (
const auto& pair : estimateResults) {
155 for (
const auto& testCase : pair.second) {
157 failedTests += testCase.failed ? 1 : 0;
161 for (
const auto& pair : equationResults) {
162 for (
const auto& testCase : pair.second) {
164 failedTests += testCase.failed ? 1 : 0;
170 if(
output->settings.outputToFile &&
171 !
output->settings.outputFiles.size() &&
172 !
settings.estimateOutputFiles.size() &&
173 !
settings.equationOutputFiles.size() &&
181 std::vector<std::string> outputFiles;
187 settings.estimateOutputFiles.begin(),
192 estimateResults,
settings.estimateColumns, outputFiles
200 settings.equationOutputFiles.begin(),
205 equationResults,
settings.equationColumns, outputFiles
210 double percent = totalTests > 0 ? (failedTests / (double) totalTests) * 100 : 0;
214 std::to_string(totalTests) +
" total tests, " + std::to_string(failedTests) +
215 " failed (" + std::to_string(percent).substr(0, 4) +
"%)"
219 output->warn(
"No tests were executed!");
223 std::exit(failedTests);
226 wasTerminated =
true;
234 prec_context(
const std::string& moduleName,
int argc,
const char** argv) {
235 setup(moduleName, argc, argv);
249 std::lock_guard<std::mutex> lock(estimateMutex);
250 estimateResults = other.estimateResults;
251 equationResults = other.equationResults;
261 std::lock_guard<std::mutex> lock(estimateMutex);
262 estimateResults = other.estimateResults;
263 equationResults = other.equationResults;
281 typename R,
typename ...Args,
282 typename Function1 = std::function<R(Args...)>,
283 typename Function2 = Function1
286 const std::string& name,
287 Function1 funcApprox,
288 Function2 funcExpected,
297 output->debug(
"Running estimate case: " + name);
299 auto task = [
this, name, funcApprox, funcExpected, opt]() {
302 auto res = opt.
estimator(funcApprox, funcExpected, opt);
307 res.quiet = opt.
quiet;
311 res.failed = opt.
fail(res);
313 std::lock_guard<std::mutex> lock(estimateMutex);
314 estimateResults[name].push_back(res);
318 estimateThreads.emplace_back(task);
338 typename R,
typename ...Args,
339 typename Function1 = std::function<R(Args...)>,
340 typename Function2 = Function1
343 const std::string& name,
344 Function1 funcApprox,
345 Function2 funcExpected,
346 std::vector<interval> domain,
347 prec_t tolerance,
unsigned int iterations,
350 bool quiet =
false) {
354 opt.tolerance = tolerance;
355 opt.iterations = iterations;
360 estimate(name, funcApprox, funcExpected, opt);
379 const std::string& name,
384 unsigned int iterations = 0,
387 bool quiet =
false) {
389 if (tolerance != tolerance)
390 tolerance =
settings.defaultTolerance;
393 iterations =
settings.defaultIterations;
403 estimate(name, funcApprox, funcExpected, opt);
417 const std::string& name,
431 estimate(name, funcApprox, funcExpected, opt);
446 const std::string& name,
452 return invol(invol(x));
460 estimate(name, funcApprox, funcExpected, opt);
475 const std::string& name,
481 return idem(idem(x));
489 estimate(name, funcApprox, funcExpected, opt);
505 typename OutputType,
typename InputType = OutputType,
506 typename Homogeneous = std::function<OutputType(InputType)>
509 const std::string& name,
512 OutputType zero_element = OutputType(0.0)) {
515 std::function<OutputType(InputType)> funcApprox =
516 [=](InputType x) -> OutputType {
521 std::function<OutputType(InputType)> funcExpected =
522 [=](InputType x) -> OutputType {
526 estimate(name, funcApprox, funcExpected, opt);
537 template<
typename Type =
double>
539 const std::string& name,
540 const Type& evaluated,
const Type& expected,
549 output->debug(
"Running equation case: " + name);
565 equationResults[name].push_back(res);
578 template<
typename Type =
double>
580 const std::string& name,
581 const Type& evaluated,
const Type& expected,
584 bool quiet =
false) {
591 equals(name, evaluated, expected, opt);
604 const std::string& name,
607 bool quiet =
false) {
609 if (tolerance != tolerance)
610 tolerance =
settings.defaultTolerance;
618 output->debug(
"Running equation case: " + name);
626 res.
failed = (diff > tolerance) || (diff != diff);
637 equationResults[name].push_back(res);
648 template<
typename Type>
650 const std::string& name,
651 std::vector<std::array<Type, 2>> values,
653 bool quiet =
false) {
655 if (tolerance != tolerance)
656 tolerance =
settings.defaultTolerance;
664 for (
const auto& v : values)
665 equals(name, v[0], v[1], tolerance, quiet);
672 for (
auto& t : estimateThreads)
676 estimateThreads.clear();
685 inline std::vector<estimate_result>
get_estimate(
const std::string& name) {
689 return estimateResults[name];
702 return estimateResults[name].at(i);
707 inline std::vector<equation_result>
get_equation(
const std::string& name) {
708 return equationResults[name];
714 return equationResults[name].at(i);
726 int argc = 0,
const char** argv =
nullptr) {
Precision testing context, handling precision test cases.
Definition prec.h:84
void equals(const std::string &name, const Type &evaluated, const Type &expected, equation_options< Type > opt)
Test an equivalence up to a tolerance, with the given options (e.g.
Definition prec.h:538
std::shared_ptr< random::random_context > random
Random module settings for the context, dynamically allocated and possibly shared between multiple co...
Definition prec.h:113
void equals(const std::string &name, std::vector< std::array< Type, 2 > > values, prec_t tolerance=get_nan(), bool quiet=false)
Evaluate multiple pairs of values for equivalence up to the given tolerance (e.g.
Definition prec.h:649
void setup(const std::string &moduleName, int argc=0, const char **argv=nullptr)
Setup the precision testing module.
Definition prec.h:121
void idempotence(const std::string &name, Involution idem, const estimate_options< Type, Type > &opt)
Precision testing of an endofunction which is idempotent.
Definition prec.h:474
equation_result get_equation(const std::string &name, unsigned int i)
Get a single result of equation testing by label and index.
Definition prec.h:713
std::vector< equation_result > get_equation(const std::string &name)
Get the results of equation testing by label.
Definition prec.h:707
prec_context(const std::string &moduleName, int argc, const char **argv)
Construct a precision testing context.
Definition prec.h:234
void equals(const std::string &name, prec_t evaluated, prec_t expected, prec_t tolerance=get_nan(), bool quiet=false)
Test an equivalence up to a tolerance, with the given options (e.g.
Definition prec.h:603
estimate_result get_estimate(const std::string &name, unsigned int i)
Get a single result of error estimation by label and index.
Definition prec.h:698
void estimate(const std::string &name, EndoFunction< double > funcApprox, EndoFunction< double > funcExpected, interval domain, prec_t tolerance=get_nan(), unsigned int iterations=0, FailFunction fail=fail::fail_on_max_err(), Estimator< double, double > estimator=estimator::quadrature1D< double >(), bool quiet=false)
Estimate error integrals over a real function of real variable, with respect to an exact function.
Definition prec.h:378
void homogeneous(const std::string &name, Homogeneous hom, const estimate_options< OutputType, InputType > &opt, OutputType zero_element=OutputType(0.0))
Precision testing of an function which is homogeneous over the domain.
Definition prec.h:508
prec_settings settings
Settings for the precision testing context.
Definition prec.h:105
prec_context(const prec_context &other)
Custom copy constructor to avoid copying std::mutex.
Definition prec.h:247
void estimate(const std::string &name, Function1 funcApprox, Function2 funcExpected, estimate_options< R, Args... > opt)
Estimate error integrals over a function with respect to an exact function, with the given options.
Definition prec.h:285
void involution(const std::string &name, Involution invol, const estimate_options< Type, Type > &opt)
Precision testing of an endofunction which is an involution.
Definition prec.h:445
prec_context & operator=(const prec_context &other)
Custom assignment operator to avoid copying std::mutex.
Definition prec.h:259
void wait_results()
Wait for all concurrent test cases to finish execution.
Definition prec.h:670
void estimate(const std::string &name, Function1 funcApprox, Function2 funcExpected, std::vector< interval > domain, prec_t tolerance, unsigned int iterations, FailFunction fail, Estimator< R, Args... > estimator, bool quiet=false)
Estimate error integrals over a function with respect to an exact function.
Definition prec.h:342
void identity(const std::string &name, Identity id, const estimate_options< Type, Type > &opt)
Precision testing of an endofunction which is equivalent to the identity.
Definition prec.h:416
void equals(const std::string &name, const Type &evaluated, const Type &expected, prec_t tolerance, DistanceFunction< Type > distance, bool quiet=false)
Test an equivalence up to a tolerance, with the given options (e.g.
Definition prec.h:579
void terminate(bool exit=false)
Terminate the precision testing module.
Definition prec.h:145
~prec_context()
Destructor for the context, automatically terminates the module.
Definition prec.h:240
std::shared_ptr< output::output_context > output
Output module settings for the context, dynamically allocated and possibly shared between multiple co...
Definition prec.h:109
std::vector< estimate_result > get_estimate(const std::string &name)
Get the results of error estimation by label.
Definition prec.h:685
#define CHEBYSHEV_PREC_ITER
Default number of function evaluations in precision testing.
Definition common.h:12
#define CHEBYSHEV_PREC_TOLERANCE
Default tolerance in precision testing.
Definition common.h:17
long double prec_t
Floating-point type of higher precision, used in computations, such as error estimation.
Definition common.h:42
Default precision estimators.
Distance functions for use in prec_context::equals().
prec_t absolute(Type a, Type b)
Absolute distance between two values which have an ordering with respect to zero.
Definition distance.h:21
auto quadrature1D()
Use Simpson's quadrature scheme to approximate error integrals for univariate real functions (endofun...
Definition estimator.h:30
Fail functions for use in prec::estimate.
auto fail_on_max_err()
Default fail function which marks the test as failed if the maximum error on the domain is bigger tha...
Definition fail.h:34
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
constexpr FloatType get_nan()
Get a quiet NaN of the specified floating point type.
Definition common.h:65
The output module, with formatting capabilities.
prec_context make_context(const std::string &moduleName, int argc=0, const char **argv=nullptr)
Construct a precision testing context with the given parameters.
Definition prec.h:725
Structures for precision testing.
std::function< bool(const estimate_result &)> FailFunction
A function which determines whether an estimation failed.
Definition prec_structures.h:66
typename estimate_options< R, Args... >::Estimator_t Estimator
Generic precision estimator function signature.
Definition prec_structures.h:162
std::function< prec_t(Type, Type)> DistanceFunction
Distance function between two elements.
Definition prec_structures.h:71
The pseudorandom number generation and sampling module.
Structure holding options for equivalence evaluation.
Definition prec_structures.h:199
bool quiet
Print to standard output or not.
Definition prec_structures.h:211
prec_t tolerance
Tolerance on the absolute difference.
Definition prec_structures.h:202
DistanceFunction< T > distance
Distance function to measure the distance between the expected and evaluated value.
Definition prec_structures.h:206
A structure holding the result of an evaluation.
Definition prec_structures.h:167
bool failed
Whether the test failed.
Definition prec_structures.h:189
bool quiet
Print to standard output or not.
Definition prec_structures.h:192
std::string name
Identifying name of the function or test case.
Definition prec_structures.h:170
prec_t evaluated
Evaluated value.
Definition prec_structures.h:173
prec_t difference
Evaluated difference between expected and evaluated values.
Definition prec_structures.h:179
prec_t expected
Expected value.
Definition prec_structures.h:176
prec_t tolerance
Tolerance on the absolute difference.
Definition prec_structures.h:186
A structure holding the options for precision estimation.
Definition prec_structures.h:77
FailFunction fail
The function to determine whether the test failed (defaults to fail::fail_on_max_err).
Definition prec_structures.h:105
prec_t tolerance
The tolerance to use to determine whether the test failed.
Definition prec_structures.h:98
unsigned int iterations
Number of function evaluations to use.
Definition prec_structures.h:101
std::vector< interval > domain
The domain of estimation.
Definition prec_structures.h:86
Estimator_t estimator
The precision estimator to use (defaults to a dummy estimator).
Definition prec_structures.h:90
bool quiet
Whether to show the test result or not.
Definition prec_structures.h:110
A structure holding the result of precision estimation.
Definition prec_structures.h:25
An interval on the real numbers.
Definition interval.h:16
Settings for the precision testing module, used in prec_context.
Definition prec.h:32
std::map< std::string, bool > pickedTests
Target tests marked for execution, can be picked by passing test case names by command line (all test...
Definition prec.h:70
prec_t defaultTolerance
Default tolerance on max absolute error.
Definition prec.h:44
bool multithreading
Whether to use multithreading for the execution of accuracy estimation tasks (defaults to true).
Definition prec.h:77
std::string moduleName
Name of the module being tested.
Definition prec.h:35
std::vector< std::string > estimateOutputFiles
The files to write estimate results to (if empty, all results are output to a generic file).
Definition prec.h:56
std::vector< std::string > equationOutputFiles
The files to write equation results to (if empty, all results are output to a generic file).
Definition prec.h:65
FailFunction defaultFailFunction
Default fail function.
Definition prec.h:41
std::vector< std::string > outputFiles
The files to write all precision testing results to.
Definition prec.h:47
unsigned int defaultIterations
Default number of iterations for integral quadrature.
Definition prec.h:38
std::vector< std::string > estimateColumns
Default columns to print for precision estimates.
Definition prec.h:50
std::vector< std::string > equationColumns
Default columns to print for equations.
Definition prec.h:59