From 64f07fc21a985c403b5295f5bef0ae5401c9efbd Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 21:43:52 -0400 Subject: [PATCH 01/33] initial cmake --- CMakeLists.txt | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..f98109382 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.3) +project(Algorithms_in_C + LANGUAGES C CXX + VERSION 1.0.0 + DESCRIPTION "Set of algorithms implemented in C." +) + +option(USE_OPENMP "flag to use OpenMP for multithreading" ON) + +cmake_policy(SET CMP0054 NEW) +cmake_policy(SET CMP0057 NEW) +find_package(Doxygen OPTIONAL_COMPONENTS dot dia) +if(DOXYGEN_FOUND) + set(DOXYGEN_GENERATE_MAN NO) + set(DOXYGEN_USE_MATHJAX YES) + set(DOXYGEN_GENERATE_HTML YES) + set(DOXYGEN_INLINE_SOURCES YES) + set(DOXYGEN_CREATE_SUBDIRS YES) + set(DOCYGEN_GENERATE_TREEVIEW YES) + set(DOXYGEN_STRIP_CODE_COMMENTS NO) + set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES) + set(DOXYGEN_MATHJAX_RELPATH "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML") + if(Doxygen_dot_FOUND) + set(DOXYGEN_HAVE_DOT YES) + set(DOXYGEN_CALL_GRAPH YES) + set(DOXYGEN_INTERACTIVE_SVG YES) + set(DOXYGEN_DOT_IMAGE_FORMAT "svg") + endif() + + doxygen_add_docs( + doc + ${PROJECT_SOURCE_DIR} + COMMENT "Generate documentation" + ) +endif() + +function(function_timer) + set(CMAKE_BUILD_TYPE "Release") # This is local to function + add_subdirectory(function_timer EXCLUDE_FROM_ALL) +endfunction() + +function_timer() + +include_directories(function_timer/include) +# link_libraries(function_timer) +# include_directories(${CMAKE_BINARY_DIR}/include) +# link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +if(MSVC) + add_compile_definitions(_CRT_SECURE_NO_WARNINGS) + add_compile_options(/Za) +endif(MSVC) + +add_subdirectory(conversions) +add_subdirectory(misc) +add_subdirectory(project_euler) +add_subdirectory(sorting) +add_subdirectory(searching) +add_subdirectory(numerical_methods) + +if(USE_OPENMP) + find_package(OpenMP) + if (OpenMP_C_FOUND) + message(STATUS "Building with OpenMP Multithreading.") + else() + message(STATUS "No OpenMP found, no multithreading.") + endif() +endif() + +set(CPACK_PROJECT_NAME ${PROJECT_NAME}) +set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) +include(CPack) From 0e3a57ab3fdf9bf11a4dd9c3b575656ba50decc3 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:08 -0400 Subject: [PATCH 02/33] ignore build directory --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ed3934e45..12318c6cd 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ a.out *.out *.app + +build/ \ No newline at end of file From 412d3ff1a2fc491e4c8e0b05db814c186bdf7dc4 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:39 -0400 Subject: [PATCH 03/33] file rename to standards --- math/{primes_up_to_10^8.cpp => primes_up_to_billion.cpp} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename math/{primes_up_to_10^8.cpp => primes_up_to_billion.cpp} (79%) diff --git a/math/primes_up_to_10^8.cpp b/math/primes_up_to_billion.cpp similarity index 79% rename from math/primes_up_to_10^8.cpp rename to math/primes_up_to_billion.cpp index db9b56cab..d8f5a9f9d 100644 --- a/math/primes_up_to_10^8.cpp +++ b/math/primes_up_to_billion.cpp @@ -1,12 +1,12 @@ -#include #include +#include char prime[100000000]; void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index - prime[0] = '0'; // 0 is not prime - prime[1] = '0'; // 1 is not prime + prime[0] = '0'; // 0 is not prime + prime[1] = '0'; // 1 is not prime for (int p = 2; p * p <= n; p++) { if (prime[p] == '1') { for (int i = p * p; i <= n; i += p) @@ -15,7 +15,6 @@ void Sieve(int64_t n) { } } - int main() { Sieve(100000000); int64_t n; From 260ba8d3a46e76b8d319c96b23983c9c2d93d046 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:52 -0400 Subject: [PATCH 04/33] syntax correction --- math/fibonacci.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 1c07cd93f..e82875da3 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -1,5 +1,5 @@ -#include #include +#include /* Calculate the the value on Fibonacci's sequence given an integer as input @@ -7,14 +7,13 @@ Fibonacci = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... */ -int fibonacci(uint n) { +int fibonacci(unsigned int n) { /* If the input is 0 or 1 just return the same This will set the first 2 values of the sequence */ - if (n <= 1) - return n; + if (n <= 1) return n; /* Add the last 2 values of the sequence to get next */ - return fibonacci(n-1) + fibonacci(n-2); + return fibonacci(n - 1) + fibonacci(n - 2); } int main() { From a1663277c76e8d814a745a596f864c2e1fdd9080 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:51:18 -0400 Subject: [PATCH 05/33] cherry pick changes from "cmake branch" From 8207c776ebdbd3a77018651223e37780a151cc2a Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:52 -0400 Subject: [PATCH 06/33] syntax correction (cherry picked from commit 260ba8d3a46e76b8d319c96b23983c9c2d93d046) --- math/fibonacci.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 1c07cd93f..e82875da3 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -1,5 +1,5 @@ -#include #include +#include /* Calculate the the value on Fibonacci's sequence given an integer as input @@ -7,14 +7,13 @@ Fibonacci = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... */ -int fibonacci(uint n) { +int fibonacci(unsigned int n) { /* If the input is 0 or 1 just return the same This will set the first 2 values of the sequence */ - if (n <= 1) - return n; + if (n <= 1) return n; /* Add the last 2 values of the sequence to get next */ - return fibonacci(n-1) + fibonacci(n-2); + return fibonacci(n - 1) + fibonacci(n - 2); } int main() { From b36322c629809375de3abc30d48479efa4acddbf Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:39 -0400 Subject: [PATCH 07/33] file rename to standards (cherry picked from commit 412d3ff1a2fc491e4c8e0b05db814c186bdf7dc4) --- math/{primes_up_to_10^8.cpp => primes_up_to_billion.cpp} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename math/{primes_up_to_10^8.cpp => primes_up_to_billion.cpp} (79%) diff --git a/math/primes_up_to_10^8.cpp b/math/primes_up_to_billion.cpp similarity index 79% rename from math/primes_up_to_10^8.cpp rename to math/primes_up_to_billion.cpp index db9b56cab..d8f5a9f9d 100644 --- a/math/primes_up_to_10^8.cpp +++ b/math/primes_up_to_billion.cpp @@ -1,12 +1,12 @@ -#include #include +#include char prime[100000000]; void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index - prime[0] = '0'; // 0 is not prime - prime[1] = '0'; // 1 is not prime + prime[0] = '0'; // 0 is not prime + prime[1] = '0'; // 1 is not prime for (int p = 2; p * p <= n; p++) { if (prime[p] == '1') { for (int i = p * p; i <= n; i += p) @@ -15,7 +15,6 @@ void Sieve(int64_t n) { } } - int main() { Sieve(100000000); int64_t n; From 1e5b9f842da6d3d25ef608f5fbdaf54f38631db8 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:08 -0400 Subject: [PATCH 08/33] ignore build directory (cherry picked from commit 0e3a57ab3fdf9bf11a4dd9c3b575656ba50decc3) --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ed3934e45..12318c6cd 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ a.out *.out *.app + +build/ \ No newline at end of file From baea3b07c1588361ed1a514d39a2979c56ff4a6a Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:56:38 -0400 Subject: [PATCH 09/33] major corrections in the files - bad CPPLINT and non-compilable codes --- .../Bisection_method.CPP | 75 ++++++++----------- .../Newton_Raphson.CPP | 53 ------------- .../Secant_method.CPP | 70 +++++++---------- .../newton_raphson_method.cpp | 41 ++++++++++ .../successive_approximation.CPP | 63 +++++++--------- math/prime_factorization.cpp | 46 +++++------- 6 files changed, 145 insertions(+), 203 deletions(-) delete mode 100644 computer_oriented_statistical_methods/Newton_Raphson.CPP create mode 100644 computer_oriented_statistical_methods/newton_raphson_method.cpp diff --git a/computer_oriented_statistical_methods/Bisection_method.CPP b/computer_oriented_statistical_methods/Bisection_method.CPP index 717987321..266083de1 100644 --- a/computer_oriented_statistical_methods/Bisection_method.CPP +++ b/computer_oriented_statistical_methods/Bisection_method.CPP @@ -1,53 +1,40 @@ -#include -#include -#include +#include +#include -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation +float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation } -void main() -{ - float a, b, x, z; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } +int main() { + float a, b, x, z; -START: + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - x = (a + b) / 2; - z = eq(x); - cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]"; + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + x = (a + b) / 2; + z = eq(x); + std::cout << "\n\nz: " << z << "\t[" << a << " , " << b + << " | Bisect: " << x << "]"; - if (z < 0) - { - a = x; - } - else - { - b = x; - } + if (z < 0) { + a = x; + } else { + b = x; + } - if (z > 0 && z < 0.0009) // stoping criteria - { - goto END; - } - } + if (z > 0 && z < 0.0009) // stoping criteria + break; + } -END: - cout << "\n\nRoot: " << x; - getch(); + std::cout << "\n\nRoot: " << x; + return 0; } diff --git a/computer_oriented_statistical_methods/Newton_Raphson.CPP b/computer_oriented_statistical_methods/Newton_Raphson.CPP deleted file mode 100644 index 183a78daf..000000000 --- a/computer_oriented_statistical_methods/Newton_Raphson.CPP +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation -} -float eq_der(float i) -{ - return ((3 * pow(i, 2)) - 4); // derivative of equation -} - -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } - -START: - - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - c = (a + b) / 2; - - for (i = 0; i < 100; i++) - { - float h; - m = eq(c); - n = eq_der(c); - - z = c - (m / n); - c = z; - - if (m > 0 && m < 0.009) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << z; - getch(); -} diff --git a/computer_oriented_statistical_methods/Secant_method.CPP b/computer_oriented_statistical_methods/Secant_method.CPP index b897e9184..bd805ed36 100644 --- a/computer_oriented_statistical_methods/Secant_method.CPP +++ b/computer_oriented_statistical_methods/Secant_method.CPP @@ -1,49 +1,37 @@ -#include -#include -#include +#include +#include -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation +float eq(float i) { + return (pow(i, 3) - (4 * i) - 9); // original equation } -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } +int main() { + float a, b, z, c, m, n; + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } -START: + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + float h, d; + m = eq(a); + n = eq(b); - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - float h, d; - m = eq(a); - n = eq(b); + c = ((a * n) - (b * m)) / (n - m); + a = b; + b = c; - c = ((a * n) - (b * m)) / (n - m); - a = b; - b = c; + z = eq(c); + if (z > 0 && z < 0.09) // stoping criteria + break; + } - z = eq(c); - if (z > 0 && z < 0.09) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << c; - getch(); + std::cout << "\n\nRoot: " << c; + return 0; } diff --git a/computer_oriented_statistical_methods/newton_raphson_method.cpp b/computer_oriented_statistical_methods/newton_raphson_method.cpp new file mode 100644 index 000000000..a2a57768e --- /dev/null +++ b/computer_oriented_statistical_methods/newton_raphson_method.cpp @@ -0,0 +1,41 @@ +#include +#include + +float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation +} +float eq_der(float i) { + return ((3 * std::pow(i, 2)) - 4); // derivative of equation +} + +int main() { + float a, b, z, c, m, n; + + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } + + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + c = (a + b) / 2; + + for (int i = 0; i < 100; i++) { + float h; + m = eq(c); + n = eq_der(c); + + z = c - (m / n); + c = z; + + if (m > 0 && m < 0.009) // stoping criteria + break; + } + + std::cout << "\n\nRoot: " << z << std::endl; + return 0; +} diff --git a/computer_oriented_statistical_methods/successive_approximation.CPP b/computer_oriented_statistical_methods/successive_approximation.CPP index b42ab8f8c..85c42c9fb 100644 --- a/computer_oriented_statistical_methods/successive_approximation.CPP +++ b/computer_oriented_statistical_methods/successive_approximation.CPP @@ -1,37 +1,28 @@ -#include -#include -#include -float eq(float y) -{ - return ((3 * y) - (cos(y)) - 2); + +#include +#include + +float eq(float y) { return ((3 * y) - (cos(y)) - 2); } +float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } + +int main() { + float y, x1, x2, x3, sum, s, a, f1, f2, gd; + int i, n; + + for (i = 0; i < 10; i++) { + sum = eq(y); + std::cout << "value of equation at " << i << " " << sum << "\n"; + y++; + } + std::cout << "enter the x1->"; + std::cin >> x1; + std::cout << "enter the no iteration to perform->\n"; + std::cin >> n; + + for (i = 0; i <= n; i++) { + x2 = eqd(x1); + std::cout << "\nenter the x2->" << x2; + x1 = x2; + } + return 0; } -float eqd(float y) -{ - return ((0.5) * ((cos(y)) + 2)); -} - -void main() -{ - float y, x1, x2, x3, sum, s, a, f1, f2, gd; - int i, n; - - clrscr(); - for (i = 0; i < 10; i++) - { - sum = eq(y); - cout << "value of equation at " << i << " " << sum << "\n"; - y++; - } - cout << "enter the x1->"; - cin >> x1; - cout << "enter the no iteration to perform->\n"; - cin >> n; - - for (i = 0; i <= n; i++) - { - x2 = eqd(x1); - cout << "\nenter the x2->" << x2; - x1 = x2; - } - getch(); -} \ No newline at end of file diff --git a/math/prime_factorization.cpp b/math/prime_factorization.cpp index 822cad332..bd1b99d97 100644 --- a/math/prime_factorization.cpp +++ b/math/prime_factorization.cpp @@ -1,67 +1,56 @@ +#include +#include #include #include -#include using namespace std; -// Declaring variables for maintaing prime numbers and to check whether a number is prime or not +// Declaring variables for maintaing prime numbers and to check whether a number +// is prime or not bool isprime[1000006]; vector prime_numbers; vector> factors; // Calculating prime number upto a given range -void SieveOfEratosthenes(int N) -{ +void SieveOfEratosthenes(int N) { // initializes the array isprime memset(isprime, true, sizeof isprime); - for (int i = 2; i <= N; i++) - { - if (isprime[i]) - { - for (int j = 2 * i; j <= N; j += i) - isprime[j] = false; + for (int i = 2; i <= N; i++) { + if (isprime[i]) { + for (int j = 2 * i; j <= N; j += i) isprime[j] = false; } } - for (int i = 2; i <= N; i++) - { - if (isprime[i]) - prime_numbers.push_back(i); + for (int i = 2; i <= N; i++) { + if (isprime[i]) prime_numbers.push_back(i); } } // Prime factorization of a number -void prime_factorization(int num) -{ - +void prime_factorization(int num) { int number = num; - for (int i = 0; prime_numbers[i] <= num; i++) - { + for (int i = 0; prime_numbers[i] <= num; i++) { int count = 0; // termination condition - if (number == 1) - { + if (number == 1) { break; } - while (number % prime_numbers[i] == 0) - { + while (number % prime_numbers[i] == 0) { count++; number = number / prime_numbers[i]; } - if (count) - factors.push_back(make_pair(prime_numbers[i], count)); + if (count) factors.push_back(make_pair(prime_numbers[i], count)); } } /* I added a simple UI. */ -int main() -{ +int main() { int num; cout << "\t\tComputes the prime factorization\n\n"; cout << "Type in a number: "; @@ -72,8 +61,7 @@ int main() prime_factorization(num); // Prime factors with their powers in the given number in new line - for (auto it : factors) - { + for (auto it : factors) { cout << it.first << " " << it.second << endl; } From ef24ae3a09a0db7a8988ddf9fcaff985e2d7d43f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 02:57:04 +0000 Subject: [PATCH 10/33] updating DIRECTORY.md --- DIRECTORY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e36309dc..17cd5f5c6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -13,7 +13,7 @@ * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Bisection_method.CPP) * [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) - * [Newton Raphson](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Newton_Raphson.CPP) + * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp) * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Secant_method.CPP) * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.CPP) @@ -116,7 +116,7 @@ * [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp) * [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp) * [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_numbers.cpp) - * [Primes Up To 10^8](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_10^8.cpp) + * [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_billion.cpp) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sieve_of_eratosthenes.cpp) * [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sqrt_double.cpp) From 4e8a2f715197b92879cf599d9073578b940ab9af Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:12:12 -0400 Subject: [PATCH 11/33] non compiling and illegible code removed --- others/Strassen Matrix Multiplication.cpp | 56 ----------------------- 1 file changed, 56 deletions(-) delete mode 100644 others/Strassen Matrix Multiplication.cpp diff --git a/others/Strassen Matrix Multiplication.cpp b/others/Strassen Matrix Multiplication.cpp deleted file mode 100644 index 85b627763..000000000 --- a/others/Strassen Matrix Multiplication.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -using namespace std; - -Multiply(int A[][], int B[][], int n) -{ - if (n == 2) - { - int p1 = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]); - int p2 = (a[1][0] + a[1][1]) * b[0][0]; - int p3 = a[0][0] * (b[0][1] - b[1][1]); - int p4 = a[1][1] * (b[1][0] - b[0][0]); - int p5 = (a[0][0] + a[0][1]) * b[1][1]; - int p6 = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]); - int p7 = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]); - - int c[n][n]; - c[0][0] = p1 + p4 - p5 + p7; - c[0][1] = p3 + p5; - c[1][0] = p2 + p4; - c[1][1] = p1 - p2 + p3 + p6; - - return c[][]; - } - else - { - } -} - -int main() -{ - int p, q, r, s; - cout << "Enter the dimensions of Matrices"; - cin >> n; - int A[n][n], ; - int B[n][n], ; - cout << "Enter the elements of Matrix A"; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - cin >> A[i][j]; - } - } - - cout << "Enter the elements of Matrix B"; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - cin >> B[i][j]; - } - } - - Multiply(A, B, n); - return 0; -} \ No newline at end of file From 120fe06fcb0d75c944c9453d703a20ce91e46e80 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:13:26 -0400 Subject: [PATCH 12/33] files renamed to standard - without spaces and made CPPLINT compatible --- others/GCD_of_n_numbers.cpp | 41 ++++--- ...al To Binary.cpp => decimal_to_binary.cpp} | 0 ...ecimal .cpp => decimal_to_hexadecimal.cpp} | 0 ...meral.cpp => decimal_to_roman_numeral.cpp} | 0 others/fibonacci.cpp | 42 ------- others/fibonacci_fast.cpp | 37 ++++++ others/matrix_exponentiation.cpp | 14 +-- ... Matching.cpp => paranthesis_matching.cpp} | 0 others/pascal_triangle.cpp | 96 +++++++--------- ...{Primality Test.cpp => primality_test.cpp} | 0 .../{Sparse matrix.cpp => sparse_matrix.cpp} | 0 ...ing Fibonacci.cpp => string_fibonacci.cpp} | 0 ...{Tower of Hanoi.cpp => tower_of_hanoi.cpp} | 0 sorting/shell_sort2.cpp | 105 ++++++++++++++++++ 14 files changed, 211 insertions(+), 124 deletions(-) rename others/{Decimal To Binary.cpp => decimal_to_binary.cpp} (100%) rename others/{Decimal To Hexadecimal .cpp => decimal_to_hexadecimal.cpp} (100%) rename others/{Decimal to Roman Numeral.cpp => decimal_to_roman_numeral.cpp} (100%) delete mode 100644 others/fibonacci.cpp create mode 100644 others/fibonacci_fast.cpp rename others/{Paranthesis Matching.cpp => paranthesis_matching.cpp} (100%) rename others/{Primality Test.cpp => primality_test.cpp} (100%) rename others/{Sparse matrix.cpp => sparse_matrix.cpp} (100%) rename others/{String Fibonacci.cpp => string_fibonacci.cpp} (100%) rename others/{Tower of Hanoi.cpp => tower_of_hanoi.cpp} (100%) create mode 100644 sorting/shell_sort2.cpp diff --git a/others/GCD_of_n_numbers.cpp b/others/GCD_of_n_numbers.cpp index 8158052f8..b592e5931 100644 --- a/others/GCD_of_n_numbers.cpp +++ b/others/GCD_of_n_numbers.cpp @@ -1,23 +1,22 @@ -//This program aims at calculating the GCD of n numbers by division method +// This program aims at calculating the GCD of n numbers by division method #include -using namepsace std; -int main() -{ - cout << "Enter value of n:" << endl; - cin >> n; - int a[n]; - int i, j, gcd; - cout << "Enter the n numbers:" << endl; - for (i = 0; i < n; i++) - cin >> a[i]; - j = 1; //to access all elements of the array starting from 1 - gcd = a[0]; - while (j < n) - { - if (a[j] % gcd == 0) //value of gcd is as needed so far - j++; //so we check for next element - else - gcd = a[j] % gcd; //calculating GCD by division method - } - cout << "GCD of entered n numbers:" << gcd; + +int main() { + int n; + std::cout << "Enter value of n:" << std::endl; + std::cin >> n; + int a[n]; + int i, j, gcd; + std::cout << "Enter the n numbers:" << std::endl; + for (i = 0; i < n; i++) std::cin >> a[i]; + j = 1; // to access all elements of the array starting from 1 + gcd = a[0]; + while (j < n) { + if (a[j] % gcd == 0) // value of gcd is as needed so far + j++; // so we check for next element + else + gcd = a[j] % gcd; // calculating GCD by division method + } + std::cout << "GCD of entered n numbers:" << gcd; + return 0; } diff --git a/others/Decimal To Binary.cpp b/others/decimal_to_binary.cpp similarity index 100% rename from others/Decimal To Binary.cpp rename to others/decimal_to_binary.cpp diff --git a/others/Decimal To Hexadecimal .cpp b/others/decimal_to_hexadecimal.cpp similarity index 100% rename from others/Decimal To Hexadecimal .cpp rename to others/decimal_to_hexadecimal.cpp diff --git a/others/Decimal to Roman Numeral.cpp b/others/decimal_to_roman_numeral.cpp similarity index 100% rename from others/Decimal to Roman Numeral.cpp rename to others/decimal_to_roman_numeral.cpp diff --git a/others/fibonacci.cpp b/others/fibonacci.cpp deleted file mode 100644 index 87ccda6d3..000000000 --- a/others/fibonacci.cpp +++ /dev/null @@ -1,42 +0,0 @@ -//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation -//This works by using both recursion and dynamic programming. -//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci -//we can use it for 10000th fibonacci etc, if we implement bigintegers. -//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci -//It is a property of fibonacci similar to matrix exponentiation. - -#include -#include -using namespace std; - -const long long MAX = 93; - -long long f[MAX] = {0}; - -long long fib(long long n) -{ - - if (n == 0) - return 0; - if (n == 1 || n == 2) - return (f[n] = 1); - - if (f[n]) - return f[n]; - - long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; - - f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) - : (2 * fib(k - 1) + fib(k)) * fib(k); - return f[n]; -} - -int main() -{ - //Main Function - for (long long i = 1; i < 93; i++) - { - cout << i << " th fibonacci number is " << fib(i) << "\n"; - } - return 0; -} diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp new file mode 100644 index 000000000..dd3481422 --- /dev/null +++ b/others/fibonacci_fast.cpp @@ -0,0 +1,37 @@ +// An efficient way to calculate nth fibonacci number faster and simpler than +// O(nlogn) method of matrix exponentiation This works by using both recursion +// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be +// stored in a single long long variable, we can only use it till 92nd fibonacci +// we can use it for 10000th fibonacci etc, if we implement bigintegers. +// This algorithm works with the fact that nth fibonacci can easily found if we +// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci +// similar to matrix exponentiation. + +#include +#include +using namespace std; + +const long long MAX = 93; + +long long f[MAX] = {0}; + +long long fib(long long n) { + if (n == 0) return 0; + if (n == 1 || n == 2) return (f[n] = 1); + + if (f[n]) return f[n]; + + long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; + + f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) + : (2 * fib(k - 1) + fib(k)) * fib(k); + return f[n]; +} + +int main() { + // Main Function + for (long long i = 1; i < 93; i++) { + cout << i << " th fibonacci number is " << fib(i) << "\n"; + } + return 0; +} diff --git a/others/matrix_exponentiation.cpp b/others/matrix_exponentiation.cpp index 25c411dda..f9b4997e9 100644 --- a/others/matrix_exponentiation.cpp +++ b/others/matrix_exponentiation.cpp @@ -19,6 +19,8 @@ The first element of this matrix is the required result. */ #include +#include + using std::cin; using std::cout; using std::vector; @@ -46,8 +48,7 @@ vector> multiply(vector> A, vector> B) { // computing power of a matrix vector> power(vector> A, ll p) { - if (p == 1) - return A; + if (p == 1) return A; if (p % 2 == 1) { return multiply(A, power(A, p - 1)); } else { @@ -58,14 +59,11 @@ vector> power(vector> A, ll p) { // main function ll ans(ll n) { - if (n == 0) - return 0; - if (n <= k) - return b[n - 1]; + if (n == 0) return 0; + if (n <= k) return b[n - 1]; // F1 vector F1(k + 1); - for (ll i = 1; i <= k; i++) - F1[i] = b[i - 1]; + for (ll i = 1; i <= k; i++) F1[i] = b[i - 1]; // Transpose matrix vector> T(k + 1, vector(k + 1)); diff --git a/others/Paranthesis Matching.cpp b/others/paranthesis_matching.cpp similarity index 100% rename from others/Paranthesis Matching.cpp rename to others/paranthesis_matching.cpp diff --git a/others/pascal_triangle.cpp b/others/pascal_triangle.cpp index 101100018..063a6666a 100644 --- a/others/pascal_triangle.cpp +++ b/others/pascal_triangle.cpp @@ -1,63 +1,53 @@ -#include +#include +#include -using namespace std; - -void show_pascal(int **arr, int n) -{ - //pint Pascal's Triangle - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n + i; ++j) - { - if (arr[i][j] == 0) - cout << " "; - else - cout << arr[i][j]; - } - cout << endl; - } +void show_pascal(int **arr, int n) { + // pint Pascal's Triangle + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n + i; ++j) { + if (arr[i][j] == 0) + std::cout << " "; + else + std::cout << arr[i][j]; + } + std::cout << std::endl; + } } -int **pascal_triangle(int **arr, int n) -{ - for (int i = 0; i < n; ++i) - { - for (int j = n - i - 1; j < n + i; ++j) - { - if (j == n - i - 1 || j == n + i - 1) - arr[i][j] = 1; //The edge of the Pascal triangle goes in 1 - else - arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; - } - } +int **pascal_triangle(int **arr, int n) { + for (int i = 0; i < n; ++i) { + for (int j = n - i - 1; j < n + i; ++j) { + if (j == n - i - 1 || j == n + i - 1) + arr[i][j] = 1; // The edge of the Pascal triangle goes in 1 + else + arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; + } + } - return arr; + return arr; } -int main() -{ - int n = 0; +int main() { + int n = 0; - cout << "Set Pascal's Triangle Height" << endl; - cin >> n; - - //memory allocation (Assign two-dimensional array to store Pascal triangle) - int **arr = new int*[n]; - for (int i = 0; i < n; ++i) - { - arr[i] = new int[2 * n - 1]; - memset(arr[i], 0, sizeof(int)*(2 * n - 1)); - } - - pascal_triangle(arr, n); - show_pascal(arr, n); + std::cout << "Set Pascal's Triangle Height" << std::endl; + std::cin >> n; - //deallocation - for (int i = 0; i < n; ++i) - { - delete[] arr[i]; - } - delete[] arr; + // memory allocation (Assign two-dimensional array to store Pascal triangle) + int **arr = new int *[n]; + for (int i = 0; i < n; ++i) { + arr[i] = new int[2 * n - 1]; + memset(arr[i], 0, sizeof(int) * (2 * n - 1)); + } - return 0; + pascal_triangle(arr, n); + show_pascal(arr, n); + + // deallocation + for (int i = 0; i < n; ++i) { + delete[] arr[i]; + } + delete[] arr; + + return 0; } diff --git a/others/Primality Test.cpp b/others/primality_test.cpp similarity index 100% rename from others/Primality Test.cpp rename to others/primality_test.cpp diff --git a/others/Sparse matrix.cpp b/others/sparse_matrix.cpp similarity index 100% rename from others/Sparse matrix.cpp rename to others/sparse_matrix.cpp diff --git a/others/String Fibonacci.cpp b/others/string_fibonacci.cpp similarity index 100% rename from others/String Fibonacci.cpp rename to others/string_fibonacci.cpp diff --git a/others/Tower of Hanoi.cpp b/others/tower_of_hanoi.cpp similarity index 100% rename from others/Tower of Hanoi.cpp rename to others/tower_of_hanoi.cpp diff --git a/sorting/shell_sort2.cpp b/sorting/shell_sort2.cpp new file mode 100644 index 000000000..1268c7a50 --- /dev/null +++ b/sorting/shell_sort2.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +// for std::swap +#include + +template void show_data(T *arr, size_t LEN) { + size_t i; + + for (i = 0; i < LEN; i++) + std::cout << arr[i] << ", "; + std::cout << std::endl; +} + +template void show_data(T (&arr)[N]) { show_data(arr, N); } + +/** + * Optimized algorithm - takes half the time by utilizing + * Mar + **/ +template void shell_sort(T *arr, size_t LEN) { + const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1}; + const unsigned int gap_len = 8; + size_t i, j, g; + + for (g = 0; g < gap_len; g++) { + unsigned int gap = gaps[g]; + for (i = gap; i < LEN; i++) { + T tmp = arr[i]; + + for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) + arr[j] = arr[j - gap]; + + arr[j] = tmp; + } + } +} + +template void shell_sort(T (&arr)[N]) { + shell_sort(arr, N); +} + +/** + * function to compare sorting using cstdlib's qsort + **/ +int compare(const void *a, const void *b) { + int arg1 = *static_cast(a); + int arg2 = *static_cast(b); + + if (arg1 < arg2) + return -1; + if (arg1 > arg2) + return 1; + return 0; + + // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut + // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) +} + +int main(int argc, char *argv[]) { + int i, NUM_DATA; + + if (argc == 2) + NUM_DATA = atoi(argv[1]); + else + NUM_DATA = 200; + + // int array = new int[NUM_DATA]; + int *data = new int[NUM_DATA]; + int *data2 = new int[NUM_DATA]; + // int array2 = new int[NUM_DATA]; + int range = 1800; + + std::srand(time(NULL)); + for (i = 0; i < NUM_DATA; i++) + data[i] = data2[i] = (std::rand() % range) - (range >> 1); + + std::cout << "Unsorted original data: " << std::endl; + show_data(data, NUM_DATA); + std::clock_t start = std::clock(); + shell_sort(data, NUM_DATA); + std::clock_t end = std::clock(); + + std::cout << std::endl + << "Data Sorted using custom implementation: " << std::endl; + show_data(data, NUM_DATA); + + double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + start = std::clock(); + qsort(data2, NUM_DATA, sizeof(data2[0]), compare); + end = std::clock(); + std::cout << "Data Sorted using cstdlib qsort: " << std::endl; + show_data(data2, NUM_DATA); + + elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + free(data); + free(data2); + return 0; +} From 15d481f8ca7e7a33b2d567108ea58252ec2318fe Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 03:13:48 +0000 Subject: [PATCH 13/33] updating DIRECTORY.md --- DIRECTORY.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 17cd5f5c6..644c57056 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -133,27 +133,26 @@ ## Others * [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Buzz_number.cpp) - * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Binary.cpp) - * [Decimal To Hexadecimal ](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Hexadecimal%20.cpp) - * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20to%20Roman%20Numeral.cpp) + * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_binary.cpp) + * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_hexadecimal.cpp) + * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_roman_numeral.cpp) * [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp) - * [Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci.cpp) + * [Fibonacci Fast](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci_fast.cpp) * [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/GCD_of_n_numbers.cpp) * [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp) * [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp) * [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp) * [Palindromeofnumber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Palindromeofnumber.cpp) - * [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Paranthesis%20Matching.cpp) + * [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/paranthesis_matching.cpp) * [Pascal Triangle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/pascal_triangle.cpp) - * [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Primality%20Test.cpp) + * [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/primality_test.cpp) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sieve_of_Eratosthenes.cpp) * [Smallest-Circle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/smallest-circle.cpp) - * [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Sparse%20matrix.cpp) + * [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sparse_matrix.cpp) * [Spiral Print](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/spiral_print.cpp) * [Stairs Pattern](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/stairs_pattern.cpp) - * [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Strassen%20Matrix%20Multiplication.cpp) - * [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/String%20Fibonacci.cpp) - * [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Tower%20of%20Hanoi.cpp) + * [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/string_fibonacci.cpp) + * [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/tower_of_hanoi.cpp) * [Vector Important Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/vector_important_functions.cpp) ## Probability @@ -200,6 +199,7 @@ * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp) * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp) * [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp) + * [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort2.cpp) * [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Slow%20Sort.cpp) * [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp) * [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Tim%20Sort.cpp) From 82dab7515cfc8a0ce11843ecb5bebcec64788c7c Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:22:26 -0400 Subject: [PATCH 14/33] filenames and namespace fixes --- .../{Secant_method.CPP => secant_method.cpp} | 0 ...oximation.CPP => successive_approximation.cpp} | 0 math/prime_factorization.cpp | 15 +++++++-------- others/GCD_of_n_numbers.cpp | 3 ++- others/fibonacci_fast.cpp | 3 +-- 5 files changed, 10 insertions(+), 11 deletions(-) rename computer_oriented_statistical_methods/{Secant_method.CPP => secant_method.cpp} (100%) rename computer_oriented_statistical_methods/{successive_approximation.CPP => successive_approximation.cpp} (100%) diff --git a/computer_oriented_statistical_methods/Secant_method.CPP b/computer_oriented_statistical_methods/secant_method.cpp similarity index 100% rename from computer_oriented_statistical_methods/Secant_method.CPP rename to computer_oriented_statistical_methods/secant_method.cpp diff --git a/computer_oriented_statistical_methods/successive_approximation.CPP b/computer_oriented_statistical_methods/successive_approximation.cpp similarity index 100% rename from computer_oriented_statistical_methods/successive_approximation.CPP rename to computer_oriented_statistical_methods/successive_approximation.cpp diff --git a/math/prime_factorization.cpp b/math/prime_factorization.cpp index bd1b99d97..c018de666 100644 --- a/math/prime_factorization.cpp +++ b/math/prime_factorization.cpp @@ -2,13 +2,12 @@ #include #include #include -using namespace std; // Declaring variables for maintaing prime numbers and to check whether a number // is prime or not bool isprime[1000006]; -vector prime_numbers; -vector> factors; +std::vector prime_numbers; +std::vector> factors; // Calculating prime number upto a given range void SieveOfEratosthenes(int N) { @@ -43,7 +42,7 @@ void prime_factorization(int num) { number = number / prime_numbers[i]; } - if (count) factors.push_back(make_pair(prime_numbers[i], count)); + if (count) factors.push_back(std::make_pair(prime_numbers[i], count)); } } @@ -52,9 +51,9 @@ void prime_factorization(int num) { */ int main() { int num; - cout << "\t\tComputes the prime factorization\n\n"; - cout << "Type in a number: "; - cin >> num; + std::cout << "\t\tComputes the prime factorization\n\n"; + std::cout << "Type in a number: "; + std::cin >> num; SieveOfEratosthenes(num); @@ -62,7 +61,7 @@ int main() { // Prime factors with their powers in the given number in new line for (auto it : factors) { - cout << it.first << " " << it.second << endl; + std::cout << it.first << " " << it.second << std::endl; } return 0; diff --git a/others/GCD_of_n_numbers.cpp b/others/GCD_of_n_numbers.cpp index b592e5931..37e82173a 100644 --- a/others/GCD_of_n_numbers.cpp +++ b/others/GCD_of_n_numbers.cpp @@ -5,7 +5,7 @@ int main() { int n; std::cout << "Enter value of n:" << std::endl; std::cin >> n; - int a[n]; + int *a = new int[n]; int i, j, gcd; std::cout << "Enter the n numbers:" << std::endl; for (i = 0; i < n; i++) std::cin >> a[i]; @@ -18,5 +18,6 @@ int main() { gcd = a[j] % gcd; // calculating GCD by division method } std::cout << "GCD of entered n numbers:" << gcd; + delete[] a; return 0; } diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp index dd3481422..381a82d7c 100644 --- a/others/fibonacci_fast.cpp +++ b/others/fibonacci_fast.cpp @@ -9,7 +9,6 @@ #include #include -using namespace std; const long long MAX = 93; @@ -31,7 +30,7 @@ long long fib(long long n) { int main() { // Main Function for (long long i = 1; i < 93; i++) { - cout << i << " th fibonacci number is " << fib(i) << "\n"; + std::cout << i << " th fibonacci number is " << fib(i) << "\n"; } return 0; } From 4c7685fdf6f14a6ae9924757f5f9bf05281812d8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 03:22:58 +0000 Subject: [PATCH 15/33] updating DIRECTORY.md --- DIRECTORY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 644c57056..f21574454 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -15,8 +15,8 @@ * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp) - * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Secant_method.CPP) - * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.CPP) + * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/secant_method.cpp) + * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.cpp) ## Data Structure * [Avltree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structure/AVLtree.cpp) From c93f3ef35eb05cef312be1cab83bafda83f6011b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:25:44 -0400 Subject: [PATCH 16/33] fix inttypes --- others/fibonacci_fast.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp index 381a82d7c..a0b83b640 100644 --- a/others/fibonacci_fast.cpp +++ b/others/fibonacci_fast.cpp @@ -7,20 +7,21 @@ // have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci // similar to matrix exponentiation. +#include #include #include -const long long MAX = 93; +const uint64_t MAX = 93; -long long f[MAX] = {0}; +uint64_t f[MAX] = {0}; -long long fib(long long n) { +uint64_t fib(uint64_t n) { if (n == 0) return 0; if (n == 1 || n == 2) return (f[n] = 1); if (f[n]) return f[n]; - long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; + uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) : (2 * fib(k - 1) + fib(k)) * fib(k); @@ -29,7 +30,7 @@ long long fib(long long n) { int main() { // Main Function - for (long long i = 1; i < 93; i++) { + for (uint64_t i = 1; i < 93; i++) { std::cout << i << " th fibonacci number is " << fib(i) << "\n"; } return 0; From b9bb6caa73083da8fc0d74d366dd9262da081c3d Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:32:31 -0400 Subject: [PATCH 17/33] made functions static to files *BUG* in CPP lint github action --- .../Gaussian_elimination.cpp | 52 ++++++++----------- ...ection_method.CPP => bisection_method.cpp} | 2 +- .../false-position.cpp | 21 ++++---- .../newton_raphson_method.cpp | 5 +- .../secant_method.cpp | 2 +- .../successive_approximation.cpp | 5 +- 6 files changed, 40 insertions(+), 47 deletions(-) rename computer_oriented_statistical_methods/{Bisection_method.CPP => bisection_method.cpp} (96%) diff --git a/computer_oriented_statistical_methods/Gaussian_elimination.cpp b/computer_oriented_statistical_methods/Gaussian_elimination.cpp index 58e634505..ecfbfacc9 100644 --- a/computer_oriented_statistical_methods/Gaussian_elimination.cpp +++ b/computer_oriented_statistical_methods/Gaussian_elimination.cpp @@ -1,29 +1,23 @@ #include -using namespace std; -int main() -{ +int main() { int mat_size, i, j, step; - cout << "Matrix size: "; - cin >> mat_size; + std::cout << "Matrix size: "; + std::cin >> mat_size; double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1]; - cout << endl - << "Enter value of the matrix: " << endl; - for (i = 0; i < mat_size; i++) - { - for (j = 0; j <= mat_size; j++) - { - cin >> mat[i][j]; //Enter (mat_size*mat_size) value of the matrix. + std::cout << std::endl << "Enter value of the matrix: " << std::endl; + for (i = 0; i < mat_size; i++) { + for (j = 0; j <= mat_size; j++) { + std::cin >> + mat[i][j]; // Enter (mat_size*mat_size) value of the matrix. } } - for (step = 0; step < mat_size - 1; step++) - { - for (i = step; i < mat_size - 1; i++) - { + for (step = 0; step < mat_size - 1; step++) { + for (i = step; i < mat_size - 1; i++) { double a = (mat[i + 1][step] / mat[step][step]); for (j = step; j <= mat_size; j++) @@ -31,24 +25,20 @@ int main() } } - cout << endl - << "Matrix using Gaussian Elimination method: " << endl; - for (i = 0; i < mat_size; i++) - { - for (j = 0; j <= mat_size; j++) - { + std::cout << std::endl + << "Matrix using Gaussian Elimination method: " << std::endl; + for (i = 0; i < mat_size; i++) { + for (j = 0; j <= mat_size; j++) { x[i][j] = mat[i][j]; - cout << mat[i][j] << " "; + std::cout << mat[i][j] << " "; } - cout << endl; + std::cout << std::endl; } - cout << endl - << "Value of the Gaussian Elimination method: " << endl; - for (i = mat_size - 1; i >= 0; i--) - { + std::cout << std::endl + << "Value of the Gaussian Elimination method: " << std::endl; + for (i = mat_size - 1; i >= 0; i--) { double sum = 0; - for (j = mat_size - 1; j > i; j--) - { + for (j = mat_size - 1; j > i; j--) { x[i][j] = x[j][j] * x[i][j]; sum = x[i][j] + sum; } @@ -57,7 +47,7 @@ int main() else x[i][i] = (x[i][mat_size] - sum) / (x[i][i]); - cout << "x" << i << "= " << x[i][i] << endl; + std::cout << "x" << i << "= " << x[i][i] << std::endl; } return 0; } diff --git a/computer_oriented_statistical_methods/Bisection_method.CPP b/computer_oriented_statistical_methods/bisection_method.cpp similarity index 96% rename from computer_oriented_statistical_methods/Bisection_method.CPP rename to computer_oriented_statistical_methods/bisection_method.cpp index 266083de1..089828c9a 100644 --- a/computer_oriented_statistical_methods/Bisection_method.CPP +++ b/computer_oriented_statistical_methods/bisection_method.cpp @@ -1,7 +1,7 @@ #include #include -float eq(float i) { +static float eq(float i) { return (std::pow(i, 3) - (4 * i) - 9); // original equation } diff --git a/computer_oriented_statistical_methods/false-position.cpp b/computer_oriented_statistical_methods/false-position.cpp index 5e15e92cc..c5a314508 100644 --- a/computer_oriented_statistical_methods/false-position.cpp +++ b/computer_oriented_statistical_methods/false-position.cpp @@ -1,9 +1,11 @@ -#include -#include +#include +#include #include -float eq(float i) { + +static float eq(float i) { return (pow(i, 3) - (4 * i) - 9); // origial equation } + int main() { float a, b, z, c, m, n; system("clear"); @@ -12,12 +14,13 @@ int main() { if (z >= 0) { b = i; a = --i; - goto START; - } + break; } - START: + } + std::cout << "\nFirst initial: " << a; std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { float h, d; m = eq(a); @@ -26,10 +29,10 @@ int main() { a = c; z = eq(c); if (z > 0 && z < 0.09) { // stoping criteria - goto END; + break; } } - END: + std::cout << "\n\nRoot: " << c; - system("pause"); + return 0; } diff --git a/computer_oriented_statistical_methods/newton_raphson_method.cpp b/computer_oriented_statistical_methods/newton_raphson_method.cpp index a2a57768e..47d276490 100644 --- a/computer_oriented_statistical_methods/newton_raphson_method.cpp +++ b/computer_oriented_statistical_methods/newton_raphson_method.cpp @@ -1,10 +1,11 @@ #include #include -float eq(float i) { +static float eq(float i) { return (std::pow(i, 3) - (4 * i) - 9); // original equation } -float eq_der(float i) { + +static float eq_der(float i) { return ((3 * std::pow(i, 2)) - 4); // derivative of equation } diff --git a/computer_oriented_statistical_methods/secant_method.cpp b/computer_oriented_statistical_methods/secant_method.cpp index bd805ed36..c353ef850 100644 --- a/computer_oriented_statistical_methods/secant_method.cpp +++ b/computer_oriented_statistical_methods/secant_method.cpp @@ -1,7 +1,7 @@ #include #include -float eq(float i) { +static float eq(float i) { return (pow(i, 3) - (4 * i) - 9); // original equation } diff --git a/computer_oriented_statistical_methods/successive_approximation.cpp b/computer_oriented_statistical_methods/successive_approximation.cpp index 85c42c9fb..efbcc3bbf 100644 --- a/computer_oriented_statistical_methods/successive_approximation.cpp +++ b/computer_oriented_statistical_methods/successive_approximation.cpp @@ -1,9 +1,8 @@ - #include #include -float eq(float y) { return ((3 * y) - (cos(y)) - 2); } -float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } +static float eq(float y) { return ((3 * y) - (cos(y)) - 2); } +static float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } int main() { float y, x1, x2, x3, sum, s, a, f1, f2, gd; From 2e2f9c209ca0ea04e59117859f2f0f08f638fbf4 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 03:32:54 +0000 Subject: [PATCH 18/33] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index f21574454..26210d6d4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -10,7 +10,7 @@ * [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp) ## Computer Oriented Statistical Methods - * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Bisection_method.CPP) + * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/bisection_method.cpp) * [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) From 9c0025aa3d1c1a01ef78b0ceeab775a796d66abd Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:42:37 -0400 Subject: [PATCH 19/33] added cmake to math, others and compu_stats --- CMakeLists.txt | 32 ++++++------------- .../CMakeLists.txt | 18 +++++++++++ math/CMakeLists.txt | 18 +++++++++++ others/CMakeLists.txt | 18 +++++++++++ 4 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 computer_oriented_statistical_methods/CMakeLists.txt create mode 100644 math/CMakeLists.txt create mode 100644 others/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index f98109382..b9c7831c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,13 @@ cmake_minimum_required(VERSION 3.3) -project(Algorithms_in_C - LANGUAGES C CXX +project(Algorithms_in_C++ + LANGUAGES CXX VERSION 1.0.0 DESCRIPTION "Set of algorithms implemented in C." ) +# set(CMAKE_CXX_CPPLINT "~/anaconda3/bin/cpplint --filter=-legal/copyright --std=c++11") +# find_program(CLANG_FORMAT "clang-format") + option(USE_OPENMP "flag to use OpenMP for multithreading" ON) cmake_policy(SET CMP0054 NEW) @@ -34,32 +37,17 @@ if(DOXYGEN_FOUND) ) endif() -function(function_timer) - set(CMAKE_BUILD_TYPE "Release") # This is local to function - add_subdirectory(function_timer EXCLUDE_FROM_ALL) -endfunction() - -function_timer() - -include_directories(function_timer/include) -# link_libraries(function_timer) -# include_directories(${CMAKE_BINARY_DIR}/include) -# link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) - -set(CMAKE_C_STANDARD 11) -set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) if(MSVC) add_compile_definitions(_CRT_SECURE_NO_WARNINGS) add_compile_options(/Za) endif(MSVC) -add_subdirectory(conversions) -add_subdirectory(misc) -add_subdirectory(project_euler) -add_subdirectory(sorting) -add_subdirectory(searching) -add_subdirectory(numerical_methods) +add_subdirectory(math) +add_subdirectory(others) +add_subdirectory(computer_oriented_statistical_methods) if(USE_OPENMP) find_package(OpenMP) diff --git a/computer_oriented_statistical_methods/CMakeLists.txt b/computer_oriented_statistical_methods/CMakeLists.txt new file mode 100644 index 000000000..acff1ed1c --- /dev/null +++ b/computer_oriented_statistical_methods/CMakeLists.txt @@ -0,0 +1,18 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. RELATIVE may makes it easier to extract an executable name +# automatically. +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) +foreach( testsourcefile ${APP_SOURCES} ) + # I used a simple string replace, to cut off .cpp. + string( REPLACE ".cpp" "" testname ${testsourcefile} ) + add_executable( ${testname} ${testsourcefile} ) + + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) + if(OpenMP_CXX_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_CXX) + endif() + install(TARGETS ${testname} DESTINATION "bin/stats") + +endforeach( testsourcefile ${APP_SOURCES} ) diff --git a/math/CMakeLists.txt b/math/CMakeLists.txt new file mode 100644 index 000000000..2b70b2d31 --- /dev/null +++ b/math/CMakeLists.txt @@ -0,0 +1,18 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. RELATIVE may makes it easier to extract an executable name +# automatically. +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) +foreach( testsourcefile ${APP_SOURCES} ) + # I used a simple string replace, to cut off .cpp. + string( REPLACE ".cpp" "" testname ${testsourcefile} ) + add_executable( ${testname} ${testsourcefile} ) + + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) + if(OpenMP_CXX_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_CXX) + endif() + install(TARGETS ${testname} DESTINATION "bin/math") + +endforeach( testsourcefile ${APP_SOURCES} ) diff --git a/others/CMakeLists.txt b/others/CMakeLists.txt new file mode 100644 index 000000000..f049b5f2d --- /dev/null +++ b/others/CMakeLists.txt @@ -0,0 +1,18 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. RELATIVE may makes it easier to extract an executable name +# automatically. +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) +foreach( testsourcefile ${APP_SOURCES} ) + # I used a simple string replace, to cut off .cpp. + string( REPLACE ".cpp" "" testname ${testsourcefile} ) + add_executable( ${testname} ${testsourcefile} ) + + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) + if(OpenMP_CXX_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_CXX) + endif() + install(TARGETS ${testname} DESTINATION "bin/others") + +endforeach( testsourcefile ${APP_SOURCES} ) From 565031ba73b6668a244ccdba78f56e197ae5eada Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 00:09:07 -0400 Subject: [PATCH 20/33] added documentation --- .../ordinary_least_squares_regressor.cpp | 609 ++++++++++-------- 1 file changed, 329 insertions(+), 280 deletions(-) diff --git a/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp b/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp index f50ccf1b1..41acfa10a 100644 --- a/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp +++ b/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp @@ -1,349 +1,398 @@ /** + * @file + * * Program that gets the number of data samples and number of features per * sample along with output per sample. It applies OLS regression to compute * the regression output for additional test data samples. - **/ + */ #include #include #include +/** + * operator to print a matrix + */ template std::ostream &operator<<(std::ostream &out, std::vector> const &v) { - const int width = 10; - const char separator = ' '; + const int width = 10; + const char separator = ' '; - for (size_t row = 0; row < v.size(); row++) { - for (size_t col = 0; col < v[row].size(); col++) - out << std::left << std::setw(width) << std::setfill(separator) - << v[row][col]; - out << std::endl; - } + for (size_t row = 0; row < v.size(); row++) { + for (size_t col = 0; col < v[row].size(); col++) + out << std::left << std::setw(width) << std::setfill(separator) + << v[row][col]; + out << std::endl; + } - return out; -} - -template -std::ostream &operator<<(std::ostream &out, std::vector const &v) { - const int width = 15; - const char separator = ' '; - - for (size_t row = 0; row < v.size(); row++) - out << std::left << std::setw(width) << std::setfill(separator) << v[row]; - - return out; -} - -template -inline bool is_square(std::vector> const &A) { - // Assuming A is square matrix - size_t N = A.size(); - for (size_t i = 0; i < N; i++) - if (A[i].size() != N) - return false; - return true; + return out; } /** - * matrix multiplication + * operator to print a vector + */ +template +std::ostream &operator<<(std::ostream &out, std::vector const &v) { + const int width = 15; + const char separator = ' '; + + for (size_t row = 0; row < v.size(); row++) + out << std::left << std::setw(width) << std::setfill(separator) + << v[row]; + + return out; +} + +/** + * function to check if given matrix is a square matrix + * \returns 1 if true, 0 if false + */ +template +inline bool is_square(std::vector> const &A) { + // Assuming A is square matrix + size_t N = A.size(); + for (size_t i = 0; i < N; i++) + if (A[i].size() != N) return false; + return true; +} + +/** + * Matrix multiplication such that if A is size (mxn) and + * B is of size (pxq) then the multiplication is defined + * only when n = p and the resultant matrix is of size (mxq) + * + * \returns resultant matrix **/ template std::vector> operator*(std::vector> const &A, std::vector> const &B) { - // Number of rows in A - size_t N_A = A.size(); - // Number of columns in B - size_t N_B = B[0].size(); + // Number of rows in A + size_t N_A = A.size(); + // Number of columns in B + size_t N_B = B[0].size(); - std::vector> result(N_A); + std::vector> result(N_A); - if (A[0].size() != B.size()) { - std::cerr << "Number of columns in A != Number of rows in B (" - << A[0].size() << ", " << B.size() << ")" << std::endl; - return result; - } - - for (size_t row = 0; row < N_A; row++) { - std::vector v(N_B); - for (size_t col = 0; col < N_B; col++) { - v[col] = static_cast(0); - for (size_t j = 0; j < B.size(); j++) - v[col] += A[row][j] * B[j][col]; + if (A[0].size() != B.size()) { + std::cerr << "Number of columns in A != Number of rows in B (" + << A[0].size() << ", " << B.size() << ")" << std::endl; + return result; } - result[row] = v; - } - return result; -} + for (size_t row = 0; row < N_A; row++) { + std::vector v(N_B); + for (size_t col = 0; col < N_B; col++) { + v[col] = static_cast(0); + for (size_t j = 0; j < B.size(); j++) + v[col] += A[row][j] * B[j][col]; + } + result[row] = v; + } -template -std::vector operator*(std::vector> const &A, - std::vector const &B) { - // Number of rows in A - size_t N_A = A.size(); - - std::vector result(N_A); - - if (A[0].size() != B.size()) { - std::cerr << "Number of columns in A != Number of rows in B (" - << A[0].size() << ", " << B.size() << ")" << std::endl; return result; - } - - for (size_t row = 0; row < N_A; row++) { - result[row] = static_cast(0); - for (size_t j = 0; j < B.size(); j++) - result[row] += A[row][j] * B[j]; - } - - return result; -} - -template -std::vector operator*(float const scalar, std::vector const &A) { - // Number of rows in A - size_t N_A = A.size(); - - std::vector result(N_A); - - for (size_t row = 0; row < N_A; row++) { - result[row] += A[row] * static_cast(scalar); - } - - return result; -} - -template -std::vector operator*(std::vector const &A, float const scalar) { - // Number of rows in A - size_t N_A = A.size(); - - std::vector result(N_A); - - for (size_t row = 0; row < N_A; row++) - result[row] = A[row] * static_cast(scalar); - - return result; -} - -template -std::vector operator/(std::vector const &A, float const scalar) { - return (1.f / scalar) * A; -} - -template -std::vector operator-(std::vector const &A, std::vector const &B) { - // Number of rows in A - size_t N = A.size(); - - std::vector result(N); - - if (B.size() != N) { - std::cerr << "Vector dimensions shouldbe identical!" << std::endl; - return A; - } - - for (size_t row = 0; row < N; row++) - result[row] = A[row] - B[row]; - - return result; -} - -template -std::vector operator+(std::vector const &A, std::vector const &B) { - // Number of rows in A - size_t N = A.size(); - - std::vector result(N); - - if (B.size() != N) { - std::cerr << "Vector dimensions shouldbe identical!" << std::endl; - return A; - } - - for (size_t row = 0; row < N; row++) - result[row] = A[row] + B[row]; - - return result; } /** - * Get matrix inverse using Row-trasnformations + * multiplication of a matrix with a column vector + * \returns resultant vector + */ +template +std::vector operator*(std::vector> const &A, + std::vector const &B) { + // Number of rows in A + size_t N_A = A.size(); + + std::vector result(N_A); + + if (A[0].size() != B.size()) { + std::cerr << "Number of columns in A != Number of rows in B (" + << A[0].size() << ", " << B.size() << ")" << std::endl; + return result; + } + + for (size_t row = 0; row < N_A; row++) { + result[row] = static_cast(0); + for (size_t j = 0; j < B.size(); j++) result[row] += A[row][j] * B[j]; + } + + return result; +} + +/** + * pre-multiplication of a vector by a scalar + * \returns resultant vector + */ +template +std::vector operator*(float const scalar, std::vector const &A) { + // Number of rows in A + size_t N_A = A.size(); + + std::vector result(N_A); + + for (size_t row = 0; row < N_A; row++) { + result[row] += A[row] * static_cast(scalar); + } + + return result; +} + +/** + * post-multiplication of a vector by a scalar + * \returns resultant vector + */ +template +std::vector operator*(std::vector const &A, float const scalar) { + // Number of rows in A + size_t N_A = A.size(); + + std::vector result(N_A); + + for (size_t row = 0; row < N_A; row++) + result[row] = A[row] * static_cast(scalar); + + return result; +} + +/** + * division of a vector by a scalar + * \returns resultant vector + */ +template +std::vector operator/(std::vector const &A, float const scalar) { + return (1.f / scalar) * A; +} + +/** + * subtraction of two vectors of identical lengths + * \returns resultant vector + */ +template +std::vector operator-(std::vector const &A, std::vector const &B) { + // Number of rows in A + size_t N = A.size(); + + std::vector result(N); + + if (B.size() != N) { + std::cerr << "Vector dimensions shouldbe identical!" << std::endl; + return A; + } + + for (size_t row = 0; row < N; row++) result[row] = A[row] - B[row]; + + return result; +} + +/** + * addition of two vectors of identical lengths + * \returns resultant vector + */ +template +std::vector operator+(std::vector const &A, std::vector const &B) { + // Number of rows in A + size_t N = A.size(); + + std::vector result(N); + + if (B.size() != N) { + std::cerr << "Vector dimensions shouldbe identical!" << std::endl; + return A; + } + + for (size_t row = 0; row < N; row++) result[row] = A[row] + B[row]; + + return result; +} + +/** + * Get matrix inverse using Row-trasnformations. Given matrix must + * be a square and non-singular. + * \returns inverse matrix **/ template -std::vector> -get_inverse(std::vector> const &A) { - // Assuming A is square matrix - size_t N = A.size(); +std::vector> get_inverse( + std::vector> const &A) { + // Assuming A is square matrix + size_t N = A.size(); - std::vector> inverse(N); - for (size_t row = 0; row < N; row++) { - // preallocatae a resultant identity matrix - inverse[row] = std::vector(N); - for (size_t col = 0; col < N; col++) - inverse[row][col] = (row == col) ? 1.f : 0.f; - } + std::vector> inverse(N); + for (size_t row = 0; row < N; row++) { + // preallocatae a resultant identity matrix + inverse[row] = std::vector(N); + for (size_t col = 0; col < N; col++) + inverse[row][col] = (row == col) ? 1.f : 0.f; + } + + if (!is_square(A)) { + std::cerr << "A must be a square matrix!" << std::endl; + return inverse; + } + + // preallocatae a temporary matrix identical to A + std::vector> temp(N); + for (size_t row = 0; row < N; row++) { + std::vector v(N); + for (size_t col = 0; col < N; col++) + v[col] = static_cast(A[row][col]); + temp[row] = v; + } + + // start transformations + for (size_t row = 0; row < N; row++) { + for (size_t row2 = row; row2 < N && temp[row][row] == 0; row2++) { + // this to ensure diagonal elements are not 0 + temp[row] = temp[row] + temp[row2]; + inverse[row] = inverse[row] + inverse[row2]; + } + + for (size_t col2 = row; col2 < N && temp[row][row] == 0; col2++) { + // this to further ensure diagonal elements are not 0 + for (size_t row2 = 0; row2 < N; row2++) { + temp[row2][row] = temp[row2][row] + temp[row2][col2]; + inverse[row2][row] = inverse[row2][row] + inverse[row2][col2]; + } + } + + if (temp[row][row] == 0) { + // Probably a low-rank matrix and hence singular + std::cerr << "Low-rank matrix, no inverse!" << std::endl; + return inverse; + } + + // set diagonal to 1 + float divisor = static_cast(temp[row][row]); + temp[row] = temp[row] / divisor; + inverse[row] = inverse[row] / divisor; + // Row transformations + for (size_t row2 = 0; row2 < N; row2++) { + if (row2 == row) continue; + float factor = temp[row2][row]; + temp[row2] = temp[row2] - factor * temp[row]; + inverse[row2] = inverse[row2] - factor * inverse[row]; + } + } - if (!is_square(A)) { - std::cerr << "A must be a square matrix!" << std::endl; return inverse; - } - - // preallocatae a temporary matrix identical to A - std::vector> temp(N); - for (size_t row = 0; row < N; row++) { - std::vector v(N); - for (size_t col = 0; col < N; col++) - v[col] = static_cast(A[row][col]); - temp[row] = v; - } - - // start transformations - for (size_t row = 0; row < N; row++) { - for (size_t row2 = row; row2 < N && temp[row][row] == 0; row2++) { - // this to ensure diagonal elements are not 0 - temp[row] = temp[row] + temp[row2]; - inverse[row] = inverse[row] + inverse[row2]; - } - - for (size_t col2 = row; col2 < N && temp[row][row] == 0; col2++) { - // this to further ensure diagonal elements are not 0 - for (size_t row2 = 0; row2 < N; row2++) { - temp[row2][row] = temp[row2][row] + temp[row2][col2]; - inverse[row2][row] = inverse[row2][row] + inverse[row2][col2]; - } - } - - if (temp[row][row] == 0) { - // Probably a low-rank matrix and hence singular - std::cerr << "Low-rank matrix, no inverse!" << std::endl; - return inverse; - } - - // set diagonal to 1 - float divisor = static_cast(temp[row][row]); - temp[row] = temp[row] / divisor; - inverse[row] = inverse[row] / divisor; - // Row transformations - for (size_t row2 = 0; row2 < N; row2++) { - if (row2 == row) - continue; - float factor = temp[row2][row]; - temp[row2] = temp[row2] - factor * temp[row]; - inverse[row2] = inverse[row2] - factor * inverse[row]; - } - } - - return inverse; } /** * matrix transpose + * \returns resultant matrix **/ template -std::vector> -get_transpose(std::vector> const &A) { - std::vector> result(A[0].size()); +std::vector> get_transpose( + std::vector> const &A) { + std::vector> result(A[0].size()); - for (size_t row = 0; row < A[0].size(); row++) { - std::vector v(A.size()); - for (size_t col = 0; col < A.size(); col++) - v[col] = A[col][row]; + for (size_t row = 0; row < A[0].size(); row++) { + std::vector v(A.size()); + for (size_t col = 0; col < A.size(); col++) v[col] = A[col][row]; - result[row] = v; - } - return result; + result[row] = v; + } + return result; } +/** + * Perform Ordinary Least Squares curve fit. + * \param X feature matrix with rows representing sample vector of features + * \param Y known regression value for each sample + * \returns fitted regression model polynomial coefficients + */ template std::vector fit_OLS_regressor(std::vector> const &X, std::vector const &Y) { - // NxF - std::vector> X2 = X; - for (size_t i = 0; i < X2.size(); i++) - // add Y-intercept -> Nx(F+1) - X2[i].push_back(1); - // (F+1)xN - std::vector> Xt = get_transpose(X2); - // (F+1)x(F+1) - std::vector> tmp = get_inverse(Xt * X2); - // (F+1)xN - std::vector> out = tmp * Xt; - // cout << endl - // << "Projection matrix: " << X2 * out << endl; + // NxF + std::vector> X2 = X; + for (size_t i = 0; i < X2.size(); i++) + // add Y-intercept -> Nx(F+1) + X2[i].push_back(1); + // (F+1)xN + std::vector> Xt = get_transpose(X2); + // (F+1)x(F+1) + std::vector> tmp = get_inverse(Xt * X2); + // (F+1)xN + std::vector> out = tmp * Xt; + // cout << endl + // << "Projection matrix: " << X2 * out << endl; - // Fx1,1 -> (F+1)^th element is the independent constant - return out * Y; + // Fx1,1 -> (F+1)^th element is the independent constant + return out * Y; } /** * Given data and OLS model coeffficients, predict - * regression estimates + * regression estimates. + * \param X feature matrix with rows representing sample vector of features + * \param \f$\beta\f$ fitted regression model + * \return vector with regression values for each sample **/ template std::vector predict_OLS_regressor(std::vector> const &X, - std::vector const &beta) { - std::vector result(X.size()); + std::vector const &beta /**< */ +) { + std::vector result(X.size()); - for (size_t rows = 0; rows < X.size(); rows++) { - // -> start with constant term - result[rows] = beta[X[0].size()]; - for (size_t cols = 0; cols < X[0].size(); cols++) - result[rows] += beta[cols] * X[rows][cols]; - } - // Nx1 - return result; + for (size_t rows = 0; rows < X.size(); rows++) { + // -> start with constant term + result[rows] = beta[X[0].size()]; + for (size_t cols = 0; cols < X[0].size(); cols++) + result[rows] += beta[cols] * X[rows][cols]; + } + // Nx1 + return result; } +/** + * main function + */ int main() { - size_t N, F; + size_t N, F; - std::cout << "Enter number of features: "; - // number of features = columns - std::cin >> F; - std::cout << "Enter number of samples: "; - // number of samples = rows - std::cin >> N; + std::cout << "Enter number of features: "; + // number of features = columns + std::cin >> F; + std::cout << "Enter number of samples: "; + // number of samples = rows + std::cin >> N; - std::vector> data(N); - std::vector Y(N); + std::vector> data(N); + std::vector Y(N); - std::cout - << "Enter training data. Per sample, provide features ad one output." - << std::endl; + std::cout + << "Enter training data. Per sample, provide features ad one output." + << std::endl; - for (size_t rows = 0; rows < N; rows++) { - std::vector v(F); - std::cout << "Sample# " << rows + 1 << ": "; - for (size_t cols = 0; cols < F; cols++) - // get the F features - std::cin >> v[cols]; - data[rows] = v; - // get the corresponding output - std::cin >> Y[rows]; - } + for (size_t rows = 0; rows < N; rows++) { + std::vector v(F); + std::cout << "Sample# " << rows + 1 << ": "; + for (size_t cols = 0; cols < F; cols++) + // get the F features + std::cin >> v[cols]; + data[rows] = v; + // get the corresponding output + std::cin >> Y[rows]; + } - std::vector beta = fit_OLS_regressor(data, Y); - std::cout << std::endl << std::endl << "beta:" << beta << std::endl; + std::vector beta = fit_OLS_regressor(data, Y); + std::cout << std::endl << std::endl << "beta:" << beta << std::endl; - size_t T; - std::cout << "Enter number of test samples: "; - // number of test sample inputs - std::cin >> T; - std::vector> data2(T); - // vector Y2(T); + size_t T; + std::cout << "Enter number of test samples: "; + // number of test sample inputs + std::cin >> T; + std::vector> data2(T); + // vector Y2(T); - for (size_t rows = 0; rows < T; rows++) { - std::cout << "Sample# " << rows + 1 << ": "; - std::vector v(F); - for (size_t cols = 0; cols < F; cols++) - std::cin >> v[cols]; - data2[rows] = v; - } + for (size_t rows = 0; rows < T; rows++) { + std::cout << "Sample# " << rows + 1 << ": "; + std::vector v(F); + for (size_t cols = 0; cols < F; cols++) std::cin >> v[cols]; + data2[rows] = v; + } - std::vector out = predict_OLS_regressor(data2, beta); - for (size_t rows = 0; rows < T; rows++) - std::cout << out[rows] << std::endl; + std::vector out = predict_OLS_regressor(data2, beta); + for (size_t rows = 0; rows < T; rows++) std::cout << out[rows] << std::endl; - return 0; + return 0; } From 880fc707fc29169c445e27ff5c66a6fcba71f428 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 00:09:17 -0400 Subject: [PATCH 21/33] cpp code docs optimize --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b9c7831c8..bf1f1d3c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,10 @@ if(DOXYGEN_FOUND) set(DOXYGEN_CREATE_SUBDIRS YES) set(DOCYGEN_GENERATE_TREEVIEW YES) set(DOXYGEN_STRIP_CODE_COMMENTS NO) - set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES) + set(DOXYGEN_BUILTIN_STL_SUPPORT YES) + if(MSVC) + set(DOXYGEN_CPP_CLI_SUPPORT YES) + endif() set(DOXYGEN_MATHJAX_RELPATH "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML") if(Doxygen_dot_FOUND) set(DOXYGEN_HAVE_DOT YES) From 79f53543b90449baa0bd5788a828e593a9caea02 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 00:21:06 -0400 Subject: [PATCH 22/33] added doxygen CI github action --- .github/workflows/gh-pages.yml | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/gh-pages.yml diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml new file mode 100644 index 000000000..aff5dcdce --- /dev/null +++ b/.github/workflows/gh-pages.yml @@ -0,0 +1,35 @@ +name: Doxygen CI + +on: + push: + branches: [master] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + with: + submodules: true + - name: Install requirements + run: | + sudo apt -qq -y update + sudo apt -qq install doxygen graphviz ninja-build + - name: configure + run: cmake -G Ninja -B ./build -S . + - name: build + run: cmake --build build -t doc + - name: gh-pages + uses: actions/checkout@master + with: + ref: "gh-pages" + clean: false + - name: Move & Commit files + run: | + cp -rp ./build/html/* . && rm -rf ./build && ls -lah + git config --global user.name github-actions + git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY + git add * + git commit -m "Documentation for $GITHUB_SHA" || true + git push --force || true From 59a6db23cd530ab6ca04baf850febf2091390764 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 00:41:57 -0400 Subject: [PATCH 23/33] added CPP compile action --- .github/workflows/ccpp.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/workflows/ccpp.yml diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml new file mode 100644 index 000000000..7eff58053 --- /dev/null +++ b/.github/workflows/ccpp.yml @@ -0,0 +1,27 @@ +name: C/C++ CI + +on: [push] +# push: +# branches: [ master ] +# pull_request: +# branches: [ master ] + +jobs: + build: + + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + + steps: + - uses: actions/checkout@master + with: + submodules: true + - name: Install requirements + run: sudo apt -qq install ninja-buil + - name: configure + run: cmake -G Ninja -B ./build -S . + - name: build + run: cmake --build build + From 22b375c058cf2eec6c47990ae1b46e08d6cdd3ee Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 00:49:27 -0400 Subject: [PATCH 24/33] fix cpp_lint bug --- .github/workflows/cpplint_modified_files.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cpplint_modified_files.yml b/.github/workflows/cpplint_modified_files.yml index 07a32ece9..8de6a5e41 100644 --- a/.github/workflows/cpplint_modified_files.yml +++ b/.github/workflows/cpplint_modified_files.yml @@ -11,7 +11,7 @@ jobs: cpplint_modified_files: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 # v2 is broken for git diff + - uses: actions/checkout@v1 # v2 is broken for git diff - uses: actions/setup-python@v1 - run: python -m pip install cpplint - run: git remote -v @@ -42,7 +42,8 @@ jobs: print("g++:") # compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split()) # compile_files = [file for file in cpp_files if file.lower().endswith(compile_exts)] - subprocess.run(["g++"] + cpp_files, check=True, text=True) + for cpp_file in cpp_files: + subprocess.run(["g++", cpp_file], check=True, text=True) upper_files = [file for file in cpp_files if file != file.lower()] if upper_files: From dddcbee9ce2432b90f28ebf50aebe9500dcd854e Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 00:52:38 -0400 Subject: [PATCH 25/33] fixed more filenames --- .../{Gaussian_elimination.cpp => gaussian_elimination.cpp} | 0 others/{Buzz_number.cpp => buzz_number.cpp} | 0 others/{GCD_of_n_numbers.cpp => gcd_of_n_numbers.cpp} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename computer_oriented_statistical_methods/{Gaussian_elimination.cpp => gaussian_elimination.cpp} (100%) rename others/{Buzz_number.cpp => buzz_number.cpp} (100%) rename others/{GCD_of_n_numbers.cpp => gcd_of_n_numbers.cpp} (100%) diff --git a/computer_oriented_statistical_methods/Gaussian_elimination.cpp b/computer_oriented_statistical_methods/gaussian_elimination.cpp similarity index 100% rename from computer_oriented_statistical_methods/Gaussian_elimination.cpp rename to computer_oriented_statistical_methods/gaussian_elimination.cpp diff --git a/others/Buzz_number.cpp b/others/buzz_number.cpp similarity index 100% rename from others/Buzz_number.cpp rename to others/buzz_number.cpp diff --git a/others/GCD_of_n_numbers.cpp b/others/gcd_of_n_numbers.cpp similarity index 100% rename from others/GCD_of_n_numbers.cpp rename to others/gcd_of_n_numbers.cpp From 9d64ff014e817d0f7f6442ab0b226b3c8f6464a8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 04:52:59 +0000 Subject: [PATCH 26/33] updating DIRECTORY.md --- DIRECTORY.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 26210d6d4..b7f6ebdd5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -12,7 +12,7 @@ ## Computer Oriented Statistical Methods * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/bisection_method.cpp) * [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp) - * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) + * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/gaussian_elimination.cpp) * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp) * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/secant_method.cpp) @@ -132,13 +132,13 @@ * [Union Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/Union_of_2_arrays.cpp) ## Others - * [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Buzz_number.cpp) + * [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/buzz_number.cpp) * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_binary.cpp) * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_hexadecimal.cpp) * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_roman_numeral.cpp) * [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp) * [Fibonacci Fast](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci_fast.cpp) - * [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/GCD_of_n_numbers.cpp) + * [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/gcd_of_n_numbers.cpp) * [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp) * [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp) * [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp) From ecffb77f86164cc0ec4d2614df7239abd65f21e9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 00:54:03 -0400 Subject: [PATCH 27/33] remove dash from filename --- .../{false-position.cpp => false_position.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename computer_oriented_statistical_methods/{false-position.cpp => false_position.cpp} (100%) diff --git a/computer_oriented_statistical_methods/false-position.cpp b/computer_oriented_statistical_methods/false_position.cpp similarity index 100% rename from computer_oriented_statistical_methods/false-position.cpp rename to computer_oriented_statistical_methods/false_position.cpp From f28099d3f86dd18fc1f9e02aa13b837a9dab13a7 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 04:54:27 +0000 Subject: [PATCH 28/33] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index b7f6ebdd5..9e163d414 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -11,7 +11,7 @@ ## Computer Oriented Statistical Methods * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/bisection_method.cpp) - * [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp) + * [False Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false_position.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/gaussian_elimination.cpp) * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp) From 7bb69e4183dda6dff56719b0f63c168670d5cbd0 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 01:17:12 -0400 Subject: [PATCH 29/33] added documentation equations --- .../ordinary_least_squares_regressor.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp b/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp index 41acfa10a..06bd4ea52 100644 --- a/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp +++ b/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp @@ -294,7 +294,8 @@ std::vector> get_transpose( } /** - * Perform Ordinary Least Squares curve fit. + * Perform Ordinary Least Squares curve fit. This operation is defined as + * \f[\beta = \left(X^TXX^T\right)Y\f] * \param X feature matrix with rows representing sample vector of features * \param Y known regression value for each sample * \returns fitted regression model polynomial coefficients @@ -322,9 +323,11 @@ std::vector fit_OLS_regressor(std::vector> const &X, /** * Given data and OLS model coeffficients, predict - * regression estimates. + * regression estimates. This operation is defined as + * \f[y_{\text{row}=i} = \sum_{j=\text{columns}}\beta_j\cdot X_{i,j}\f] + * * \param X feature matrix with rows representing sample vector of features - * \param \f$\beta\f$ fitted regression model + * \param beta fitted regression model * \return vector with regression values for each sample **/ template From c4e49b95578d632f2a9524ae36ef2518f92ca0d1 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 01:17:30 -0400 Subject: [PATCH 30/33] project description correction --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf1f1d3c7..ff568fc9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.3) project(Algorithms_in_C++ LANGUAGES CXX VERSION 1.0.0 - DESCRIPTION "Set of algorithms implemented in C." + DESCRIPTION "Set of algorithms implemented in C++." ) # set(CMAKE_CXX_CPPLINT "~/anaconda3/bin/cpplint --filter=-legal/copyright --std=c++11") From 06fe0efc9429ab72787faa5d6317aff9593b414b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 01:18:59 -0400 Subject: [PATCH 31/33] yml file indentation correction --- .github/workflows/ccpp.yml | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 7eff58053..392c8bd8f 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -8,20 +8,18 @@ on: [push] jobs: build: - runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, windows-latest, macOS-latest] steps: - - uses: actions/checkout@master - with: - submodules: true - - name: Install requirements - run: sudo apt -qq install ninja-buil - - name: configure - run: cmake -G Ninja -B ./build -S . - - name: build - run: cmake --build build - + - uses: actions/checkout@master + with: + submodules: true + - name: Install requirements + run: sudo apt -qq install ninja-build + - name: configure + run: cmake -G Ninja -B ./build -S . + - name: build + run: cmake --build build From 249b91d5ff4af928711f7d9b411f4e3e2fd961d9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 01:21:32 -0400 Subject: [PATCH 32/33] remove install ninja --- .github/workflows/ccpp.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 392c8bd8f..30b8edf19 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -17,8 +17,6 @@ jobs: - uses: actions/checkout@master with: submodules: true - - name: Install requirements - run: sudo apt -qq install ninja-build - name: configure run: cmake -G Ninja -B ./build -S . - name: build From 65d4fe035a2c101b6471e3cf8f6080cca243ed41 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 01:22:46 -0400 Subject: [PATCH 33/33] remove ninja as cmake generator --- .github/workflows/ccpp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 30b8edf19..34085cdec 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -18,6 +18,6 @@ jobs: with: submodules: true - name: configure - run: cmake -G Ninja -B ./build -S . + run: cmake -B ./build -S . - name: build run: cmake --build build