mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-24 04:34:51 +08:00
Merge pull request #11 from kvedala/add_namespaces
[feat:] Better organization using namespaces
This commit is contained in:
@@ -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<double> &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<double> &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 <int N>
|
||||
void fit(std::vector<double> const (&X)[N], const int *y) {
|
||||
@@ -181,6 +183,10 @@ class adaline {
|
||||
std::vector<double> 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.
|
||||
|
||||
@@ -10,6 +10,12 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* \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
|
||||
*/
|
||||
|
||||
@@ -8,13 +8,14 @@
|
||||
#include <iostream>
|
||||
#include <utility> // for std::move & std::remove_reference_t
|
||||
|
||||
namespace sorting {
|
||||
template <class Iterator>
|
||||
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 : ";
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
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] << " ";
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Shell sort](https://en.wikipedia.org/wiki/Shell_sort) algorithm
|
||||
*/
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <utility> // for std::swap
|
||||
|
||||
// for std::swap
|
||||
#include <utility>
|
||||
|
||||
/** pretty print array
|
||||
* \param[in] arr array to print
|
||||
* \param[in] LEN length of array to print
|
||||
*/
|
||||
template <class T>
|
||||
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 <class T, size_t N>
|
||||
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 <class T, size_t N>
|
||||
void shell_sort(T (&arr)[N]) {
|
||||
shell_sort(arr, N);
|
||||
}
|
||||
|
||||
} // namespace sorting
|
||||
|
||||
using sorting::shell_sort;
|
||||
|
||||
/**
|
||||
* function to compare sorting using cstdlib's qsort
|
||||
**/
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#endif
|
||||
#include <vector>
|
||||
|
||||
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<std::vector<std::string>> test_set = {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#endif
|
||||
#include <vector>
|
||||
|
||||
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() {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user