From 1f6c39bcf28759e46195426f2dfa2b82cf776a21 Mon Sep 17 00:00:00 2001 From: Allen Guan Date: Mon, 1 Nov 2021 07:47:16 +0800 Subject: [PATCH 1/7] fix: rewrite prime numbers using linear sieve (#1810) * fix: rewrite prime numbers using linear sieve * fix code scanning error * clang-format and clang-tidy fixes for 0afd463b * fix sign-compare warning * clang-format and clang-tidy fixes for 6ddff0f7 Co-authored-by: David Leal Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- math/prime_numbers.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/math/prime_numbers.cpp b/math/prime_numbers.cpp index 4dd54f136..cd6aca0ca 100644 --- a/math/prime_numbers.cpp +++ b/math/prime_numbers.cpp @@ -9,14 +9,22 @@ /** Generate an increasingly large number of primes * and store in a list */ -std::vector primes(int max) { - max++; +std::vector primes(size_t max) { std::vector res; - std::vector numbers(max, false); - for (int i = 2; i < max; i++) { - if (!numbers[i]) { - for (int j = i; j < max; j += i) numbers[j] = true; - res.push_back(i); + std::vector is_not_prime(max + 1, false); + for (size_t i = 2; i <= max; i++) { + if (!is_not_prime[i]) { + res.emplace_back(i); + } + for (int p : res) { + size_t k = i * p; + if (k > max) { + break; + } + is_not_prime[k] = true; + if (i % p == 0) { + break; + } } } return res; @@ -25,9 +33,9 @@ std::vector primes(int max) { /** main function */ int main() { std::cout << "Calculate primes up to:\n>> "; - int n; + int n = 0; std::cin >> n; std::vector ans = primes(n); - for (int i = 0; i < ans.size(); i++) std::cout << ans[i] << ' '; + for (int p : ans) std::cout << p << ' '; std::cout << std::endl; } From 1e8376eedb03c6ebd34c167e45f4fe512084b622 Mon Sep 17 00:00:00 2001 From: David Leal Date: Sun, 31 Oct 2021 21:05:51 -0600 Subject: [PATCH 2/7] fix: Update the CoC to match the `.github` repository (#1832) --- CODE_OF_CONDUCT.md | 152 +++++++++++++++++++++++++++++++-------------- 1 file changed, 104 insertions(+), 48 deletions(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 535cbef32..c89771e8f 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -2,75 +2,131 @@ ## Our Pledge -In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and -our community a harassment-free experience for everyone, regardless of age, body -size, disability, ethnicity, sex characteristics, gender identity and expression, -level of experience, education, socio-economic status, nationality, personal -appearance, race, religion, or sexual identity and orientation. +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. ## Our Standards -Examples of behavior that contributes to creating a positive environment -include: +Examples of behavior that contributes to a positive environment for our +community include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community -Examples of unacceptable behavior by participants include: +Examples of unacceptable behavior include: -* The use of sexualized language or imagery and unwelcome sexual attention or - advances -* Trolling, insulting/derogatory comments, and personal or political attacks +* The use of sexualized language or imagery, and sexual attention or advances of + any kind +* Trolling, insulting or derogatory comments, and personal or political attacks * Public or private harassment -* Publishing others' private information, such as a physical or electronic - address, without explicit permission +* Publishing others' private information, such as a physical or email address, + without their explicit permission * Other conduct which could reasonably be considered inappropriate in a - professional setting + professional setting -## Our Responsibilities +## Enforcement Responsibilities -Project maintainers are responsible for clarifying the standards of acceptable -behavior and are expected to take appropriate and fair corrective action in -response to any instances of unacceptable behavior. +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. -Project maintainers have the right and responsibility to remove, edit, or -reject comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct, or to ban temporarily or -permanently any contributor for other behaviors that they deem inappropriate, -threatening, offensive, or harmful. +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. ## Scope -This Code of Conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. Examples of -representing a project or community include using an official project e-mail -address, posting via an official social media account, or acting as an appointed -representative at an online or offline event. Representation of a project may be -further defined and clarified by project maintainers. +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at 1anuppanwar@gmail.com, dynamitechetan@gmail.com, nikhilkala8@gmail.com. All -complaints will be reviewed and investigated and will result in a response that -is deemed necessary and appropriate to the circumstances. The project team is -obligated to maintain confidentiality with regard to the reporter of an incident. -Further details of specific enforcement policies may be posted separately. +reported to the community leaders responsible for enforcement at +hello@the-algorithms.com. +All complaints will be reviewed and investigated promptly and fairly. -Project maintainers who do not follow or enforce the Code of Conduct in good -faith may face temporary or permanent repercussions as determined by other -members of the project's leadership. +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of +actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the +community. ## Attribution -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, -available at +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at +[https://www.contributor-covenant.org/translations][translations]. [homepage]: https://www.contributor-covenant.org - -For answers to common questions about this code of conduct, see - +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations From 8a6f2052e2da09127a92eef1de68968c17c97aa1 Mon Sep 17 00:00:00 2001 From: ggkogkou <76820848+ggkogkou@users.noreply.github.com> Date: Mon, 1 Nov 2021 15:56:40 +0200 Subject: [PATCH 3/7] 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 * Update numerical_methods/midpoint_integral_method.cpp Co-authored-by: David Leal * Update numerical_methods/midpoint_integral_method.cpp Co-authored-by: David Leal * updating DIRECTORY.md * clang-format and clang-tidy fixes for ec5e0cce * Update numerical_methods/midpoint_integral_method.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for 7f16cc14 * Update numerical_methods/midpoint_integral_method.cpp Co-authored-by: David Leal * 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 * Update numerical_methods/midpoint_integral_method.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for a5a50f89 * Update numerical_methods/midpoint_integral_method.cpp Co-authored-by: David Leal * 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 * clang-format and clang-tidy fixes for 27f76052 * Update midpoint_integral_method.cpp Co-authored-by: ggkogkou Co-authored-by: David Leal Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + graph/is_graph_bipartite2.cpp | 133 ++++++------ .../midpoint_integral_method.cpp | 199 ++++++++++++++++++ 3 files changed, 262 insertions(+), 71 deletions(-) create mode 100644 numerical_methods/midpoint_integral_method.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 618142c55..278a9ed69 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -229,6 +229,7 @@ * [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 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) * [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) diff --git a/graph/is_graph_bipartite2.cpp b/graph/is_graph_bipartite2.cpp index 07374da9a..f1b04d070 100644 --- a/graph/is_graph_bipartite2.cpp +++ b/graph/is_graph_bipartite2.cpp @@ -1,22 +1,23 @@ /** * @brief Check whether a given graph is bipartite or not * @details - * A bipartite graph is the one whose nodes can be divided into two - * disjoint sets in such a way that the nodes in a set are not - * connected to each other at all, i.e. no intra-set connections. - * The only connections that exist are that of inter-set, - * i.e. the nodes from one set are connected to a subset of nodes + * A bipartite graph is the one whose nodes can be divided into two + * disjoint sets in such a way that the nodes in a set are not + * connected to each other at all, i.e. no intra-set connections. + * The only connections that exist are that of inter-set, + * i.e. the nodes from one set are connected to a subset of nodes * in the other set. - * 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. - * - * References used: [GeeksForGeeks](https://www.geeksforgeeks.org/bipartite-graph/) + * + * References used: + * [GeeksForGeeks](https://www.geeksforgeeks.org/bipartite-graph/) * @author [tushar2407](https://github.com/tushar2407) */ -#include /// for IO operations -#include /// for queue data structure -#include /// for vector data structure -#include /// for assert +#include /// for assert +#include /// for IO operations +#include /// for queue data structure +#include /// for vector data structure /** * @namespace graph @@ -28,62 +29,61 @@ namespace graph { * @param graph is a 2D matrix whose rows or the first index signify the node * and values in that row signify the nodes it is connected to * @param index is the valus of the node currently under observation - * @param visited is the vector which stores whether a given node has been + * @param visited is the vector which stores whether a given node has been * traversed or not yet * @returns boolean */ -bool checkBipartite( - const std::vector> &graph, - int64_t index, - std::vector *visited -) -{ - std::queue q; ///< stores the neighbouring node indexes in squence - /// of being reached - q.push(index); /// insert the current node into the queue - (*visited)[index] = 1; /// mark the current node as travelled - while(q.size()) - { +bool checkBipartite(const std::vector> &graph, + int64_t index, std::vector *visited) { + std::queue q; ///< stores the neighbouring node indexes in squence + /// of being reached + q.push(index); /// insert the current node into the queue + (*visited)[index] = 1; /// mark the current node as travelled + while (q.size()) { int64_t u = q.front(); q.pop(); - for(uint64_t i=0;i> &graph) -{ - std::vector visited(graph.size()); ///< stores boolean values - /// which signify whether that node had been visited or not - - for(uint64_t i=0;i> &graph) { + std::vector visited( + graph.size()); ///< stores boolean values + /// which signify whether that node had been visited or + /// not + + for (uint64_t i = 0; i < graph.size(); i++) { + if (!visited[i]) /// if the current node is not visited then check + /// whether the sub-graph of that node is a bipartite + /// or not { - if(!checkBipartite(graph, i, &visited)) - { + if (!checkBipartite(graph, i, &visited)) { return false; } } @@ -96,39 +96,30 @@ bool isBipartite(const std::vector> &graph) * @brief Self-test implementations * @returns void */ -static void test() -{ - std::vector> graph = { - {1,3}, - {0,2}, - {1,3}, - {0,2} - }; +static void test() { + std::vector> graph = {{1, 3}, {0, 2}, {1, 3}, {0, 2}}; - assert(graph::isBipartite(graph) == true); /// check whether the above - /// defined graph is indeed bipartite + assert(graph::isBipartite(graph) == + true); /// check whether the above + /// defined graph is indeed bipartite std::vector> graph_not_bipartite = { - {1,2,3}, - {0,2}, - {0,1,3}, - {0,2} - }; + {1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}}; - assert(graph::isBipartite(graph_not_bipartite) == false); /// check whether - /// the above defined graph is indeed bipartite + assert(graph::isBipartite(graph_not_bipartite) == + false); /// check whether + /// the above defined graph is indeed bipartite std::cout << "All tests have successfully passed!\n"; } /** * @brief Main function - * Instantitates a dummy graph of a small size with + * Instantitates a dummy graph of a small size with * a few edges between random nodes. - * On applying the algorithm, it checks if the instantiated + * On applying the algorithm, it checks if the instantiated * graph is bipartite or not. * @returns 0 on exit */ -int main() -{ +int main() { test(); // run self-test implementations return 0; } diff --git a/numerical_methods/midpoint_integral_method.cpp b/numerical_methods/midpoint_integral_method.cpp new file mode 100644 index 000000000..3eab8fd1e --- /dev/null +++ b/numerical_methods/midpoint_integral_method.cpp @@ -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 /// for assert +#include /// for math functions +#include /// for integer allocation +#include /// for std::atof +#include /// for std::function +#include /// for IO operations +#include /// 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& 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& func) { + std::map + 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(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 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; +} From b4b0864da10c47d675c8da1343e5f78d050850d6 Mon Sep 17 00:00:00 2001 From: Ameya Chawla <88154798+ameyachawlaggsipu@users.noreply.github.com> Date: Tue, 2 Nov 2021 22:22:18 +0530 Subject: [PATCH 4/7] feat: Implemented Fast Fourier Transform algorithm (#1700) * feat ; Implemented Fast Fourier Transform * feat : Implemented Fast Fourier Transform * fix : added comments to header * Fixing Code Formatter errors * fix : updated the documentation * fix : removed bad practice of using namespace td * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * updating DIRECTORY.md * fix : fixed the integer values with their appropriate size * Fixed errors * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * Fixed many errors * fix : fixed array errors * fix: fixing memory leak errors * fix: using delete instead of free as mentioned in errors * fix : using delete[ ] instead of delete * fix : fixing errors for memory leaks * Update fast_fourier_transform.cpp * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for a6594c85 * fix : updated documentation * fix : added time complexity in documentation * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * fix : update code as per the namespace numerical_methods * fix : use of auto keyword to reduce complexity * fix : updated documentation * fix : fixed segmentation fault error * fix : fixing clang-tidy errors * fix : fixing clang-tidy errors * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * fix : updated documentation * fix : fixing warnings * Update fast_fourier_transform.cpp * Update fast_fourier_transform.cpp * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for 198b4297 * Update fast_fourier_transform.cpp * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/fast_fourier_transform.cpp Co-authored-by: David Leal * a * Apply suggestions from code review * fix : updating leak memeory * Update fast_fourier_transform.cpp Co-authored-by: David Leal Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + numerical_methods/fast_fourier_transform.cpp | 165 +++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 numerical_methods/fast_fourier_transform.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 278a9ed69..781a4cbe0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -225,6 +225,7 @@ * [Brent Method Extrema](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/brent_method_extrema.cpp) * [Durand Kerner Roots](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/durand_kerner_roots.cpp) * [False Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/false_position.cpp) + * [Fast Fourier Transform](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/fast_fourier_transform.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gaussian_elimination.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) diff --git a/numerical_methods/fast_fourier_transform.cpp b/numerical_methods/fast_fourier_transform.cpp new file mode 100644 index 000000000..86e9d6d79 --- /dev/null +++ b/numerical_methods/fast_fourier_transform.cpp @@ -0,0 +1,165 @@ +/** + * @file + * @brief [A fast Fourier transform + * (FFT)](https://medium.com/@aiswaryamathur/understanding-fast-fouriertransform-from-scratch-to-solve-polynomial-multiplication-8018d511162f) + * is an algorithm that computes the + * discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). + * @details + * This + * algorithm has application in use case scenario where a user wants to find points of a + * function + * in a short time by just using the coefficients of the polynomial + * function. + * It can be also used to find inverse fourier transform by just switching the + value of omega. + * Time complexity + * this algorithm computes the DFT in O(nlogn) time in comparison to traditional + O(n^2). + * @author [Ameya Chawla](https://github.com/ameyachawlaggsipu) + */ + +#include /// for assert +#include /// for mathematical-related functions +#include /// for storing points and coefficents +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace numerical_methods + * @brief Numerical algorithms/methods + */ +namespace numerical_methods { +/** + * @brief FastFourierTransform is a recursive function which returns list of + * complex numbers + * @param p List of Coefficents in form of complex numbers + * @param n Count of elements in list p + * @returns p if n==1 + * @returns y if n!=1 + */ +std::complex *FastFourierTransform(std::complex *p, uint8_t n) { + if (n == 1) { + return p; /// Base Case To return + } + + double pi = 2 * asin(1.0); /// Declaring value of pi + + std::complex om = std::complex( + cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega + + auto *pe = new std::complex[n / 2]; /// Coefficients of even power + + auto *po = new std::complex[n / 2]; /// Coefficients of odd power + + int k1 = 0, k2 = 0; + for (int j = 0; j < n; j++) { + if (j % 2 == 0) { + pe[k1++] = p[j]; /// Assigning values of even Coefficients + + } else + po[k2++] = p[j]; /// Assigning value of odd Coefficients + } + + std::complex *ye = + FastFourierTransform(pe, n / 2); /// Recursive Call + + std::complex *yo = + FastFourierTransform(po, n / 2); /// Recursive Call + + auto *y = new std::complex[n]; /// Final value representation list + + k1 = 0, k2 = 0; + + for (int i = 0; i < n / 2; i++) { + y[i] = + ye[k1] + pow(om, i) * yo[k2]; /// Updating the first n/2 elements + y[i + n / 2] = + ye[k1] - pow(om, i) * yo[k2]; /// Updating the last n/2 elements + + k1++; + k2++; + } + + if(n!=2){ + + delete[] pe; + delete[] po; + + } + + delete[] ye; /// Deleting dynamic array ye + delete[] yo; /// Deleting dynamic array yo + return y; +} + +} // namespace numerical_methods + +/** + * @brief Self-test implementations + * @details + * Declaring two test cases and checking for the error + * in predicted and true value is less than 0.000000000001. + * @returns void + */ +static void test() { + /* descriptions of the following test */ + + auto *t1 = new std::complex[2]; /// Test case 1 + auto *t2 = new std::complex[4]; /// Test case 2 + + t1[0] = {1, 0}; + t1[1] = {2, 0}; + t2[0] = {1, 0}; + t2[1] = {2, 0}; + t2[2] = {3, 0}; + t2[3] = {4, 0}; + + uint8_t n1 = 2; + uint8_t n2 = 4; + std::vector> r1 = { + {3, 0}, {-1, 0}}; /// True Answer for test case 1 + + std::vector> r2 = { + {10, 0}, {-2, -2}, {-2, 0}, {-2, 2}}; /// True Answer for test case 2 + + std::complex *o1 = numerical_methods::FastFourierTransform(t1, n1); + std::complex *t3=o1; /// Temporary variable used to delete memory location of o1 + std::complex *o2 = numerical_methods::FastFourierTransform(t2, n2); + std::complex *t4=o2; /// Temporary variable used to delete memory location of o2 + for (uint8_t i = 0; i < n1; i++) { + assert((r1[i].real() - o1->real() < 0.000000000001) && + (r1[i].imag() - o1->imag() < + 0.000000000001)); /// Comparing for both real and imaginary + /// values for test case 1 + o1++; + } + + for (uint8_t i = 0; i < n2; i++) { + assert((r2[i].real() - o2->real() < 0.000000000001) && + (r2[i].imag() - o2->imag() < + 0.000000000001)); /// Comparing for both real and imaginary + /// values for test case 2 + o2++; + } + + + delete[] t1; + delete[] t2; + delete[] t3; + delete[] t4; + std::cout << "All tests have successfully passed!\n"; +} + +/** + * @brief Main function + * @param argc commandline argument count (ignored) + * @param argv commandline array of arguments (ignored) + * calls automated test function to test the working of fast fourier transform. + * @returns 0 on exit + */ + +int main(int argc, char const *argv[]) { + test(); // run self-test implementations + // with 2 defined test cases + return 0; +} From 1f0eff28d07a21b2426f1be0aab4f8f683e9f736 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 2 Nov 2021 22:33:49 +0530 Subject: [PATCH 5/7] fix: Circular linked list (#1825) * Create reverse_binary_tree.cpp * Added documentation Added Documentation for the level_order_traversal() function, and implemented a print() function to display the tree to STDOUT * Added documentation * Renamed tests to test * Fixed issue with incorrect using statement * updating DIRECTORY.md * clang-format and clang-tidy fixes for fb86292d * Added Test cases * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Changed int to int64_t * Updated documentation wording * Rewrote node struct and file structure * Added Circular Linked List * Update doc * Wrote bulk of code, added documentation * clang-format and clang-tidy fixes for 06f11f1e * Update operations_on_datastructures/circular_linked_list.cpp Co-authored-by: David Leal * Update operations_on_datastructures/circular_linked_list.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for 25ed3ea0 * Added parameter documentation * Added destructor for CircularLinkedList * Fixed memory bug * Update circular_linked_list.cpp * Update circular_linked_list.cpp * Added move constructor and assignment operator * clang-format and clang-tidy fixes for b36e4736 * Fixed copy and move operators/constructors and added documentation * clang-format and clang-tidy fixes for 24d3b9c2 * Added missing returns Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> --- .../circular_linked_list.cpp | 411 ++++++++++++++---- 1 file changed, 331 insertions(+), 80 deletions(-) diff --git a/operations_on_datastructures/circular_linked_list.cpp b/operations_on_datastructures/circular_linked_list.cpp index 1119bb5e7..bc97d1a75 100644 --- a/operations_on_datastructures/circular_linked_list.cpp +++ b/operations_on_datastructures/circular_linked_list.cpp @@ -1,97 +1,348 @@ -#include -using namespace std; +/** + * @file + * @brief Implementation for a [Circular Linked + * List](https://www.geeksforgeeks.org/circular-linked-list/). + * @details A Circular Linked List is a variation on the regular linked list, in + * which the last node has a pointer to the first node, which creates a full + * circle. Consequently, this allows any node to be used as the starting point + * for the list. + * @author [Alvin](https://github.com/polarvoid) + */ -struct node { - int val; - node *next; +#include /// for assert +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace operations_on_datastructures + * @brief Operations on Data Structures + */ +namespace operations_on_datastructures { + +/** + * @namespace circular_linked_list + * @brief Functions for the [Circular Linked + * List](https://www.geeksforgeeks.org/circular-linked-list/) implementation + */ +namespace circular_linked_list { + +/** + * @brief A Node struct that represents a single Node in a Binary Tree + */ +struct Node { + int64_t data; ///< The value of the Node + Node* next; ///< The Node's successor + /** + * @brief Creates a new Node with some initial data + * @param _data Value of Node + */ + explicit Node(int64_t _data) { + data = _data; ///< Set value of Node data + next = nullptr; ///< Initialize successor + } + /** + * @brief Creates a new Node with initial data and a successor + * @param _data Value of Node + * @param _next Pointer to the next Node + */ + explicit Node(int64_t _data, Node* _next) { + data = _data; ///< Set value of Node data + next = _next; ///< Initialize successor + } }; -node *start; +/** + * @brief A class that implements a Circular Linked List. + */ +class CircularLinkedList { + private: + Node* root; ///< Pointer to the root Node + Node* end{}; ///< Pointer to the last Node -void insert(int x) { - node *t = start; - - if (start != NULL) { - while (t->next != start) { - t = t->next; + public: + /** + * @brief Creates an empty CircularLinkedList. + */ + CircularLinkedList() { + root = nullptr; + end = nullptr; + } + /** + * @brief Copy constructor for CircularLinkedList. + */ + CircularLinkedList(const CircularLinkedList& copy) { + erase(); + root = nullptr; + Node* node = copy.root; + while (node != nullptr) { + insert(node->data); + node = node->next; } - node *n = new node; - t->next = n; - n->val = x; - n->next = start; - } else { - node *n = new node; - n->val = x; - start = n; - n->next = start; } -} - -void remove(int x) { - node *t = start; - node *p; - while (t->val != x) { - p = t; - t = t->next; + /** + * @brief Move constructor for CircularLinkedList + * @param source rvalue reference to a Circular Linked List + */ + CircularLinkedList(CircularLinkedList&& source) noexcept { + root = source.root; + end = source.end; + source.root = nullptr; + source.end = nullptr; } - p->next = t->next; - delete t; -} - -void search(int x) { - node *t = start; - int found = 0; - while (t->next != start) { - if (t->val == x) { - cout << "\nFound"; - found = 1; - break; + /** + * @brief Copy assignment operator + * @param other Reference to a Circular Linked List + * @returns Reference to CircularLinkedList + */ + CircularLinkedList& operator=(const CircularLinkedList& other) { + erase(); + root = nullptr; + Node* node = other.root; + while (node != nullptr) { + insert(node->data); + node = node->next; } - t = t->next; + return *this; } - if (found == 0) { - cout << "\nNot Found"; + /** + * @brief Move assignment operator + * @param other rvalue reference to a Circular Linked List + * @returns Reference to CircularLinkedList + */ + CircularLinkedList& operator=(CircularLinkedList&& other) noexcept { + root = other.root; + end = other.end; + other.root = nullptr; + other.end = nullptr; + return *this; } + /** + * @brief Cleans up memory when destroyed + */ + ~CircularLinkedList() { erase(); } + /** + * Iteratively frees each node in the Circular Linked List from the heap + */ + void erase() { + if (root == nullptr) { + return; + } + Node* node = root; + do { + Node* temp = node; + node = node->next; + delete (temp); + } while (node != root); + root = nullptr; + end = nullptr; + } + /** + * @brief Inserts all the values from a vector into the Circular Linked List + * @details Goes through each element in the vector sequentially, inserting + * it into the list + * @param values The vector of integer values that is to be inserted + * @returns void + */ + void insert(const std::vector& values) { + for (int64_t value : values) { + insert(value); + } + } + /** + * @brief Inserts a single value into the Circular Linked List + * @details Creates a Node with the given value, pointing to the root Node + * and inserts it into the list + * @param data The integer valus to be inserted + * @returns void + */ + void insert(int64_t data) { + Node* node = new Node(data, root); + insert(node); + } + /** + * @brief Inserts a given Node into the Circular Linked List + * @details Checks wheter the list is empty, and inserts the Node, modifying + * the end pointer + * @param node The Node that is to be inserted + * @returns void + */ + void insert(Node* node) { + if (root == nullptr) { + root = node; ///< Set node as the root + node->next = root; ///< Point node to itself + end = root; ///< Set the end to the root + } else { + end->next = node; ///< Append node to the end + node->next = root; ///< Set the next value to the root + end = node; ///< Make end point to node + } + } + /** + * @brief Prints the values of the Circular Linked List, beginning from the + * root Node + * @details Goes through each Node from the root and prints them out in + * order + * @returns void + */ + void print() { print(root); } + /** + * @brief Prints the values of the Circular Linked List, beginning from a + * given Node to be used as the root + * @details Goes through each Node from the given Node and prints them out + * in order. If the list is empty, it prints the message 'Empty List!' + * @param root The Node to start at + * @returns void + */ + void print(Node* root) { + Node* temp = root; + if (root == nullptr) { + std::cout << "Empty List!\n"; + return; + } + do { + std::cout << temp->data << " "; + temp = temp->next; + } while (temp != root); + std::cout << "\n"; + } + /** + * @brief Returns a std::vector of the values of the Circular Linked List + * @details Starting from the root Node, appends each value of the list to a + * std::vector and returns it + * @returns A std::vector of the list's values + */ + std::vector values() { return values(root); } + /** + * @brief Returns a std::vector of the values of the Circular Linked List, + * beginning from a given Node + * @details Starting from a given Node, appends each value of the list to a + * std::vector and returns it + * @param root The Node to start at + * @returns A std::vector of the list's values + */ + std::vector values(Node* root) { + std::vector res; + if (root == nullptr) { + return res; ///< Return empty vector + } + Node* temp = root; + do { + res.push_back(temp->data); + temp = temp->next; + } while (temp != root); + return res; + } +}; + +} // namespace circular_linked_list + +} // namespace operations_on_datastructures + +/** + * @namespace tests + * @brief Testcases to check Circular Linked List. + */ +namespace tests { +using operations_on_datastructures::circular_linked_list::CircularLinkedList; +using operations_on_datastructures::circular_linked_list::Node; +/** + * @brief A Test to check a single value + * @returns void + */ +void test1() { + std::cout << "TEST CASE 1\n"; + std::cout << "Intialized a = {2}\n"; + std::cout << "Expected result: {2}\n"; + CircularLinkedList a; + std::vector res = {2}; + a.insert(2); + assert(a.values() == res); + a.print(); + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check a few values + * @returns void + */ +void test2() { + std::cout << "TEST CASE 2\n"; + std::cout << "Intialized a = {2, 5, 6}\n"; + std::cout << "Expected result: {2, 5, 6}\n"; + CircularLinkedList a; + std::vector res = {2, 5, 6}; + a.insert(2); + a.insert(5); + a.insert(6); + assert(a.values() == res); + a.print(); + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check an input array + * @returns void + */ +void test3() { + std::cout << "TEST CASE 3\n"; + std::cout << "Intialized a = {2, 7, 8, 3, 2, 6}\n"; + std::cout << "Expected result: {2, 7, 8, 3, 2, 6}\n"; + CircularLinkedList a; + std::vector res = {2, 7, 8, 3, 2, 6}; + a.insert({2, 7, 8, 3, 2, 6}); + a.print(); + assert(a.values() == res); + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check using a specific Node as the starting point + * @returns void + */ +void test4() { + std::cout << "TEST CASE 4\n"; + std::cout << "Intialized a = {2, 5}\n"; + std::cout << "Expected result: {5, 2}\n"; + CircularLinkedList a; + std::vector res = {5, 2}; + a.insert(2); + Node* start = new Node(5); ///< Node we will start printing from + a.insert(start); + assert(a.values(start) == res); + a.print(start); + std::cout << "TEST PASSED!\n\n"; } -void show() { - node *t = start; - do { - cout << t->val << "\t"; - t = t->next; - } while (t != start); +/** + * @brief A Test to check an empty list + * @returns void + */ +void test5() { + std::cout << "TEST CASE 5\n"; + std::cout << "Intialized a = {}\n"; + std::cout << "Expected result: Empty List!\n"; + CircularLinkedList a; + std::vector res = {}; + assert(a.values() == res); + a.print(); + std::cout << "TEST PASSED!\n\n"; +} +} // namespace tests + +/** + * @brief Function to test the correctness of the Circular Linked List + * @returns void + */ +static void test() { + tests::test1(); + tests::test2(); + tests::test3(); + tests::test4(); + tests::test5(); } +/** + * @brief main function + * @returns 0 on exit + */ int main() { - int choice, x; - do { - cout << "\n1. Insert"; - cout << "\n2. Delete"; - cout << "\n3. Search"; - cout << "\n4. Print"; - cout << "\n\nEnter you choice : "; - cin >> choice; - switch (choice) { - case 1: - cout << "\nEnter the element to be inserted : "; - cin >> x; - insert(x); - break; - case 2: - cout << "\nEnter the element to be removed : "; - cin >> x; - remove(x); - break; - case 3: - cout << "\nEnter the element to be searched : "; - cin >> x; - search(x); - break; - case 4: - show(); - break; - } - } while (choice != 0); - + test(); // run self-test implementations return 0; } From 0c08cd75f9218826d6d4b5c48e415737b9c33cca Mon Sep 17 00:00:00 2001 From: ggkogkou <76820848+ggkogkou@users.noreply.github.com> Date: Wed, 3 Nov 2021 20:22:08 +0200 Subject: [PATCH 6/7] feat: Created composite Simpson's numerical integration method (#1773) * Created composite Simpson's numerical integration method * Created midpoint numerical integration method * Corrections * deleted: unnecessary file * fixed: doucumentation and structure * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * updating DIRECTORY.md * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * Update numerical_methods/composite_simpson_rule.cpp Co-authored-by: David Leal * fixed: compilation and documentation error Co-authored-by: ggkogkou Co-authored-by: David Leal Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Ayaan Khan --- .vscode/settings.json | 66 +++++- DIRECTORY.md | 1 + numerical_methods/composite_simpson_rule.cpp | 202 +++++++++++++++++++ 3 files changed, 265 insertions(+), 4 deletions(-) create mode 100644 numerical_methods/composite_simpson_rule.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 074c4ab03..67fe06477 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,64 @@ { - "C_Cpp.clang_format_style": "{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }", - "editor.formatOnSave": true, - "editor.formatOnType": true, - "editor.formatOnPaste": true + "C_Cpp.clang_format_style": "{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }", + "editor.formatOnSave": true, + "editor.formatOnType": true, + "editor.formatOnPaste": true, + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "complex": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "set": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "valarray": "cpp" + } } diff --git a/DIRECTORY.md b/DIRECTORY.md index 781a4cbe0..8d7b34a85 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -223,6 +223,7 @@ ## Numerical Methods * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/bisection_method.cpp) * [Brent Method Extrema](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/brent_method_extrema.cpp) + * [Composite Simpson Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/composite_simpson_rule.cpp) * [Durand Kerner Roots](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/durand_kerner_roots.cpp) * [False Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/false_position.cpp) * [Fast Fourier Transform](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/fast_fourier_transform.cpp) diff --git a/numerical_methods/composite_simpson_rule.cpp b/numerical_methods/composite_simpson_rule.cpp new file mode 100644 index 000000000..9a5e8c180 --- /dev/null +++ b/numerical_methods/composite_simpson_rule.cpp @@ -0,0 +1,202 @@ +/** + * @file + * @brief Implementation of the Composite Simpson Rule for the approximation + * + * @details The following is an implementation of the Composite Simpson Rule for + * the approximation of definite integrals. More info -> wiki: + * https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule + * + * The idea is to split the interval in an EVEN number 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/3 * {f(x0) + 4*f(x1) + 2*f(x2) + + * ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)} + * + * That means that the first and last indexed i f(xi) are multiplied by 1, + * the odd indexed f(xi) by 4 and the even by 2. + * + * In this program there are 4 sample test functions f, g, k, l that are + * evaluated in the same interval. + * + * Arguments can be passed as parameters from the command line argv[1] = N, + * argv[2] = a, argv[3] = b + * + * N must be even number and a /// for assert +#include /// for math functions +#include /// for integer allocation +#include /// for std::atof +#include /// for std::function +#include /// for IO operations +#include /// for std::map container + +/** + * @namespace numerical_methods + * @brief Numerical algorithms/methods + */ +namespace numerical_methods { +/** + * @namespace simpson_method + * @brief Contains the Simpson's method implementation + */ +namespace simpson_method { +/** + * @fn double evaluate_by_simpson(int N, double h, double a, + * std::function func) + * @brief Calculate integral or assert if integral is not a number (Nan) + * @param N number of intervals + * @param h step + * @param a x0 + * @param func: choose the function that will be evaluated + * @returns the result of the integration + */ +double evaluate_by_simpson(std::int32_t N, double h, double a, + std::function func) { + std::map + 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 + double temp; + for (std::int32_t i = 0; i <= N; i++) { + temp = func(xi); + data_table.insert( + std::pair(i, temp)); // add i and f(xi) + xi += h; // Get the next point xi for the next iteration + } + + // Evaluate the integral. + // Remember: f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN) + double evaluate_integral = 0; + for (std::int32_t i = 0; i <= N; i++) { + if (i == 0 || i == N) + evaluate_integral += data_table.at(i); + else if (i % 2 == 1) + evaluate_integral += 4 * data_table.at(i); + else + evaluate_integral += 2 * data_table.at(i); + } + + // Multiply by the coefficient h/3 + evaluate_integral *= h / 3; + + // 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; +} + +/** + * @fn double f(double x) + * @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) + */ +double f(double x) { return std::sqrt(x) + std::log(x); } +/** @brief Another test function */ +double g(double x) { return std::exp(-x) * (4 - std::pow(x, 2)); } +/** @brief Another test function */ +double k(double x) { return std::sqrt(2 * std::pow(x, 3) + 3); } +/** @brief Another test function*/ +double l(double x) { return x + std::log(2 * x + 1); } +} // namespace simpson_method +} // 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 the functions and find the integral of each function + double result_f = numerical_methods::simpson_method::evaluate_by_simpson( + N, h, a, numerical_methods::simpson_method::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::simpson_method::evaluate_by_simpson( + N, h, a, numerical_methods::simpson_method::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::simpson_method::evaluate_by_simpson( + N, h, a, numerical_methods::simpson_method::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::simpson_method::evaluate_by_simpson( + N, h, a, numerical_methods::simpson_method::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; /// 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 tst cases + + // Get user input (by the command line parameters or the console after + // displaying messages) + if (argc == 4) { + N = std::atoi(argv[1]); + a = (double)std::atof(argv[2]); + b = (double)std::atof(argv[3]); + // Check if a 0 && "N has to be > 0"); + if (N < 16 || 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; +} From 87ef61ae2363f88e88ef1644c28937cd7222cf92 Mon Sep 17 00:00:00 2001 From: Ameya Chawla <88154798+ameyachawlaggsipu@users.noreply.github.com> Date: Thu, 4 Nov 2021 00:46:21 +0530 Subject: [PATCH 7/7] feat : Implemented Inverse Fast Fourier Transform (#1834) * feat : Implemented Inverse Fast Fourier Transform Slightly different from fast Fourier transform Just the om variable declared in line 40 is divided by n and swapping the testing inputs with testing outputs . * Update numerical_methods/Inverse_fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/Inverse_fast_fourier_transform.cpp Co-authored-by: David Leal * Update numerical_methods/Inverse_fast_fourier_transform.cpp Co-authored-by: David Leal * formatting filenames d7f9a946 * updating DIRECTORY.md * fix : optimized the code * Apply suggestions from code review Co-authored-by: David Leal Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Ayaan Khan --- DIRECTORY.md | 1 + .../inverse_fast_fourier_transform.cpp | 161 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 numerical_methods/inverse_fast_fourier_transform.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 8d7b34a85..d2c85ebda 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -229,6 +229,7 @@ * [Fast Fourier Transform](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/fast_fourier_transform.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gaussian_elimination.cpp) * [Golden Search Extrema](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/golden_search_extrema.cpp) + * [Inverse Fast Fourier Transform](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/inverse_fast_fourier_transform.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) * [Midpoint Integral Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/midpoint_integral_method.cpp) diff --git a/numerical_methods/inverse_fast_fourier_transform.cpp b/numerical_methods/inverse_fast_fourier_transform.cpp new file mode 100644 index 000000000..d2248be7b --- /dev/null +++ b/numerical_methods/inverse_fast_fourier_transform.cpp @@ -0,0 +1,161 @@ +/** + * @file + * @brief [An inverse fast Fourier transform + * (IFFT)](https://www.geeksforgeeks.org/python-inverse-fast-fourier-transformation/) + * is an algorithm that computes the inverse fourier transform. + * @details + * This algorithm has an application in use case scenario where a user wants find coefficients of + * a function in a short time by just using points generated by DFT. + * Time complexity + * this algorithm computes the IDFT in O(nlogn) time in comparison to traditional O(n^2). + * @author [Ameya Chawla](https://github.com/ameyachawlaggsipu) + */ + +#include /// for assert +#include /// for mathematical-related functions +#include /// for storing points and coefficents +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace numerical_methods + * @brief Numerical algorithms/methods + */ +namespace numerical_methods { +/** + * @brief InverseFastFourierTransform is a recursive function which returns list of + * complex numbers + * @param p List of Coefficents in form of complex numbers + * @param n Count of elements in list p + * @returns p if n==1 + * @returns y if n!=1 + */ +std::complex *InverseFastFourierTransform(std::complex *p, uint8_t n) { + if (n == 1) { + return p; /// Base Case To return + } + + double pi = 2 * asin(1.0); /// Declaring value of pi + + std::complex om = std::complex( + cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega + + om.real(om.real()/n); /// One change in comparison with DFT + om.imag(om.imag()/n); /// One change in comparison with DFT + + auto *pe = new std::complex[n / 2]; /// Coefficients of even power + + auto *po = new std::complex[n / 2]; /// Coefficients of odd power + + int k1 = 0, k2 = 0; + for (int j = 0; j < n; j++) { + if (j % 2 == 0) { + pe[k1++] = p[j]; /// Assigning values of even Coefficients + + } else + po[k2++] = p[j]; /// Assigning value of odd Coefficients + } + + std::complex *ye = + InverseFastFourierTransform(pe, n / 2); /// Recursive Call + + std::complex *yo = + InverseFastFourierTransform(po, n / 2); /// Recursive Call + + auto *y = new std::complex[n]; /// Final value representation list + + k1 = 0, k2 = 0; + + for (int i = 0; i < n / 2; i++) { + y[i] = + ye[k1] + pow(om, i) * yo[k2]; /// Updating the first n/2 elements + y[i + n / 2] = + ye[k1] - pow(om, i) * yo[k2]; /// Updating the last n/2 elements + + k1++; + k2++; + } + + if(n!=2){ + + delete[] pe; + delete[] po; + + } + + delete[] ye; /// Deleting dynamic array ye + delete[] yo; /// Deleting dynamic array yo + return y; +} + +} // namespace numerical_methods + +/** + * @brief Self-test implementations + * @details + * Declaring two test cases and checking for the error + * in predicted and true value is less than 0.000000000001. + * @returns void + */ +static void test() { + /* descriptions of the following test */ + + auto *t1 = new std::complex[2]; /// Test case 1 + auto *t2 = new std::complex[4]; /// Test case 2 + + t1[0] = {3, 0}; + t1[1] = {-1, 0}; + t2[0] = {10, 0}; + t2[1] = {-2, -2}; + t2[2] = {-2, 0}; + t2[3] = {-2, 2}; + + uint8_t n1 = 2; + uint8_t n2 = 4; + std::vector> r1 = { + {1, 0}, {2, 0}}; /// True Answer for test case 1 + + std::vector> r2 = { + {1, 0}, {2, 0}, {3, 0}, {4, 0}}; /// True Answer for test case 2 + + std::complex *o1 = numerical_methods::InverseFastFourierTransform(t1, n1); + + std::complex *o2 = numerical_methods::InverseFastFourierTransform(t2, n2); + + for (uint8_t i = 0; i < n1; i++) { + assert((r1[i].real() - o1[i].real() < 0.000000000001) && + (r1[i].imag() - o1[i].imag() < + 0.000000000001)); /// Comparing for both real and imaginary + /// values for test case 1 + + } + + for (uint8_t i = 0; i < n2; i++) { + assert((r2[i].real() - o2[i].real() < 0.000000000001) && + (r2[i].imag() - o2[i].imag() < + 0.000000000001)); /// Comparing for both real and imaginary + /// values for test case 2 + + } + + + delete[] t1; + delete[] t2; + delete[] o1; + delete[] o2; + std::cout << "All tests have successfully passed!\n"; +} + +/** + * @brief Main function + * @param argc commandline argument count (ignored) + * @param argv commandline array of arguments (ignored) + * calls automated test function to test the working of fast fourier transform. + * @returns 0 on exit + */ + +int main(int argc, char const *argv[]) { + test(); // run self-test implementations + // with 2 defined test cases + return 0; +}