feat: Created midpoint integration numerical method (#1785)

* Created composite Simpson's numerical integration method

* Created midpoint numerical integration method

* Corrections

* Midpoint method

* Improved Documentation

* added namespace numerical_methods

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for ec5e0cce

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* clang-format and clang-tidy fixes for 7f16cc14

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update midpoint_integral_method.cpp

* All changes have been applied

* clang-format and clang-tidy fixes for 6617e060

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* clang-format and clang-tidy fixes for a5a50f89

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* clang-format and clang-tidy fixes for 4c60e180

* Create midpoint_integral_method.cpp

* Update numerical_methods/midpoint_integral_method.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* clang-format and clang-tidy fixes for 27f76052

* Update midpoint_integral_method.cpp

Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr>
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
ggkogkou 2021-11-01 15:56:40 +02:00 committed by GitHub
parent 1e8376eedb
commit 8a6f2052e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 262 additions and 71 deletions

View File

@ -229,6 +229,7 @@
* [Golden Search Extrema](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/golden_search_extrema.cpp) * [Golden Search Extrema](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/golden_search_extrema.cpp)
* [Lu Decompose](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lu_decompose.cpp) * [Lu Decompose](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lu_decompose.cpp)
* [Lu Decomposition](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lu_decomposition.h) * [Lu Decomposition](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lu_decomposition.h)
* [Midpoint Integral Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/midpoint_integral_method.cpp)
* [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/newton_raphson_method.cpp) * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/newton_raphson_method.cpp)
* [Ode Forward Euler](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/ode_forward_euler.cpp) * [Ode Forward Euler](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/ode_forward_euler.cpp)
* [Ode Midpoint Euler](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/ode_midpoint_euler.cpp) * [Ode Midpoint Euler](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/ode_midpoint_euler.cpp)

View File

@ -10,13 +10,14 @@
* In this implementation, using a graph in the form of adjacency * In this implementation, using a graph in the form of adjacency
* list, check whether the given graph is a bipartite or not. * list, check whether the given graph is a bipartite or not.
* *
* References used: [GeeksForGeeks](https://www.geeksforgeeks.org/bipartite-graph/) * References used:
* [GeeksForGeeks](https://www.geeksforgeeks.org/bipartite-graph/)
* @author [tushar2407](https://github.com/tushar2407) * @author [tushar2407](https://github.com/tushar2407)
*/ */
#include <cassert> /// for assert
#include <iostream> /// for IO operations #include <iostream> /// for IO operations
#include <queue> /// for queue data structure #include <queue> /// for queue data structure
#include <vector> /// for vector data structure #include <vector> /// for vector data structure
#include <cassert> /// for assert
/** /**
* @namespace graph * @namespace graph
@ -32,32 +33,31 @@ namespace graph {
* traversed or not yet * traversed or not yet
* @returns boolean * @returns boolean
*/ */
bool checkBipartite( bool checkBipartite(const std::vector<std::vector<int64_t>> &graph,
const std::vector<std::vector<int64_t>> &graph, int64_t index, std::vector<int64_t> *visited) {
int64_t index,
std::vector<int64_t> *visited
)
{
std::queue<int64_t> q; ///< stores the neighbouring node indexes in squence std::queue<int64_t> q; ///< stores the neighbouring node indexes in squence
/// of being reached /// of being reached
q.push(index); /// insert the current node into the queue q.push(index); /// insert the current node into the queue
(*visited)[index] = 1; /// mark the current node as travelled (*visited)[index] = 1; /// mark the current node as travelled
while(q.size()) while (q.size()) {
{
int64_t u = q.front(); int64_t u = q.front();
q.pop(); q.pop();
for(uint64_t i=0;i<graph[u].size();i++) for (uint64_t i = 0; i < graph[u].size(); i++) {
{ int64_t v =
int64_t v = graph[u][i]; ///< stores the neighbour of the current node graph[u][i]; ///< stores the neighbour of the current node
if(!(*visited)[v]) /// check whether the neighbour node is if (!(*visited)[v]) /// check whether the neighbour node is
/// travelled already or not /// travelled already or not
{ {
(*visited)[v] = ((*visited)[u]==1)?-1:1; /// colour the neighbouring node with (*visited)[v] =
((*visited)[u] == 1)
? -1
: 1; /// colour the neighbouring node with
/// different colour than the current node /// different colour than the current node
q.push(v); /// insert the neighbouring node into the queue q.push(v); /// insert the neighbouring node into the queue
} } else if ((*visited)[v] ==
else if((*visited)[v] == (*visited)[u]) /// if both the current node and its neighbour (*visited)[u]) /// if both the current node and its
/// has the same state then it is not a bipartite graph /// neighbour has the same state then it
/// is not a bipartite graph
{ {
return false; return false;
} }
@ -72,18 +72,18 @@ bool checkBipartite(
* and values in that row signify the nodes it is connected to * and values in that row signify the nodes it is connected to
* @returns booleans * @returns booleans
*/ */
bool isBipartite(const std::vector<std::vector<int64_t>> &graph) bool isBipartite(const std::vector<std::vector<int64_t>> &graph) {
{ std::vector<int64_t> visited(
std::vector<int64_t> visited(graph.size()); ///< stores boolean values graph.size()); ///< stores boolean values
/// which signify whether that node had been visited or not /// which signify whether that node had been visited or
/// not
for(uint64_t i=0;i<graph.size();i++) for (uint64_t i = 0; i < graph.size(); i++) {
{ if (!visited[i]) /// if the current node is not visited then check
if(!visited[i]) /// if the current node is not visited then check /// whether the sub-graph of that node is a bipartite
/// whether the sub-graph of that node is a bipartite or not /// or not
{
if(!checkBipartite(graph, i, &visited))
{ {
if (!checkBipartite(graph, i, &visited)) {
return false; return false;
} }
} }
@ -96,26 +96,18 @@ bool isBipartite(const std::vector<std::vector<int64_t>> &graph)
* @brief Self-test implementations * @brief Self-test implementations
* @returns void * @returns void
*/ */
static void test() static void test() {
{ std::vector<std::vector<int64_t>> graph = {{1, 3}, {0, 2}, {1, 3}, {0, 2}};
std::vector<std::vector<int64_t>> graph = {
{1,3},
{0,2},
{1,3},
{0,2}
};
assert(graph::isBipartite(graph) == true); /// check whether the above assert(graph::isBipartite(graph) ==
true); /// check whether the above
/// defined graph is indeed bipartite /// defined graph is indeed bipartite
std::vector<std::vector<int64_t>> graph_not_bipartite = { std::vector<std::vector<int64_t>> graph_not_bipartite = {
{1,2,3}, {1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}};
{0,2},
{0,1,3},
{0,2}
};
assert(graph::isBipartite(graph_not_bipartite) == false); /// check whether assert(graph::isBipartite(graph_not_bipartite) ==
false); /// check whether
/// the above defined graph is indeed bipartite /// the above defined graph is indeed bipartite
std::cout << "All tests have successfully passed!\n"; std::cout << "All tests have successfully passed!\n";
} }
@ -127,8 +119,7 @@ static void test()
* graph is bipartite or not. * graph is bipartite or not.
* @returns 0 on exit * @returns 0 on exit
*/ */
int main() int main() {
{
test(); // run self-test implementations test(); // run self-test implementations
return 0; return 0;
} }

View File

@ -0,0 +1,199 @@
/**
* @file
* @brief A numerical method for easy [approximation of
* integrals](https://en.wikipedia.org/wiki/Midpoint_method)
* @details The idea is to split the interval into N of intervals and use as
* interpolation points the xi for which it applies that xi = x0 + i*h, where h
* is a step defined as h = (b-a)/N where a and b are the first and last points
* of the interval of the integration [a, b].
*
* We create a table of the xi and their corresponding f(xi) values and we
* evaluate the integral by the formula: I = h * {f(x0+h/2) + f(x1+h/2) + ... +
* f(xN-1+h/2)}
*
* Arguments can be passed as parameters from the command line argv[1] = N,
* argv[2] = a, argv[3] = b. In this case if the default values N=16, a=1, b=3
* are changed then the tests/assert are disabled.
*
*
* @author [ggkogkou](https://github.com/ggkogkou)
*/
#include <cassert> /// for assert
#include <cmath> /// for math functions
#include <cstdint> /// for integer allocation
#include <cstdlib> /// for std::atof
#include <functional> /// for std::function
#include <iostream> /// for IO operations
#include <map> /// for std::map container
/**
* @namespace numerical_methods
* @brief Numerical algorithms/methods
*/
namespace numerical_methods {
/**
* @namespace midpoint_rule
* @brief Functions for the [Midpoint
* Integral](https://en.wikipedia.org/wiki/Midpoint_method) method
* implementation
*/
namespace midpoint_rule {
/**
* @fn double midpoint(const std::int32_t N, const double h, const double a,
* const std::function<double (double)>& func)
* @brief Main function for implementing the Midpoint Integral Method
* implementation
* @param N is the number of intervals
* @param h is the step
* @param a is x0
* @param func is the function that will be integrated
* @returns the result of the integration
*/
double midpoint(const std::int32_t N, const double h, const double a,
const std::function<double(double)>& func) {
std::map<int, double>
data_table; // Contains the data points, key: i, value: f(xi)
double xi = a; // Initialize xi to the starting point x0 = a
// Create the data table
// Loop from x0 to xN-1
double temp = NAN;
for (std::int32_t i = 0; i < N; i++) {
temp = func(xi + h / 2); // find f(xi+h/2)
data_table.insert(
std::pair<std::int32_t, double>(i, temp)); // add i and f(xi)
xi += h; // Get the next point xi for the next iteration
}
// Evaluate the integral.
// Remember: {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
double evaluate_integral = 0;
for (std::int32_t i = 0; i < N; i++) evaluate_integral += data_table.at(i);
// Multiply by the coefficient h
evaluate_integral *= h;
// If the result calculated is nan, then the user has given wrong input
// interval.
assert(!std::isnan(evaluate_integral) &&
"The definite integral can't be evaluated. Check the validity of "
"your input.\n");
// Else return
return evaluate_integral;
}
/**
* @brief A function f(x) that will be used to test the method
* @param x The independent variable xi
* @returns the value of the dependent variable yi = f(xi) = sqrt(xi) + ln(xi)
*/
double f(double x) { return std::sqrt(x) + std::log(x); }
/**
* @brief A function g(x) that will be used to test the method
* @param x The independent variable xi
* @returns the value of the dependent variable yi = g(xi) = e^(-xi) * (4 -
* xi^2)
*/
double g(double x) { return std::exp(-x) * (4 - std::pow(x, 2)); }
/**
* @brief A function k(x) that will be used to test the method
* @param x The independent variable xi
* @returns the value of the dependent variable yi = k(xi) = sqrt(2*xi^3 + 3)
*/
double k(double x) { return std::sqrt(2 * std::pow(x, 3) + 3); }
/**
* @brief A function l(x) that will be used to test the method
* @param x The independent variable xi
* @returns the value of the dependent variable yi = l(xi) = xi + ln(2*xi + 1)
*/
double l(double x) { return x + std::log(2 * x + 1); }
} // namespace midpoint_rule
} // namespace numerical_methods
/**
* @brief Self-test implementations
* @param N is the number of intervals
* @param h is the step
* @param a is x0
* @param b is the end of the interval
* @param used_argv_parameters is 'true' if argv parameters are given and
* 'false' if not
*/
static void test(std::int32_t N, double h, double a, double b,
bool used_argv_parameters) {
// Call midpoint() for each of the test functions f, g, k, l
// Assert with two decimal point precision
double result_f = numerical_methods::midpoint_rule::midpoint(
N, h, a, numerical_methods::midpoint_rule::f);
assert((used_argv_parameters || (result_f >= 4.09 && result_f <= 4.10)) &&
"The result of f(x) is wrong");
std::cout << "The result of integral f(x) on interval [" << a << ", " << b
<< "] is equal to: " << result_f << std::endl;
double result_g = numerical_methods::midpoint_rule::midpoint(
N, h, a, numerical_methods::midpoint_rule::g);
assert((used_argv_parameters || (result_g >= 0.27 && result_g <= 0.28)) &&
"The result of g(x) is wrong");
std::cout << "The result of integral g(x) on interval [" << a << ", " << b
<< "] is equal to: " << result_g << std::endl;
double result_k = numerical_methods::midpoint_rule::midpoint(
N, h, a, numerical_methods::midpoint_rule::k);
assert((used_argv_parameters || (result_k >= 9.06 && result_k <= 9.07)) &&
"The result of k(x) is wrong");
std::cout << "The result of integral k(x) on interval [" << a << ", " << b
<< "] is equal to: " << result_k << std::endl;
double result_l = numerical_methods::midpoint_rule::midpoint(
N, h, a, numerical_methods::midpoint_rule::l);
assert((used_argv_parameters || (result_l >= 7.16 && result_l <= 7.17)) &&
"The result of l(x) is wrong");
std::cout << "The result of integral l(x) on interval [" << a << ", " << b
<< "] is equal to: " << result_l << std::endl;
}
/**
* @brief Main function
* @param argc commandline argument count (ignored)
* @param argv commandline array of arguments (ignored)
* @returns 0 on exit
*/
int main(int argc, char** argv) {
std::int32_t N =
16; /// Number of intervals to divide the integration interval.
/// MUST BE EVEN
double a = 1, b = 3; /// Starting and ending point of the integration in
/// the real axis
double h = NAN; /// Step, calculated by a, b and N
bool used_argv_parameters =
false; // If argv parameters are used then the assert must be omitted
// for the test cases
// Get user input (by the command line parameters or the console after
// displaying messages)
if (argc == 4) {
N = std::atoi(argv[1]);
a = std::atof(argv[2]);
b = std::atof(argv[3]);
// Check if a<b else abort
assert(a < b && "a has to be less than b");
assert(N > 0 && "N has to be > 0");
if (N < 4 || a != 1 || b != 3) {
used_argv_parameters = true;
}
std::cout << "You selected N=" << N << ", a=" << a << ", b=" << b
<< std::endl;
} else {
std::cout << "Default N=" << N << ", a=" << a << ", b=" << b
<< std::endl;
}
// Find the step
h = (b - a) / N;
test(N, h, a, b, used_argv_parameters); // run self-test implementations
return 0;
}