Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
qr_decompose.h
Go to the documentation of this file.
1 /**
2  * @file
3  * \brief Library functions to compute [QR
4  * decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given
5  * matrix.
6  * \author [Krishna Vedala](https://github.com/kvedala)
7  */
8 
9 #ifndef QR_DECOMPOSE_H
10 #define QR_DECOMPOSE_H
11 
12 #include <math.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #ifdef _OPENMP
16 #include <omp.h>
17 #endif
18 
19 /**
20  * function to display matrix on stdout
21  */
22 void print_matrix(double **A, /**< matrix to print */
23  int M, /**< number of rows of matrix */
24  int N) /**< number of columns of matrix */
25 {
26  for (int row = 0; row < M; row++)
27  {
28  for (int col = 0; col < N; col++) printf("% 9.3g\t", A[row][col]);
29  putchar('\n');
30  }
31  putchar('\n');
32 }
33 
34 /**
35  * Compute dot product of two vectors of equal lengths
36  *
37  * If \f$\vec{a}=\left[a_0,a_1,a_2,...,a_L\right]\f$ and
38  * \f$\vec{b}=\left[b_0,b_1,b_1,...,b_L\right]\f$ then
39  * \f$\vec{a}\cdot\vec{b}=\displaystyle\sum_{i=0}^L a_i\times b_i\f$
40  *
41  * \returns \f$\vec{a}\cdot\vec{b}\f$
42  */
43 double vector_dot(double *a, double *b, int L)
44 {
45  double mag = 0.f;
46  int i;
47 #ifdef _OPENMP
48 // parallelize on threads
49 #pragma omp parallel for reduction(+ : mag)
50 #endif
51  for (i = 0; i < L; i++) mag += a[i] * b[i];
52 
53  return mag;
54 }
55 
56 /**
57  * Compute magnitude of vector.
58  *
59  * If \f$\vec{a}=\left[a_0,a_1,a_2,...,a_L\right]\f$ then
60  * \f$\left|\vec{a}\right|=\sqrt{\displaystyle\sum_{i=0}^L a_i^2}\f$
61  *
62  * \returns \f$\left|\vec{a}\right|\f$
63  */
64 double vector_mag(double *vector, int L)
65 {
66  double dot = vector_dot(vector, vector, L);
67  return sqrt(dot);
68 }
69 
70 /**
71  * Compute projection of vector \f$\vec{a}\f$ on \f$\vec{b}\f$ defined as
72  * \f[\text{proj}_\vec{b}\vec{a}=\frac{\vec{a}\cdot\vec{b}}{\left|\vec{b}\right|^2}\vec{b}\f]
73  *
74  * \returns NULL if error, otherwise pointer to output
75  */
76 double *vector_proj(double *a, double *b, double *out, int L)
77 {
78  const double num = vector_dot(a, b, L);
79  const double deno = vector_dot(b, b, L);
80  if (deno == 0) /*! check for division by zero */
81  return NULL;
82 
83  const double scalar = num / deno;
84  int i;
85 #ifdef _OPENMP
86 // parallelize on threads
87 #pragma omp for
88 #endif
89  for (i = 0; i < L; i++) out[i] = scalar * b[i];
90 
91  return out;
92 }
93 
94 /**
95  * Compute vector subtraction
96  *
97  * \f$\vec{c}=\vec{a}-\vec{b}\f$
98  *
99  * \returns pointer to output vector
100  */
101 double *vector_sub(double *a, /**< minuend */
102  double *b, /**< subtrahend */
103  double *out, /**< resultant vector */
104  int L /**< length of vectors */
105 )
106 {
107  int i;
108 #ifdef _OPENMP
109 // parallelize on threads
110 #pragma omp for
111 #endif
112  for (i = 0; i < L; i++) out[i] = a[i] - b[i];
113 
114  return out;
115 }
116 
117 /**
118  * Decompose matrix \f$A\f$ using [Gram-Schmidt
119  *process](https://en.wikipedia.org/wiki/QR_decomposition).
120  *
121  * \f{eqnarray*}{
122  * \text{given that}\quad A &=&
123  *\left[\mathbf{a}_1,\mathbf{a}_2,\ldots,\mathbf{a}_{N-1},\right]\\
124  * \text{where}\quad\mathbf{a}_i &=&
125  *\left[a_{0i},a_{1i},a_{2i},\ldots,a_{(M-1)i}\right]^T\quad\ldots\mbox{(column
126  *vectors)}\\
127  * \text{then}\quad\mathbf{u}_i &=& \mathbf{a}_i
128  *-\sum_{j=0}^{i-1}\text{proj}_{\mathbf{u}_j}\mathbf{a}_i\\
129  * \mathbf{e}_i &=&\frac{\mathbf{u}_i}{\left|\mathbf{u}_i\right|}\\
130  * Q &=& \begin{bmatrix}\mathbf{e}_0 & \mathbf{e}_1 & \mathbf{e}_2 & \dots &
131  *\mathbf{e}_{N-1}\end{bmatrix}\\
132  * R &=& \begin{bmatrix}\langle\mathbf{e}_0\,,\mathbf{a}_0\rangle &
133  *\langle\mathbf{e}_1\,,\mathbf{a}_1\rangle &
134  *\langle\mathbf{e}_2\,,\mathbf{a}_2\rangle & \dots \\
135  * 0 & \langle\mathbf{e}_1\,,\mathbf{a}_1\rangle &
136  *\langle\mathbf{e}_2\,,\mathbf{a}_2\rangle & \dots\\
137  * 0 & 0 & \langle\mathbf{e}_2\,,\mathbf{a}_2\rangle & \dots\\
138  * \vdots & \vdots & \vdots & \ddots
139  * \end{bmatrix}\\
140  * \f}
141  */
142 void qr_decompose(double **A, /**< input matrix to decompose */
143  double **Q, /**< output decomposed matrix */
144  double **R, /**< output decomposed matrix */
145  int M, /**< number of rows of matrix A */
146  int N /**< number of columns of matrix A */
147 )
148 {
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;
153  i++) /* for each column => R is a square matrix of NxN */
154  {
155  int j;
156 #ifdef _OPENMP
157 // parallelize on threads
158 #pragma omp for
159 #endif
160  for (j = 0; j < i; j++) /* second dimension of column */
161  R[i][j] = 0.; /* make R upper triangular */
162 
163  /* get corresponding Q vector */
164 #ifdef _OPENMP
165 // parallelize on threads
166 #pragma omp for
167 #endif
168  for (j = 0; j < M; j++)
169  {
170  tmp_vector[j] = A[j][i]; /* accumulator for uk */
171  col_vector[j] = A[j][i];
172  }
173  for (j = 0; j < i; j++)
174  {
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);
178  }
179  double mag = vector_mag(tmp_vector, M);
180 
181 #ifdef _OPENMP
182 // parallelize on threads
183 #pragma omp for
184 #endif
185  for (j = 0; j < M; j++) Q[j][i] = tmp_vector[j] / mag;
186 
187  /* compute upper triangular values of R */
188  for (int kk = 0; kk < M; kk++) col_vector[kk] = Q[kk][i];
189  for (int k = i; k < N; k++)
190  {
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);
193  }
194  }
195 
196  free(col_vector);
197  free(col_vector2);
198  free(tmp_vector);
199 }
200 
201 #endif // QR_DECOMPOSE_H
main
int main(int argc, char *argv[])
Main function.
Definition: collatz.c:16
_large_num
dynamically large number
Definition: factorial_large_number.c:15
test2
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:347
MAX_ITER
#define MAX_ITER
Maximum number of iterations to learn.
Definition: adaline_learning.c:34
L
Definition: list.h:8
get_weights_str
char * get_weights_str(struct adaline *ada)
Operator to print the weights of the model.
Definition: adaline_learning.c:100
save_nd_data
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:64
adaline::weights
double * weights
weights of the neural network
Definition: adaline_learning.c:40
forward_euler_step
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
max
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_trace.c:26
test3
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:596
exact_solution
void exact_solution(const double *x, double *y)
Exact solution of the problem.
Definition: ode_midpoint_euler.c:67
_cantor_set::next
struct _cantor_set * next
pointer to next set
Definition: cantor_set.c:15
main
int main(int argc, char const *argv[])
Main function.
Definition: cantor_set.c:84
compare
int compare(const void *a, const void *b)
comparison function for use with internal qsort algorithm
Definition: sol1.c:19
main
int main(int argc, char **argv)
Main function.
Definition: lu_decompose.c:79
main
int main(int argc, char *argv[])
Main Function.
Definition: ode_semi_implicit_euler.c:147
min
#define min(a, b)
shorthand for minimum value
Definition: kohonen_som_topology.c:36
propagate
void propagate(CantorSet *head)
Iterative constructor of all sets in the current level.
Definition: cantor_set.c:23
vector_mag
double vector_mag(double *vector, int L)
Compute magnitude of vector.
Definition: qr_decompose.h:64
delete_adaline
void delete_adaline(struct adaline *ada)
delete dynamically allocated memory
Definition: adaline_learning.c:82
test_circle
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:212
forward_euler
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
update_weights
double update_weights(const double *X, struct 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:227
array_3d::data
double * data
pointer to data
Definition: kohonen_som_topology.c:45
data
Definition: prime_factoriziation.c:25
pid
PID Controller.
Definition: pid.c:31
d_func
double complex d_func(double complex x)
Return first order derivative of the function.
Definition: newton_raphson_root.c:32
CantorSet
struct _cantor_set CantorSet
structure to define Cantor set
main
int main(int argc, char **argv)
Main function.
Definition: kohonen_som_topology.c:656
main
int main(int argc, char **argv)
Main function.
Definition: adaline_learning.c:379
data_3d
double * data_3d(const struct 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:60
kohonen_som_tracer
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:173
test_3d_classes1
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:452
update_weights
void 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:123
BSTIterator
Definition for a binary tree node.
Definition: 173.c:13
node
Kyler Smith, 2017 Stack data structure implementation.
Definition: binary_search_tree.c:14
vector_proj
double * vector_proj(double *a, double *b, double *out, int L)
Compute projection of vector on defined as.
Definition: qr_decompose.h:76
free_memory
void free_memory(CantorSet *head)
Clear memory allocated by propagate function.
Definition: cantor_set.c:72
_large_num::num_digits
unsigned int num_digits
number of digits in the number
Definition: factorial_large_number.c:17
test3
void test3()
Test that creates a random set of points distributed in six clusters in 3D space.
Definition: kohonen_som_trace.c:451
test_3d_classes2
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:551
N
#define N
number of digits of the large number
Definition: sol1.c:109
get_min_1d
void 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:98
qr_decompose
void qr_decompose(double **A, double **Q, double **R, int M, int N)
Decompose matrix using Gram-Schmidt process.
Definition: qr_decompose.h:142
complex_str
const char * complex_str(long double complex x)
create a textual form of complex number
Definition: durand_kerner_roots.c:66
T
Definition: stack.c:16
main
int main(int argc, char *argv[])
main function
Definition: fibonacci_fast.c:61
large_num
struct _large_num large_num
dynamically large number
save_u_matrix
int save_u_matrix(const char *fname, struct array_3d *W)
Create the distance matrix or U-matrix from the trained weights and save to disk.
Definition: kohonen_som_topology.c:132
new_number
large_num * new_number(void)
create a new large number
Definition: factorial_large_number.c:24
check_termination
char check_termination(long double delta)
check for termination condition
Definition: durand_kerner_roots.c:83
semi_implicit_euler
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
poly_function
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
main
int main(int argc, char *argv[])
main function
Definition: factorial_large_number.c:94
multiply
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
_random
double _random(double a, double b)
Helper function to generate a random number in a given interval.
Definition: kohonen_som_trace.c:48
test_lamniscate
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:308
lcm
unsigned long lcm(unsigned long a, unsigned long b)
Compute Least Common Multiple (LCM) of two numbers.
Definition: sol.c:31
midpoint_euler_step
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
main
int main(int argc, char **argv)
main function
Definition: newton_raphson_root.c:37
main
int main(int argc, char **argv)
Main function.
Definition: kohonen_som_trace.c:506
fib
void fib(unsigned long n, unsigned long *C, unsigned long *D)
Returns the and Fibonacci number.
Definition: fibonacci_fast.c:20
_cantor_set::start
double start
start of interval
Definition: cantor_set.c:13
_cantor_set::end
double end
end of interval
Definition: cantor_set.c:14
print_matrix
void print_matrix(double **A, int M, int N)
function to display matrix on stdout
Definition: qr_decompose.h:22
vector_dot
double vector_dot(double *a, double *b, int L)
Compute dot product of two vectors of equal lengths.
Definition: qr_decompose.h:43
adaline::eta
double eta
learning rate of the algorithm
Definition: adaline_learning.c:39
array_3d::dim3
int dim3
lengths of thirddimension
Definition: kohonen_som_topology.c:44
exact_solution
void exact_solution(const double *x, double *y)
Exact solution of the problem.
Definition: ode_semi_implicit_euler.c:71
main
int main(int argc, char *argv[])
Main Function.
Definition: ode_midpoint_euler.c:144
array_3d::dim1
int dim1
lengths of first dimension
Definition: kohonen_som_topology.c:42
test2
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:493
problem
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
get_min_2d
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:197
problem
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
_random
double _random(double a, double b)
Helper function to generate a random number in a given interval.
Definition: kohonen_som_topology.c:80
get_clock_diff
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:500
order
#define order
number of dependent variables in problem
Definition: ode_semi_implicit_euler.c:47
MAX_LEN
#define MAX_LEN
length of resulting recurring fraction number
Definition: sol1.c:15
node
struct Node node
Node, the basic data structure of the tree.
search
void search(node *root, int ele)
searches for the element
Definition: threaded_binary_trees.c:98
gcd
unsigned long gcd(unsigned long a, unsigned long b)
Compute Greatest Common Divisor (GCD) of two numbers using Euclids algorithm.
Definition: sol.c:11
predict
int predict(struct adaline *ada, const double *x, double *out)
predict the output of the model for given set of features
Definition: adaline_learning.c:124
delete_number
void delete_number(large_num *num)
delete all memory allocated for large number
Definition: factorial_large_number.c:37
lu_decomposition
int lu_decomposition(double **A, double **L, double **U, int mat_size)
Perform LU decomposition on matrix.
Definition: lu_decompose.c:20
array_3d
to store info regarding 3D arrays
Definition: kohonen_som_topology.c:41
new_adaline
struct adaline new_adaline(const int num_features, const double eta)
Default constructor.
Definition: adaline_learning.c:52
vector_sub
double * vector_sub(double *a, double *b, double *out, int L)
Compute vector subtraction.
Definition: qr_decompose.h:101
main
int main(int argc, char **argv)
Definition: durand_kerner_roots.c:95
test3
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:317
main
int main(int argc, char **argv)
the main function take one argument of type char* example : .
Definition: c_atoi_str_to_integer.c:72
display
void display(double **A, int N)
Function to display square matrix.
Definition: lu_decompose.c:66
order
#define order
number of dependent variables in problem
Definition: ode_midpoint_euler.c:43
exact_solution
void exact_solution(const double *x, double *y)
Exact solution of the problem.
Definition: ode_forward_euler.c:68
test1
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:250
min
#define min(a, b)
shorthand for minimum value
Definition: kohonen_som_trace.c:30
main
int main(int argc, char *argv[])
Main Function.
Definition: ode_forward_euler.c:137
fit_sample
double 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:145
save_2d_data
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:95
func
double complex func(double complex x)
Return value of the function to find the root for.
Definition: newton_raphson_root.c:22
activation
int activation(double x)
Heaviside activation function
Definition: adaline_learning.c:95
test
void test()
Test implementations.
Definition: binary_search.c:75
test1
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:206
test2
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:254
ACCURACY
#define ACCURACY
maximum accuracy limit
Definition: durand_kerner_roots.c:41
midpoint_euler
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
ACCURACY
#define ACCURACY
solution accuracy
Definition: newton_raphson_root.c:16
test_3d_classes
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:399
swap
void swap(int *a, int *b)
Function to swap values of two integers.
Definition: shell_sort2.c:19
max
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_topology.c:32
fit
void 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:171
adaline
structure to hold adaline model parameters
Definition: adaline_learning.c:38
semi_implicit_euler_step
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
ACCURACY
#define ACCURACY
convergence accuracy
Definition: adaline_learning.c:44
test1
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:393
array_3d::dim2
int dim2
lengths of second dimension
Definition: kohonen_som_topology.c:43
_cantor_set
structure to define Cantor set
Definition: cantor_set.c:12
_large_num::digits
char * digits
array to store individual digits
Definition: factorial_large_number.c:16
kohonen_som
void kohonen_som(double **X, struct 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:306
problem
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
print
void print(CantorSet *head)
Print sets in the current range to stdout
Definition: cantor_set.c:55
order
#define order
number of dependent variables in problem
Definition: ode_forward_euler.c:44
adaline::num_weights
int num_weights
number of weights of the neural network
Definition: adaline_learning.c:41
add_digit
void add_digit(large_num *num, unsigned int value)
add a digit to the large number
Definition: factorial_large_number.c:48
get_clock_diff
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:650
test_2d_classes
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:353