6 #ifndef CHEBYSHEV_PREC_H
7 #define CHEBYSHEV_PREC_H
60 "name",
"meanErr",
"rmsErr",
"maxErr",
"failed"
69 "name",
"difference",
"tolerance",
"failed"
111 const char** argv =
nullptr) {
116 for (
int i = 1; i < argc; ++i)
117 settings.pickedTests[argv[i]] =
true;
119 std::cout <<
"Starting precision testing of the "
120 << moduleName <<
" module ..." << std::endl;
123 results.failedTests = 0;
124 results.totalTests = 0;
137 output::settings.quiet = settings.
quiet;
141 !output::settings.outputFiles.size() &&
142 !settings.estimateOutputFiles.size() &&
143 !settings.equationOutputFiles.size() &&
149 std::vector<std::string> outputFiles;
153 outputFiles.insert(outputFiles.end(), settings.estimateOutputFiles.begin(), settings.estimateOutputFiles.end());
159 outputFiles.insert(outputFiles.end(), settings.equationOutputFiles.begin(), settings.equationOutputFiles.end());
163 std::cout <<
"Finished testing " << settings.
moduleName <<
'\n';
164 std::cout << results.totalTests <<
" total tests, "
165 << results.failedTests <<
" failed (" << std::setprecision(3) <<
166 (results.failedTests / (double) results.totalTests) * 100 <<
"%)"
174 std::exit(results.failedTests);
187 template<
typename R,
typename ...Args,
188 typename Function1 = std::function<R(Args...)>,
189 typename Function2 = Function1>
193 Function1 funcApprox,
194 Function2 funcExpected,
199 if(settings.pickedTests.size())
200 if(settings.pickedTests.find(name) == settings.pickedTests.end())
204 auto res = opt.
estimator(funcApprox, funcExpected, opt);
209 res.quiet = opt.
quiet;
213 res.failed = opt.
fail(res);
215 results.totalTests++;
217 results.failedTests++;
219 results.estimateResults[name].push_back(res);
236 template<
typename R,
typename ...Args,
237 typename Function1 = std::function<R(Args...)>,
238 typename Function2 = Function1>
242 Function1 funcApprox,
243 Function2 funcExpected,
244 std::vector<interval> domain,
245 long double tolerance,
unsigned int iterations,
248 bool quiet =
false) {
252 opt.tolerance = tolerance;
253 opt.iterations = iterations;
255 opt.estimator = estimator;
258 estimate(name, funcApprox, funcExpected, opt);
281 long double tolerance = settings.defaultTolerance,
285 bool quiet =
false) {
289 opt.tolerance = tolerance;
290 opt.iterations = iterations;
292 opt.estimator = estimator;
295 estimate(name, funcApprox, funcExpected, opt);
313 template<
typename Type,
typename Identity = EndoFunction<Type>>
329 estimate(name, funcApprox, funcExpected, opt);
339 template<
typename Type,
typename Involution = EndoFunction<Type>>
347 return invol(invol(x));
355 estimate(name, funcApprox, funcExpected, opt);
366 template<
typename Type,
typename Involution = EndoFunction<Type>>
374 return idem(idem(x));
382 estimate(name, funcApprox, funcExpected, opt);
397 template<
typename InputType,
typename OutputType = InputType,
398 typename Homogeneous = std::function<OutputType(InputType)>>
403 OutputType zero_element = OutputType(0.0)) {
406 std::function<OutputType(InputType)> funcApprox =
407 [&](InputType x) -> OutputType {
412 std::function<OutputType(InputType)> funcExpected =
413 [&](InputType x) -> OutputType {
417 estimate(name, funcApprox, funcExpected, opt);
429 template<
typename T =
double>
432 const T& evaluated,
const T& expected,
437 if(settings.pickedTests.size())
438 if(settings.pickedTests.find(name) == settings.pickedTests.end())
443 long double diff = opt.distance(evaluated, expected);
448 res.
failed = (diff > opt.tolerance);
451 res.difference = diff;
452 res.tolerance = opt.tolerance;
453 res.quiet = opt.quiet;
455 results.totalTests++;
457 results.failedTests++;
460 results.equationResults[name].push_back(res);
473 template<
typename T =
double>
476 const T& evaluated,
const T& expected,
477 long double tolerance,
479 bool quiet =
false) {
483 opt.distance = distance;
486 equals(name, evaluated, expected, opt);
500 long double evaluated,
long double expected,
501 long double tolerance = settings.defaultTolerance,
502 bool quiet =
false) {
506 if(settings.pickedTests.size())
507 if(settings.pickedTests.find(name) == settings.pickedTests.end())
517 res.failed = (diff > tolerance);
520 res.difference = diff;
521 res.tolerance = tolerance;
524 res.evaluated = evaluated;
525 res.expected = expected;
527 results.totalTests++;
529 results.failedTests++;
532 results.equationResults[name].push_back(res);
546 std::vector<std::array<T, 2>> values,
547 long double tolerance = settings.defaultTolerance,
548 bool quiet =
false) {
552 if(settings.pickedTests.size())
553 if(settings.pickedTests.find(name) == settings.pickedTests.end())
556 for (
const auto& v : values)
557 equals(name, v[0], v[1], tolerance, quiet);
#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.
void print_results(const std::map< std::string, std::vector< ResultType >> &results, const std::vector< std::string > &fields, const std::vector< std::string > &filenames)
Print the test results to standard output and output files with their given formats,...
Definition: output.h:907
void setup()
Setup printing to the output stream with default options.
Definition: output.h:539
void terminate()
Terminate the output module by closing all output files and resetting its settings.
Definition: output.h:598
FloatType abs_distance(FloatType a, FloatType b)
Absolute distance between two real values.
Definition: distance.h:19
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
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:340
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:314
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:399
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:367
std::function< long double(Type, Type)> DistanceFunction
Distance function between two elements.
Definition: prec_structures.h:71
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:191
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:158
void setup(std::string moduleName, int argc=0, const char **argv=nullptr)
Setup the precision testing environment.
Definition: prec.h:108
void equals(const std::string &name, const T &evaluated, const T &expected, equation_options< T > opt=equation_options< T >())
Test an equivalence up to a tolerance, with the given options (e.g.
Definition: prec.h:430
void terminate(bool exit=true)
Terminate the precision testing environment, printing the results to standard output and output files...
Definition: prec.h:135
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
void setup(uint64_t seed=0)
Initialize the random module.
Definition: random.h:32
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
The output module, with formatting capabilities.
Structures for precision testing.
The pseudorandom number generation and sampling module.
std::string moduleName
Name of the module currently being benchmarked.
Definition: benchmark.h:38
bool outputToFile
Whether to output results to a file.
Definition: benchmark.h:47
unsigned int defaultIterations
Default number of iterations.
Definition: benchmark.h:41
std::vector< std::string > outputFiles
The files to write all benchmark results to.
Definition: benchmark.h:50
bool quiet
Whether to print benchmark results to standard output.
Definition: benchmark.h:35
Structure holding options for equivalence evaluation.
Definition: prec_structures.h:195
long double tolerance
Tolerance on the absolute difference.
Definition: prec_structures.h:198
A structure holding the result of an evaluation.
Definition: prec_structures.h:163
bool failed
Whether the test failed.
Definition: prec_structures.h:185
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
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
long double tolerance
The tolerance to use to determine whether the test failed.
Definition: prec_structures.h:98
bool quiet
Whether to show the test result or not.
Definition: prec_structures.h:110
An interval on the real numbers.
Definition: interval.h:16
of the precision testing module.
Definition: prec.h:85
std::map< std::string, std::vector< estimate_result > > estimateResults
Results of error estimation.
Definition: prec.h:94
unsigned int failedTests
Number of failed tests.
Definition: prec.h:91
unsigned int totalTests
Total number of tests run.
Definition: prec.h:88
std::map< std::string, std::vector< equation_result > > equationResults
Results of equation evaluation.
Definition: prec.h:97
of the precision testing module.
Definition: prec.h:35
std::map< std::string, bool > pickedTests
Target tests marked for execution, can be picked by passing test case names by command line.
Definition: prec.h:79
std::string moduleName
Name of the module being tested.
Definition: prec.h:38
bool quiet
Print to standard output or not.
Definition: prec.h:41
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:65
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:74
FailFunction defaultFailFunction
Default fail function.
Definition: prec.h:50
std::vector< std::string > outputFiles
The files to write all precision testing results to.
Definition: prec.h:56
unsigned int defaultIterations
Default number of iterations for integral quadrature.
Definition: prec.h:47
bool outputToFile
Output to file?
Definition: prec.h:44
std::vector< std::string > estimateColumns
Default columns to print for precision estimates.
Definition: prec.h:59
long double defaultTolerance
Default tolerance on max absolute error.
Definition: prec.h:53
std::vector< std::string > equationColumns
Default columns to print for equations.
Definition: prec.h:68