mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-10 05:58:22 +08:00
feat: Reworked/updated sorting/selection_sort.cpp. (#1613)
* Reworked selection_sort.cpp with fixes. * Added Recursive implementation for tree traversing * Fix #2 * Delete recursive_tree_traversals.cpp * Update selection_sort.cpp * Changes done in selection_sort_iterative.cpp * updating DIRECTORY.md * clang-format and clang-tidy fixes for4681e4f7* Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update selection_sort_iterative.cpp * Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes forca2a7c64* Finished changes requested by ayaankhan98. * Reworked on changes. * clang-format and clang-tidy fixes forf79b79b7* Corrected errors. * Fix #2 * Fix #3 * Major Fix #3 * clang-format and clang-tidy fixes for79341db8* clang-format and clang-tidy fixes for9bdf2ce4* Update selection_sort_iterative.cpp * clang-format and clang-tidy fixes for9833d7a7* clang-format and clang-tidy fixes forb7726460Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com>
This commit is contained in:
@@ -1,29 +1,34 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Monte Carlo Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration)
|
||||
* @brief [Monte Carlo
|
||||
* Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration)
|
||||
*
|
||||
* @details
|
||||
* In mathematics, Monte Carlo integration is a technique for numerical integration using random numbers.
|
||||
* It is a particular Monte Carlo method that numerically computes a definite integral.
|
||||
* While other algorithms usually evaluate the integrand at a regular grid, Monte Carlo randomly chooses points at which the integrand is evaluated.
|
||||
* This method is particularly useful for higher-dimensional integrals.
|
||||
* In mathematics, Monte Carlo integration is a technique for numerical
|
||||
* integration using random numbers. It is a particular Monte Carlo method that
|
||||
* numerically computes a definite integral. While other algorithms usually
|
||||
* evaluate the integrand at a regular grid, Monte Carlo randomly chooses points
|
||||
* at which the integrand is evaluated. This method is particularly useful for
|
||||
* higher-dimensional integrals.
|
||||
*
|
||||
* This implementation supports arbitrary pdfs.
|
||||
* These pdfs are sampled using the [Metropolis-Hastings algorithm](https://en.wikipedia.org/wiki/Metropolis–Hastings_algorithm).
|
||||
* This can be swapped out by every other sampling techniques for example the inverse method.
|
||||
* Metropolis-Hastings was chosen because it is the most general and can also be extended for a higher dimensional sampling space.
|
||||
* These pdfs are sampled using the [Metropolis-Hastings
|
||||
* algorithm](https://en.wikipedia.org/wiki/Metropolis–Hastings_algorithm). This
|
||||
* can be swapped out by every other sampling techniques for example the inverse
|
||||
* method. Metropolis-Hastings was chosen because it is the most general and can
|
||||
* also be extended for a higher dimensional sampling space.
|
||||
*
|
||||
* @author [Domenic Zingsheim](https://github.com/DerAndereDomenic)
|
||||
*/
|
||||
|
||||
#define _USE_MATH_DEFINES /// for M_PI on windows
|
||||
#include <cmath> /// for math functions
|
||||
#include <cstdint> /// for fixed size data types
|
||||
#include <ctime> /// for time to initialize rng
|
||||
#include <functional> /// for function pointers
|
||||
#include <iostream> /// for std::cout
|
||||
#include <random> /// for random number generation
|
||||
#include <vector> /// for std::vector
|
||||
#define _USE_MATH_DEFINES /// for M_PI on windows
|
||||
#include <cmath> /// for math functions
|
||||
#include <cstdint> /// for fixed size data types
|
||||
#include <ctime> /// for time to initialize rng
|
||||
#include <functional> /// for function pointers
|
||||
#include <iostream> /// for std::cout
|
||||
#include <random> /// for random number generation
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
@@ -32,25 +37,34 @@
|
||||
namespace math {
|
||||
/**
|
||||
* @namespace monte_carlo
|
||||
* @brief Functions for the [Monte Carlo Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration) implementation
|
||||
* @brief Functions for the [Monte Carlo
|
||||
* Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration)
|
||||
* implementation
|
||||
*/
|
||||
namespace monte_carlo {
|
||||
|
||||
using Function = std::function<double(double&)>; /// short-hand for std::functions used in this implementation
|
||||
using Function = std::function<double(
|
||||
double&)>; /// short-hand for std::functions used in this implementation
|
||||
|
||||
/**
|
||||
* @brief Generate samples according to some pdf
|
||||
* @details This function uses Metropolis-Hastings to generate random numbers. It generates a sequence of random numbers by using a markov chain.
|
||||
* Therefore, we need to define a start_point and the number of samples we want to generate.
|
||||
* Because the first samples generated by the markov chain may not be distributed according to the given pdf, one can specify how many samples
|
||||
* @details This function uses Metropolis-Hastings to generate random numbers.
|
||||
* It generates a sequence of random numbers by using a markov chain. Therefore,
|
||||
* we need to define a start_point and the number of samples we want to
|
||||
* generate. Because the first samples generated by the markov chain may not be
|
||||
* distributed according to the given pdf, one can specify how many samples
|
||||
* should be discarded before storing samples.
|
||||
* @param start_point The starting point of the markov chain
|
||||
* @param pdf The pdf to sample
|
||||
* @param num_samples The number of samples to generate
|
||||
* @param discard How many samples should be discarded at the start
|
||||
* @returns A vector of size num_samples with samples distributed according to the pdf
|
||||
* @returns A vector of size num_samples with samples distributed according to
|
||||
* the pdf
|
||||
*/
|
||||
std::vector<double> generate_samples(const double& start_point, const Function& pdf, const uint32_t& num_samples, const uint32_t& discard = 100000) {
|
||||
std::vector<double> generate_samples(const double& start_point,
|
||||
const Function& pdf,
|
||||
const uint32_t& num_samples,
|
||||
const uint32_t& discard = 100000) {
|
||||
std::vector<double> samples;
|
||||
samples.reserve(num_samples);
|
||||
|
||||
@@ -61,19 +75,19 @@ std::vector<double> generate_samples(const double& start_point, const Function&
|
||||
std::normal_distribution<double> normal(0.0, 1.0);
|
||||
generator.seed(time(nullptr));
|
||||
|
||||
for(uint32_t t = 0; t < num_samples + discard; ++t) {
|
||||
for (uint32_t t = 0; t < num_samples + discard; ++t) {
|
||||
// Generate a new proposal according to some mutation strategy.
|
||||
// This is arbitrary and can be swapped.
|
||||
double x_dash = normal(generator) + x_t;
|
||||
double acceptance_probability = std::min(pdf(x_dash)/pdf(x_t), 1.0);
|
||||
double acceptance_probability = std::min(pdf(x_dash) / pdf(x_t), 1.0);
|
||||
double u = uniform(generator);
|
||||
|
||||
// Accept "new state" according to the acceptance_probability
|
||||
if(u <= acceptance_probability) {
|
||||
if (u <= acceptance_probability) {
|
||||
x_t = x_dash;
|
||||
}
|
||||
|
||||
if(t >= discard) {
|
||||
if (t >= discard) {
|
||||
samples.push_back(x_t);
|
||||
}
|
||||
}
|
||||
@@ -92,13 +106,17 @@ std::vector<double> generate_samples(const double& start_point, const Function&
|
||||
* @param function The function to integrate
|
||||
* @param pdf The pdf to sample
|
||||
* @param num_samples The number of samples used to approximate the integral
|
||||
* @returns The approximation of the integral according to 1/N \sum_{i}^N f(x_i) / p(x_i)
|
||||
* @returns The approximation of the integral according to 1/N \sum_{i}^N f(x_i)
|
||||
* / p(x_i)
|
||||
*/
|
||||
double integral_monte_carlo(const double& start_point, const Function& function, const Function& pdf, const uint32_t& num_samples = 1000000) {
|
||||
double integral_monte_carlo(const double& start_point, const Function& function,
|
||||
const Function& pdf,
|
||||
const uint32_t& num_samples = 1000000) {
|
||||
double integral = 0.0;
|
||||
std::vector<double> samples = generate_samples(start_point, pdf, num_samples);
|
||||
std::vector<double> samples =
|
||||
generate_samples(start_point, pdf, num_samples);
|
||||
|
||||
for(double sample : samples) {
|
||||
for (double sample : samples) {
|
||||
integral += function(sample) / pdf(sample);
|
||||
}
|
||||
|
||||
@@ -113,8 +131,13 @@ double integral_monte_carlo(const double& start_point, const Function& function,
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
std::cout << "Disclaimer: Because this is a randomized algorithm," << std::endl;
|
||||
std::cout << "it may happen that singular samples deviate from the true result." << std::endl << std::endl;;
|
||||
std::cout << "Disclaimer: Because this is a randomized algorithm,"
|
||||
<< std::endl;
|
||||
std::cout
|
||||
<< "it may happen that singular samples deviate from the true result."
|
||||
<< std::endl
|
||||
<< std::endl;
|
||||
;
|
||||
|
||||
math::monte_carlo::Function f;
|
||||
math::monte_carlo::Function pdf;
|
||||
@@ -122,60 +145,58 @@ static void test() {
|
||||
double lower_bound = 0, upper_bound = 0;
|
||||
|
||||
/* \int_{-2}^{2} -x^2 + 4 dx */
|
||||
f = [&](double& x) {
|
||||
return -x*x + 4.0;
|
||||
};
|
||||
f = [&](double& x) { return -x * x + 4.0; };
|
||||
|
||||
lower_bound = -2.0;
|
||||
upper_bound = 2.0;
|
||||
pdf = [&](double& x) {
|
||||
if(x >= lower_bound && x <= -1.0) {
|
||||
if (x >= lower_bound && x <= -1.0) {
|
||||
return 0.1;
|
||||
}
|
||||
if(x <= upper_bound && x >= 1.0) {
|
||||
if (x <= upper_bound && x >= 1.0) {
|
||||
return 0.1;
|
||||
}
|
||||
if(x > -1.0 && x < 1.0) {
|
||||
if (x > -1.0 && x < 1.0) {
|
||||
return 0.4;
|
||||
}
|
||||
return 0.0;
|
||||
};
|
||||
|
||||
integral = math::monte_carlo::integral_monte_carlo((upper_bound - lower_bound) / 2.0, f, pdf);
|
||||
integral = math::monte_carlo::integral_monte_carlo(
|
||||
(upper_bound - lower_bound) / 2.0, f, pdf);
|
||||
|
||||
std::cout << "This number should be close to 10.666666: " << integral << std::endl;
|
||||
std::cout << "This number should be close to 10.666666: " << integral
|
||||
<< std::endl;
|
||||
|
||||
/* \int_{0}^{1} e^x dx */
|
||||
f = [&](double& x) {
|
||||
return std::exp(x);
|
||||
};
|
||||
f = [&](double& x) { return std::exp(x); };
|
||||
|
||||
lower_bound = 0.0;
|
||||
upper_bound = 1.0;
|
||||
pdf = [&](double& x) {
|
||||
if(x >= lower_bound && x <= 0.2) {
|
||||
if (x >= lower_bound && x <= 0.2) {
|
||||
return 0.1;
|
||||
}
|
||||
if(x > 0.2 && x <= 0.4) {
|
||||
if (x > 0.2 && x <= 0.4) {
|
||||
return 0.4;
|
||||
}
|
||||
if(x > 0.4 && x < upper_bound) {
|
||||
if (x > 0.4 && x < upper_bound) {
|
||||
return 1.5;
|
||||
}
|
||||
return 0.0;
|
||||
};
|
||||
|
||||
integral = math::monte_carlo::integral_monte_carlo((upper_bound - lower_bound) / 2.0, f, pdf);
|
||||
integral = math::monte_carlo::integral_monte_carlo(
|
||||
(upper_bound - lower_bound) / 2.0, f, pdf);
|
||||
|
||||
std::cout << "This number should be close to 1.7182818: " << integral << std::endl;
|
||||
std::cout << "This number should be close to 1.7182818: " << integral
|
||||
<< std::endl;
|
||||
|
||||
/* \int_{-\infty}^{\infty} sinc(x) dx, sinc(x) = sin(pi * x) / (pi * x)
|
||||
This is a difficult integral because of its infinite domain.
|
||||
Therefore, it may deviate largely from the expected result.
|
||||
*/
|
||||
f = [&](double& x) {
|
||||
return std::sin(M_PI * x) / (M_PI * x);
|
||||
};
|
||||
f = [&](double& x) { return std::sin(M_PI * x) / (M_PI * x); };
|
||||
|
||||
pdf = [&](double& x) {
|
||||
return 1.0 / std::sqrt(2.0 * M_PI) * std::exp(-x * x / 2.0);
|
||||
@@ -183,7 +204,8 @@ static void test() {
|
||||
|
||||
integral = math::monte_carlo::integral_monte_carlo(0.0, f, pdf, 10000000);
|
||||
|
||||
std::cout << "This number should be close to 1.0: " << integral << std::endl;
|
||||
std::cout << "This number should be close to 1.0: " << integral
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user