diff --git a/d1/dbe/lu__decomposition_8h_source.html b/d1/dbe/lu__decomposition_8h_source.html index cec8e6897..1059639dd 100644 --- a/d1/dbe/lu__decomposition_8h_source.html +++ b/d1/dbe/lu__decomposition_8h_source.html @@ -250,6 +250,7 @@ $(document).ready(function(){initNavTree('d1/dbe/lu__decomposition_8h_source.htm
std::isnan
T isnan(T... args)
statistics
Statistical algorithms.
test2
void test2()
Definition: sum_of_digits.cpp:49
+
EPSILON
constexpr double EPSILON
system accuracy limit
Definition: newton_raphson_method.cpp:20
std::snprintf
T snprintf(T... args)
std::random_device
std::numeric_limits::infinity
T infinity(T... args)
diff --git a/d4/d60/kohonen__som__trace_8cpp__incl.map b/d4/d60/kohonen__som__trace_8cpp__incl.map index 8c5c42875..f2ffc3fd8 100644 --- a/d4/d60/kohonen__som__trace_8cpp__incl.map +++ b/d4/d60/kohonen__som__trace_8cpp__incl.map @@ -1,11 +1,12 @@ - + - - - - - - - + + + + + + + + diff --git a/d4/d60/kohonen__som__trace_8cpp__incl.md5 b/d4/d60/kohonen__som__trace_8cpp__incl.md5 index 22bd2e644..98b3d50e8 100644 --- a/d4/d60/kohonen__som__trace_8cpp__incl.md5 +++ b/d4/d60/kohonen__som__trace_8cpp__incl.md5 @@ -1 +1 @@ -b35af84eeb4930be52757e882a8fae69 \ No newline at end of file +68e4d2a09d8d5f7bbb65ad5310533e4a \ No newline at end of file diff --git a/d4/d60/kohonen__som__trace_8cpp__incl.svg b/d4/d60/kohonen__som__trace_8cpp__incl.svg index fd535f686..347b20916 100644 --- a/d4/d60/kohonen__som__trace_8cpp__incl.svg +++ b/d4/d60/kohonen__som__trace_8cpp__incl.svg @@ -46,7 +46,7 @@ if (edges && edges.length) { @@ -54,14 +54,14 @@ var sectionId = 'dynsection-0'; machine_learning/kohonen_som_trace.cpp - + Node1 - -machine_learning/kohonen -_som_trace.cpp + +machine_learning/kohonen +_som_trace.cpp @@ -77,113 +77,128 @@ var sectionId = 'dynsection-0'; Node1->Node2 - - + + Node3 - -cmath + +array Node1->Node3 - - + + Node4 - -cstdlib + +cmath Node1->Node4 - - + + Node5 - -ctime + +cstdlib Node1->Node5 - - + + Node6 - -fstream + +ctime Node1->Node6 - - + + Node7 - -iostream + +fstream Node1->Node7 - - + + Node8 - -valarray + +iostream Node1->Node8 - - + + Node9 - -vector + +valarray Node1->Node9 - - + + + + + +Node10 + + +vector + + + + + +Node1->Node10 + + diff --git a/d4/d60/kohonen__som__trace_8cpp__incl_org.svg b/d4/d60/kohonen__som__trace_8cpp__incl_org.svg index 4f2c0fa3a..bff297931 100644 --- a/d4/d60/kohonen__som__trace_8cpp__incl_org.svg +++ b/d4/d60/kohonen__som__trace_8cpp__incl_org.svg @@ -4,18 +4,18 @@ - + machine_learning/kohonen_som_trace.cpp - + Node1 - -machine_learning/kohonen -_som_trace.cpp + +machine_learning/kohonen +_som_trace.cpp @@ -31,113 +31,128 @@ Node1->Node2 - - + + Node3 - -cmath + +array Node1->Node3 - - + + Node4 - -cstdlib + +cmath Node1->Node4 - - + + Node5 - -ctime + +cstdlib Node1->Node5 - - + + Node6 - -fstream + +ctime Node1->Node6 - - + + Node7 - -iostream + +fstream Node1->Node7 - - + + Node8 - -valarray + +iostream Node1->Node8 - - + + Node9 - -vector + +valarray Node1->Node9 - - + + + + + +Node10 + + +vector + + + + + +Node1->Node10 + + diff --git a/d4/d68/qr__decompose_8h_source.html b/d4/d68/qr__decompose_8h_source.html index 23034d7f4..4f9077c19 100644 --- a/d4/d68/qr__decompose_8h_source.html +++ b/d4/d68/qr__decompose_8h_source.html @@ -309,26 +309,27 @@ $(document).ready(function(){initNavTree('d4/d68/qr__decompose_8h_source.html','
save_exact_solution
void save_exact_solution(const double &X0, const double &X_MAX, const double &step_size, const std::valarray< double > &Y0)
Definition: ode_midpoint_euler.cpp:156
std::srand
T srand(T... args)
semi_implicit_euler
double semi_implicit_euler(double dx, double x0, double x_max, std::valarray< double > *y, bool save_to_file=false)
Compute approximation using the semi-implicit-Euler method in the given limits.
Definition: ode_semi_implicit_euler.cpp:103
-
MAX_ITERATIONS
#define MAX_ITERATIONS
Maximum number of iterations to check.
Definition: newton_raphson_method.cpp:21
semi_implicit_euler_step
void semi_implicit_euler_step(const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the semi-implicit-Euler method.
Definition: ode_semi_implicit_euler.cpp:82
std::clock_t
eq
static double eq(double i)
Definition: newton_raphson_method.cpp:29
+
forward_euler_step
void forward_euler_step(const double dx, const double x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the forward-Euler method.
Definition: ode_forward_euler.cpp:86
std::cos
T cos(T... args)
eq_der
static double eq_der(double i)
Definition: newton_raphson_method.cpp:39
exact_solution
void exact_solution(const double &x, std::valarray< double > *y)
Exact solution of the problem. Used for solution comparison.
Definition: ode_forward_euler.cpp:67
std::atof
T atof(T... args)
-
main
int main(int argc, char *argv[])
Definition: ode_forward_euler.cpp:188
+
main
int main(int argc, char *argv[])
Definition: ode_forward_euler.cpp:189
std::setfill
T setfill(T... args)
midpoint_euler_step
void midpoint_euler_step(const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the midpoint-Euler method.
Definition: ode_midpoint_euler.cpp:85
std::sqrt
T sqrt(T... args)
qr_algorithm
Functions to compute QR decomposition of any rectangular matrix.
-
forward_euler_step
void forward_euler_step(const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the forward-Euler method.
Definition: ode_forward_euler.cpp:86
std::clock
T clock(T... args)
-
save_exact_solution
void save_exact_solution(const double &X0, const double &X_MAX, const double &step_size, const std::valarray< double > &Y0)
Definition: ode_forward_euler.cpp:152
+
save_exact_solution
void save_exact_solution(const double &X0, const double &X_MAX, const double &step_size, const std::valarray< double > &Y0)
Definition: ode_forward_euler.cpp:153
+
EPSILON
constexpr double EPSILON
system accuracy limit
Definition: newton_raphson_method.cpp:20
qr_algorithm::vector_proj
std::valarray< T > vector_proj(const std::valarray< T > &a, const std::valarray< T > &b)
Definition: qr_decompose.h:104
std::cout
std::ofstream
STL class.
qr_algorithm::vector_mag
double vector_mag(const std::valarray< T > &a)
Definition: qr_decompose.h:92
+
MAX_ITERATIONS
constexpr int16_t MAX_ITERATIONS
Maximum number of iterations.
Definition: newton_raphson_method.cpp:21
std::ofstream::close
T close(T... args)
std::perror
T perror(T... args)
std::valarray
STL class.
@@ -338,7 +339,6 @@ $(document).ready(function(){initNavTree('d4/d68/qr__decompose_8h_source.html','
std::rand
T rand(T... args)
problem
void problem(const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition: ode_semi_implicit_euler.cpp:53
std::sin
T sin(T... args)
-
EPSILON
#define EPSILON
system accuracy limit
Definition: newton_raphson_method.cpp:20
problem
void problem(const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition: ode_forward_euler.cpp:54
std::endl
T endl(T... args)
qr_algorithm::vector_dot
double vector_dot(const std::valarray< T > &a, const std::valarray< T > &b)
Definition: qr_decompose.h:76
diff --git a/d4/d86/large__number_8h_source.html b/d4/d86/large__number_8h_source.html index 71b29860e..361a05639 100644 --- a/d4/d86/large__number_8h_source.html +++ b/d4/d86/large__number_8h_source.html @@ -415,7 +415,7 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
Complex
Class Complex to represent complex numbers as a field.
Definition: complex_numbers.cpp:20
std::move
T move(T... args)
main
int main()
Main function.
Definition: fibonacci.cpp:28
-
operator+
std::vector< T > operator+(std::vector< T > const &A, std::vector< T > const &B)
Definition: ordinary_least_squares_regressor.cpp:198
+
operator+
std::vector< T > operator+(std::vector< T > const &A, std::vector< T > const &B)
Definition: ordinary_least_squares_regressor.cpp:204
large_number::operator<<
friend std::ostream & operator<<(std::ostream &out, const large_number &a)
Definition: large_number.h:146
large_number::large_number
large_number(std::vector< unsigned char > &vec)
Definition: large_number.h:51
std::atan2
T atan2(T... args)
@@ -424,14 +424,14 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
graph
Definition: bfs.cpp:3
double_factorial_iterative
uint64_t double_factorial_iterative(uint64_t n)
Definition: double_factorial.cpp:17
std::cos
T cos(T... args)
-
machine_learning::adaline::adaline
adaline(int num_features, const double eta=0.01f, const double accuracy=1e-5)
Definition: adaline_learning.cpp:53
+
machine_learning::adaline::adaline
adaline(int num_features, const double eta=0.01f, const double accuracy=1e-5)
Definition: adaline_learning.cpp:55
Trie
Definition: trie_modern.cpp:16
std::vector< int >
std::system
T system(T... args)
std::unordered_map::find
T find(T... args)
-
test1
void test1()
Definition: kohonen_som_topology.cpp:359
+
test1
void test1()
Definition: kohonen_som_topology.cpp:369
std::string::size
T size(T... args)
-
machine_learning::adaline::fit
double fit(const std::vector< double > &x, const int &y)
Definition: adaline_learning.cpp:112
+
machine_learning::adaline::fit
double fit(const std::vector< double > &x, const int &y)
Definition: adaline_learning.cpp:119
Complex::arg
double arg() const
Member function to give the argument of our complex number.
Definition: complex_numbers.cpp:87
ans
ll ans(ll n)
Definition: matrix_exponentiation.cpp:91
std::stack
STL class.
@@ -440,7 +440,7 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
fib
large_number fib(uint64_t n)
Definition: fibonacci_large.cpp:24
main
int main()
Definition: fibonacci_fast.cpp:51
std::iterator
-
main
int main(int argc, char **argv)
Definition: kohonen_som_topology.cpp:566
+
main
int main(int argc, char **argv)
Definition: kohonen_som_topology.cpp:582
Trie::hasChildren
static bool hasChildren(std::shared_ptr< TrieNode > node)
Definition: trie_modern.cpp:41
Complex::operator-
Complex operator-(const Complex &other)
Operator overload of '-' on Complex class. Operator overload to be able to subtract two complex numbe...
Definition: complex_numbers.cpp:106
main
int main(int argc, char **argv)
Definition: spirograph.cpp:268
@@ -450,7 +450,7 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
std::stringstream
STL class.
Complex::abs
double abs() const
Member function to give the modulus of our complex number. Member function to which gives the absolut...
Definition: complex_numbers.cpp:79
main
int main(int argc, char *argv[])
Definition: large_factorial.cpp:89
-
test_3d_classes
void test_3d_classes(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_trace.cpp:347
+
test_3d_classes
void test_3d_classes(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_trace.cpp:359
Trie::TrieNode
Definition: trie_modern.cpp:26
std::distance
T distance(T... args)
gcd
int gcd(int num1, int num2)
Definition: gcd_recursive_euclidean.cpp:14
@@ -460,38 +460,41 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
binExpo_alt
int binExpo_alt(int a, int b)
Definition: binary_exponent.cpp:42
graph::explore
void explore(int, vector< bool > &)
Utility function for depth first seach algorithm this function explores the vertex which is passed in...
Definition: connected_components.cpp:101
std::search
T search(T... args)
-
save_nd_data
int save_nd_data(const char *fname, const std::vector< std::valarray< double >> &X)
Definition: kohonen_som_trace.cpp:57
+
save_nd_data
int save_nd_data(const char *fname, const std::vector< std::valarray< double >> &X)
Definition: kohonen_som_trace.cpp:58
node
struct list node
std::scanf
T scanf(T... args)
-
get_clock_diff
double get_clock_diff(clock_t start_t, clock_t end_t)
Definition: kohonen_som_trace.cpp:438
+
get_clock_diff
double get_clock_diff(clock_t start_t, clock_t end_t)
Definition: kohonen_som_trace.cpp:452
test2
void test2(const std::string &text)
Self test 2 - using 8x8 randomly generated key.
Definition: hill_cipher.cpp:505
std::setfill
T setfill(T... args)
std::reverse
T reverse(T... args)
Graph::Graph
Graph(unsigned int vertices, std::vector< Edge > const &edges)
Definition: cycle_check_directed_graph.cpp:90
std::vector::back
T back(T... args)
-
machine_learning::adaline::predict
int predict(const std::vector< double > &x, double *out=nullptr)
Definition: adaline_learning.cpp:92
+
machine_learning::adaline::predict
int predict(const std::vector< double > &x, double *out=nullptr)
Definition: adaline_learning.cpp:95
tests
void tests()
Definition: complex_numbers.cpp:206
machine_learning
Machine learning algorithms.
Complex::operator=
const Complex & operator=(const Complex &other)
Operator overload of '=' on Complex class. Operator overload to be able to copy RHS instance of Compl...
Definition: complex_numbers.cpp:160
-
is_square
bool is_square(std::vector< std::vector< T >> const &A)
Definition: ordinary_least_squares_regressor.cpp:57
+
std::strerror
T strerror(T... args)
+
is_square
bool is_square(std::vector< std::vector< T >> const &A)
Definition: ordinary_least_squares_regressor.cpp:59
+
machine_learning::adaline::activation
int activation(double x)
Definition: adaline_learning.cpp:186
main
int main()
Definition: gcd_recursive_euclidean.cpp:42
-
machine_learning::update_weights
double update_weights(const std::valarray< double > &X, std::vector< std::vector< std::valarray< double >>> *W, std::vector< std::valarray< double >> *D, double alpha, int R)
Definition: kohonen_som_topology.cpp:193
+
machine_learning::update_weights
double update_weights(const std::valarray< double > &X, std::vector< std::vector< std::valarray< double >>> *W, std::vector< std::valarray< double >> *D, double alpha, int R)
Definition: kohonen_som_topology.cpp:200
std::queue
STL class.
large_number::num_digits
size_t num_digits() const
Definition: large_number.h:130
Graph
Definition: bellman_ford.cpp:13
Solution
Definition: bridge_finding_with_tarjan_algorithm.cpp:13
-
get_min_2d
void get_min_2d(const std::vector< std::valarray< double >> &X, double *val, int *x_idx, int *y_idx)
Definition: kohonen_som_topology.cpp:99
+
get_min_2d
void get_min_2d(const std::vector< std::valarray< double >> &X, double *val, int *x_idx, int *y_idx)
Definition: kohonen_som_topology.cpp:105
std::queue::front
T front(T... args)
std::sort
T sort(T... args)
-
machine_learning::adaline::check_size_match
bool check_size_match(const std::vector< double > &x)
Definition: adaline_learning.cpp:176
+
machine_learning::adaline::check_size_match
bool check_size_match(const std::vector< double > &x)
Definition: adaline_learning.cpp:196
std::sqrt
T sqrt(T... args)
stack_idx
int stack_idx
pointer to track stack index
Definition: paranthesis_matching.cpp:23
Entry
Definition: double_hash_hash_table.cpp:28
-
test1
void test1()
Definition: kohonen_som_trace.cpp:225
+
test1
void test1()
Definition: kohonen_som_trace.cpp:233
std::stringstream::clear
T clear(T... args)
large_number::operator+=
large_number & operator+=(large_number n)
Definition: large_number.h:192
query
Definition: mo.cpp:6
-
operator*
std::vector< std::vector< T > > operator*(std::vector< std::vector< T >> const &A, std::vector< std::vector< T >> const &B)
Definition: ordinary_least_squares_regressor.cpp:74
+
operator*
std::vector< std::vector< T > > operator*(std::vector< std::vector< T >> const &A, std::vector< std::vector< T >> const &B)
Definition: ordinary_least_squares_regressor.cpp:78
+
MAX_ITER
constexpr int MAX_ITER
Definition: adaline_learning.cpp:40
spirograph
is_prime
bool is_prime(T num)
Definition: check_prime.cpp:22
fast_power_recursive
double fast_power_recursive(T a, T b)
Definition: fast_power.cpp:26
@@ -502,14 +505,14 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
Point::y
int y
Point respect to x coordinate.
Definition: line_segment_intersection.cpp:14
graph::depth_first_search
void depth_first_search()
Function that perfoms depth first search algorithm on graph.
Definition: connected_components.cpp:83
std::clock
T clock(T... args)
-
main
int main(int argc, char **argv)
Definition: kohonen_som_trace.cpp:443
+
main
int main(int argc, char **argv)
Definition: kohonen_som_trace.cpp:457
Complex::real
double real() const
Member function to get real value of our complex number. Member function (getter) to access the class...
Definition: complex_numbers.cpp:64
large_number::large_number
large_number(const large_number &a)
Definition: large_number.h:48
fibonacci
int fibonacci(unsigned int n)
Definition: fibonacci.cpp:17
Trie::insert
void insert(const std::string &word)
Definition: trie_modern.cpp:109
operator<<
std::ostream & operator<<(std::ostream &out, std::vector< std::vector< T >> const &v)
Definition: ordinary_least_squares_regressor.cpp:22
CycleCheck::isCyclicDFS
static bool isCyclicDFS(Graph const &graph)
Definition: cycle_check_directed_graph.cpp:206
-
test3
void test3()
Definition: kohonen_som_topology.cpp:523
+
test3
void test3()
Definition: kohonen_som_topology.cpp:537
createNode
node * createNode(int data)
Definition: avltree.cpp:21
CycleCheck
Definition: cycle_check_directed_graph.cpp:153
CycleCheck::isCyclicDFSHelper
static bool isCyclicDFSHelper(AdjList const &adjList, std::vector< nodeStates > *state, unsigned int node)
Definition: cycle_check_directed_graph.cpp:165
@@ -522,7 +525,7 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
main
int main()
Definition: connected_components.cpp:111
Complex::operator/
Complex operator/(const Complex &other)
Operator overload of '/' on Complex class. Operator overload to be able to divide two complex numbers...
Definition: complex_numbers.cpp:142
large_number::operator++
large_number & operator++()
Definition: large_number.h:175
-
test1
void test1(double eta=0.01)
Definition: adaline_learning.cpp:204
+
test1
void test1(double eta=0.01)
Definition: adaline_learning.cpp:224
insert
node * insert(node *root, int item)
Definition: avltree.cpp:66
std::printf
T printf(T... args)
std::vector::at
T at(T... args)
@@ -533,7 +536,7 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
std::ofstream
STL class.
SegmentIntersection::direction
int direction(Point first_point, Point second_point, Point third_point)
Definition: line_segment_intersection.cpp:63
Graph::getAdjList
std::remove_reference_t< AdjList > const & getAdjList() const
Definition: cycle_check_directed_graph.cpp:105
-
machine_learning::adaline::eta
const double eta
learning rate of the algorithm
Definition: adaline_learning.cpp:187
+
machine_learning::adaline::eta
const double eta
learning rate of the algorithm
Definition: adaline_learning.cpp:207
std::min_element
T min_element(T... args)
phiFunction
uint64_t phiFunction(uint64_t n)
Definition: eulers_totient_function.cpp:32
std::queue::pop
T pop(T... args)
@@ -548,39 +551,39 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
main
int main()
Definition: factorial.cpp:15
large_number::multiply
void multiply(const T n)
Definition: large_number.h:258
Complex::operator~
Complex operator~() const
Operator overload of '~' on Complex class. Operator overload of the BITWISE NOT which gives us the co...
Definition: complex_numbers.cpp:130
-
std::perror
T perror(T... args)
operator<<
std::ostream & operator<<(std::ostream &os, const Complex &num)
Operator overload of '<<' of ostream for Complex class. Overloaded insersion operator to accommodate ...
Definition: complex_numbers.cpp:186
ll
#define ll
Definition: matrix_exponentiation.cpp:33
std::set::erase
T erase(T... args)
-
machine_learning::adaline::weights
std::vector< double > weights
weights of the neural network
Definition: adaline_learning.cpp:189
+
machine_learning::adaline::weights
std::vector< double > weights
weights of the neural network
Definition: adaline_learning.cpp:209
std::valarray
STL class.
SegmentIntersection
Definition: line_segment_intersection.cpp:22
-
main
int main()
Definition: ordinary_least_squares_regressor.cpp:410
+
main
int main()
Definition: ordinary_least_squares_regressor.cpp:423
std::complex::real
T real(T... args)
-
predict_OLS_regressor
std::vector< float > predict_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< float > const &beta)
Definition: ordinary_least_squares_regressor.cpp:342
+
predict_OLS_regressor
std::vector< float > predict_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< float > const &beta)
Definition: ordinary_least_squares_regressor.cpp:352
std::runtime_error
STL class.
large_number::operator=
large_number & operator=(const large_number &b)
Definition: large_number.h:229
main
int main()
Main function.
Definition: extended_euclid_algorithm.cpp:87
std::ifstream::open
T open(T... args)
-
test_3d_classes1
void test_3d_classes1(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_topology.cpp:399
+
machine_learning::adaline::fit
void fit(std::array< std::vector< double >, N > const &X, std::array< int, N > const &Y)
Definition: adaline_learning.cpp:145
+
test_3d_classes1
void test_3d_classes1(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_topology.cpp:411
fib
uint64_t fib(uint64_t n)
Definition: fibonacci_fast.cpp:30
std::invalid_argument
STL class.
-
test2
void test2()
Definition: kohonen_som_topology.cpp:439
+
test2
void test2()
Definition: kohonen_som_topology.cpp:451
SegmentIntersection::on_segment
bool on_segment(Point first_point, Point second_point, Point third_point)
Definition: line_segment_intersection.cpp:75
operator==
bool operator==(const Complex &a, const Complex &b)
Operator overload of '==' on Complex class. Logical Equal overload for our Complex class.
Definition: complex_numbers.cpp:175
std::remove
T remove(T... args)
large_number::_digits
std::vector< unsigned char > _digits
Definition: large_number.h:285
endl
#define endl
Definition: matrix_exponentiation.cpp:36
stack::push
void push(Type item)
Definition: stack.h:83
-
test2
void test2()
Definition: kohonen_som_trace.cpp:305
+
test2
void test2()
Definition: kohonen_som_trace.cpp:315
std::is_integral
main
int main()
Definition: check_prime.cpp:45
test2
bool test2()
Definition: large_factorial.cpp:54
Graph::Graph
Graph(unsigned int vertices, AdjList &&adjList)
Definition: cycle_check_directed_graph.cpp:78
std::priority_queue
STL class.
std::rand
T rand(T... args)
-
save_2d_data
int save_2d_data(const char *fname, const std::vector< std::valarray< double >> &X)
Definition: kohonen_som_topology.cpp:62
-
test_lamniscate
void test_lamniscate(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_trace.cpp:267
+
save_2d_data
int save_2d_data(const char *fname, const std::vector< std::valarray< double >> &X)
Definition: kohonen_som_topology.cpp:65
+
test_lamniscate
void test_lamniscate(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_trace.cpp:277
std::swap
T swap(T... args)
main
int main()
Definition: trie_modern.cpp:160
std::min
T min(T... args)
@@ -595,34 +598,36 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
spirograph::test
void test()
Test function to save resulting points to a CSV file.
Definition: spirograph.cpp:93
queue
Definition: queue.h:17
large_number::test
static bool test()
Definition: large_number.h:65
-
fit_OLS_regressor
std::vector< float > fit_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< T > const &Y)
Definition: ordinary_least_squares_regressor.cpp:312
-
operator-
std::vector< T > operator-(std::vector< T > const &A, std::vector< T > const &B)
Definition: ordinary_least_squares_regressor.cpp:177
+
fit_OLS_regressor
std::vector< float > fit_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< T > const &Y)
Definition: ordinary_least_squares_regressor.cpp:321
+
operator-
std::vector< T > operator-(std::vector< T > const &A, std::vector< T > const &B)
Definition: ordinary_least_squares_regressor.cpp:183
std::set::lower_bound
T lower_bound(T... args)
std::endl
T endl(T... args)
main
int main()
Definition: armstrong_number.cpp:77
-
get_inverse
std::vector< std::vector< float > > get_inverse(std::vector< std::vector< T >> const &A)
Definition: ordinary_least_squares_regressor.cpp:220
+
get_inverse
std::vector< std::vector< float > > get_inverse(std::vector< std::vector< T >> const &A)
Definition: ordinary_least_squares_regressor.cpp:226
main
int main()
Definition: complex_numbers.cpp:268
std::left
T left(T... args)
graph::adj
vector< vector< int > > adj
adj stores adjacency list representation of graph
Definition: connected_components.cpp:40
large_number::operator==
friend bool operator==(large_number const &a, large_number const &b)
Definition: large_number.h:155
-
machine_learning::adaline::operator<<
friend std::ostream & operator<<(std::ostream &out, const adaline &ada)
Definition: adaline_learning.cpp:74
+
machine_learning::adaline::operator<<
friend std::ostream & operator<<(std::ostream &out, const adaline &ada)
Definition: adaline_learning.cpp:76
std::exp
T exp(T... args)
std::begin
T begin(T... args)
-
machine_learning::save_u_matrix
int save_u_matrix(const char *fname, const std::vector< std::vector< std::valarray< double >>> &W)
Definition: kohonen_som_topology.cpp:135
+
machine_learning::save_u_matrix
int save_u_matrix(const char *fname, const std::vector< std::vector< std::valarray< double >>> &W)
Definition: kohonen_som_topology.cpp:142
factorial
unsigned int factorial(unsigned int n)
Definition: factorial.cpp:8
std::greater
std
STL namespace.
tests
void tests()
Definition: double_factorial.cpp:50
std::set::insert
T insert(T... args)
+
machine_learning::MIN_DISTANCE
constexpr double MIN_DISTANCE
Definition: kohonen_som_topology.cpp:129
show
void show(const struct tower *const F, const struct tower *const T, const struct tower *const U)
Definition: tower_of_hanoi.cpp:19
compare
Definition: huffman.cpp:28
std::ios_base::sync_with_stdio
T sync_with_stdio(T... args)
stack
Definition: stack.h:26
-
get_clock_diff
double get_clock_diff(clock_t start_t, clock_t end_t)
Definition: kohonen_som_topology.cpp:561
+
get_clock_diff
double get_clock_diff(clock_t start_t, clock_t end_t)
Definition: kohonen_som_topology.cpp:577
gcd
int gcd(int *a, int n)
Definition: gcd_of_n_numbers.cpp:15
main
int main()
Definition: fast_power.cpp:68
double_factorial_recursive
uint64_t double_factorial_recursive(uint64_t n)
Definition: double_factorial.cpp:30
std::count
T count(T... args)
+
std::ptrdiff_t
Trie::search
bool search(const std::string &word)
Definition: trie_modern.cpp:132
std::fixed
T fixed(T... args)
Item
Definition: knapsack.cpp:4
@@ -631,11 +636,11 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
std::vector::assign
T assign(T... args)
stack::display
void display()
Definition: stack.h:29
extendedEuclid
void extendedEuclid(T A, T B, T *GCD, T2 *x, T2 *y)
Definition: extended_euclid_algorithm.cpp:70
-
test3
void test3()
Definition: kohonen_som_trace.cpp:402
+
test3
void test3()
Definition: kohonen_som_trace.cpp:414
std::stack::push
T push(T... args)
graph::graph
graph(int n)
Constructor that intiliazes the graph on creation and set the connected components to 0.
Definition: connected_components.cpp:53
test
void test()
Definition: check_amicable_pair.cpp:56
-
ols_test
void ols_test()
Definition: ordinary_least_squares_regressor.cpp:358
+
ols_test
void ols_test()
Definition: ordinary_least_squares_regressor.cpp:369
stack::isEmptyStack
bool isEmptyStack()
Definition: stack.h:80
Graph::getVertices
std::remove_reference_t< unsigned int > const & getVertices() const
Definition: cycle_check_directed_graph.cpp:112
std::complex
STL class.
@@ -652,20 +657,19 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
Complex::operator+
Complex operator+(const Complex &other)
Operator overload of '+' on Complex class. Operator overload to be able to add two complex numbers.
Definition: complex_numbers.cpp:95
extendedEuclid_1
void extendedEuclid_1(T1 A, T1 B, T1 *GCD, T2 *x, T2 *y)
Definition: extended_euclid_algorithm.cpp:41
std::setw
T setw(T... args)
-
machine_learning::adaline::accuracy
const double accuracy
model fit convergence accuracy
Definition: adaline_learning.cpp:188
+
machine_learning::adaline::accuracy
const double accuracy
model fit convergence accuracy
Definition: adaline_learning.cpp:208
main
int main()
Definition: gcd_iterative_euclidean.cpp:47
std::max
T max(T... args)
-
test_2d_classes
void test_2d_classes(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_topology.cpp:320
+
test_2d_classes
void test_2d_classes(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_topology.cpp:330
std::range_error
STL class.
Point
Definition: line_segment_intersection.cpp:12
large_number::operator!=
friend bool operator!=(large_number const &a, large_number const &b)
Definition: large_number.h:168
-
machine_learning::kohonen_som
void kohonen_som(const std::vector< std::valarray< double >> &X, std::vector< std::vector< std::valarray< double >>> *W, double alpha_min)
Definition: kohonen_som_topology.cpp:261
+
machine_learning::kohonen_som
void kohonen_som(const std::vector< std::valarray< double >> &X, std::vector< std::vector< std::valarray< double >>> *W, double alpha_min)
Definition: kohonen_som_topology.cpp:269
push
void push(char ch)
push byte to stack variable
Definition: paranthesis_matching.cpp:26
Trie::removeWordHelper
std::shared_ptr< TrieNode > removeWordHelper(const std::string &word, std::shared_ptr< TrieNode > curr, size_t index)
Definition: trie_modern.cpp:64
-
test_3d_classes2
void test_3d_classes2(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_topology.cpp:479
+
test_3d_classes2
void test_3d_classes2(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_topology.cpp:493
pop
char pop()
pop a byte out of stack variable
Definition: paranthesis_matching.cpp:29
-
machine_learning::adaline::fit
void fit(std::vector< double > const (&X)[N], const int *y)
Definition: adaline_learning.cpp:136
-
_random
double _random(double a, double b)
Definition: kohonen_som_topology.cpp:50
+
_random
double _random(double a, double b)
Definition: kohonen_som_topology.cpp:53
Graph::addEdge
void addEdge(Edge const &edge)
Definition: cycle_check_directed_graph.cpp:128
fast_power_linear
double fast_power_linear(T a, T b)
Definition: fast_power.cpp:50
spirograph::spirograph
void spirograph(std::array< std::pair< double, double >, N > *points, double l, double k, double rot)
Definition: spirograph.cpp:70
@@ -673,34 +677,34 @@ $(document).ready(function(){initNavTree('d4/d86/large__number_8h_source.html','
Graph::Graph
Graph(unsigned int vertices, AdjList const &adjList)
Definition: cycle_check_directed_graph.cpp:70
large_number::operator++
large_number & operator++(int)
Definition: large_number.h:183
sorting::quickSort
void quickSort(int arr[], int low, int high)
Definition: quick_sort.cpp:63
-
test2
void test2(double eta=0.01)
Definition: adaline_learning.cpp:238
+
test2
void test2(double eta=0.01)
Definition: adaline_learning.cpp:262
std::partition
T partition(T... args)
-
test3
void test3(double eta=0.01)
Definition: adaline_learning.cpp:289
+
test3
void test3(double eta=0.01)
Definition: adaline_learning.cpp:313
std::unordered_map< unsigned int, std::vector< unsigned int > >
-
machine_learning::adaline
Definition: adaline_learning.cpp:44
+
machine_learning::adaline
Definition: adaline_learning.cpp:46
main
int main(int argc, char *argv[])
Main function.
Definition: eulers_totient_function.cpp:48
large_number::operator+
friend large_number & operator+(const large_number &a, const T &b)
Definition: large_number.h:220
std::vector::data
T data(T... args)
std::ofstream::is_open
T is_open(T... args)
std::set
STL class.
graph::addEdge
void addEdge(int, int)
Function that add edge between two nodes or vertices of graph.
Definition: connected_components.cpp:75
-
operator/
std::vector< float > operator/(std::vector< T > const &A, float const scalar)
Definition: ordinary_least_squares_regressor.cpp:168
-
machine_learning::kohonen_som_tracer
void kohonen_som_tracer(const std::vector< std::valarray< double >> &X, std::vector< std::valarray< double >> *W, double alpha_min)
Definition: kohonen_som_trace.cpp:145
+
operator/
std::vector< float > operator/(std::vector< T > const &A, float const scalar)
Definition: ordinary_least_squares_regressor.cpp:174
+
machine_learning::kohonen_som_tracer
void kohonen_som_tracer(const std::vector< std::valarray< double >> &X, std::vector< std::valarray< double >> *W, double alpha_min)
Definition: kohonen_som_trace.cpp:149
graph::connected_components
int connected_components
keep track of connected components
Definition: connected_components.cpp:43
are_amicable
bool are_amicable(int x, int y)
Definition: check_amicable_pair.cpp:48
std::exit
T exit(T... args)
stack::clear
void clear()
Definition: stack.h:112
-
main
int main(int argc, char **argv)
Definition: adaline_learning.cpp:333
+
main
int main(int argc, char **argv)
Definition: adaline_learning.cpp:357
list< int >
std::memset
T memset(T... args)
std::hash
-
test_circle
void test_circle(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_trace.cpp:188
+
test_circle
void test_circle(std::vector< std::valarray< double >> *data)
Definition: kohonen_som_trace.cpp:196
main
int main()
Definition: check_amicable_pair.cpp:68
std::next
T next(T... args)
stack.h
This class specifies the basic operation on a stack as a linked list.
std::ifstream
STL class.
std::pow
T pow(T... args)
-
get_transpose
std::vector< std::vector< T > > get_transpose(std::vector< std::vector< T >> const &A)
Definition: ordinary_least_squares_regressor.cpp:291
+
get_transpose
std::vector< std::vector< T > > get_transpose(std::vector< std::vector< T >> const &A)
Definition: ordinary_least_squares_regressor.cpp:300
kohonen_som_topology.cpp File Reference
@@ -102,8 +103,11 @@ $(document).ready(function(){initNavTree('d4/def/kohonen__som__topology_8cpp.htm

Kohonen self organizing map (topological map) More...

#include <algorithm>
+#include <array>
+#include <cerrno>
#include <cmath>
#include <cstdlib>
+#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
@@ -122,16 +126,6 @@ Namespaces  Machine learning algorithms.
  - - - - - - -

-Macros

-#define _USE_MATH_DEFINES
 
-#define MIN_DISTANCE   1e-4
 Minimum average distance of image nodes.
 
@@ -162,6 +156,11 @@ Functions +

Functions

double _random (double a, double b)
 
int main (int argc, char **argv)
 
+ + +

+Variables

constexpr double machine_learning::MIN_DISTANCE = 1e-4
 

Detailed Description

Kohonen self organizing map (topological map)

@@ -205,9 +204,9 @@ The compiled code is much slower when compiled with MS Visual C++ 2019 than with
Returns
time difference in seconds
-
561  {
-
562  return static_cast<double>(end_t - start_t) / CLOCKS_PER_SEC;
-
563 }
+
577  {
+
578  return static_cast<double>(end_t - start_t) / CLOCKS_PER_SEC;
+
579 }
@@ -237,38 +236,38 @@ The compiled code is much slower when compiled with MS Visual C++ 2019 than with

Main function

-
566  {
-
567 #ifdef _OPENMP
-
568  std::cout << "Using OpenMP based parallelization\n";
-
569 #else
-
570  std::cout << "NOT using OpenMP based parallelization\n";
-
571 #endif
-
572 
-
573  std::srand(std::time(nullptr));
-
574 
-
575  std::clock_t start_clk = std::clock();
-
576  test1();
-
577  auto end_clk = std::clock();
-
578  std::cout << "Test 1 completed in " << get_clock_diff(start_clk, end_clk)
-
579  << " sec\n";
-
580 
-
581  start_clk = std::clock();
-
582  test2();
-
583  end_clk = std::clock();
-
584  std::cout << "Test 2 completed in " << get_clock_diff(start_clk, end_clk)
-
585  << " sec\n";
-
586 
-
587  start_clk = std::clock();
-
588  test3();
-
589  end_clk = std::clock();
-
590  std::cout << "Test 3 completed in " << get_clock_diff(start_clk, end_clk)
-
591  << " sec\n";
-
592 
-
593  std::cout
-
594  << "(Note: Calculated times include: creating test sets, training "
-
595  "model and writing files to disk.)\n\n";
-
596  return 0;
-
597 }
+
582  {
+
583 #ifdef _OPENMP
+
584  std::cout << "Using OpenMP based parallelization\n";
+
585 #else
+
586  std::cout << "NOT using OpenMP based parallelization\n";
+
587 #endif
+
588 
+
589  std::srand(std::time(nullptr));
+
590 
+
591  std::clock_t start_clk = std::clock();
+
592  test1();
+
593  auto end_clk = std::clock();
+
594  std::cout << "Test 1 completed in " << get_clock_diff(start_clk, end_clk)
+
595  << " sec\n";
+
596 
+
597  start_clk = std::clock();
+
598  test2();
+
599  end_clk = std::clock();
+
600  std::cout << "Test 2 completed in " << get_clock_diff(start_clk, end_clk)
+
601  << " sec\n";
+
602 
+
603  start_clk = std::clock();
+
604  test3();
+
605  end_clk = std::clock();
+
606  std::cout << "Test 3 completed in " << get_clock_diff(start_clk, end_clk)
+
607  << " sec\n";
+
608 
+
609  std::cout
+
610  << "(Note: Calculated times include: creating test sets, training "
+
611  "model and writing files to disk.)\n\n";
+
612  return 0;
+
613 }
Here is the call graph for this function:
@@ -297,36 +296,38 @@ Here is the call graph for this function:
  • w11.csv: initial random map
  • w12.csv: trained SOM map
  • -
    359  {
    -
    360  int j, N = 300;
    -
    361  int features = 2;
    -
    362  int num_out = 30;
    - - -
    365  for (int i = 0; i < std::max(num_out, N); i++) {
    -
    366  // loop till max(N, num_out)
    -
    367  if (i < N) // only add new arrays if i < N
    -
    368  X[i] = std::valarray<double>(features);
    -
    369  if (i < num_out) { // only add new arrays if i < num_out
    -
    370  W[i] = std::vector<std::valarray<double>>(num_out);
    -
    371  for (int k = 0; k < num_out; k++) {
    -
    372  W[i][k] = std::valarray<double>(features);
    -
    373 #ifdef _OPENMP
    -
    374 #pragma omp for
    -
    375 #endif
    -
    376  for (j = 0; j < features; j++)
    -
    377  // preallocate with random initial weights
    -
    378  W[i][k][j] = _random(-10, 10);
    -
    379  }
    -
    380  }
    -
    381  }
    -
    382 
    -
    383  test_2d_classes(&X); // create test data around circumference of a circle
    -
    384  save_2d_data("test1.csv", X); // save test data points
    -
    385  save_u_matrix("w11.csv", W); // save initial random weights
    -
    386  kohonen_som(X, &W, 1e-4); // train the SOM
    -
    387  save_u_matrix("w12.csv", W); // save the resultant weights
    -
    388 }
    +
    369  {
    +
    370  int j = 0, N = 300;
    +
    371  int features = 2;
    +
    372  int num_out = 30;
    + + +
    375  for (int i = 0; i < std::max(num_out, N); i++) {
    +
    376  // loop till max(N, num_out)
    +
    377  if (i < N) { // only add new arrays if i < N
    +
    378  X[i] = std::valarray<double>(features);
    +
    379  }
    +
    380  if (i < num_out) { // only add new arrays if i < num_out
    +
    381  W[i] = std::vector<std::valarray<double>>(num_out);
    +
    382  for (int k = 0; k < num_out; k++) {
    +
    383  W[i][k] = std::valarray<double>(features);
    +
    384 #ifdef _OPENMP
    +
    385 #pragma omp for
    +
    386 #endif
    +
    387  for (j = 0; j < features; j++) {
    +
    388  // preallocate with random initial weights
    +
    389  W[i][k][j] = _random(-10, 10);
    +
    390  }
    +
    391  }
    +
    392  }
    +
    393  }
    +
    394 
    +
    395  test_2d_classes(&X); // create test data around circumference of a circle
    +
    396  save_2d_data("test1.csv", X); // save test data points
    +
    397  save_u_matrix("w11.csv", W); // save initial random weights
    +
    398  kohonen_som(X, &W, 1e-4); // train the SOM
    +
    399  save_u_matrix("w12.csv", W); // save the resultant weights
    +
    400 }
    Here is the call graph for this function:
    @@ -355,36 +356,38 @@ Here is the call graph for this function:
  • w21.csv: initial random map
  • w22.csv: trained SOM map
  • -
    439  {
    -
    440  int j, N = 300;
    -
    441  int features = 3;
    -
    442  int num_out = 30;
    - - -
    445  for (int i = 0; i < std::max(num_out, N); i++) {
    -
    446  // loop till max(N, num_out)
    -
    447  if (i < N) // only add new arrays if i < N
    -
    448  X[i] = std::valarray<double>(features);
    -
    449  if (i < num_out) { // only add new arrays if i < num_out
    -
    450  W[i] = std::vector<std::valarray<double>>(num_out);
    -
    451  for (int k = 0; k < num_out; k++) {
    -
    452  W[i][k] = std::valarray<double>(features);
    -
    453 #ifdef _OPENMP
    -
    454 #pragma omp for
    -
    455 #endif
    -
    456  for (j = 0; j < features; j++)
    -
    457  // preallocate with random initial weights
    -
    458  W[i][k][j] = _random(-10, 10);
    -
    459  }
    -
    460  }
    -
    461  }
    -
    462 
    -
    463  test_3d_classes1(&X); // create test data around circumference of a circle
    -
    464  save_2d_data("test2.csv", X); // save test data points
    -
    465  save_u_matrix("w21.csv", W); // save initial random weights
    -
    466  kohonen_som(X, &W, 1e-4); // train the SOM
    -
    467  save_u_matrix("w22.csv", W); // save the resultant weights
    -
    468 }
    +
    451  {
    +
    452  int j = 0, N = 300;
    +
    453  int features = 3;
    +
    454  int num_out = 30;
    + + +
    457  for (int i = 0; i < std::max(num_out, N); i++) {
    +
    458  // loop till max(N, num_out)
    +
    459  if (i < N) { // only add new arrays if i < N
    +
    460  X[i] = std::valarray<double>(features);
    +
    461  }
    +
    462  if (i < num_out) { // only add new arrays if i < num_out
    +
    463  W[i] = std::vector<std::valarray<double>>(num_out);
    +
    464  for (int k = 0; k < num_out; k++) {
    +
    465  W[i][k] = std::valarray<double>(features);
    +
    466 #ifdef _OPENMP
    +
    467 #pragma omp for
    +
    468 #endif
    +
    469  for (j = 0; j < features; j++) {
    +
    470  // preallocate with random initial weights
    +
    471  W[i][k][j] = _random(-10, 10);
    +
    472  }
    +
    473  }
    +
    474  }
    +
    475  }
    +
    476 
    +
    477  test_3d_classes1(&X); // create test data around circumference of a circle
    +
    478  save_2d_data("test2.csv", X); // save test data points
    +
    479  save_u_matrix("w21.csv", W); // save initial random weights
    +
    480  kohonen_som(X, &W, 1e-4); // train the SOM
    +
    481  save_u_matrix("w22.csv", W); // save the resultant weights
    +
    482 }
    Here is the call graph for this function:
    @@ -413,36 +416,38 @@ Here is the call graph for this function:
  • w31.csv: initial random map
  • w32.csv: trained SOM map
  • -
    523  {
    -
    524  int j, N = 500;
    -
    525  int features = 3;
    -
    526  int num_out = 30;
    - - -
    529  for (int i = 0; i < std::max(num_out, N); i++) {
    -
    530  // loop till max(N, num_out)
    -
    531  if (i < N) // only add new arrays if i < N
    -
    532  X[i] = std::valarray<double>(features);
    -
    533  if (i < num_out) { // only add new arrays if i < num_out
    -
    534  W[i] = std::vector<std::valarray<double>>(num_out);
    -
    535  for (int k = 0; k < num_out; k++) {
    -
    536  W[i][k] = std::valarray<double>(features);
    -
    537 #ifdef _OPENMP
    -
    538 #pragma omp for
    -
    539 #endif
    -
    540  for (j = 0; j < features; j++)
    -
    541  // preallocate with random initial weights
    -
    542  W[i][k][j] = _random(-10, 10);
    -
    543  }
    -
    544  }
    -
    545  }
    -
    546 
    -
    547  test_3d_classes2(&X); // create test data around circumference of a circle
    -
    548  save_2d_data("test3.csv", X); // save test data points
    -
    549  save_u_matrix("w31.csv", W); // save initial random weights
    -
    550  kohonen_som(X, &W, 1e-4); // train the SOM
    -
    551  save_u_matrix("w32.csv", W); // save the resultant weights
    -
    552 }
    +
    537  {
    +
    538  int j = 0, N = 500;
    +
    539  int features = 3;
    +
    540  int num_out = 30;
    + + +
    543  for (int i = 0; i < std::max(num_out, N); i++) {
    +
    544  // loop till max(N, num_out)
    +
    545  if (i < N) { // only add new arrays if i < N
    +
    546  X[i] = std::valarray<double>(features);
    +
    547  }
    +
    548  if (i < num_out) { // only add new arrays if i < num_out
    +
    549  W[i] = std::vector<std::valarray<double>>(num_out);
    +
    550  for (int k = 0; k < num_out; k++) {
    +
    551  W[i][k] = std::valarray<double>(features);
    +
    552 #ifdef _OPENMP
    +
    553 #pragma omp for
    +
    554 #endif
    +
    555  for (j = 0; j < features; j++) {
    +
    556  // preallocate with random initial weights
    +
    557  W[i][k][j] = _random(-10, 10);
    +
    558  }
    +
    559  }
    +
    560  }
    +
    561  }
    +
    562 
    +
    563  test_3d_classes2(&X); // create test data around circumference of a circle
    +
    564  save_2d_data("test3.csv", X); // save test data points
    +
    565  save_u_matrix("w31.csv", W); // save initial random weights
    +
    566  kohonen_som(X, &W, 1e-4); // train the SOM
    +
    567  save_u_matrix("w32.csv", W); // save the resultant weights
    +
    568 }
    Here is the call graph for this function:
    @@ -479,36 +484,36 @@ Here is the call graph for this function:
    -
    320  {
    -
    321  const int N = data->size();
    -
    322  const double R = 0.3; // radius of cluster
    -
    323  int i;
    -
    324  const int num_classes = 4;
    -
    325  const double centres[][2] = {
    -
    326  // centres of each class cluster
    -
    327  {.5, .5}, // centre of class 1
    -
    328  {.5, -.5}, // centre of class 2
    -
    329  {-.5, .5}, // centre of class 3
    -
    330  {-.5, -.5} // centre of class 4
    -
    331  };
    -
    332 
    -
    333 #ifdef _OPENMP
    -
    334 #pragma omp for
    -
    335 #endif
    -
    336  for (i = 0; i < N; i++) {
    -
    337  // select a random class for the point
    -
    338  int cls = std::rand() % num_classes;
    -
    339 
    -
    340  // create random coordinates (x,y,z) around the centre of the class
    -
    341  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    -
    342  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    -
    343 
    -
    344  /* The follosing can also be used
    -
    345  for (int j = 0; j < 2; j++)
    -
    346  data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);
    -
    347  */
    -
    348  }
    -
    349 }
    +
    330  {
    +
    331  const int N = data->size();
    +
    332  const double R = 0.3; // radius of cluster
    +
    333  int i = 0;
    +
    334  const int num_classes = 4;
    +
    335  std::array<std::array<double, 2>, num_classes> centres = {
    +
    336  // centres of each class cluster
    +
    337  std::array<double, 2>({.5, .5}), // centre of class 1
    +
    338  std::array<double, 2>({.5, -.5}), // centre of class 2
    +
    339  std::array<double, 2>({-.5, .5}), // centre of class 3
    +
    340  std::array<double, 2>({-.5, -.5}) // centre of class 4
    +
    341  };
    +
    342 
    +
    343 #ifdef _OPENMP
    +
    344 #pragma omp for
    +
    345 #endif
    +
    346  for (i = 0; i < N; i++) {
    +
    347  // select a random class for the point
    +
    348  int cls = std::rand() % num_classes;
    +
    349 
    +
    350  // create random coordinates (x,y,z) around the centre of the class
    +
    351  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    +
    352  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    +
    353 
    +
    354  /* The follosing can also be used
    +
    355  for (int j = 0; j < 2; j++)
    +
    356  data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);
    +
    357  */
    +
    358  }
    +
    359 }
    Here is the call graph for this function:
    @@ -545,37 +550,37 @@ Here is the call graph for this function:
    -
    399  {
    -
    400  const int N = data->size();
    -
    401  const double R = 0.3; // radius of cluster
    -
    402  int i;
    -
    403  const int num_classes = 4;
    -
    404  const double centres[][3] = {
    -
    405  // centres of each class cluster
    -
    406  {.5, .5, .5}, // centre of class 1
    -
    407  {.5, -.5, -.5}, // centre of class 2
    -
    408  {-.5, .5, .5}, // centre of class 3
    -
    409  {-.5, -.5 - .5} // centre of class 4
    -
    410  };
    -
    411 
    -
    412 #ifdef _OPENMP
    -
    413 #pragma omp for
    -
    414 #endif
    -
    415  for (i = 0; i < N; i++) {
    -
    416  // select a random class for the point
    -
    417  int cls = std::rand() % num_classes;
    -
    418 
    -
    419  // create random coordinates (x,y,z) around the centre of the class
    -
    420  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    -
    421  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    -
    422  data[0][i][2] = _random(centres[cls][2] - R, centres[cls][2] + R);
    +
    411  {
    +
    412  const size_t N = data->size();
    +
    413  const double R = 0.3; // radius of cluster
    +
    414  int i = 0;
    +
    415  const int num_classes = 4;
    +
    416  const std::array<std::array<double, 3>, num_classes> centres = {
    +
    417  // centres of each class cluster
    +
    418  std::array<double, 3>({.5, .5, .5}), // centre of class 1
    +
    419  std::array<double, 3>({.5, -.5, -.5}), // centre of class 2
    +
    420  std::array<double, 3>({-.5, .5, .5}), // centre of class 3
    +
    421  std::array<double, 3>({-.5, -.5 - .5}) // centre of class 4
    +
    422  };
    423 
    -
    424  /* The follosing can also be used
    -
    425  for (int j = 0; j < 3; j++)
    -
    426  data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);
    -
    427  */
    -
    428  }
    -
    429 }
    +
    424 #ifdef _OPENMP
    +
    425 #pragma omp for
    +
    426 #endif
    +
    427  for (i = 0; i < N; i++) {
    +
    428  // select a random class for the point
    +
    429  int cls = std::rand() % num_classes;
    +
    430 
    +
    431  // create random coordinates (x,y,z) around the centre of the class
    +
    432  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    +
    433  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    +
    434  data[0][i][2] = _random(centres[cls][2] - R, centres[cls][2] + R);
    +
    435 
    +
    436  /* The follosing can also be used
    +
    437  for (int j = 0; j < 3; j++)
    +
    438  data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);
    +
    439  */
    +
    440  }
    +
    441 }
    Here is the call graph for this function:
    @@ -612,41 +617,41 @@ Here is the call graph for this function:
    -
    479  {
    -
    480  const int N = data->size();
    -
    481  const double R = 0.2; // radius of cluster
    -
    482  int i;
    -
    483  const int num_classes = 8;
    -
    484  const double centres[][3] = {
    -
    485  // centres of each class cluster
    -
    486  {.5, .5, .5}, // centre of class 1
    -
    487  {.5, .5, -.5}, // centre of class 2
    -
    488  {.5, -.5, .5}, // centre of class 3
    -
    489  {.5, -.5, -.5}, // centre of class 4
    -
    490  {-.5, .5, .5}, // centre of class 5
    -
    491  {-.5, .5, -.5}, // centre of class 6
    -
    492  {-.5, -.5, .5}, // centre of class 7
    -
    493  {-.5, -.5, -.5} // centre of class 8
    -
    494  };
    -
    495 
    -
    496 #ifdef _OPENMP
    -
    497 #pragma omp for
    -
    498 #endif
    -
    499  for (i = 0; i < N; i++) {
    -
    500  // select a random class for the point
    -
    501  int cls = std::rand() % num_classes;
    -
    502 
    -
    503  // create random coordinates (x,y,z) around the centre of the class
    -
    504  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    -
    505  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    -
    506  data[0][i][2] = _random(centres[cls][2] - R, centres[cls][2] + R);
    -
    507 
    -
    508  /* The follosing can also be used
    -
    509  for (int j = 0; j < 3; j++)
    -
    510  data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);
    -
    511  */
    -
    512  }
    -
    513 }
    +
    493  {
    +
    494  const size_t N = data->size();
    +
    495  const double R = 0.2; // radius of cluster
    +
    496  int i = 0;
    +
    497  const int num_classes = 8;
    +
    498  const std::array<std::array<double, 3>, num_classes> centres = {
    +
    499  // centres of each class cluster
    +
    500  std::array<double, 3>({.5, .5, .5}), // centre of class 1
    +
    501  std::array<double, 3>({.5, .5, -.5}), // centre of class 2
    +
    502  std::array<double, 3>({.5, -.5, .5}), // centre of class 3
    +
    503  std::array<double, 3>({.5, -.5, -.5}), // centre of class 4
    +
    504  std::array<double, 3>({-.5, .5, .5}), // centre of class 5
    +
    505  std::array<double, 3>({-.5, .5, -.5}), // centre of class 6
    +
    506  std::array<double, 3>({-.5, -.5, .5}), // centre of class 7
    +
    507  std::array<double, 3>({-.5, -.5, -.5}) // centre of class 8
    +
    508  };
    +
    509 
    +
    510 #ifdef _OPENMP
    +
    511 #pragma omp for
    +
    512 #endif
    +
    513  for (i = 0; i < N; i++) {
    +
    514  // select a random class for the point
    +
    515  int cls = std::rand() % num_classes;
    +
    516 
    +
    517  // create random coordinates (x,y,z) around the centre of the class
    +
    518  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    +
    519  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    +
    520  data[0][i][2] = _random(centres[cls][2] - R, centres[cls][2] + R);
    +
    521 
    +
    522  /* The follosing can also be used
    +
    523  for (int j = 0; j < 3; j++)
    +
    524  data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);
    +
    525  */
    +
    526  }
    +
    527 }
    Here is the call graph for this function:
    @@ -661,24 +666,25 @@ Here is the call graph for this function:
    T srand(T... args)
    STL class.
    -
    void test1()
    Definition: kohonen_som_topology.cpp:359
    +
    void test1()
    Definition: kohonen_som_topology.cpp:369
    T clock(T... args)
    -
    void test3()
    Definition: kohonen_som_topology.cpp:523
    +
    void test3()
    Definition: kohonen_som_topology.cpp:537
    +
    STL class.
    STL class.
    -
    void test_3d_classes1(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_topology.cpp:399
    -
    void test2()
    Definition: kohonen_som_topology.cpp:439
    +
    void test_3d_classes1(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_topology.cpp:411
    +
    void test2()
    Definition: kohonen_som_topology.cpp:451
    T rand(T... args)
    -
    int save_2d_data(const char *fname, const std::vector< std::valarray< double >> &X)
    Definition: kohonen_som_topology.cpp:62
    +
    int save_2d_data(const char *fname, const std::vector< std::valarray< double >> &X)
    Definition: kohonen_som_topology.cpp:65
    int data[MAX]
    test data
    Definition: hash_search.cpp:24
    -
    int save_u_matrix(const char *fname, const std::vector< std::vector< std::valarray< double >>> &W)
    Definition: kohonen_som_topology.cpp:135
    -
    double get_clock_diff(clock_t start_t, clock_t end_t)
    Definition: kohonen_som_topology.cpp:561
    +
    int save_u_matrix(const char *fname, const std::vector< std::vector< std::valarray< double >>> &W)
    Definition: kohonen_som_topology.cpp:142
    +
    double get_clock_diff(clock_t start_t, clock_t end_t)
    Definition: kohonen_som_topology.cpp:577
    T time(T... args)
    T max(T... args)
    -
    void test_2d_classes(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_topology.cpp:320
    -
    void kohonen_som(const std::vector< std::valarray< double >> &X, std::vector< std::vector< std::valarray< double >>> *W, double alpha_min)
    Definition: kohonen_som_topology.cpp:261
    -
    void test_3d_classes2(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_topology.cpp:479
    -
    double _random(double a, double b)
    Definition: kohonen_som_topology.cpp:50
    +
    void test_2d_classes(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_topology.cpp:330
    +
    void kohonen_som(const std::vector< std::valarray< double >> &X, std::vector< std::vector< std::valarray< double >>> *W, double alpha_min)
    Definition: kohonen_som_topology.cpp:269
    +
    void test_3d_classes2(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_topology.cpp:493
    +
    double _random(double a, double b)
    Definition: kohonen_som_topology.cpp:53

    Main function

    -
    443  {
    -
    444 #ifdef _OPENMP
    -
    445  std::cout << "Using OpenMP based parallelization\n";
    -
    446 #else
    -
    447  std::cout << "NOT using OpenMP based parallelization\n";
    -
    448 #endif
    -
    449 
    -
    450  std::srand(std::time(nullptr));
    -
    451 
    -
    452  std::clock_t start_clk = std::clock();
    -
    453  test1();
    -
    454  auto end_clk = std::clock();
    -
    455  std::cout << "Test 1 completed in " << get_clock_diff(start_clk, end_clk)
    -
    456  << " sec\n";
    -
    457 
    -
    458  start_clk = std::clock();
    -
    459  test2();
    -
    460  end_clk = std::clock();
    -
    461  std::cout << "Test 2 completed in " << get_clock_diff(start_clk, end_clk)
    -
    462  << " sec\n";
    +
    457  {
    +
    458 #ifdef _OPENMP
    +
    459  std::cout << "Using OpenMP based parallelization\n";
    +
    460 #else
    +
    461  std::cout << "NOT using OpenMP based parallelization\n";
    +
    462 #endif
    463 
    -
    464  start_clk = std::clock();
    -
    465  test3();
    -
    466  end_clk = std::clock();
    -
    467  std::cout << "Test 3 completed in " << get_clock_diff(start_clk, end_clk)
    -
    468  << " sec\n";
    -
    469 
    -
    470  std::cout
    -
    471  << "(Note: Calculated times include: creating test sets, training "
    -
    472  "model and writing files to disk.)\n\n";
    -
    473  return 0;
    -
    474 }
    +
    464  std::srand(std::time(nullptr));
    +
    465 
    +
    466  std::clock_t start_clk = std::clock();
    +
    467  test1();
    +
    468  auto end_clk = std::clock();
    +
    469  std::cout << "Test 1 completed in " << get_clock_diff(start_clk, end_clk)
    +
    470  << " sec\n";
    +
    471 
    +
    472  start_clk = std::clock();
    +
    473  test2();
    +
    474  end_clk = std::clock();
    +
    475  std::cout << "Test 2 completed in " << get_clock_diff(start_clk, end_clk)
    +
    476  << " sec\n";
    +
    477 
    +
    478  start_clk = std::clock();
    +
    479  test3();
    +
    480  end_clk = std::clock();
    +
    481  std::cout << "Test 3 completed in " << get_clock_diff(start_clk, end_clk)
    +
    482  << " sec\n";
    +
    483 
    +
    484  std::cout
    +
    485  << "(Note: Calculated times include: creating test sets, training "
    +
    486  "model and writing files to disk.)\n\n";
    +
    487  return 0;
    +
    488 }
    Here is the call graph for this function:
    @@ -290,34 +291,36 @@ Here is the call graph for this function:
    "w12.csv" title "w2"

    Sample execution
 output

    -
    225  {
    -
    226  int j, N = 500;
    -
    227  int features = 2;
    -
    228  int num_out = 50;
    - - -
    231  for (int i = 0; i < std::max(num_out, N); i++) {
    -
    232  // loop till max(N, num_out)
    -
    233  if (i < N) // only add new arrays if i < N
    -
    234  X[i] = std::valarray<double>(features);
    -
    235  if (i < num_out) { // only add new arrays if i < num_out
    -
    236  W[i] = std::valarray<double>(features);
    -
    237 
    -
    238 #ifdef _OPENMP
    -
    239 #pragma omp for
    -
    240 #endif
    -
    241  for (j = 0; j < features; j++)
    -
    242  // preallocate with random initial weights
    -
    243  W[i][j] = _random(-1, 1);
    -
    244  }
    -
    245  }
    +
    233  {
    +
    234  int j = 0, N = 500;
    +
    235  int features = 2;
    +
    236  int num_out = 50;
    + + +
    239  for (int i = 0; i < std::max(num_out, N); i++) {
    +
    240  // loop till max(N, num_out)
    +
    241  if (i < N) { // only add new arrays if i < N
    +
    242  X[i] = std::valarray<double>(features);
    +
    243  }
    +
    244  if (i < num_out) { // only add new arrays if i < num_out
    +
    245  W[i] = std::valarray<double>(features);
    246 
    -
    247  test_circle(&X); // create test data around circumference of a circle
    -
    248  save_nd_data("test1.csv", X); // save test data points
    -
    249  save_nd_data("w11.csv", W); // save initial random weights
    -
    250  kohonen_som_tracer(X, &W, 0.1); // train the SOM
    -
    251  save_nd_data("w12.csv", W); // save the resultant weights
    -
    252 }
    +
    247 #ifdef _OPENMP
    +
    248 #pragma omp for
    +
    249 #endif
    +
    250  for (j = 0; j < features; j++) {
    +
    251  // preallocate with random initial weights
    +
    252  W[i][j] = _random(-1, 1);
    +
    253  }
    +
    254  }
    +
    255  }
    +
    256 
    +
    257  test_circle(&X); // create test data around circumference of a circle
    +
    258  save_nd_data("test1.csv", X); // save test data points
    +
    259  save_nd_data("w11.csv", W); // save initial random weights
    +
    260  kohonen_som_tracer(X, &W, 0.1); // train the SOM
    +
    261  save_nd_data("w12.csv", W); // save the resultant weights
    +
    262 }
    Here is the call graph for this function:
    @@ -352,34 +355,36 @@ Here is the call graph for this function:
    "w22.csv" title "w2"

    Sample execution
 output

    -
    305  {
    -
    306  int j, N = 500;
    -
    307  int features = 2;
    -
    308  int num_out = 20;
    - - -
    311  for (int i = 0; i < std::max(num_out, N); i++) {
    -
    312  // loop till max(N, num_out)
    -
    313  if (i < N) // only add new arrays if i < N
    -
    314  X[i] = std::valarray<double>(features);
    -
    315  if (i < num_out) { // only add new arrays if i < num_out
    -
    316  W[i] = std::valarray<double>(features);
    -
    317 
    -
    318 #ifdef _OPENMP
    -
    319 #pragma omp for
    -
    320 #endif
    -
    321  for (j = 0; j < features; j++)
    -
    322  // preallocate with random initial weights
    -
    323  W[i][j] = _random(-1, 1);
    -
    324  }
    -
    325  }
    -
    326 
    -
    327  test_lamniscate(&X); // create test data around the lamniscate
    -
    328  save_nd_data("test2.csv", X); // save test data points
    -
    329  save_nd_data("w21.csv", W); // save initial random weights
    -
    330  kohonen_som_tracer(X, &W, 0.01); // train the SOM
    -
    331  save_nd_data("w22.csv", W); // save the resultant weights
    -
    332 }
    +
    315  {
    +
    316  int j = 0, N = 500;
    +
    317  int features = 2;
    +
    318  int num_out = 20;
    + + +
    321  for (int i = 0; i < std::max(num_out, N); i++) {
    +
    322  // loop till max(N, num_out)
    +
    323  if (i < N) { // only add new arrays if i < N
    +
    324  X[i] = std::valarray<double>(features);
    +
    325  }
    +
    326  if (i < num_out) { // only add new arrays if i < num_out
    +
    327  W[i] = std::valarray<double>(features);
    +
    328 
    +
    329 #ifdef _OPENMP
    +
    330 #pragma omp for
    +
    331 #endif
    +
    332  for (j = 0; j < features; j++) {
    +
    333  // preallocate with random initial weights
    +
    334  W[i][j] = _random(-1, 1);
    +
    335  }
    +
    336  }
    +
    337  }
    +
    338 
    +
    339  test_lamniscate(&X); // create test data around the lamniscate
    +
    340  save_nd_data("test2.csv", X); // save test data points
    +
    341  save_nd_data("w21.csv", W); // save initial random weights
    +
    342  kohonen_som_tracer(X, &W, 0.01); // train the SOM
    +
    343  save_nd_data("w22.csv", W); // save the resultant weights
    +
    344 }
    Here is the call graph for this function:
    @@ -414,34 +419,36 @@ Here is the call graph for this function:
    "w32.csv" title "w2"

    Sample execution
 output

    -
    402  {
    -
    403  int j, N = 200;
    -
    404  int features = 3;
    -
    405  int num_out = 20;
    - - -
    408  for (int i = 0; i < std::max(num_out, N); i++) {
    -
    409  // loop till max(N, num_out)
    -
    410  if (i < N) // only add new arrays if i < N
    -
    411  X[i] = std::valarray<double>(features);
    -
    412  if (i < num_out) { // only add new arrays if i < num_out
    -
    413  W[i] = std::valarray<double>(features);
    -
    414 
    -
    415 #ifdef _OPENMP
    -
    416 #pragma omp for
    -
    417 #endif
    -
    418  for (j = 0; j < features; j++)
    -
    419  // preallocate with random initial weights
    -
    420  W[i][j] = _random(-1, 1);
    -
    421  }
    -
    422  }
    -
    423 
    -
    424  test_3d_classes(&X); // create test data around the lamniscate
    -
    425  save_nd_data("test3.csv", X); // save test data points
    -
    426  save_nd_data("w31.csv", W); // save initial random weights
    -
    427  kohonen_som_tracer(X, &W, 0.01); // train the SOM
    -
    428  save_nd_data("w32.csv", W); // save the resultant weights
    -
    429 }
    +
    414  {
    +
    415  int j = 0, N = 200;
    +
    416  int features = 3;
    +
    417  int num_out = 20;
    + + +
    420  for (int i = 0; i < std::max(num_out, N); i++) {
    +
    421  // loop till max(N, num_out)
    +
    422  if (i < N) { // only add new arrays if i < N
    +
    423  X[i] = std::valarray<double>(features);
    +
    424  }
    +
    425  if (i < num_out) { // only add new arrays if i < num_out
    +
    426  W[i] = std::valarray<double>(features);
    +
    427 
    +
    428 #ifdef _OPENMP
    +
    429 #pragma omp for
    +
    430 #endif
    +
    431  for (j = 0; j < features; j++) {
    +
    432  // preallocate with random initial weights
    +
    433  W[i][j] = _random(-1, 1);
    +
    434  }
    +
    435  }
    +
    436  }
    +
    437 
    +
    438  test_3d_classes(&X); // create test data around the lamniscate
    +
    439  save_nd_data("test3.csv", X); // save test data points
    +
    440  save_nd_data("w31.csv", W); // save initial random weights
    +
    441  kohonen_som_tracer(X, &W, 0.01); // train the SOM
    +
    442  save_nd_data("w32.csv", W); // save the resultant weights
    +
    443 }
    Here is the call graph for this function:
    @@ -482,41 +489,41 @@ Here is the call graph for this function:
    -
    347  {
    -
    348  const int N = data->size();
    -
    349  const double R = 0.1; // radius of cluster
    -
    350  int i;
    -
    351  const int num_classes = 8;
    -
    352  const double centres[][3] = {
    -
    353  // centres of each class cluster
    -
    354  {.5, .5, .5}, // centre of class 0
    -
    355  {.5, .5, -.5}, // centre of class 1
    -
    356  {.5, -.5, .5}, // centre of class 2
    -
    357  {.5, -.5, -.5}, // centre of class 3
    -
    358  {-.5, .5, .5}, // centre of class 4
    -
    359  {-.5, .5, -.5}, // centre of class 5
    -
    360  {-.5, -.5, .5}, // centre of class 6
    -
    361  {-.5, -.5, -.5} // centre of class 7
    -
    362  };
    -
    363 
    -
    364 #ifdef _OPENMP
    -
    365 #pragma omp for
    -
    366 #endif
    -
    367  for (i = 0; i < N; i++) {
    -
    368  int cls =
    -
    369  std::rand() % num_classes; // select a random class for the point
    -
    370 
    -
    371  // create random coordinates (x,y,z) around the centre of the class
    -
    372  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    -
    373  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    -
    374  data[0][i][2] = _random(centres[cls][2] - R, centres[cls][2] + R);
    +
    359  {
    +
    360  const int N = data->size();
    +
    361  const double R = 0.1; // radius of cluster
    +
    362  int i = 0;
    +
    363  const int num_classes = 8;
    +
    364  const std::array<const std::array<double, 3>, num_classes> centres = {
    +
    365  // centres of each class cluster
    +
    366  std::array<double, 3>({.5, .5, .5}), // centre of class 0
    +
    367  std::array<double, 3>({.5, .5, -.5}), // centre of class 1
    +
    368  std::array<double, 3>({.5, -.5, .5}), // centre of class 2
    +
    369  std::array<double, 3>({.5, -.5, -.5}), // centre of class 3
    +
    370  std::array<double, 3>({-.5, .5, .5}), // centre of class 4
    +
    371  std::array<double, 3>({-.5, .5, -.5}), // centre of class 5
    +
    372  std::array<double, 3>({-.5, -.5, .5}), // centre of class 6
    +
    373  std::array<double, 3>({-.5, -.5, -.5}) // centre of class 7
    +
    374  };
    375 
    -
    376  /* The follosing can also be used
    -
    377  for (int j = 0; j < 3; j++)
    -
    378  data[0][i][j] = _random(centres[cls][j] - R, centres[cls][j] + R);
    -
    379  */
    -
    380  }
    -
    381 }
    +
    376 #ifdef _OPENMP
    +
    377 #pragma omp for
    +
    378 #endif
    +
    379  for (i = 0; i < N; i++) {
    +
    380  int cls =
    +
    381  std::rand() % num_classes; // select a random class for the point
    +
    382 
    +
    383  // create random coordinates (x,y,z) around the centre of the class
    +
    384  data[0][i][0] = _random(centres[cls][0] - R, centres[cls][0] + R);
    +
    385  data[0][i][1] = _random(centres[cls][1] - R, centres[cls][1] + R);
    +
    386  data[0][i][2] = _random(centres[cls][2] - R, centres[cls][2] + R);
    +
    387 
    +
    388  /* The follosing can also be used
    +
    389  for (int j = 0; j < 3; j++)
    +
    390  data[0][i][j] = _random(centres[cls][j] - R, centres[cls][j] + R);
    +
    391  */
    +
    392  }
    +
    393 }
    Here is the call graph for this function:
    @@ -550,23 +557,23 @@ Here is the call graph for this function:
    -
    188  {
    -
    189  const int N = data->size();
    -
    190  const double R = 0.75, dr = 0.3;
    -
    191  double a_t = 0., b_t = 2.f * M_PI; // theta random between 0 and 2*pi
    -
    192  double a_r = R - dr, b_r = R + dr; // radius random between R-dr and R+dr
    -
    193  int i;
    -
    194 
    -
    195 #ifdef _OPENMP
    -
    196 #pragma omp for
    -
    197 #endif
    -
    198  for (i = 0; i < N; i++) {
    -
    199  double r = _random(a_r, b_r); // random radius
    -
    200  double theta = _random(a_t, b_t); // random theta
    -
    201  data[0][i][0] = r * cos(theta); // convert from polar to cartesian
    -
    202  data[0][i][1] = r * sin(theta);
    -
    203  }
    -
    204 }
    +
    196  {
    +
    197  const int N = data->size();
    +
    198  const double R = 0.75, dr = 0.3;
    +
    199  double a_t = 0., b_t = 2.f * M_PI; // theta random between 0 and 2*pi
    +
    200  double a_r = R - dr, b_r = R + dr; // radius random between R-dr and R+dr
    +
    201  int i = 0;
    +
    202 
    +
    203 #ifdef _OPENMP
    +
    204 #pragma omp for
    +
    205 #endif
    +
    206  for (i = 0; i < N; i++) {
    +
    207  double r = _random(a_r, b_r); // random radius
    +
    208  double theta = _random(a_t, b_t); // random theta
    +
    209  data[0][i][0] = r * cos(theta); // convert from polar to cartesian
    +
    210  data[0][i][1] = r * sin(theta);
    +
    211  }
    +
    212 }
    Here is the call graph for this function:
    @@ -600,22 +607,22 @@ Here is the call graph for this function:
    -
    267  {
    -
    268  const int N = data->size();
    -
    269  const double dr = 0.2;
    -
    270  int i;
    -
    271 
    -
    272 #ifdef _OPENMP
    -
    273 #pragma omp for
    -
    274 #endif
    -
    275  for (i = 0; i < N; i++) {
    -
    276  double dx = _random(-dr, dr); // random change in x
    -
    277  double dy = _random(-dr, dr); // random change in y
    -
    278  double theta = _random(0, M_PI); // random theta
    -
    279  data[0][i][0] = dx + cos(theta); // convert from polar to cartesian
    -
    280  data[0][i][1] = dy + sin(2. * theta) / 2.f;
    -
    281  }
    -
    282 }
    +
    277  {
    +
    278  const int N = data->size();
    +
    279  const double dr = 0.2;
    +
    280  int i = 0;
    +
    281 
    +
    282 #ifdef _OPENMP
    +
    283 #pragma omp for
    +
    284 #endif
    +
    285  for (i = 0; i < N; i++) {
    +
    286  double dx = _random(-dr, dr); // random change in x
    +
    287  double dy = _random(-dr, dr); // random change in y
    +
    288  double theta = _random(0, M_PI); // random theta
    +
    289  data[0][i][0] = dx + cos(theta); // convert from polar to cartesian
    +
    290  data[0][i][1] = dy + sin(2. * theta) / 2.f;
    +
    291  }
    +
    292 }
    Here is the call graph for this function:
    @@ -631,24 +638,25 @@ Here is the call graph for this function:
    T cos(T... args)
    STL class.
    -
    void test_3d_classes(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_trace.cpp:347
    -
    int save_nd_data(const char *fname, const std::vector< std::valarray< double >> &X)
    Definition: kohonen_som_trace.cpp:57
    -
    double get_clock_diff(clock_t start_t, clock_t end_t)
    Definition: kohonen_som_trace.cpp:438
    -
    void test1()
    Definition: kohonen_som_trace.cpp:225
    +
    void test_3d_classes(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_trace.cpp:359
    +
    int save_nd_data(const char *fname, const std::vector< std::valarray< double >> &X)
    Definition: kohonen_som_trace.cpp:58
    +
    double get_clock_diff(clock_t start_t, clock_t end_t)
    Definition: kohonen_som_trace.cpp:452
    +
    void test1()
    Definition: kohonen_som_trace.cpp:233
    T clock(T... args)
    +
    STL class.
    STL class.
    -
    void test2()
    Definition: kohonen_som_trace.cpp:305
    +
    void test2()
    Definition: kohonen_som_trace.cpp:315
    T rand(T... args)
    -
    void test_lamniscate(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_trace.cpp:267
    +
    void test_lamniscate(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_trace.cpp:277
    T sin(T... args)
    int data[MAX]
    test data
    Definition: hash_search.cpp:24
    -
    void test3()
    Definition: kohonen_som_trace.cpp:402
    +
    void test3()
    Definition: kohonen_som_trace.cpp:414
    T time(T... args)
    T max(T... args)
    -
    double _random(double a, double b)
    Definition: kohonen_som_topology.cpp:50
    -
    void kohonen_som_tracer(const std::vector< std::valarray< double >> &X, std::vector< std::valarray< double >> *W, double alpha_min)
    Definition: kohonen_som_trace.cpp:145
    -
    void test_circle(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_trace.cpp:188
    +
    double _random(double a, double b)
    Definition: kohonen_som_topology.cpp:53
    +
    void kohonen_som_tracer(const std::vector< std::valarray< double >> &X, std::vector< std::valarray< double >> *W, double alpha_min)
    Definition: kohonen_som_trace.cpp:149
    +
    void test_circle(std::vector< std::valarray< double >> *data)
    Definition: kohonen_som_trace.cpp:196
    +Functions | +Variables
    Machine Learning Algorithms
    @@ -118,18 +119,6 @@ Namespaces  Machine learning algorithms.
      - - - - - - - -

    -Macros

    -#define MAX_ITER   500
     
    -#define _USE_MATH_DEFINES
     
    -#define _USE_MATH_DEFINES
     
    @@ -140,6 +129,11 @@ Functions +

    Functions

    double _random (double a, double b)
     
    int save_nd_data (const char *fname, const std::vector< std::valarray< double >> &X)
     
    + + +

    +Variables

    constexpr int MAX_ITER = 500
     

    Detailed Description

    Function Documentation

    @@ -182,9 +176,9 @@ Functions
    Returns
    random number in the range \([a,b]\)
    -
    50  {
    -
    51  return ((b - a) * (std::rand() % 100) / 100.f) + a;
    -
    52 }
    +
    53  {
    +
    54  return ((b - a) * (std::rand() % 100) / 100.f) + a;
    +
    55 }
    Here is the call graph for this function:
    @@ -241,23 +235,23 @@ Here is the call graph for this function:
    -
    100  {
    -
    101  val[0] = INFINITY; // initial min value
    -
    102  int N = X.size();
    -
    103 
    -
    104  for (int i = 0; i < N; i++) { // traverse each x-index
    -
    105  auto result = std::min_element(std::begin(X[i]), std::end(X[i]));
    -
    106  double d_min = *result;
    -
    107  int j = std::distance(std::begin(X[i]), result);
    -
    108 
    -
    109  if (d_min < val[0]) { // if a lower value is found
    -
    110  // save the value and its index
    -
    111  x_idx[0] = i;
    -
    112  y_idx[0] = j;
    -
    113  val[0] = d_min;
    -
    114  }
    -
    115  }
    -
    116 }
    +
    106  {
    +
    107  val[0] = INFINITY; // initial min value
    +
    108  size_t N = X.size();
    +
    109 
    +
    110  for (int i = 0; i < N; i++) { // traverse each x-index
    +
    111  auto result = std::min_element(std::begin(X[i]), std::end(X[i]));
    +
    112  double d_min = *result;
    +
    113  std::ptrdiff_t j = std::distance(std::begin(X[i]), result);
    +
    114 
    +
    115  if (d_min < val[0]) { // if a lower value is found
    +
    116  // save the value and its index
    +
    117  x_idx[0] = i;
    +
    118  y_idx[0] = j;
    +
    119  val[0] = d_min;
    +
    120  }
    +
    121  }
    +
    122 }
    Here is the call graph for this function:
    @@ -303,37 +297,40 @@ Here is the call graph for this function:
    Returns
    0 if all ok
    -1 if file creation failed
    -
    63  {
    -
    64  size_t num_points = X.size(); // number of rows
    -
    65  size_t num_features = X[0].size(); // number of columns
    -
    66 
    -
    67  std::ofstream fp;
    -
    68  fp.open(fname);
    -
    69  if (!fp.is_open()) {
    -
    70  // error with opening file to write
    -
    71  std::cerr << "Error opening file " << fname << "\n";
    -
    72  return -1;
    -
    73  }
    -
    74 
    -
    75  // for each point in the array
    -
    76  for (int i = 0; i < num_points; i++) {
    -
    77  // for each feature in the array
    -
    78  for (int j = 0; j < num_features; j++) {
    -
    79  fp << X[i][j]; // print the feature value
    -
    80  if (j < num_features - 1) // if not the last feature
    -
    81  fp << ","; // suffix comma
    -
    82  }
    -
    83  if (i < num_points - 1) // if not the last row
    -
    84  fp << "\n"; // start a new line
    -
    85  }
    -
    86 
    -
    87  fp.close();
    -
    88  return 0;
    -
    89 }
    +
    66  {
    +
    67  size_t num_points = X.size(); // number of rows
    +
    68  size_t num_features = X[0].size(); // number of columns
    +
    69 
    +
    70  std::ofstream fp;
    +
    71  fp.open(fname);
    +
    72  if (!fp.is_open()) {
    +
    73  // error with opening file to write
    +
    74  std::cerr << "Error opening file " << fname << ": "
    +
    75  << std::strerror(errno) << "\n";
    +
    76  return -1;
    +
    77  }
    +
    78 
    +
    79  // for each point in the array
    +
    80  for (int i = 0; i < num_points; i++) {
    +
    81  // for each feature in the array
    +
    82  for (int j = 0; j < num_features; j++) {
    +
    83  fp << X[i][j]; // print the feature value
    +
    84  if (j < num_features - 1) { // if not the last feature
    +
    85  fp << ","; // suffix comma
    +
    86  }
    +
    87  }
    +
    88  if (i < num_points - 1) { // if not the last row
    +
    89  fp << "\n"; // start a new line
    +
    90  }
    +
    91  }
    +
    92 
    +
    93  fp.close();
    +
    94  return 0;
    +
    95 }
    Here is the call graph for this function:
    -
    +
    @@ -375,33 +372,35 @@ Here is the call graph for this function:
    Returns
    0 if all ok
    -1 if file creation failed
    -
    58  {
    -
    59  size_t num_points = X.size(); // number of rows
    -
    60  size_t num_features = X[0].size(); // number of columns
    -
    61 
    -
    62  std::ofstream fp;
    -
    63  fp.open(fname);
    -
    64  if (!fp.is_open()) {
    -
    65  // error with opening file to write
    -
    66  std::cerr << "Error opening file " << fname << "\n";
    -
    67  return -1;
    -
    68  }
    -
    69 
    -
    70  // for each point in the array
    -
    71  for (int i = 0; i < num_points; i++) {
    -
    72  // for each feature in the array
    -
    73  for (int j = 0; j < num_features; j++) {
    -
    74  fp << X[i][j]; // print the feature value
    -
    75  if (j < num_features - 1) // if not the last feature
    -
    76  fp << ","; // suffix comma
    -
    77  }
    -
    78  if (i < num_points - 1) // if not the last row
    -
    79  fp << "\n"; // start a new line
    -
    80  }
    -
    81 
    -
    82  fp.close();
    -
    83  return 0;
    -
    84 }
    +
    59  {
    +
    60  size_t num_points = X.size(); // number of rows
    +
    61  size_t num_features = X[0].size(); // number of columns
    +
    62 
    +
    63  std::ofstream fp;
    +
    64  fp.open(fname);
    +
    65  if (!fp.is_open()) {
    +
    66  // error with opening file to write
    +
    67  std::cerr << "Error opening file " << fname << "\n";
    +
    68  return -1;
    +
    69  }
    +
    70 
    +
    71  // for each point in the array
    +
    72  for (int i = 0; i < num_points; i++) {
    +
    73  // for each feature in the array
    +
    74  for (int j = 0; j < num_features; j++) {
    +
    75  fp << X[i][j]; // print the feature value
    +
    76  if (j < num_features - 1) { // if not the last feature
    +
    77  fp << ","; // suffix comma
    +
    78  }
    +
    79  }
    +
    80  if (i < num_points - 1) { // if not the last row
    +
    81  fp << "\n"; // start a new line
    +
    82  }
    +
    83  }
    +
    84 
    +
    85  fp.close();
    +
    86  return 0;
    +
    87 }
    Here is the call graph for this function:
    @@ -409,12 +408,37 @@ Here is the call graph for this function:
    +
    +
    +

    Variable Documentation

    + +

    ◆ MAX_ITER

    + +
    +
    + + + + + +
    + + + + +
    constexpr int MAX_ITER = 500
    +
    +constexpr
    +
    +

    Maximum number of iterations to learn

    +
    T size(T... args)
    T distance(T... args)
    +
    T strerror(T... args)
    STL class.
    T min_element(T... args)
    @@ -422,6 +446,7 @@ Here is the call graph for this function:
    T open(T... args)
    T rand(T... args)
    T begin(T... args)
    +
    T end(T... args)
    T is_open(T... args)
    diff --git a/d9/d66/group__machine__learning.js b/d9/d66/group__machine__learning.js index ecd44e4a4..e03e72da4 100644 --- a/d9/d66/group__machine__learning.js +++ b/d9/d66/group__machine__learning.js @@ -7,5 +7,6 @@ var group__machine__learning = [ "_random", "d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485", null ], [ "get_min_2d", "d9/d66/group__machine__learning.html#gab53c14440b2b2dd3172c66afc5c2f63f", null ], [ "save_2d_data", "d9/d66/group__machine__learning.html#ga6c606bc85a3e9fd9d010f874ad760336", null ], - [ "save_nd_data", "d9/d66/group__machine__learning.html#ga72699c805c19a6cc47a937a6f3378afa", null ] + [ "save_nd_data", "d9/d66/group__machine__learning.html#ga72699c805c19a6cc47a937a6f3378afa", null ], + [ "MAX_ITER", "d9/d66/group__machine__learning.html#ga5118e5cbc4f0886e27b3a7a2544dded1", null ] ]; \ No newline at end of file diff --git a/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.map b/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.map index ca7ba6828..c15e27a5a 100644 --- a/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.map +++ b/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.map @@ -1,6 +1,7 @@ - + + diff --git a/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.md5 b/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.md5 index fa9139da4..e3dbdb9ae 100644 --- a/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.md5 +++ b/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.md5 @@ -1 +1 @@ -9ed111738ba70d8365b2ca085ed06a6c \ No newline at end of file +cfebf403de75ebfbd2d43d680ff65584 \ No newline at end of file diff --git a/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.svg b/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.svg index 0cce4ec13..c7f9b579a 100644 --- a/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.svg +++ b/d9/d66/group__machine__learning_ga6c606bc85a3e9fd9d010f874ad760336_cgraph.svg @@ -4,17 +4,17 @@ - - + + save_2d_data - + Node1 - -save_2d_data + +save_2d_data @@ -22,46 +22,61 @@ Node2 - -std::ofstream::close + +std::ofstream::close Node1->Node2 - - + + Node3 - -std::ofstream::is_open + +std::ofstream::is_open Node1->Node3 - - + + Node4 - -std::ofstream::open + +std::ofstream::open Node1->Node4 - - + + + + + +Node5 + + +std::strerror + + + + + +Node1->Node5 + + diff --git a/db/dd3/ode__forward__euler_8cpp.html b/db/dd3/ode__forward__euler_8cpp.html index 731ab5d75..5704edb50 100644 --- a/db/dd3/ode__forward__euler_8cpp.html +++ b/db/dd3/ode__forward__euler_8cpp.html @@ -119,9 +119,9 @@ Functions
    void exact_solution (const double &x, std::valarray< double > *y)
     Exact solution of the problem. Used for solution comparison. More...

      -void forward_euler_step (const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy) - Compute next step approximation using the forward-Euler method. More...
    -  +void forward_euler_step (const double dx, const double x, std::valarray< double > *y, std::valarray< double > *dy) + Compute next step approximation using the forward-Euler method. More...
    +  double forward_euler (double dx, double x0, double x_max, std::valarray< double > *y, bool save_to_file=false)  Compute approximation using the forward-Euler method in the given limits. More...
      @@ -218,29 +218,29 @@ Here is the call graph for this function:

    Main Function

    -
    188  {
    -
    189  double X0 = 0.f; /* initial value of x0 */
    -
    190  double X_MAX = 10.F; /* upper limit of integration */
    -
    191  std::valarray<double> Y0 = {1.f, 0.f}; /* initial value Y = y(x = x_0) */
    -
    192  double step_size;
    -
    193 
    -
    194  if (argc == 1) {
    -
    195  std::cout << "\nEnter the step size: ";
    -
    196  std::cin >> step_size;
    -
    197  } else {
    -
    198  // use commandline argument as independent variable step size
    -
    199  step_size = std::atof(argv[1]);
    -
    200  }
    -
    201 
    -
    202  // get approximate solution
    -
    203  double total_time = forward_euler(step_size, X0, X_MAX, &Y0, true);
    -
    204  std::cout << "\tTime = " << total_time << " ms\n";
    -
    205 
    -
    206  /* compute exact solution for comparion */
    -
    207  save_exact_solution(X0, X_MAX, step_size, Y0);
    -
    208 
    -
    209  return 0;
    -
    210 }
    +
    189  {
    +
    190  double X0 = 0.f; /* initial value of x0 */
    +
    191  double X_MAX = 10.F; /* upper limit of integration */
    +
    192  std::valarray<double> Y0{1.f, 0.f}; /* initial value Y = y(x = x_0) */
    +
    193  double step_size = NAN;
    +
    194 
    +
    195  if (argc == 1) {
    +
    196  std::cout << "\nEnter the step size: ";
    +
    197  std::cin >> step_size;
    +
    198  } else {
    +
    199  // use commandline argument as independent variable step size
    +
    200  step_size = std::atof(argv[1]);
    +
    201  }
    +
    202 
    +
    203  // get approximate solution
    +
    204  double total_time = forward_euler(step_size, X0, X_MAX, &Y0, true);
    +
    205  std::cout << "\tTime = " << total_time << " ms\n";
    +
    206 
    +
    207  /* compute exact solution for comparion */
    +
    208  save_exact_solution(X0, X_MAX, step_size, Y0);
    +
    209 
    +
    210  return 0;
    +
    211 }
    Here is the call graph for this function:
    @@ -294,8 +294,8 @@ Here is the call graph for this function:
    55  {
    56  const double omega = 1.F; // some const for the problem
    -
    57  dy[0][0] = y[0][1]; // x dot
    -
    58  dy[0][1] = -omega * omega * y[0][0]; // y dot
    +
    57  (*dy)[0] = (*y)[1]; // x dot // NOLINT
    +
    58  (*dy)[1] = -omega * omega * (*y)[0]; // y dot // NOLINT
    59 }
    @@ -347,36 +347,36 @@ Here is the call graph for this function:
    -
    154  {
    -
    155  double x = X0;
    -
    156  std::valarray<double> y = Y0;
    -
    157 
    -
    158  std::ofstream fp("exact.csv", std::ostream::out);
    -
    159  if (!fp.is_open()) {
    -
    160  std::perror("Error! ");
    -
    161  return;
    -
    162  }
    -
    163  std::cout << "Finding exact solution\n";
    -
    164 
    -
    165  std::clock_t t1 = std::clock();
    -
    166  do {
    -
    167  fp << x << ",";
    -
    168  for (int i = 0; i < y.size() - 1; i++) {
    -
    169  fp << y[i] << ",";
    -
    170  }
    -
    171  fp << y[y.size() - 1] << "\n";
    -
    172 
    -
    173  exact_solution(x, &y);
    -
    174 
    -
    175  x += step_size;
    -
    176  } while (x <= X_MAX);
    -
    177 
    -
    178  std::clock_t t2 = std::clock();
    -
    179  double total_time = static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
    -
    180  std::cout << "\tTime = " << total_time << " ms\n";
    -
    181 
    -
    182  fp.close();
    -
    183 }
    +
    155  {
    +
    156  double x = X0;
    +
    157  std::valarray<double> y(Y0);
    +
    158 
    +
    159  std::ofstream fp("exact.csv", std::ostream::out);
    +
    160  if (!fp.is_open()) {
    +
    161  std::perror("Error! ");
    +
    162  return;
    +
    163  }
    +
    164  std::cout << "Finding exact solution\n";
    +
    165 
    +
    166  std::clock_t t1 = std::clock();
    +
    167  do {
    +
    168  fp << x << ",";
    +
    169  for (int i = 0; i < y.size() - 1; i++) {
    +
    170  fp << y[i] << ","; // NOLINT
    +
    171  }
    +
    172  fp << y[y.size() - 1] << "\n"; // NOLINT
    +
    173 
    +
    174  exact_solution(x, &y);
    +
    175 
    +
    176  x += step_size;
    +
    177  } while (x <= X_MAX);
    +
    178 
    +
    179  std::clock_t t2 = std::clock();
    +
    180  double total_time = static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
    +
    181  std::cout << "\tTime = " << total_time << " ms\n";
    +
    182 
    +
    183  fp.close();
    +
    184 }
    Here is the call graph for this function:
    @@ -393,7 +393,7 @@ Here is the call graph for this function:
    void exact_solution(const double &x, std::valarray< double > *y)
    Exact solution of the problem. Used for solution comparison.
    Definition: ode_forward_euler.cpp:67
    T atof(T... args)
    T clock(T... args)
    -
    void save_exact_solution(const double &X0, const double &X_MAX, const double &step_size, const std::valarray< double > &Y0)
    Definition: ode_forward_euler.cpp:152
    +
    void save_exact_solution(const double &X0, const double &X_MAX, const double &step_size, const std::valarray< double > &Y0)
    Definition: ode_forward_euler.cpp:153
    STL class.
    T perror(T... args)
    diff --git a/db/dd3/ode__forward__euler_8cpp.js b/db/dd3/ode__forward__euler_8cpp.js index d08321c38..0bba266a1 100644 --- a/db/dd3/ode__forward__euler_8cpp.js +++ b/db/dd3/ode__forward__euler_8cpp.js @@ -2,7 +2,7 @@ var ode__forward__euler_8cpp = [ [ "exact_solution", "db/dd3/ode__forward__euler_8cpp.html#af3adf7b092a87868917ee5fb4255192b", null ], [ "forward_euler", "d6/d60/group__ode.html#gae0509f8843e2bc42de2abbd00a14b7b9", null ], - [ "forward_euler_step", "d6/d60/group__ode.html#gaa197c6b7085ac9ea71abc4c063e6f52f", null ], + [ "forward_euler_step", "d6/d60/group__ode.html#ga195d23bbdfcb80e83c9cda45c9ad5723", null ], [ "main", "db/dd3/ode__forward__euler_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97", null ], [ "problem", "db/dd3/ode__forward__euler_8cpp.html#abaeae8f62a018d197f0187a1c80a90fe", null ], [ "save_exact_solution", "db/dd3/ode__forward__euler_8cpp.html#aa13517b8e5de1b75592052db7f7e237f", null ] diff --git a/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.map b/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.map index 4d692f27e..762bf6c67 100644 --- a/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.map +++ b/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.map @@ -5,7 +5,7 @@ - + diff --git a/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.md5 b/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.md5 index 24ed67c66..440efa6eb 100644 --- a/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.md5 +++ b/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.md5 @@ -1 +1 @@ -6781d4c4936ba396d4566021a2445e88 \ No newline at end of file +31797d87f3c14bcdf5e30c95102ec3ad \ No newline at end of file diff --git a/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg b/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg index b25912759..27dc6aa55 100644 --- a/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg +++ b/db/dd3/ode__forward__euler_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg @@ -96,7 +96,7 @@ Node6 - + forward_euler_step diff --git a/dc/d38/ordinary__least__squares__regressor_8cpp.html b/dc/d38/ordinary__least__squares__regressor_8cpp.html index 6f2219edc..a3acb7e8b 100644 --- a/dc/d38/ordinary__least__squares__regressor_8cpp.html +++ b/dc/d38/ordinary__least__squares__regressor_8cpp.html @@ -203,28 +203,29 @@ template<typename T >
    Returns
    fitted regression model polynomial coefficients
    -
    313  {
    -
    314  // NxF
    - -
    316  for (size_t i = 0; i < X2.size(); i++)
    -
    317  // add Y-intercept -> Nx(F+1)
    -
    318  X2[i].push_back(1);
    -
    319  // (F+1)xN
    - -
    321  // (F+1)x(F+1)
    -
    322  std::vector<std::vector<T>> tmp = get_inverse(Xt * X2);
    -
    323  // (F+1)xN
    -
    324  std::vector<std::vector<float>> out = tmp * Xt;
    -
    325  // cout << endl
    -
    326  // << "Projection matrix: " << X2 * out << endl;
    -
    327 
    -
    328  // Fx1,1 -> (F+1)^th element is the independent constant
    -
    329  return out * Y;
    -
    330 }
    +
    322  {
    +
    323  // NxF
    + +
    325  for (size_t i = 0; i < X2.size(); i++) {
    +
    326  // add Y-intercept -> Nx(F+1)
    +
    327  X2[i].push_back(1);
    +
    328  }
    +
    329  // (F+1)xN
    + +
    331  // (F+1)x(F+1)
    +
    332  std::vector<std::vector<T>> tmp = get_inverse(Xt * X2);
    +
    333  // (F+1)xN
    +
    334  std::vector<std::vector<float>> out = tmp * Xt;
    +
    335  // cout << endl
    +
    336  // << "Projection matrix: " << X2 * out << endl;
    +
    337 
    +
    338  // Fx1,1 -> (F+1)^th element is the independent constant
    +
    339  return out * Y;
    +
    340 }
    Here is the call graph for this function:
    -
    +
    @@ -248,70 +249,73 @@ template<typename T >

    Get matrix inverse using Row-trasnformations. Given matrix must be a square and non-singular.

    Returns
    inverse matrix
    -
    221  {
    -
    222  // Assuming A is square matrix
    -
    223  size_t N = A.size();
    -
    224 
    -
    225  std::vector<std::vector<float>> inverse(N);
    -
    226  for (size_t row = 0; row < N; row++) {
    -
    227  // preallocatae a resultant identity matrix
    -
    228  inverse[row] = std::vector<float>(N);
    -
    229  for (size_t col = 0; col < N; col++)
    -
    230  inverse[row][col] = (row == col) ? 1.f : 0.f;
    -
    231  }
    -
    232 
    -
    233  if (!is_square(A)) {
    -
    234  std::cerr << "A must be a square matrix!" << std::endl;
    -
    235  return inverse;
    -
    236  }
    -
    237 
    -
    238  // preallocatae a temporary matrix identical to A
    - -
    240  for (size_t row = 0; row < N; row++) {
    -
    241  std::vector<float> v(N);
    -
    242  for (size_t col = 0; col < N; col++)
    -
    243  v[col] = static_cast<float>(A[row][col]);
    -
    244  temp[row] = v;
    -
    245  }
    -
    246 
    -
    247  // start transformations
    -
    248  for (size_t row = 0; row < N; row++) {
    -
    249  for (size_t row2 = row; row2 < N && temp[row][row] == 0; row2++) {
    -
    250  // this to ensure diagonal elements are not 0
    -
    251  temp[row] = temp[row] + temp[row2];
    -
    252  inverse[row] = inverse[row] + inverse[row2];
    -
    253  }
    +
    227  {
    +
    228  // Assuming A is square matrix
    +
    229  size_t N = A.size();
    +
    230 
    +
    231  std::vector<std::vector<float>> inverse(N);
    +
    232  for (size_t row = 0; row < N; row++) {
    +
    233  // preallocatae a resultant identity matrix
    +
    234  inverse[row] = std::vector<float>(N);
    +
    235  for (size_t col = 0; col < N; col++) {
    +
    236  inverse[row][col] = (row == col) ? 1.f : 0.f;
    +
    237  }
    +
    238  }
    +
    239 
    +
    240  if (!is_square(A)) {
    +
    241  std::cerr << "A must be a square matrix!" << std::endl;
    +
    242  return inverse;
    +
    243  }
    +
    244 
    +
    245  // preallocatae a temporary matrix identical to A
    + +
    247  for (size_t row = 0; row < N; row++) {
    +
    248  std::vector<float> v(N);
    +
    249  for (size_t col = 0; col < N; col++) {
    +
    250  v[col] = static_cast<float>(A[row][col]);
    +
    251  }
    +
    252  temp[row] = v;
    +
    253  }
    254 
    -
    255  for (size_t col2 = row; col2 < N && temp[row][row] == 0; col2++) {
    -
    256  // this to further ensure diagonal elements are not 0
    -
    257  for (size_t row2 = 0; row2 < N; row2++) {
    -
    258  temp[row2][row] = temp[row2][row] + temp[row2][col2];
    -
    259  inverse[row2][row] = inverse[row2][row] + inverse[row2][col2];
    -
    260  }
    +
    255  // start transformations
    +
    256  for (size_t row = 0; row < N; row++) {
    +
    257  for (size_t row2 = row; row2 < N && temp[row][row] == 0; row2++) {
    +
    258  // this to ensure diagonal elements are not 0
    +
    259  temp[row] = temp[row] + temp[row2];
    +
    260  inverse[row] = inverse[row] + inverse[row2];
    261  }
    262 
    -
    263  if (temp[row][row] == 0) {
    -
    264  // Probably a low-rank matrix and hence singular
    -
    265  std::cerr << "Low-rank matrix, no inverse!" << std::endl;
    -
    266  return inverse;
    -
    267  }
    -
    268 
    -
    269  // set diagonal to 1
    -
    270  float divisor = static_cast<float>(temp[row][row]);
    -
    271  temp[row] = temp[row] / divisor;
    -
    272  inverse[row] = inverse[row] / divisor;
    -
    273  // Row transformations
    -
    274  for (size_t row2 = 0; row2 < N; row2++) {
    -
    275  if (row2 == row)
    -
    276  continue;
    -
    277  float factor = temp[row2][row];
    -
    278  temp[row2] = temp[row2] - factor * temp[row];
    -
    279  inverse[row2] = inverse[row2] - factor * inverse[row];
    -
    280  }
    -
    281  }
    -
    282 
    -
    283  return inverse;
    -
    284 }
    +
    263  for (size_t col2 = row; col2 < N && temp[row][row] == 0; col2++) {
    +
    264  // this to further ensure diagonal elements are not 0
    +
    265  for (size_t row2 = 0; row2 < N; row2++) {
    +
    266  temp[row2][row] = temp[row2][row] + temp[row2][col2];
    +
    267  inverse[row2][row] = inverse[row2][row] + inverse[row2][col2];
    +
    268  }
    +
    269  }
    +
    270 
    +
    271  if (temp[row][row] == 0) {
    +
    272  // Probably a low-rank matrix and hence singular
    +
    273  std::cerr << "Low-rank matrix, no inverse!" << std::endl;
    +
    274  return inverse;
    +
    275  }
    +
    276 
    +
    277  // set diagonal to 1
    +
    278  auto divisor = static_cast<float>(temp[row][row]);
    +
    279  temp[row] = temp[row] / divisor;
    +
    280  inverse[row] = inverse[row] / divisor;
    +
    281  // Row transformations
    +
    282  for (size_t row2 = 0; row2 < N; row2++) {
    +
    283  if (row2 == row) {
    +
    284  continue;
    +
    285  }
    +
    286  float factor = temp[row2][row];
    +
    287  temp[row2] = temp[row2] - factor * temp[row];
    +
    288  inverse[row2] = inverse[row2] - factor * inverse[row];
    +
    289  }
    +
    290  }
    +
    291 
    +
    292  return inverse;
    +
    293 }
    Here is the call graph for this function:
    @@ -339,17 +343,17 @@ template<typename T >

    matrix transpose

    Returns
    resultant matrix
    -
    292  {
    -
    293  std::vector<std::vector<T>> result(A[0].size());
    -
    294 
    -
    295  for (size_t row = 0; row < A[0].size(); row++) {
    -
    296  std::vector<T> v(A.size());
    -
    297  for (size_t col = 0; col < A.size(); col++) v[col] = A[col][row];
    -
    298 
    -
    299  result[row] = v;
    -
    300  }
    -
    301  return result;
    -
    302 }
    +
    301  {
    +
    302  std::vector<std::vector<T>> result(A[0].size());
    +
    303 
    +
    304  for (size_t row = 0; row < A[0].size(); row++) {
    +
    305  std::vector<T> v(A.size());
    +
    306  for (size_t col = 0; col < A.size(); col++) v[col] = A[col][row];
    +
    307 
    +
    308  result[row] = v;
    +
    309  }
    +
    310  return result;
    +
    311 }
    @@ -379,14 +383,16 @@ template<typename T >

    function to check if given matrix is a square matrix

    Returns
    1 if true, 0 if false
    -
    57  {
    -
    58  // Assuming A is square matrix
    -
    59  size_t N = A.size();
    -
    60  for (size_t i = 0; i < N; i++)
    -
    61  if (A[i].size() != N)
    -
    62  return false;
    -
    63  return true;
    -
    64 }
    +
    59  {
    +
    60  // Assuming A is square matrix
    +
    61  size_t N = A.size();
    +
    62  for (size_t i = 0; i < N; i++) {
    +
    63  if (A[i].size() != N) {
    +
    64  return false;
    +
    65  }
    +
    66  }
    +
    67  return true;
    +
    68 }
    @@ -405,62 +411,63 @@ template<typename T >

    main function

    -
    410  {
    -
    411  ols_test();
    -
    412 
    -
    413  size_t N, F;
    -
    414 
    -
    415  std::cout << "Enter number of features: ";
    -
    416  // number of features = columns
    -
    417  std::cin >> F;
    -
    418  std::cout << "Enter number of samples: ";
    -
    419  // number of samples = rows
    -
    420  std::cin >> N;
    -
    421 
    - -
    423  std::vector<float> Y(N);
    -
    424 
    -
    425  std::cout
    -
    426  << "Enter training data. Per sample, provide features and one output."
    -
    427  << std::endl;
    -
    428 
    -
    429  for (size_t rows = 0; rows < N; rows++) {
    -
    430  std::vector<float> v(F);
    -
    431  std::cout << "Sample# " << rows + 1 << ": ";
    -
    432  for (size_t cols = 0; cols < F; cols++)
    -
    433  // get the F features
    -
    434  std::cin >> v[cols];
    -
    435  data[rows] = v;
    -
    436  // get the corresponding output
    -
    437  std::cin >> Y[rows];
    -
    438  }
    -
    439 
    - -
    441  std::cout << std::endl << std::endl << "beta:" << beta << std::endl;
    -
    442 
    -
    443  size_t T;
    -
    444  std::cout << "Enter number of test samples: ";
    -
    445  // number of test sample inputs
    -
    446  std::cin >> T;
    - -
    448  // vector<float> Y2(T);
    -
    449 
    -
    450  for (size_t rows = 0; rows < T; rows++) {
    -
    451  std::cout << "Sample# " << rows + 1 << ": ";
    -
    452  std::vector<float> v(F);
    -
    453  for (size_t cols = 0; cols < F; cols++) std::cin >> v[cols];
    -
    454  data2[rows] = v;
    -
    455  }
    +
    423  {
    +
    424  ols_test();
    +
    425 
    +
    426  size_t N = 0, F = 0;
    +
    427 
    +
    428  std::cout << "Enter number of features: ";
    +
    429  // number of features = columns
    +
    430  std::cin >> F;
    +
    431  std::cout << "Enter number of samples: ";
    +
    432  // number of samples = rows
    +
    433  std::cin >> N;
    +
    434 
    + +
    436  std::vector<float> Y(N);
    +
    437 
    +
    438  std::cout
    +
    439  << "Enter training data. Per sample, provide features and one output."
    +
    440  << std::endl;
    +
    441 
    +
    442  for (size_t rows = 0; rows < N; rows++) {
    +
    443  std::vector<float> v(F);
    +
    444  std::cout << "Sample# " << rows + 1 << ": ";
    +
    445  for (size_t cols = 0; cols < F; cols++) {
    +
    446  // get the F features
    +
    447  std::cin >> v[cols];
    +
    448  }
    +
    449  data[rows] = v;
    +
    450  // get the corresponding output
    +
    451  std::cin >> Y[rows];
    +
    452  }
    +
    453 
    + +
    455  std::cout << std::endl << std::endl << "beta:" << beta << std::endl;
    456 
    -
    457  std::vector<float> out = predict_OLS_regressor(data2, beta);
    -
    458  for (size_t rows = 0; rows < T; rows++) std::cout << out[rows] << std::endl;
    -
    459 
    -
    460  return 0;
    -
    461 }
    +
    457  size_t T = 0;
    +
    458  std::cout << "Enter number of test samples: ";
    +
    459  // number of test sample inputs
    +
    460  std::cin >> T;
    + +
    462  // vector<float> Y2(T);
    +
    463 
    +
    464  for (size_t rows = 0; rows < T; rows++) {
    +
    465  std::cout << "Sample# " << rows + 1 << ": ";
    +
    466  std::vector<float> v(F);
    +
    467  for (size_t cols = 0; cols < F; cols++) std::cin >> v[cols];
    +
    468  data2[rows] = v;
    +
    469  }
    +
    470 
    +
    471  std::vector<float> out = predict_OLS_regressor(data2, beta);
    +
    472  for (size_t rows = 0; rows < T; rows++) std::cout << out[rows] << std::endl;
    +
    473 
    +
    474  return 0;
    +
    475 }
    Here is the call graph for this function:
    -
    +
    @@ -481,58 +488,60 @@ Here is the call graph for this function:

    Self test checks

    -
    358  {
    -
    359  int F = 3, N = 5;
    -
    360 
    -
    361  /* test function = x^2 -5 */
    -
    362  std::cout << "Test 1 (quadratic function)....";
    -
    363  // create training data set with features = x, x^2, x^3
    - -
    365  {{-5, 25, -125}, {-1, 1, -1}, {0, 0, 0}, {1, 1, 1}, {6, 36, 216}});
    -
    366  // create corresponding outputs
    -
    367  std::vector<float> Y1({20, -4, -5, -4, 31});
    -
    368  // perform regression modelling
    -
    369  std::vector<float> beta1 = fit_OLS_regressor(data1, Y1);
    -
    370  // create test data set with same features = x, x^2, x^3
    -
    371  std::vector<std::vector<float>> test_data1(
    -
    372  {{-2, 4, -8}, {2, 4, 8}, {-10, 100, -1000}, {10, 100, 1000}});
    -
    373  // expected regression outputs
    -
    374  std::vector<float> expected1({-1, -1, 95, 95});
    -
    375  // predicted regression outputs
    -
    376  std::vector<float> out1 = predict_OLS_regressor(test_data1, beta1);
    -
    377  // compare predicted results are within +-0.01 limit of expected
    -
    378  for (size_t rows = 0; rows < out1.size(); rows++)
    -
    379  assert(std::abs(out1[rows] - expected1[rows]) < 0.01);
    -
    380  std::cout << "passed\n";
    -
    381 
    -
    382  /* test function = x^3 + x^2 - 100 */
    -
    383  std::cout << "Test 2 (cubic function)....";
    -
    384  // create training data set with features = x, x^2, x^3
    - -
    386  {{-5, 25, -125}, {-1, 1, -1}, {0, 0, 0}, {1, 1, 1}, {6, 36, 216}});
    -
    387  // create corresponding outputs
    -
    388  std::vector<float> Y2({-200, -100, -100, 98, 152});
    -
    389  // perform regression modelling
    -
    390  std::vector<float> beta2 = fit_OLS_regressor(data2, Y2);
    -
    391  // create test data set with same features = x, x^2, x^3
    -
    392  std::vector<std::vector<float>> test_data2(
    -
    393  {{-2, 4, -8}, {2, 4, 8}, {-10, 100, -1000}, {10, 100, 1000}});
    -
    394  // expected regression outputs
    -
    395  std::vector<float> expected2({-104, -88, -1000, 1000});
    -
    396  // predicted regression outputs
    -
    397  std::vector<float> out2 = predict_OLS_regressor(test_data2, beta2);
    -
    398  // compare predicted results are within +-0.01 limit of expected
    -
    399  for (size_t rows = 0; rows < out2.size(); rows++)
    -
    400  assert(std::abs(out2[rows] - expected2[rows]) < 0.01);
    -
    401  std::cout << "passed\n";
    -
    402 
    -
    403  std::cout << std::endl; // ensure test results are displayed on screen
    -
    404  // (flush stdout)
    -
    405 }
    +
    369  {
    +
    370  int F = 3, N = 5;
    +
    371 
    +
    372  /* test function = x^2 -5 */
    +
    373  std::cout << "Test 1 (quadratic function)....";
    +
    374  // create training data set with features = x, x^2, x^3
    + +
    376  {{-5, 25, -125}, {-1, 1, -1}, {0, 0, 0}, {1, 1, 1}, {6, 36, 216}});
    +
    377  // create corresponding outputs
    +
    378  std::vector<float> Y1({20, -4, -5, -4, 31});
    +
    379  // perform regression modelling
    +
    380  std::vector<float> beta1 = fit_OLS_regressor(data1, Y1);
    +
    381  // create test data set with same features = x, x^2, x^3
    +
    382  std::vector<std::vector<float>> test_data1(
    +
    383  {{-2, 4, -8}, {2, 4, 8}, {-10, 100, -1000}, {10, 100, 1000}});
    +
    384  // expected regression outputs
    +
    385  std::vector<float> expected1({-1, -1, 95, 95});
    +
    386  // predicted regression outputs
    +
    387  std::vector<float> out1 = predict_OLS_regressor(test_data1, beta1);
    +
    388  // compare predicted results are within +-0.01 limit of expected
    +
    389  for (size_t rows = 0; rows < out1.size(); rows++) {
    +
    390  assert(std::abs(out1[rows] - expected1[rows]) < 0.01);
    +
    391  }
    +
    392  std::cout << "passed\n";
    +
    393 
    +
    394  /* test function = x^3 + x^2 - 100 */
    +
    395  std::cout << "Test 2 (cubic function)....";
    +
    396  // create training data set with features = x, x^2, x^3
    + +
    398  {{-5, 25, -125}, {-1, 1, -1}, {0, 0, 0}, {1, 1, 1}, {6, 36, 216}});
    +
    399  // create corresponding outputs
    +
    400  std::vector<float> Y2({-200, -100, -100, 98, 152});
    +
    401  // perform regression modelling
    +
    402  std::vector<float> beta2 = fit_OLS_regressor(data2, Y2);
    +
    403  // create test data set with same features = x, x^2, x^3
    +
    404  std::vector<std::vector<float>> test_data2(
    +
    405  {{-2, 4, -8}, {2, 4, 8}, {-10, 100, -1000}, {10, 100, 1000}});
    +
    406  // expected regression outputs
    +
    407  std::vector<float> expected2({-104, -88, -1000, 1000});
    +
    408  // predicted regression outputs
    +
    409  std::vector<float> out2 = predict_OLS_regressor(test_data2, beta2);
    +
    410  // compare predicted results are within +-0.01 limit of expected
    +
    411  for (size_t rows = 0; rows < out2.size(); rows++) {
    +
    412  assert(std::abs(out2[rows] - expected2[rows]) < 0.01);
    +
    413  }
    +
    414  std::cout << "passed\n";
    +
    415 
    +
    416  std::cout << std::endl; // ensure test results are displayed on screen
    +
    417  // (flush stdout)
    +
    418 }
    Here is the call graph for this function:
    -
    +
    @@ -566,18 +575,18 @@ template<typename T >

    pre-multiplication of a vector by a scalar

    Returns
    resultant vector
    -
    133  {
    -
    134  // Number of rows in A
    -
    135  size_t N_A = A.size();
    -
    136 
    -
    137  std::vector<float> result(N_A);
    -
    138 
    -
    139  for (size_t row = 0; row < N_A; row++) {
    -
    140  result[row] += A[row] * static_cast<float>(scalar);
    -
    141  }
    -
    142 
    -
    143  return result;
    -
    144 }
    +
    138  {
    +
    139  // Number of rows in A
    +
    140  size_t N_A = A.size();
    +
    141 
    +
    142  std::vector<float> result(N_A);
    +
    143 
    +
    144  for (size_t row = 0; row < N_A; row++) {
    +
    145  result[row] += A[row] * static_cast<float>(scalar);
    +
    146  }
    +
    147 
    +
    148  return result;
    +
    149 }
    Here is the call graph for this function:
    @@ -616,32 +625,33 @@ template<typename T >

    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
    -
    75  {
    -
    76  // Number of rows in A
    -
    77  size_t N_A = A.size();
    -
    78  // Number of columns in B
    -
    79  size_t N_B = B[0].size();
    -
    80 
    -
    81  std::vector<std::vector<T>> result(N_A);
    -
    82 
    -
    83  if (A[0].size() != B.size()) {
    -
    84  std::cerr << "Number of columns in A != Number of rows in B ("
    -
    85  << A[0].size() << ", " << B.size() << ")" << std::endl;
    -
    86  return result;
    -
    87  }
    -
    88 
    -
    89  for (size_t row = 0; row < N_A; row++) {
    -
    90  std::vector<T> v(N_B);
    -
    91  for (size_t col = 0; col < N_B; col++) {
    -
    92  v[col] = static_cast<T>(0);
    -
    93  for (size_t j = 0; j < B.size(); j++)
    -
    94  v[col] += A[row][j] * B[j][col];
    -
    95  }
    -
    96  result[row] = v;
    -
    97  }
    -
    98 
    -
    99  return result;
    -
    100 }
    +
    79  {
    +
    80  // Number of rows in A
    +
    81  size_t N_A = A.size();
    +
    82  // Number of columns in B
    +
    83  size_t N_B = B[0].size();
    +
    84 
    +
    85  std::vector<std::vector<T>> result(N_A);
    +
    86 
    +
    87  if (A[0].size() != B.size()) {
    +
    88  std::cerr << "Number of columns in A != Number of rows in B ("
    +
    89  << A[0].size() << ", " << B.size() << ")" << std::endl;
    +
    90  return result;
    +
    91  }
    +
    92 
    +
    93  for (size_t row = 0; row < N_A; row++) {
    +
    94  std::vector<T> v(N_B);
    +
    95  for (size_t col = 0; col < N_B; col++) {
    +
    96  v[col] = static_cast<T>(0);
    +
    97  for (size_t j = 0; j < B.size(); j++) {
    +
    98  v[col] += A[row][j] * B[j][col];
    +
    99  }
    +
    100  }
    +
    101  result[row] = v;
    +
    102  }
    +
    103 
    +
    104  return result;
    +
    105 }
    Here is the call graph for this function:
    @@ -679,25 +689,25 @@ template<typename T >

    multiplication of a matrix with a column vector

    Returns
    resultant vector
    -
    108  {
    -
    109  // Number of rows in A
    -
    110  size_t N_A = A.size();
    -
    111 
    -
    112  std::vector<T> result(N_A);
    -
    113 
    -
    114  if (A[0].size() != B.size()) {
    -
    115  std::cerr << "Number of columns in A != Number of rows in B ("
    -
    116  << A[0].size() << ", " << B.size() << ")" << std::endl;
    -
    117  return result;
    -
    118  }
    -
    119 
    -
    120  for (size_t row = 0; row < N_A; row++) {
    -
    121  result[row] = static_cast<T>(0);
    -
    122  for (size_t j = 0; j < B.size(); j++) result[row] += A[row][j] * B[j];
    +
    113  {
    +
    114  // Number of rows in A
    +
    115  size_t N_A = A.size();
    +
    116 
    +
    117  std::vector<T> result(N_A);
    +
    118 
    +
    119  if (A[0].size() != B.size()) {
    +
    120  std::cerr << "Number of columns in A != Number of rows in B ("
    +
    121  << A[0].size() << ", " << B.size() << ")" << std::endl;
    +
    122  return result;
    123  }
    124 
    -
    125  return result;
    -
    126 }
    +
    125  for (size_t row = 0; row < N_A; row++) {
    +
    126  result[row] = static_cast<T>(0);
    +
    127  for (size_t j = 0; j < B.size(); j++) result[row] += A[row][j] * B[j];
    +
    128  }
    +
    129 
    +
    130  return result;
    +
    131 }
    Here is the call graph for this function:
    @@ -735,17 +745,18 @@ template<typename T >

    post-multiplication of a vector by a scalar

    Returns
    resultant vector
    -
    151  {
    -
    152  // Number of rows in A
    -
    153  size_t N_A = A.size();
    -
    154 
    -
    155  std::vector<float> result(N_A);
    -
    156 
    -
    157  for (size_t row = 0; row < N_A; row++)
    -
    158  result[row] = A[row] * static_cast<float>(scalar);
    +
    156  {
    +
    157  // Number of rows in A
    +
    158  size_t N_A = A.size();
    159 
    -
    160  return result;
    -
    161 }
    +
    160  std::vector<float> result(N_A);
    +
    161 
    +
    162  for (size_t row = 0; row < N_A; row++) {
    +
    163  result[row] = A[row] * static_cast<float>(scalar);
    +
    164  }
    +
    165 
    +
    166  return result;
    +
    167 }
    Here is the call graph for this function:
    @@ -783,21 +794,21 @@ template<typename T >

    addition of two vectors of identical lengths

    Returns
    resultant vector
    -
    198  {
    -
    199  // Number of rows in A
    -
    200  size_t N = A.size();
    -
    201 
    -
    202  std::vector<T> result(N);
    -
    203 
    -
    204  if (B.size() != N) {
    -
    205  std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
    -
    206  return A;
    -
    207  }
    -
    208 
    -
    209  for (size_t row = 0; row < N; row++) result[row] = A[row] + B[row];
    -
    210 
    -
    211  return result;
    -
    212 }
    +
    204  {
    +
    205  // Number of rows in A
    +
    206  size_t N = A.size();
    +
    207 
    +
    208  std::vector<T> result(N);
    +
    209 
    +
    210  if (B.size() != N) {
    +
    211  std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
    +
    212  return A;
    +
    213  }
    +
    214 
    +
    215  for (size_t row = 0; row < N; row++) result[row] = A[row] + B[row];
    +
    216 
    +
    217  return result;
    +
    218 }
    Here is the call graph for this function:
    @@ -835,21 +846,21 @@ template<typename T >

    subtraction of two vectors of identical lengths

    Returns
    resultant vector
    -
    177  {
    -
    178  // Number of rows in A
    -
    179  size_t N = A.size();
    -
    180 
    -
    181  std::vector<T> result(N);
    -
    182 
    -
    183  if (B.size() != N) {
    -
    184  std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
    -
    185  return A;
    -
    186  }
    -
    187 
    -
    188  for (size_t row = 0; row < N; row++) result[row] = A[row] - B[row];
    -
    189 
    -
    190  return result;
    -
    191 }
    +
    183  {
    +
    184  // Number of rows in A
    +
    185  size_t N = A.size();
    +
    186 
    +
    187  std::vector<T> result(N);
    +
    188 
    +
    189  if (B.size() != N) {
    +
    190  std::cerr << "Vector dimensions shouldbe identical!" << std::endl;
    +
    191  return A;
    +
    192  }
    +
    193 
    +
    194  for (size_t row = 0; row < N; row++) result[row] = A[row] - B[row];
    +
    195 
    +
    196  return result;
    +
    197 }
    Here is the call graph for this function:
    @@ -887,9 +898,9 @@ template<typename T >

    division of a vector by a scalar

    Returns
    resultant vector
    -
    168  {
    -
    169  return (1.f / scalar) * A;
    -
    170 }
    +
    174  {
    +
    175  return (1.f / scalar) * A;
    +
    176 }
    @@ -926,14 +937,15 @@ template<typename T >
    25  const char separator = ' ';
    26 
    27  for (size_t row = 0; row < v.size(); row++) {
    -
    28  for (size_t col = 0; col < v[row].size(); col++)
    +
    28  for (size_t col = 0; col < v[row].size(); col++) {
    29  out << std::left << std::setw(width) << std::setfill(separator)
    30  << v[row][col];
    -
    31  out << std::endl;
    -
    32  }
    -
    33 
    -
    34  return out;
    -
    35 }
    +
    31  }
    +
    32  out << std::endl;
    +
    33  }
    +
    34 
    +
    35  return out;
    +
    36 }
    Here is the call graph for this function:
    @@ -971,16 +983,17 @@ template<typename T >

    operator to print a vector

    -
    41  {
    -
    42  const int width = 15;
    -
    43  const char separator = ' ';
    -
    44 
    -
    45  for (size_t row = 0; row < v.size(); row++)
    -
    46  out << std::left << std::setw(width) << std::setfill(separator)
    -
    47  << v[row];
    -
    48 
    -
    49  return out;
    -
    50 }
    +
    42  {
    +
    43  const int width = 15;
    +
    44  const char separator = ' ';
    +
    45 
    +
    46  for (size_t row = 0; row < v.size(); row++) {
    +
    47  out << std::left << std::setw(width) << std::setfill(separator)
    +
    48  << v[row];
    +
    49  }
    +
    50 
    +
    51  return out;
    +
    52 }
    Here is the call graph for this function:
    @@ -1028,18 +1041,19 @@ template<typename T >
    Returns
    vector with regression values for each sample
    -
    344  {
    -
    345  std::vector<float> result(X.size());
    -
    346 
    -
    347  for (size_t rows = 0; rows < X.size(); rows++) {
    -
    348  // -> start with constant term
    -
    349  result[rows] = beta[X[0].size()];
    -
    350  for (size_t cols = 0; cols < X[0].size(); cols++)
    -
    351  result[rows] += beta[cols] * X[rows][cols];
    -
    352  }
    -
    353  // Nx1
    -
    354  return result;
    -
    355 }
    +
    354  {
    +
    355  std::vector<float> result(X.size());
    +
    356 
    +
    357  for (size_t rows = 0; rows < X.size(); rows++) {
    +
    358  // -> start with constant term
    +
    359  result[rows] = beta[X[0].size()];
    +
    360  for (size_t cols = 0; cols < X[0].size(); cols++) {
    +
    361  result[rows] += beta[cols] * X[rows][cols];
    +
    362  }
    +
    363  }
    +
    364  // Nx1
    +
    365  return result;
    +
    366 }
    @@ -1048,18 +1062,19 @@ template<typename T >
    STL class.
    T size(T... args)
    T setfill(T... args)
    -
    bool is_square(std::vector< std::vector< T >> const &A)
    Definition: ordinary_least_squares_regressor.cpp:57
    +
    bool is_square(std::vector< std::vector< T >> const &A)
    Definition: ordinary_least_squares_regressor.cpp:59
    +
    T push_back(T... args)
    -
    std::vector< float > predict_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< float > const &beta)
    Definition: ordinary_least_squares_regressor.cpp:342
    +
    std::vector< float > predict_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< float > const &beta)
    Definition: ordinary_least_squares_regressor.cpp:352
    int data[MAX]
    test data
    Definition: hash_search.cpp:24
    -
    std::vector< float > fit_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< T > const &Y)
    Definition: ordinary_least_squares_regressor.cpp:312
    +
    std::vector< float > fit_OLS_regressor(std::vector< std::vector< T >> const &X, std::vector< T > const &Y)
    Definition: ordinary_least_squares_regressor.cpp:321
    T endl(T... args)
    -
    std::vector< std::vector< float > > get_inverse(std::vector< std::vector< T >> const &A)
    Definition: ordinary_least_squares_regressor.cpp:220
    +
    std::vector< std::vector< float > > get_inverse(std::vector< std::vector< T >> const &A)
    Definition: ordinary_least_squares_regressor.cpp:226
    T left(T... args)
    -
    void ols_test()
    Definition: ordinary_least_squares_regressor.cpp:358
    +
    void ols_test()
    Definition: ordinary_least_squares_regressor.cpp:369
    T setw(T... args)
    -
    std::vector< std::vector< T > > get_transpose(std::vector< std::vector< T >> const &A)
    Definition: ordinary_least_squares_regressor.cpp:291
    +
    std::vector< std::vector< T > > get_transpose(std::vector< std::vector< T >> const &A)
    Definition: ordinary_least_squares_regressor.cpp:300