43 const std::function<
double(
double)>& func,
44 double delta = .0001) {
46 uint64_t numDeltas =
static_cast<uint64_t
>((ub - lb) / delta);
47 for (
int i = 0; i < numDeltas; i++) {
48 double begin = lb + i * delta;
49 double end = lb + (i + 1) * delta;
50 result += delta * (func(begin) + func(end)) / 2;
62void test_eval(
double approx,
double expected,
double threshold) {
63 assert(approx >= expected * (1 - threshold));
64 assert(approx <= expected * (1 + threshold));
77 3.24, 7.56, [](
const double x) {
return log(x) +
exp(x) + x; });
78 std::cout <<
"Test Case 1" << std::endl;
79 std::cout <<
"function: log(x) + e^x + x" << std::endl;
80 std::cout <<
"range: [3.24, 7.56]" << std::endl;
81 std::cout <<
"value: " <<
test_1 << std::endl;
83 std::cout <<
"Test 1 Passed!" << std::endl;
84 std::cout <<
"=====================" << std::endl;
87 return x * x + cos(x) +
exp(x) +
log(x) *
log(x);
89 std::cout <<
"Test Case 2" << std::endl;
90 std::cout <<
"function: x^2 + cos(x) + e^x + log^2(x)" << std::endl;
91 std::cout <<
"range: [.023, 3.69]" << std::endl;
92 std::cout <<
"value: " <<
test_2 << std::endl;
94 std::cout <<
"Test 2 Passed!" << std::endl;
95 std::cout <<
"=====================" << std::endl;
98 10.78, 24.899, [](
const double x) {
return x * x * x - x * x + 378; });
99 std::cout <<
"Test Case 3" << std::endl;
100 std::cout <<
"function: x^3 - x^2 + 378" << std::endl;
101 std::cout <<
"range: [10.78, 24.899]" << std::endl;
102 std::cout <<
"value: " <<
test_3 << std::endl;
104 std::cout <<
"Test 3 Passed!" << std::endl;
105 std::cout <<
"=====================" << std::endl;
109 [](
const double x) {
return cos(x) * tan(x) * x * x +
exp(x); },
111 std::cout <<
"Test Case 4" << std::endl;
112 std::cout <<
"function: cos(x)*tan(x)*x^2 + e^x" << std::endl;
113 std::cout <<
"range: [.101, .505]" << std::endl;
114 std::cout <<
"value: " << test_4 << std::endl;
116 std::cout <<
"Test 4 Passed!" << std::endl;
117 std::cout <<
"=====================" << std::endl;
120 -1, 1, [](
const double x) {
return exp(-1 / (x * x)); });
121 std::cout <<
"Test Case 5" << std::endl;
122 std::cout <<
"function: e^(-1/x^2)" << std::endl;
123 std::cout <<
"range: [-1, 1]" << std::endl;
124 std::cout <<
"value: " << test_5 << std::endl;
126 std::cout <<
"Test 5 Passed!" << std::endl;
void log(T msg)
A function to print given message on console.
uint256_t exp(uint256_t number, uint256_t power, const uint256_t &mod)
This function calculates number raised to exponent power under modulo mod using Modular Exponentiatio...
void test_eval(double approx, double expected, double threshold)
Wrapper to evaluate if the approximated value is within .XX% threshold of the exact value.
double integral_approx(double lb, double ub, const std::function< double(double)> &func, double delta=.0001)
Computes integral approximation.