From 0874eccd82004cc91816b66643770abb2b9d55f6 Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Mon, 7 Oct 2024 12:44:06 +0530 Subject: [PATCH 01/20] Add Exponential Distribution --- probability/exponential_dist.cpp | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 probability/exponential_dist.cpp diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp new file mode 100644 index 00000000000..2c0616327f9 --- /dev/null +++ b/probability/exponential_dist.cpp @@ -0,0 +1,90 @@ +/** + * @file + * @brief [Exponential Distribution] (https://en.wikipedia.org/wiki/Exponential_distribution) + * + * The exponential distribution is used to model + * events occuring between a Poisson process like radioactive decay. + * + * P(x,⋋) = ⋋e^(-⋋x) + * + * Summary of variables used: + * ⋋: rate parameter + */ +#include +#include +#include + +/** Find the expected value of the exponential distribution + * \param [in] lambda + * \returns \f$\mu=1/lambda\f$ + */ +double exponential_expected(double lambda){ + if (lambda<=0){ + std::cout << "Error: Lambda must be greater than 0." << std::endl; + assert(lambda>0); + } + return 1/lambda; +} + +/** Find the variance of the exponential distribution + * \param [in] lambda + * \returns \f$\sigma^2=1/(lambda^2)\f$ + */ +double exponential_var(double lambda){ + if (lambda<=0){ + std::cout << "Error: Lambda must be greater than 0." << std::endl; + assert(lambda>0); + } + return 1/pow(lambda,2); +} + +/** Find the standard deviation of the exponential distribution + * \param [in] lambda + * \returns \f$\sigma=1/lambda\f$ + */ +double exponential_std(double lambda){ + if (lambda<=0){ + std::cout << "Error: Lambda must be greater than 0." << std::endl; + assert(lambda>0); + } + return 1/lambda; +} + +static void test(){ + double lambda = 2; + double expected = 0.5; + double var = 0.25; + double std = 0.5; + + //Test 1 + std::cout << "Expected Value" << std::endl; + std::cout << "Lambda : " << lambda << std::endl; + std::cout << "Expected Output : " << expected < Date: Mon, 7 Oct 2024 12:49:59 +0530 Subject: [PATCH 02/20] Modified the documentation --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 79eb258c16f..6d58fe03528 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -322,6 +322,7 @@ * [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp) * [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp) * [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp) + * [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp) * [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp) * [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp) * [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp) From 2757f94cac04d15cdee433dd6144540403702155 Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:55:09 +0530 Subject: [PATCH 03/20] Update Documentation --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1404281b2fe..33b0fe3a7a0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -322,6 +322,7 @@ * [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp) * [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp) * [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp) + * [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp) * [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp) * [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp) * [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp) From a994abb0651361d3d3d7e821d72deb4605f011db Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:51:32 +0530 Subject: [PATCH 04/20] Update probability/exponential_dist.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index 2c0616327f9..7f0effdc513 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -1,6 +1,6 @@ /** * @file - * @brief [Exponential Distribution] (https://en.wikipedia.org/wiki/Exponential_distribution) + * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) * * The exponential distribution is used to model * events occuring between a Poisson process like radioactive decay. From 05de7d0db2665f4466d9150589e3090ea30b1000 Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:52:09 +0530 Subject: [PATCH 05/20] Update probability/exponential_dist.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index 7f0effdc513..817a60da336 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -87,4 +87,4 @@ static void test(){ int main(){ test(); //Self-implemented test return 0; -} \ No newline at end of file +} From 478ba41d2aa4131c20235cc92cb9c688fad3b332 Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:52:17 +0530 Subject: [PATCH 06/20] Update probability/exponential_dist.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index 817a60da336..c24716f16a6 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -85,6 +85,6 @@ static void test(){ } int main(){ - test(); //Self-implemented test + test(); //Self test implementation return 0; } From c340e0142404a119fd0a4dce485635f922104dfe Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Tue, 8 Oct 2024 17:02:52 +0530 Subject: [PATCH 07/20] Update the files --- DIRECTORY.md | 1 - probability/exponential_dist.cpp | 39 ++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 33b0fe3a7a0..1404281b2fe 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -322,7 +322,6 @@ * [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp) * [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp) * [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp) - * [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp) * [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp) * [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp) * [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index 2c0616327f9..effda708034 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -1,22 +1,23 @@ /** * @file - * @brief [Exponential Distribution] (https://en.wikipedia.org/wiki/Exponential_distribution) + * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) * * The exponential distribution is used to model * events occuring between a Poisson process like radioactive decay. * - * P(x,⋋) = ⋋e^(-⋋x) + * \( P(x, \lambda) = \lambda e^{-\lambda x} \) * * Summary of variables used: - * ⋋: rate parameter + * \lambda : rate parameter */ -#include -#include -#include -/** Find the expected value of the exponential distribution - * \param [in] lambda - * \returns \f$\mu=1/lambda\f$ +#include // For power function +#include // For asserting the test cases +#include // For I/O operation + +/** + * @brief the expected value of the exponential distribution + * @returns \( \mu = \frac{1}{\lambda} \) */ double exponential_expected(double lambda){ if (lambda<=0){ @@ -26,9 +27,9 @@ double exponential_expected(double lambda){ return 1/lambda; } -/** Find the variance of the exponential distribution - * \param [in] lambda - * \returns \f$\sigma^2=1/(lambda^2)\f$ +/** + * @brief the variance of the exponential distribution + * @returns \( \sigma^2 = \frac{1}{\lambda^2} \) */ double exponential_var(double lambda){ if (lambda<=0){ @@ -38,9 +39,9 @@ double exponential_var(double lambda){ return 1/pow(lambda,2); } -/** Find the standard deviation of the exponential distribution - * \param [in] lambda - * \returns \f$\sigma=1/lambda\f$ +/** + * @brief the standard deviation of the exponential distribution + * @returns \( \sigma = \frac{1}{\lambda} \) */ double exponential_std(double lambda){ if (lambda<=0){ @@ -84,7 +85,11 @@ static void test(){ std::cout< Date: Tue, 8 Oct 2024 17:11:30 +0530 Subject: [PATCH 08/20] Removed the link from documentation --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 33b0fe3a7a0..1404281b2fe 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -322,7 +322,6 @@ * [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp) * [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp) * [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp) - * [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp) * [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp) * [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp) * [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp) From 489cac5edef605c314b60445dccf5bb519f416b5 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Tue, 8 Oct 2024 19:27:39 +0530 Subject: [PATCH 09/20] docs: remove the second brief tag --- probability/exponential_dist.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index 6afc42222c6..ed6e3d53627 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -1,7 +1,6 @@ /** * @file * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) - * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) * * The exponential distribution is used to model * events occuring between a Poisson process like radioactive decay. From 39981b018a45caa6349a490db043c9c8ef272df5 Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:20:56 +0530 Subject: [PATCH 10/20] Update latex notation Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index ed6e3d53627..d5d0bfaf78a 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -5,7 +5,7 @@ * The exponential distribution is used to model * events occuring between a Poisson process like radioactive decay. * - * \( P(x, \lambda) = \lambda e^{-\lambda x} \) + * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f] * * Summary of variables used: * \lambda : rate parameter From ea428247f25323b4a41d04394d6c76705ab1bb6f Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:21:09 +0530 Subject: [PATCH 11/20] Update latex notation Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index d5d0bfaf78a..0de94fdf328 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -8,7 +8,7 @@ * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f] * * Summary of variables used: - * \lambda : rate parameter + * \f$\lambda\f$ : rate parameter */ #include // For power function From c4244c8aef463bca091e0bc995f2b35259268f0b Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Wed, 9 Oct 2024 12:44:14 +0530 Subject: [PATCH 12/20] Corrected format issues --- probability/exponential_dist.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index 0de94fdf328..6665bde5f8b 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -17,7 +17,7 @@ /** * @brief the expected value of the exponential distribution - * @returns \( \mu = \frac{1}{\lambda} \) + * @returns \f[\mu = \frac{1}{\lambda}\f] */ double exponential_expected(double lambda){ if (lambda<=0){ @@ -29,7 +29,7 @@ double exponential_expected(double lambda){ /** * @brief the variance of the exponential distribution - * @returns \( \sigma^2 = \frac{1}{\lambda^2} \) + * @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f] */ double exponential_var(double lambda){ if (lambda<=0){ @@ -41,7 +41,7 @@ double exponential_var(double lambda){ /** * @brief the standard deviation of the exponential distribution - * @returns \( \sigma = \frac{1}{\lambda} \) + * @returns \f[\sigma = \frac{1}{\lambda}\f] */ double exponential_std(double lambda){ if (lambda<=0){ From e1b2e7300e5483079050db09d3d7fc3369282b7a Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:17:01 +0530 Subject: [PATCH 13/20] Update probability/exponential_dist.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index 6665bde5f8b..dbf11553515 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -51,6 +51,10 @@ double exponential_std(double lambda){ return 1/lambda; } +/** + * @brief Self-test implementation + * @returns void + */ static void test(){ double lambda = 2; double expected = 0.5; From a2bcdd485d621cc0de5e8a099e0f7a2ff603d914 Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Thu, 10 Oct 2024 11:49:46 +0530 Subject: [PATCH 14/20] Update probability/exponential_dist.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index dbf11553515..f4328f6ae66 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -11,7 +11,7 @@ * \f$\lambda\f$ : rate parameter */ -#include // For power function +#include // For std::pow #include // For asserting the test cases #include // For I/O operation From 1fd6081b03ad5c67898332e463d13c2bb8b7e3c1 Mon Sep 17 00:00:00 2001 From: Harshil Shah <143382356+HarshilShah1804@users.noreply.github.com> Date: Thu, 10 Oct 2024 11:49:54 +0530 Subject: [PATCH 15/20] Update probability/exponential_dist.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- probability/exponential_dist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index f4328f6ae66..c7e0435f8c4 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -12,7 +12,7 @@ */ #include // For std::pow -#include // For asserting the test cases +#include // For assert #include // For I/O operation /** From c6c23dc798df6e68cbc83f2a90bb34a333cd19bc Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Thu, 10 Oct 2024 17:13:35 +0530 Subject: [PATCH 16/20] Added more test, formatted with clang-format --- .clang-tidy | 2 +- probability/exponential_dist.cpp | 119 ++++++++++++++++--------------- 2 files changed, 62 insertions(+), 59 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 99d867ab450..20f2818a3c8 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -2,5 +2,5 @@ Checks: '-*,google-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,cppcoreguidelines-*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-*,openmp-*,performance-*,portability-*,modernize-*,-modernize-use-trailing-*' WarningsAsErrors: '*,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-performance-move-const-arg,-performance-noexcept-move-constructor,-performance-unnecessary-value-param,-cppcoreguidelines-init-variables,-cppcoreguidelines-pro-*,-cppcoreguidelines-owning-memory,-clang-analyzer-cplusplus.Move' HeaderFilterRegex: '' -AnalyzeTemporaryDtors: false +# AnalyzeTemporaryDtors: false FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }' diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index c7e0435f8c4..a259fdd3212 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -1,100 +1,103 @@ /** * @file - * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) - * + * @brief [Exponential + * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) + * * The exponential distribution is used to model * events occuring between a Poisson process like radioactive decay. - * + * * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f] - * + * * Summary of variables used: * \f$\lambda\f$ : rate parameter */ -#include // For std::pow -#include // For assert -#include // For I/O operation +#include // For asserting the test cases +#include // For power function +#include // For I/O operation -/** +/** * @brief the expected value of the exponential distribution * @returns \f[\mu = \frac{1}{\lambda}\f] */ -double exponential_expected(double lambda){ - if (lambda<=0){ - std::cout << "Error: Lambda must be greater than 0." << std::endl; - assert(lambda>0); +double exponential_expected(double lambda) { + if (lambda <= 0) { + std::cout << "Error: Lambda must be greater than 0." << '\n'; + assert(lambda > 0); } - return 1/lambda; + return 1 / lambda; } -/** +/** * @brief the variance of the exponential distribution * @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f] */ -double exponential_var(double lambda){ - if (lambda<=0){ - std::cout << "Error: Lambda must be greater than 0." << std::endl; - assert(lambda>0); +double exponential_var(double lambda) { + if (lambda <= 0) { + std::cout << "Error: Lambda must be greater than 0." << '\n'; + assert(lambda > 0); } - return 1/pow(lambda,2); + return 1 / pow(lambda, 2); } -/** +/** * @brief the standard deviation of the exponential distribution * @returns \f[\sigma = \frac{1}{\lambda}\f] */ -double exponential_std(double lambda){ - if (lambda<=0){ - std::cout << "Error: Lambda must be greater than 0." << std::endl; - assert(lambda>0); +double exponential_std(double lambda) { + if (lambda <= 0) { + std::cout << "Error: Lambda must be greater than 0." << '\n'; + assert(lambda > 0); } - return 1/lambda; + return 1 / lambda; } /** - * @brief Self-test implementation + * @brief Self-test implementations * @returns void */ -static void test(){ - double lambda = 2; - double expected = 0.5; - double var = 0.25; - double std = 0.5; +static void test() { + double lambda_1 = 1; + double expected_1 = 1; + double var_1 = 1; + double std_1 = 1; + + double lambda_2 = 2; + double expected_2 = 0.5; + double var_2 = 0.25; + double std_2 = 0.5; - //Test 1 - std::cout << "Expected Value" << std::endl; - std::cout << "Lambda : " << lambda << std::endl; - std::cout << "Expected Output : " << expected < Date: Thu, 10 Oct 2024 17:14:43 +0530 Subject: [PATCH 17/20] Update files --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 20f2818a3c8..99d867ab450 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -2,5 +2,5 @@ Checks: '-*,google-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,cppcoreguidelines-*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-*,openmp-*,performance-*,portability-*,modernize-*,-modernize-use-trailing-*' WarningsAsErrors: '*,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-performance-move-const-arg,-performance-noexcept-move-constructor,-performance-unnecessary-value-param,-cppcoreguidelines-init-variables,-cppcoreguidelines-pro-*,-cppcoreguidelines-owning-memory,-clang-analyzer-cplusplus.Move' HeaderFilterRegex: '' -# AnalyzeTemporaryDtors: false +AnalyzeTemporaryDtors: false FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }' From 7b916a097833a84779dae13c2c5232a5f50fb43a Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Thu, 10 Oct 2024 17:24:27 +0530 Subject: [PATCH 18/20] Update format issues --- probability/exponential_dist.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index a259fdd3212..d261473e029 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -12,8 +12,8 @@ * \f$\lambda\f$ : rate parameter */ -#include // For asserting the test cases -#include // For power function +#include // For assert +#include // For std::pow #include // For I/O operation /** From 59e39433c7204a9af20e8d649798f8c98f186680 Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Thu, 10 Oct 2024 19:39:48 +0530 Subject: [PATCH 19/20] Add more tests --- probability/exponential_dist.cpp | 42 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index d261473e029..c9150f21c6d 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -12,9 +12,11 @@ * \f$\lambda\f$ : rate parameter */ -#include // For assert -#include // For std::pow -#include // For I/O operation +#include // For assert +#include // For std::pow +#include // For I/O operation +#include // For std::invalid_argument +#include // For std::string /** * @brief the expected value of the exponential distribution @@ -22,8 +24,7 @@ */ double exponential_expected(double lambda) { if (lambda <= 0) { - std::cout << "Error: Lambda must be greater than 0." << '\n'; - assert(lambda > 0); + throw std::invalid_argument("lambda must be greater than 0"); } return 1 / lambda; } @@ -34,8 +35,7 @@ double exponential_expected(double lambda) { */ double exponential_var(double lambda) { if (lambda <= 0) { - std::cout << "Error: Lambda must be greater than 0." << '\n'; - assert(lambda > 0); + throw std::invalid_argument("lambda must be greater than 0"); } return 1 / pow(lambda, 2); } @@ -46,8 +46,7 @@ double exponential_var(double lambda) { */ double exponential_std(double lambda) { if (lambda <= 0) { - std::cout << "Error: Lambda must be greater than 0." << '\n'; - assert(lambda > 0); + throw std::invalid_argument("lambda must be greater than 0"); } return 1 / lambda; } @@ -72,6 +71,9 @@ static void test() { double var_3 = 0.111111; double std_3 = 0.333333; + double lambda_4 = 0; // Test 0 + double lambda_5 = -2.3; // Test negative value + const float threshold = 1e-3f; std::cout << "Test for lambda = 1 \n"; @@ -90,7 +92,27 @@ static void test() { assert(std::abs(expected_3 - exponential_expected(lambda_3)) < threshold); assert(std::abs(var_3 - exponential_var(lambda_3)) < threshold); assert(std::abs(std_3 - exponential_std(lambda_3)) < threshold); - std::cout << "ALL TEST PASSED\n"; + std::cout << "ALL TEST PASSED\n\n"; + + std::cout << "Test for lambda = 0 \n"; + try { + exponential_expected(lambda_4); + exponential_var(lambda_4); + exponential_std(lambda_4); + } catch (std::invalid_argument& err) { + assert(std::string(err.what()) == "lambda must be greater than 0"); + } + std::cout << "ALL TEST PASSED\n\n"; + + std::cout << "Test for lambda = -2.3 \n"; + try { + exponential_expected(lambda_5); + exponential_var(lambda_5); + exponential_std(lambda_5); + } catch (std::invalid_argument& err) { + assert(std::string(err.what()) == "lambda must be greater than 0"); + } + std::cout << "ALL TEST PASSED\n\n"; } /** From d46d760216ec3629c7fc25ba1db190a7b6eeccb5 Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Fri, 11 Oct 2024 11:47:24 +0530 Subject: [PATCH 20/20] Added namespaces --- probability/exponential_dist.cpp | 56 +++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/probability/exponential_dist.cpp b/probability/exponential_dist.cpp index c9150f21c6d..ef6d396d58c 100644 --- a/probability/exponential_dist.cpp +++ b/probability/exponential_dist.cpp @@ -18,6 +18,18 @@ #include // For std::invalid_argument #include // For std::string +/** + * @namespace probability + * @brief Probability algorithms + */ +namespace probability { +/** + * @namespace exponential_dist + * @brief Functions for the [Exponential + * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) + * algorithm implementation + */ +namespace geometric_dist { /** * @brief the expected value of the exponential distribution * @returns \f[\mu = \frac{1}{\lambda}\f] @@ -50,6 +62,8 @@ double exponential_std(double lambda) { } return 1 / lambda; } +} // namespace geometric_dist +} // namespace probability /** * @brief Self-test implementations @@ -77,28 +91,40 @@ static void test() { const float threshold = 1e-3f; std::cout << "Test for lambda = 1 \n"; - assert(std::abs(expected_1 - exponential_expected(lambda_1)) < threshold); - assert(std::abs(var_1 - exponential_var(lambda_1)) < threshold); - assert(std::abs(std_1 - exponential_std(lambda_1)) < threshold); + assert( + std::abs(expected_1 - probability::geometric_dist::exponential_expected( + lambda_1)) < threshold); + assert(std::abs(var_1 - probability::geometric_dist::exponential_var( + lambda_1)) < threshold); + assert(std::abs(std_1 - probability::geometric_dist::exponential_std( + lambda_1)) < threshold); std::cout << "ALL TEST PASSED\n\n"; std::cout << "Test for lambda = 2 \n"; - assert(std::abs(expected_2 - exponential_expected(lambda_2)) < threshold); - assert(std::abs(var_2 - exponential_var(lambda_2)) < threshold); - assert(std::abs(std_2 - exponential_std(lambda_2)) < threshold); + assert( + std::abs(expected_2 - probability::geometric_dist::exponential_expected( + lambda_2)) < threshold); + assert(std::abs(var_2 - probability::geometric_dist::exponential_var( + lambda_2)) < threshold); + assert(std::abs(std_2 - probability::geometric_dist::exponential_std( + lambda_2)) < threshold); std::cout << "ALL TEST PASSED\n\n"; std::cout << "Test for lambda = 3 \n"; - assert(std::abs(expected_3 - exponential_expected(lambda_3)) < threshold); - assert(std::abs(var_3 - exponential_var(lambda_3)) < threshold); - assert(std::abs(std_3 - exponential_std(lambda_3)) < threshold); + assert( + std::abs(expected_3 - probability::geometric_dist::exponential_expected( + lambda_3)) < threshold); + assert(std::abs(var_3 - probability::geometric_dist::exponential_var( + lambda_3)) < threshold); + assert(std::abs(std_3 - probability::geometric_dist::exponential_std( + lambda_3)) < threshold); std::cout << "ALL TEST PASSED\n\n"; std::cout << "Test for lambda = 0 \n"; try { - exponential_expected(lambda_4); - exponential_var(lambda_4); - exponential_std(lambda_4); + probability::geometric_dist::exponential_expected(lambda_4); + probability::geometric_dist::exponential_var(lambda_4); + probability::geometric_dist::exponential_std(lambda_4); } catch (std::invalid_argument& err) { assert(std::string(err.what()) == "lambda must be greater than 0"); } @@ -106,9 +132,9 @@ static void test() { std::cout << "Test for lambda = -2.3 \n"; try { - exponential_expected(lambda_5); - exponential_var(lambda_5); - exponential_std(lambda_5); + probability::geometric_dist::exponential_expected(lambda_5); + probability::geometric_dist::exponential_var(lambda_5); + probability::geometric_dist::exponential_std(lambda_5); } catch (std::invalid_argument& err) { assert(std::string(err.what()) == "lambda must be greater than 0"); }