Skip to content

Commit c3d0b57

Browse files
committed
all functions are header only now
1 parent cf8327b commit c3d0b57

File tree

13 files changed

+215
-439
lines changed

13 files changed

+215
-439
lines changed

bin/tests

-173 KB
Binary file not shown.

examples/MetropolisHastings/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
double target(const double& x) { return x < 0 ? 0 : std::exp(-x); }
55

66
int main() {
7-
std::vector<long double> nums = generate(target, 10000, 2500);
7+
std::vector<long double> nums = generate<long double>(target, 10000, 2500);
88
for (auto num : nums) {
99
std::cout << num << std::endl;
1010
}

examples/NumericalIntegration/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ int main(int argc, const char* argv[]) {
3535
std::function<long double(long double)> func2 = [](long double x) {
3636
return 1 / (1 + std::sinh(2 * x) * std::pow(std::log(x), 2));
3737
};
38-
monteCarloIntegration(a2, b2, n2, func2);
38+
monteCarloIntegration<long double>(a2, b2, n2, func2);
3939
return 0;
4040
}

src/lcg.cpp

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/lcg.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,19 @@
3535
* @return Vector of generated numbers.
3636
*
3737
*/
38-
std::vector<int> lcg(const int& seed, const int& mod, const int& mult,
39-
const int& incr, const int& count);
38+
template <typename T = int>
39+
std::vector<T> lcg(const int& seed, const int& mod, const int& mult,
40+
const int& incr, const int& count) {
41+
assert(mod > 0);
42+
assert(0 < mult < mod);
43+
assert(0 <= incr < mod);
44+
assert(0 <= seed < mod);
45+
std::vector<T> randInts(count);
46+
randInts[0] = seed;
47+
for (int n = 1; n < count; n++) {
48+
randInts[n] = (mult * randInts[n - 1] + incr) % mod;
49+
}
50+
return randInts;
51+
}
4052

4153
#endif /* LCG_H */

src/metropolisHastings.cpp

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/metropolisHastings.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <ctime>
2222
#include <functional>
2323
#include <vector>
24-
#include "../src/statistics.h"
24+
#include "statistics.h"
2525

2626
/**
2727
* @brief Metropolis-Hastings algorithm.
@@ -33,8 +33,28 @@
3333
* @return Vector of generated numbers.
3434
*
3535
*/
36-
std::vector<long double> generate(
37-
const std::function<long double(long double)> &target,
38-
const unsigned int &count, const unsigned int &burn_in_period);
36+
template <typename T = long double>
37+
std::vector<T> generate(const std::function<T(T)> &target,
38+
const unsigned int &count,
39+
const unsigned int &burn_in_period) {
40+
std::srand(std::clock());
41+
42+
std::vector<T> x(count + burn_in_period, 0);
43+
x[0] = 1;
44+
45+
for (auto i = 1; i < count + burn_in_period; i++) {
46+
auto current_x = x[i - 1];
47+
auto proposed_x = current_x + rnorm();
48+
auto A = target(proposed_x) / target(current_x);
49+
if (runif() < A) {
50+
x[i] = proposed_x;
51+
} else {
52+
x[i] = current_x;
53+
}
54+
}
55+
x.erase(x.begin(), x.begin() + burn_in_period);
56+
57+
return x;
58+
}
3959

4060
#endif // METROPOLISHASTINGS_H

src/numericalIntegration.cpp

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)