diff --git a/machine_learning/adaline_learning.cpp b/machine_learning/adaline_learning.cpp index 055c3b776..077b9408e 100644 --- a/machine_learning/adaline_learning.cpp +++ b/machine_learning/adaline_learning.cpp @@ -31,13 +31,18 @@ #define MAX_ITER 500 // INT_MAX ///< Maximum number of iterations to learn +/** \namespace machine_learning + * \brief Machine learning algorithms + */ +namespace machine_learning { class adaline { public: /** * Default constructor * \param[in] num_features number of features present * \param[in] eta learning rate (optional, default=0.1) - * \param[in] convergence accuracy (optional, default=\f$1\times10^{-5}\f$) + * \param[in] convergence accuracy (optional, + * default=\f$1\times10^{-5}\f$) */ adaline(int num_features, const double eta = 0.01f, const double accuracy = 1e-5) @@ -74,9 +79,9 @@ class adaline { /** * predict the output of the model for given set of features * \param[in] x input vector - * \param[out] out optional argument to return neuron output before applying - * activation function (optional, `nullptr` to ignore) - * \returns model prediction output + * \param[out] out optional argument to return neuron output before + * applying activation function (optional, `nullptr` to ignore) \returns + * model prediction output */ int predict(const std::vector &x, double *out = nullptr) { if (!check_size_match(x)) @@ -94,11 +99,9 @@ class adaline { } /** - * Update the weights of the model using supervised learning for one feature - * vector - * \param[in] x feature vector - * \param[in] y known output value - * \returns correction factor + * Update the weights of the model using supervised learning for one + * feature vector \param[in] x feature vector \param[in] y known output + * value \returns correction factor */ double fit(const std::vector &x, const int &y) { if (!check_size_match(x)) @@ -119,10 +122,9 @@ class adaline { } /** - * Update the weights of the model using supervised learning for an array of - * vectors. - * \param[in] X array of feature vector - * \param[in] y known output value for each feature vector + * Update the weights of the model using supervised learning for an + * array of vectors. \param[in] X array of feature vector \param[in] y + * known output value for each feature vector */ template void fit(std::vector const (&X)[N], const int *y) { @@ -181,6 +183,10 @@ class adaline { std::vector weights; ///< weights of the neural network }; +} // namespace machine_learning + +using machine_learning::adaline; + /** * test function to predict points in a 2D coordinate system above the line * \f$x=y\f$ as +1 and others as -1. diff --git a/math/realtime_stats.cpp b/math/realtime_stats.cpp index 2fbfc9bd5..26b923625 100644 --- a/math/realtime_stats.cpp +++ b/math/realtime_stats.cpp @@ -10,6 +10,12 @@ #include #include +/** + * \namespace statistics + * \brief Statistical algorithms + */ +namespace statistics { + /** * continuous mean and variance computance using * first value as an approximation for the mean. @@ -100,6 +106,11 @@ class stats_computer2 { double mu = 0, var = 0, M = 0; }; +} // namespace statistics + +using statistics::stats_computer1; +using statistics::stats_computer2; + /** Test the algorithm implementation * \param[in] test_data array of data to test the algorithms */ diff --git a/sorting/non_recursive_merge_sort.cpp b/sorting/non_recursive_merge_sort.cpp index 9d4e95f2f..b99b93108 100644 --- a/sorting/non_recursive_merge_sort.cpp +++ b/sorting/non_recursive_merge_sort.cpp @@ -8,13 +8,14 @@ #include #include // for std::move & std::remove_reference_t +namespace sorting { template void merge(Iterator, Iterator, const Iterator, char[]); /// bottom-up merge sort which sorts elements in a non-decreasing order /** - * sorts elements non-recursively by breaking them into small segments, merging - * adjacent segments into larger sorted segments, then increasing the sizes of - * segments by factors of 2 and repeating the same process. + * sorts elements non-recursively by breaking them into small segments, + * merging adjacent segments into larger sorted segments, then increasing + * the sizes of segments by factors of 2 and repeating the same process. * best-case = worst-case = O(n log(n)) * @param first points to the first element * @param last points to 1-step past the last element @@ -26,9 +27,9 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last, // create a buffer large enough to store all elements // dynamically allocated to comply with cpplint char* buffer = new char[n * sizeof(*first)]; - // buffer size can be optimized to largest power of 2 less than n elements - // divide the container into equally-sized segments whose length start at 1 - // and keeps increasing by factors of 2 + // buffer size can be optimized to largest power of 2 less than n + // elements divide the container into equally-sized segments whose + // length start at 1 and keeps increasing by factors of 2 for (size_t length(1); length < n; length <<= 1) { // merge adjacent segments whose number is n / (length * 2) Iterator left(first); @@ -86,6 +87,10 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last) { non_recursive_merge_sort(first, last, last - first); } +} // namespace sorting + +using sorting::non_recursive_merge_sort; + int main(int argc, char** argv) { int size; std::cout << "Enter the number of elements : "; diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 1db6b014e..5d102dc88 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -24,6 +24,7 @@ #include #include +namespace sorting { /** * This function takes last element as pivot, places * the pivot element at its correct position in sorted @@ -67,6 +68,10 @@ void quickSort(int arr[], int low, int high) { } } +} // namespace sorting + +using sorting::quickSort; + // prints the array after sorting void show(int arr[], int size) { for (int i = 0; i < size; i++) std::cout << arr[i] << " "; diff --git a/sorting/shell_sort2.cpp b/sorting/shell_sort2.cpp index 59f204818..85186e752 100644 --- a/sorting/shell_sort2.cpp +++ b/sorting/shell_sort2.cpp @@ -1,11 +1,17 @@ +/** + * \file + * \brief [Shell sort](https://en.wikipedia.org/wiki/Shell_sort) algorithm + */ #include #include #include #include +#include // for std::swap -// for std::swap -#include - +/** pretty print array + * \param[in] arr array to print + * \param[in] LEN length of array to print + */ template void show_data(T *arr, size_t LEN) { size_t i; @@ -14,11 +20,19 @@ void show_data(T *arr, size_t LEN) { std::cout << std::endl; } +/** pretty print array + * \param[in] arr array to print + * \param[in] N length of array to print + */ template void show_data(T (&arr)[N]) { show_data(arr, N); } +/** \namespace sorting + * \brief Sorting algorithms + */ +namespace sorting { /** * Optimized algorithm - takes half the time by utilizing * Mar @@ -42,11 +56,17 @@ void shell_sort(T *arr, size_t LEN) { } } +/** function overload - when input array is of a known length array type + */ template void shell_sort(T (&arr)[N]) { shell_sort(arr, N); } +} // namespace sorting + +using sorting::shell_sort; + /** * function to compare sorting using cstdlib's qsort **/ diff --git a/strings/brute_force_string_searching.cpp b/strings/brute_force_string_searching.cpp index 9a2b5327a..fd5244b37 100644 --- a/strings/brute_force_string_searching.cpp +++ b/strings/brute_force_string_searching.cpp @@ -10,6 +10,7 @@ #endif #include +namespace string_search { /** * Find a pattern in a string by comparing the pattern to every substring. * @param text Any string that might contain the pattern. @@ -32,6 +33,9 @@ int brute_force(const std::string &text, const std::string &pattern) { } return index; } +} // namespace string_search + +using string_search::brute_force; /** set of test cases */ const std::vector> test_set = { diff --git a/strings/knuth_morris_pratt.cpp b/strings/knuth_morris_pratt.cpp index b83cab966..d116ddcff 100644 --- a/strings/knuth_morris_pratt.cpp +++ b/strings/knuth_morris_pratt.cpp @@ -20,6 +20,7 @@ #endif #include +namespace string_search { /** * Generate the partial match table aka failure function for a pattern to * search. @@ -64,6 +65,9 @@ bool kmp(const std::string &pattern, const std::string &text) { } return false; } +} // namespace string_search + +using string_search::kmp; /** Main function */ int main() { diff --git a/strings/rabin_karp.cpp b/strings/rabin_karp.cpp index 018ff5632..91e104188 100644 --- a/strings/rabin_karp.cpp +++ b/strings/rabin_karp.cpp @@ -15,6 +15,7 @@ #define PRIME 5 ///< Prime modulus for hash functions +namespace string_search { /** * convert a string to an intger - called as hashing function * \param[in] s source of string to hash @@ -96,6 +97,10 @@ int rabin_karp(const std::string& str, const std::string& pat) { return -1; // return -1 if given pattern not found } +} // namespace string_search + +using string_search::rabin_karp; + /** Main function */ int main(void) { assert(rabin_karp("helloWorld", "world") == -1);