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>();
135 std::cout <<
"Starting precision testing of the ";
136 std::cout << moduleName <<
" module ..." << std::endl;
139 wasTerminated =
false;
155 for (
const auto&
pair : estimateResults) {
162 for (
const auto&
pair : equationResults) {
171 if(
output->settings.outputToFile &&
172 !
output->settings.outputFiles.size() &&
182 std::vector<std::string> outputFiles;
219 std::cout <<
" (" << std::setprecision(3) <<
percent <<
"%)" << std::endl;
222 std::cout <<
"\nNo tests were run!" << std::endl;
230 wasTerminated =
true;
253 std::lock_guard<std::mutex>
lock(estimateMutex);
254 estimateResults =
other.estimateResults;
255 equationResults =
other.equationResults;
265 std::lock_guard<std::mutex>
lock(estimateMutex);
266 estimateResults =
other.estimateResults;
267 equationResults =
other.equationResults;
285 typename R,
typename ...Args,
290 const std::string& name,
308 res.tolerance =
opt.tolerance;
310 res.iterations =
opt.iterations;
315 std::lock_guard<std::mutex>
lock(estimateMutex);
316 estimateResults[name].push_back(
res);
320 estimateThreads.emplace_back(
task) :
task();
338 typename R,
typename ...Args,
343 const std::string& name,
346 std::vector<interval> domain,
347 long double tolerance,
unsigned int iterations,
350 bool quiet =
false) {
354 opt.tolerance = tolerance;
355 opt.iterations = iterations;
357 opt.estimator = estimator;
379 const std::string& name,
383 long double tolerance =
get_nan(),
384 unsigned int iterations = 0,
387 bool quiet =
false) {
389 if (tolerance != tolerance)
397 opt.tolerance = tolerance;
398 opt.iterations = iterations;
400 opt.estimator = estimator;
417 const std::string& name,
446 const std::string& name,
475 const std::string& name,
509 const std::string& name,
537 template<
typename Type =
double>
539 const std::string& name,
540 const Type& evaluated,
const Type& expected,
551 long double diff =
opt.distance(evaluated, expected);
560 res.tolerance =
opt.tolerance;
564 equationResults[name].push_back(
res);
577 template<
typename Type =
double>
579 const std::string& name,
580 const Type& evaluated,
const Type& expected,
581 long double tolerance,
583 bool quiet =
false) {
587 opt.distance = distance;
603 const std::string& name,
604 long double evaluated,
long double expected,
605 long double tolerance =
get_nan(),
606 bool quiet =
false) {
608 if (tolerance != tolerance)
624 res.failed = (
diff > tolerance);
628 res.tolerance = tolerance;
631 res.evaluated = evaluated;
632 res.expected = expected;
635 equationResults[name].push_back(
res);
646 template<
typename Type>
648 const std::string& name,
649 std::vector<std::array<Type, 2>>
values,
650 long double tolerance =
get_nan(),
651 bool quiet =
false) {
653 if (tolerance != tolerance)
663 equals(name,
v[0],
v[1], tolerance, quiet);
670 for (
auto&
t : estimateThreads)
674 estimateThreads.clear();
683 inline std::vector<estimate_result>
get_estimate(
const std::string& name) {
687 return estimateResults[name];
700 return estimateResults[name].at(
i);
705 inline std::vector<equation_result>
get_equation(
const std::string& name) {
706 return equationResults[name];
712 return equationResults[name].at(
i);
724 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=equation_options< Type >())
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 estimate(const std::string &name, Function1 funcApprox, Function2 funcExpected, std::vector< interval > domain, long double 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 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
void equals(const std::string &name, long double evaluated, long double expected, long double tolerance=get_nan(), bool quiet=false)
Test an equivalence up to a tolerance, with the given options (e.g.
Definition prec.h:602
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:711
std::vector< equation_result > get_equation(const std::string &name)
Get the results of equation testing by label.
Definition prec.h:705
void equals(const std::string &name, const Type &evaluated, const Type &expected, long double tolerance, DistanceFunction< Type > distance, bool quiet=false)
Test an equivalence up to a tolerance, with the given options (e.g.
Definition prec.h:578
prec_context(const std::string &moduleName, int argc, const char **argv)
Construct a precision testing context.
Definition prec.h:238
void homogeneous(const std::string &name, Homogeneous hom, const estimate_options< InputType, OutputType > &opt, OutputType zero_element=OutputType(0.0))
Precision testing of an function which is homogeneous over the domain.
Definition prec.h:508
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:696
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:251
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:289
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:263
void wait_results()
Wait for all concurrent test cases to finish execution.
Definition prec.h:668
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 terminate(bool exit=false)
Terminate the precision testing module.
Definition prec.h:146
~prec_context()
Destructor for the context, automatically terminates the module.
Definition prec.h:244
void equals(const std::string &name, std::vector< std::array< Type, 2 > > values, long double tolerance=get_nan(), bool quiet=false)
Evaluate multiple pairs of values for equivalence up to the given tolerance (e.g.
Definition prec.h:647
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
void estimate(const std::string &name, EndoFunction< double > funcApprox, EndoFunction< double > funcExpected, interval domain, long double 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
std::vector< estimate_result > get_estimate(const std::string &name)
Get the results of error estimation by label.
Definition prec.h:683
#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
Default precision estimators.
Type abs_distance(Type a, Type b)
Absolute distance between two real values.
Definition distance.h:27
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
constexpr FloatType get_nan()
Get a quiet NaN of the specified floating point type.
Definition common.h:65
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
The output module, with formatting capabilities.
Structures for precision testing.
std::function< long double(Type, Type)> DistanceFunction
Distance function between two elements.
Definition prec_structures.h:71
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
The pseudorandom number generation and sampling module.
Structure holding options for equivalence evaluation.
Definition prec_structures.h:199
long double tolerance
Tolerance on the absolute difference.
Definition prec_structures.h:202
A structure holding the result of an evaluation.
Definition prec_structures.h:167
bool failed
Whether the test failed.
Definition prec_structures.h:189
A structure holding the options for precision estimation.
Definition prec_structures.h:77
std::vector< interval > domain
The domain of estimation.
Definition prec_structures.h:86
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
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
long double defaultTolerance
Default tolerance on max absolute error.
Definition prec.h:44
std::vector< std::string > equationColumns
Default columns to print for equations.
Definition prec.h:59