Algorithms_in_C
1.0.0
Set of algorithms implemented in C.
|
Go to the documentation of this file.
10 #define QR_DECOMPOSE_H
26 for (
int row = 0; row < M; row++)
28 for (
int col = 0; col <
N; col++) printf(
"% 9.3g\t", A[row][col]);
49 #pragma omp parallel for reduction(+ : mag)
51 for (i = 0; i <
L; i++) mag +=
a[i] * b[i];
83 const double scalar = num / deno;
89 for (i = 0; i <
L; i++) out[i] = scalar * b[i];
112 for (i = 0; i <
L; i++) out[i] =
a[i] - b[i];
149 double *col_vector = (
double *)malloc(M *
sizeof(
double));
150 double *col_vector2 = (
double *)malloc(M *
sizeof(
double));
151 double *tmp_vector = (
double *)malloc(M *
sizeof(
double));
152 for (
int i = 0; i <
N;
160 for (j = 0; j < i; j++)
168 for (j = 0; j < M; j++)
170 tmp_vector[j] = A[j][i];
171 col_vector[j] = A[j][i];
173 for (j = 0; j < i; j++)
175 for (
int k = 0; k < M; k++) col_vector2[k] = Q[k][j];
176 vector_proj(col_vector, col_vector2, col_vector2, M);
177 vector_sub(tmp_vector, col_vector2, tmp_vector, M);
185 for (j = 0; j < M; j++) Q[j][i] = tmp_vector[j] / mag;
188 for (
int kk = 0; kk < M; kk++) col_vector[kk] = Q[kk][i];
189 for (
int k = i; k <
N; k++)
191 for (
int kk = 0; kk < M; kk++) col_vector2[kk] = A[kk][k];
192 R[i][k] =
vector_dot(col_vector, col_vector2, M);
201 #endif // QR_DECOMPOSE_H
int main(int argc, char *argv[])
Main function.
Definition: collatz.c:16
Definition: k_means_clustering.c:39
dynamically large number
Definition: factorial_large_number.c:15
void test2()
Test that creates a random set of points distributed near the locus of the Lamniscate of Gerono and t...
Definition: kohonen_som_trace.c:358
#define MAX_SIZE
maximum number of elements in the set
Definition: union_find.c:8
double * weights
weights of the neural network
Definition: adaline_learning.c:46
void forward_euler_step(const double dx, const double *x, double *y, double *dy)
Compute next step approximation using the forward-Euler method.
Definition: ode_forward_euler.c:82
void print(const struct sudoku *a)
Print the matrix to stdout.
Definition: sudoku_solver.c:126
void test3()
Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM ...
Definition: kohonen_som_topology.c:609
void test()
Test function.
Definition: decimal_to_binary_recursion.c:20
void exact_solution(const double *x, double *y)
Exact solution of the problem.
Definition: ode_midpoint_euler.c:67
void test_adler32()
Test function for adler32.
Definition: hash_adler32.c:38
struct _cantor_set * next
pointer to next set
Definition: cantor_set.c:15
int group
the group no in which this observation would go
Definition: k_means_clustering.c:42
uint8_t N2
block of elements
Definition: sudoku_solver.c:36
int main(int argc, char const *argv[])
Main function.
Definition: cantor_set.c:84
int compare(const void *a, const void *b)
comparison function for use with internal qsort algorithm
Definition: sol1.c:19
int main(int argc, char **argv)
Main function.
Definition: lu_decompose.c:79
int main(int argc, char *argv[])
Main Function.
Definition: ode_semi_implicit_euler.c:147
void propagate(CantorSet *head)
Iterative constructor of all sets in the current level.
Definition: cantor_set.c:23
double vector_mag(double *vector, int L)
Compute magnitude of vector.
Definition: qr_decompose.h:64
cluster * kMeans(observation observations[], size_t size, int k)
Definition: k_means_clustering.c:134
void adaline_fit(struct adaline *ada, double **X, const int *y, const int N)
Update the weights of the model using supervised learning for an array of vectors.
Definition: adaline_learning.c:184
void join(int *p, int x, int y)
Function to join.
Definition: union_find.c:42
void test_circle(double *const *data, int N)
Creates a random set of points distributed near the circumference of a circle and trains an SOM that ...
Definition: kohonen_som_trace.c:223
double forward_euler(double dx, double x0, double x_max, double *y, char save_to_file)
Compute approximation using the forward-Euler method in the given limits.
Definition: ode_forward_euler.c:99
Definition: prime_factoriziation.c:25
PID Controller.
Definition: pid.c:31
int adaline_activation(double x)
Heaviside activation function
Definition: adaline_learning.c:105
int calculateNearst(observation *o, cluster clusters[], int k)
Definition: k_means_clustering.c:69
double complex d_func(double complex x)
Return first order derivative of the function.
Definition: newton_raphson_root.c:32
struct _cantor_set CantorSet
structure to define Cantor set
int main(int argc, char **argv)
Main function.
Definition: kohonen_som_topology.c:669
int main(int argc, char **argv)
Main function.
Definition: adaline_learning.c:398
double * data
pointer to data
Definition: kohonen_som_topology.c:52
void test_3d_classes1(double *const *data, int N)
Creates a random set of points distributed in four clusters in 3D space with centroids at the points.
Definition: kohonen_som_topology.c:465
int save_2d_data(const char *fname, double **X, int num_points, int num_features)
Save a given n-dimensional data martix to file.
Definition: kohonen_som_topology.c:102
Definition for a binary tree node.
Definition: 173.c:13
double _random(double a, double b)
Helper function to generate a random number in a given interval.
Definition: kohonen_som_topology.c:87
Node, the basic data structure in the tree.
Definition: binary_search_tree.c:15
double * vector_proj(double *a, double *b, double *out, int L)
Compute projection of vector on defined as.
Definition: qr_decompose.h:76
void free_memory(CantorSet *head)
Clear memory allocated by propagate function.
Definition: cantor_set.c:72
int main()
Definition: k_means_clustering.c:384
unsigned int num_digits
number of digits in the number
Definition: factorial_large_number.c:17
void test3()
Test that creates a random set of points distributed in six clusters in 3D space.
Definition: kohonen_som_trace.c:462
void calculateCentroid(observation observations[], size_t size, cluster *centroid)
Definition: k_means_clustering.c:97
int save_nd_data(const char *fname, double **X, int num_points, int num_features)
Save a given n-dimensional data martix to file.
Definition: kohonen_som_trace.c:70
int dim2
lengths of second dimension
Definition: kohonen_som_topology.c:50
void test_3d_classes2(double *const *data, int N)
Creates a random set of points distributed in four clusters in 3D space with centroids at the points.
Definition: kohonen_som_topology.c:564
int dim3
lengths of thirddimension
Definition: kohonen_som_topology.c:51
double x
abscissa of 2D data point
Definition: k_means_clustering.c:40
uint8_t N
number of elements
Definition: sudoku_solver.c:35
void qr_decompose(double **A, double **Q, double **R, int M, int N)
Decompose matrix using Gram-Schmidt process.
Definition: qr_decompose.h:142
const char * complex_str(long double complex x)
create a textual form of complex number
Definition: durand_kerner_roots.c:66
int main()
Main function.
Definition: cartesian_to_polar.c:118
int main(int argc, char *argv[])
main function
Definition: fibonacci_fast.c:65
struct _large_num large_num
dynamically large number
node * newNode(int data)
The node constructor, which receives the key value input and returns a node pointer.
Definition: binary_search_tree.c:28
large_num * new_number(void)
create a new large number
Definition: factorial_large_number.c:24
Definition: k_means_clustering.c:53
char check_termination(long double delta)
check for termination condition
Definition: durand_kerner_roots.c:83
double semi_implicit_euler(double dx, double x0, double x_max, double *y, char save_to_file)
Compute approximation using the semi-implicit-Euler method in the given limits.
Definition: ode_semi_implicit_euler.c:109
bool isPalindrome(int number)
Check given number whether is palindrome number or not.
Definition: palindrome.c:29
uint8_t xor8(const char *s)
8-bit XOR algorithm implementation
Definition: hash_xor8.c:19
long double complex poly_function(long double *coeffs, unsigned int degree, long double complex x)
Evaluate the value of a polynomial with given coefficients.
Definition: durand_kerner_roots.c:50
int main()
Main function.
Definition: union_find.c:45
int main(int argc, char *argv[])
main function
Definition: factorial_large_number.c:94
void test2()
Definition: k_means_clustering.c:356
#define ADALINE_ACCURACY
convergence accuracy
Definition: adaline_learning.c:51
void multiply(large_num *num, unsigned long n)
multiply large number with another integer and store the result in the same large number
Definition: factorial_large_number.c:66
double y
ordinate of centroid of this cluster
Definition: k_means_clustering.c:55
void kohonen_update_weights(double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)
Update weights of the SOM using Kohonen algorithm.
Definition: kohonen_som_trace.c:129
void test_lamniscate(double *const *data, int N)
Creates a random set of points distributed near the locus of the Lamniscate of Gerono.
Definition: kohonen_som_trace.c:319
uint64_t djb2(const char *s)
DJB2 algorithm implementation.
Definition: hash_djb2.c:18
unsigned long lcm(unsigned long a, unsigned long b)
Compute Least Common Multiple (LCM) of two numbers.
Definition: sol.c:31
bool get_next_unknown(const struct sudoku *a, int *x, int *y)
Find and get the location for next empty cell.
Definition: sudoku_solver.c:144
size_t count
count of observations present in this cluster
Definition: k_means_clustering.c:56
uint32_t crc32(const char *s)
32-bit CRC algorithm implementation
Definition: hash_crc32.c:20
int main()
Driver code.
Definition: client.c:70
void swap(int *first, int *second)
Swap two values by using pointer.
Definition: bubble_sort.c:31
void spirograph(double *x, double *y, double l, double k, size_t N, double rot)
Generate spirograph curve into arrays x and y such that the i^th point in 2D is represented by (x[i],...
Definition: spirograph.c:57
void midpoint_euler_step(double dx, double *x, double *y, double *dy)
Compute next step approximation using the midpoint-Euler method.
Definition: ode_midpoint_euler.c:83
int main(int argc, char **argv)
main function
Definition: newton_raphson_root.c:37
int main(int argc, char **argv)
Main function.
Definition: kohonen_som_trace.c:517
void fib(unsigned long n, unsigned long *C, unsigned long *D)
Get the and Fibonacci number using recursive half-interval decimation.
Definition: fibonacci_fast.c:23
bool solve(struct sudoku *a)
Function to solve a partially filled sudoku matrix.
Definition: sudoku_solver.c:172
double start
start of interval
Definition: cantor_set.c:13
double end
end of interval
Definition: cantor_set.c:14
void test_crc32()
Test function for crc32.
Definition: hash_crc32.c:42
void test_djb2(void)
Test function for djb2.
Definition: hash_djb2.c:34
double adaline_fit_sample(struct adaline *ada, const double *x, const int y)
Update the weights of the model using supervised learning for one feature vector.
Definition: adaline_learning.c:158
bool OKrow(const struct sudoku *a, int x, int y, int v)
Check if x^th row is valid.
Definition: sudoku_solver.c:48
void print_matrix(double **A, int M, int N)
function to display matrix on stdout
Definition: qr_decompose.h:22
double vector_dot(double *a, double *b, int L)
Compute dot product of two vectors of equal lengths.
Definition: qr_decompose.h:43
double eta
learning rate of the algorithm
Definition: adaline_learning.c:45
int main()
Main function.
Definition: hash_xor8.c:47
void exact_solution(const double *x, double *y)
Exact solution of the problem.
Definition: ode_semi_implicit_euler.c:71
bool OKcol(const struct sudoku *a, int x, int y, int v)
Check if y^th column is valid.
Definition: sudoku_solver.c:67
int main()
Main function.
Definition: hash_adler32.c:50
int main(int argc, char *argv[])
Main Function.
Definition: ode_midpoint_euler.c:144
double x
abscissa centroid of this cluster
Definition: k_means_clustering.c:54
void delete_adaline(struct adaline *ada)
delete dynamically allocated memory
Definition: adaline_learning.c:89
void kohonen_som_tracer(double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min)
Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given...
Definition: kohonen_som_trace.c:179
void test2()
Test that creates a random set of points distributed in 4 clusters in 3D space and trains an SOM that...
Definition: kohonen_som_topology.c:506
int find(int *p, int x)
Find index of or value in an array.
Definition: union_find.c:17
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations.
Definition: ode_forward_euler.c:55
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations.
Definition: ode_semi_implicit_euler.c:58
double get_clock_diff(clock_t start_t, clock_t end_t)
Convert clock cycle difference to time in seconds.
Definition: kohonen_som_trace.c:511
#define min(a, b)
shorthand for minimum value
Definition: kohonen_som_trace.c:36
#define order
number of dependent variables in problem
Definition: ode_semi_implicit_euler.c:47
void get_min_2d(double **X, int N, double *val, int *x_idx, int *y_idx)
Get minimum value and index of the value in a matrix.
Definition: kohonen_som_topology.c:204
#define MAX_LEN
length of resulting recurring fraction number
Definition: sol1.c:15
void to_polar(double x, double y, double *r, double *theta)
Function to convert cartesian coordinates to polar.
Definition: cartesian_to_polar.c:22
void search(node *root, int ele)
searches for the element
Definition: threaded_binary_trees.c:98
void test(void)
Test function to save resulting points to a CSV file.
Definition: spirograph.c:74
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_trace.c:32
bool OKbox(const struct sudoku *a, int x, int y, int v)
Check if a 3x3 box is valid.
Definition: sudoku_solver.c:85
unsigned long gcd(unsigned long a, unsigned long b)
Compute Greatest Common Divisor (GCD) of two numbers using Euclids algorithm.
Definition: sol.c:11
void test_sdbm()
Test function for sdbm.
Definition: hash_sdbm.c:34
#define MAX_ADALINE_ITER
Maximum number of iterations to learn.
Definition: adaline_learning.c:40
void delete_number(large_num *num)
delete all memory allocated for large number
Definition: factorial_large_number.c:37
int lu_decomposition(double **A, double **L, double **U, int mat_size)
Perform LU decomposition on matrix.
Definition: lu_decompose.c:20
to store info regarding 3D arrays
Definition: kohonen_som_topology.c:48
#define MAX
max.
Definition: client.c:28
#define min(a, b)
shorthand for minimum value
Definition: kohonen_som_topology.c:43
double * vector_sub(double *a, double *b, double *out, int L)
Compute vector subtraction.
Definition: qr_decompose.h:101
void kohonen_som(double **X, struct kohonen_array_3d *W, int num_samples, int num_features, int num_out, double alpha_min)
Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given...
Definition: kohonen_som_topology.c:314
int main(int argc, char **argv)
Definition: durand_kerner_roots.c:95
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_topology.c:39
int main()
Main function.
Definition: hash_djb2.c:46
void test3(double eta)
test function to predict points in a 3D coordinate system lying within the sphere of radius 1 and cen...
Definition: adaline_learning.c:336
void display(double **A, int N)
Function to display square matrix.
Definition: lu_decompose.c:66
bool OK(const struct sudoku *a, int x, int y, int v)
Check if element v is valid to place at (x,y) location.
Definition: sudoku_solver.c:111
#define order
number of dependent variables in problem
Definition: ode_midpoint_euler.c:43
void exact_solution(const double *x, double *y)
Exact solution of the problem.
Definition: ode_forward_euler.c:68
void test_xor8()
Test function for xor8.
Definition: hash_xor8.c:35
void test1()
Test that creates a random set of points distributed near the circumference of a circle and trains an...
Definition: kohonen_som_trace.c:261
int main(int argc, char *argv[])
Main Function.
Definition: ode_forward_euler.c:137
int main(int argc, char **argv)
Main function.
Definition: spirograph.c:271
double _random(double a, double b)
Helper function to generate a random number in a given interval.
Definition: kohonen_som_trace.c:54
int dim1
lengths of first dimension
Definition: kohonen_som_topology.c:49
double complex func(double complex x)
Return value of the function to find the root for.
Definition: newton_raphson_root.c:22
uint8_t * a
matrix as a flattened 1D row-major array
Definition: sudoku_solver.c:34
struct node * left
left child
Definition: binary_search_tree.c:16
uint64_t sdbm(const char *s)
SDBM algorithm implementation.
Definition: hash_sdbm.c:18
void test1(double eta)
test function to predict points in a 2D coordinate system above the line as +1 and others as -1.
Definition: adaline_learning.c:225
void printEPS(observation pts[], size_t len, cluster cent[], int k)
Definition: k_means_clustering.c:237
void test2(double eta)
test function to predict points in a 2D coordinate system above the line as +1 and others as -1.
Definition: adaline_learning.c:273
#define ACCURACY
maximum accuracy limit
Definition: durand_kerner_roots.c:41
Structure to hold the matrix and dimensions.
Definition: sudoku_solver.c:33
double midpoint_euler(double dx, double x0, double x_max, double *y, char save_to_file)
Compute approximation using the midpoint-Euler method in the given limits.
Definition: ode_midpoint_euler.c:106
double kohonen_update_weights(const double *X, struct kohonen_array_3d *W, double **D, int num_out, int num_features, double alpha, int R)
Update weights of the SOM using Kohonen algorithm.
Definition: kohonen_som_topology.c:234
struct node node
Node, the basic data structure in the tree.
#define ACCURACY
solution accuracy
Definition: newton_raphson_root.c:16
void test_3d_classes(double *const *data, int N)
Creates a random set of points distributed in four clusters in 3D space with centroids at the points.
Definition: kohonen_som_trace.c:410
void test()
Test implementation.
Definition: cartesian_to_polar.c:98
uint32_t adler32(const char *s)
32-bit Adler algorithm implementation
Definition: hash_adler32.c:18
static void test()
Definition: k_means_clustering.c:321
int adaline_predict(struct adaline *ada, const double *x, double *out)
predict the output of the model for given set of features
Definition: adaline_learning.c:136
int main()
Driver Code.
Definition: palindrome.c:14
int main()
Main function.
Definition: hash_crc32.c:58
structure to hold adaline model parameters
Definition: adaline_learning.c:44
double get_rand(double lim1, double lim2)
Generate a random number in the given limits.
Definition: cartesian_to_polar.c:88
int height(node *root)
Utilitary procedure to measure the height of the binary tree.
Definition: binary_search_tree.c:187
int save_u_matrix(const char *fname, struct kohonen_array_3d *W)
Create the distance matrix or U-matrix from the trained weights and save to disk.
Definition: kohonen_som_topology.c:139
void semi_implicit_euler_step(double dx, double *x, double *y, double *dy)
Compute next step approximation using the semi-implicit-Euler method.
Definition: ode_semi_implicit_euler.c:85
struct node * right
right child
Definition: binary_search_tree.c:17
void test1()
Test that creates a random set of points distributed in four clusters in 2D space and trains an SOM t...
Definition: kohonen_som_topology.c:406
structure to define Cantor set
Definition: cantor_set.c:12
double y
ordinate of 2D data point
Definition: k_means_clustering.c:41
char * digits
array to store individual digits
Definition: factorial_large_number.c:16
char * adaline_get_weights_str(const struct adaline *ada)
Operator to print the weights of the model.
Definition: adaline_learning.c:112
double * kohonen_data_3d(const struct kohonen_array_3d *arr, int x, int y, int z)
Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by:
Definition: kohonen_som_topology.c:67
int main()
Main function.
Definition: sudoku_solver.c:246
int main()
Main function.
Definition: hash_sdbm.c:46
void kohonen_get_min_1d(double const *X, int N, double *val, int *idx)
Get minimum value and index of the value in a vector.
Definition: kohonen_som_trace.c:104
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations.
Definition: ode_midpoint_euler.c:54
struct adaline new_adaline(const int num_features, const double eta)
Default constructor.
Definition: adaline_learning.c:59
void print(CantorSet *head)
Print sets in the current range to stdout
Definition: cantor_set.c:55
#define order
number of dependent variables in problem
Definition: ode_forward_euler.c:44
int num_weights
number of weights of the neural network
Definition: adaline_learning.c:47
void add_digit(large_num *num, unsigned int value)
add a digit to the large number
Definition: factorial_large_number.c:48
double get_clock_diff(clock_t start_t, clock_t end_t)
Convert clock cycle difference to time in seconds.
Definition: kohonen_som_topology.c:663
void test_2d_classes(double *const *data, int N)
Creates a random set of points distributed in four clusters in 3D space with centroids at the points.
Definition: kohonen_som_topology.c:366