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[])
Definition: collatz.c:16
_large_num
Definition: factorial_large_number.c:15
test2
void test2()
Definition: kohonen_som_trace.c:349
L
Definition: list.h:8
get_weights_str
char * get_weights_str(struct adaline *ada)
Definition: adaline_learning.c:99
save_nd_data
int save_nd_data(const char *fname, double **X, int num_points, int num_features)
Definition: kohonen_som_trace.c:65
adaline::weights
double * weights
weights of the neural network
Definition: adaline_learning.c:39
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)
Definition: kohonen_som_trace.c:24
test3
void test3()
Definition: kohonen_som_topology.c:601
exact_solution
void exact_solution(const double *x, double *y)
Exact solution of the problem. Used for solution comparison.
Definition: ode_midpoint_euler.c:67
compare
int compare(const void *a, const void *b)
Definition: sol1.c:19
main
int main(int argc, char **argv)
Definition: lu_decompose.c:79
main
int main(int argc, char *argv[])
Definition: ode_semi_implicit_euler.c:147
min
#define min(a, b)
Definition: kohonen_som_topology.c:37
vector_mag
double vector_mag(double *vector, int L)
Definition: qr_decompose.h:64
delete_adaline
void delete_adaline(struct adaline *ada)
Definition: adaline_learning.c:81
test_circle
void test_circle(double *const *data, int N)
Definition: kohonen_som_trace.c:214
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)
Definition: kohonen_som_topology.c:231
array_3d::data
double * data
Definition: kohonen_som_topology.c:49
data
Definition: prime_factoriziation.c:25
pid
Definition: pid.c:31
d_func
double complex d_func(double complex x)
Definition: newton_raphson_root.c:32
main
int main(int argc, char **argv)
Definition: kohonen_som_topology.c:661
main
int main(int argc, char **argv)
Definition: adaline_learning.c:378
data_3d
double * data_3d(const struct array_3d *arr, int x, int y, int z)
Definition: kohonen_som_topology.c:64
kohonen_som_tracer
void kohonen_som_tracer(double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min)
Definition: kohonen_som_trace.c:175
test_3d_classes1
void test_3d_classes1(double *const *data, int N)
Definition: kohonen_som_topology.c:457
update_weights
void update_weights(double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)
Definition: kohonen_som_trace.c:124
BSTIterator
Definition: 173.c:13
poly_function
long double complex poly_function(double *coeffs, unsigned int degree, long double complex x)
Definition: durand_kerner_roots.c:50
node
Definition: binary_search_tree.c:14
vector_proj
double * vector_proj(double *a, double *b, double *out, int L)
Definition: qr_decompose.h:76
_large_num::num_digits
unsigned int num_digits
Definition: factorial_large_number.c:17
test3
void test3()
Definition: kohonen_som_trace.c:453
test_3d_classes2
void test_3d_classes2(double *const *data, int N)
Definition: kohonen_som_topology.c:556
N
#define N
Definition: sol1.c:109
get_min_1d
void get_min_1d(double const *X, int N, double *val, int *idx)
Definition: kohonen_som_trace.c:99
qr_decompose
void qr_decompose(double **A, double **Q, double **R, int M, int N)
Definition: qr_decompose.h:142
complex_str
const char * complex_str(long double complex x)
Definition: durand_kerner_roots.c:66
T
Definition: stack.c:16
main
int main(int argc, char *argv[])
Definition: fibonacci_fast.c:61
large_num
struct _large_num large_num
save_u_matrix
int save_u_matrix(const char *fname, struct array_3d *W)
Definition: kohonen_som_topology.c:136
new_number
large_num * new_number(void)
Definition: factorial_large_number.c:24
check_termination
char check_termination(long double delta)
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
main
int main(int argc, char *argv[])
Definition: factorial_large_number.c:94
multiply
void multiply(large_num *num, unsigned long n)
Definition: factorial_large_number.c:66
_random
double _random(double a, double b)
Definition: kohonen_som_trace.c:50
test_lamniscate
void test_lamniscate(double *const *data, int N)
Definition: kohonen_som_trace.c:310
lcm
unsigned long lcm(unsigned long a, unsigned long b)
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)
Definition: newton_raphson_root.c:37
main
int main(int argc, char **argv)
Definition: kohonen_som_trace.c:508
fib
void fib(unsigned long n, unsigned long *C, unsigned long *D)
Definition: fibonacci_fast.c:20
print_matrix
void print_matrix(double **A, int M, int N)
Definition: qr_decompose.h:22
vector_dot
double vector_dot(double *a, double *b, int L)
Definition: qr_decompose.h:43
adaline::eta
double eta
learning rate of the algorithm
Definition: adaline_learning.c:38
array_3d::dim3
int dim3
Definition: kohonen_som_topology.c:48
exact_solution
void exact_solution(const double *x, double *y)
Exact solution of the problem. Used for solution comparison.
Definition: ode_semi_implicit_euler.c:71
main
int main(int argc, char *argv[])
Definition: ode_midpoint_euler.c:144
array_3d::dim1
int dim1
Definition: kohonen_som_topology.c:46
test2
void test2()
Definition: kohonen_som_topology.c:498
problem
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
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)
Definition: kohonen_som_topology.c:201
problem
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition: ode_semi_implicit_euler.c:58
_random
double _random(double a, double b)
Definition: kohonen_som_topology.c:84
get_clock_diff
double get_clock_diff(clock_t start_t, clock_t end_t)
Definition: kohonen_som_trace.c:502
order
#define order
Definition: ode_semi_implicit_euler.c:47
MAX_LEN
#define MAX_LEN
Definition: sol1.c:15
node
struct Node node
search
void search(node *root, int ele)
Definition: threaded_binary_trees.c:98
gcd
unsigned long gcd(unsigned long a, unsigned long b)
Definition: sol.c:11
predict
int predict(struct adaline *ada, const double *x, double *out)
Definition: adaline_learning.c:123
delete_number
void delete_number(large_num *num)
Definition: factorial_large_number.c:37
lu_decomposition
int lu_decomposition(double **A, double **L, double **U, int mat_size)
Definition: lu_decompose.c:20
array_3d
Definition: kohonen_som_topology.c:45
new_adaline
struct adaline new_adaline(const int num_features, const double eta)
Definition: adaline_learning.c:51
vector_sub
double * vector_sub(double *a, double *b, double *out, int L)
Definition: qr_decompose.h:101
main
int main(int argc, char **argv)
Definition: durand_kerner_roots.c:95
test3
void test3(double eta)
Definition: adaline_learning.c:316
main
int main(int argc, char **argv)
Definition: c_atoi_str_to_integer.c:70
display
void display(double **A, int N)
Definition: lu_decompose.c:66
order
#define order
Definition: ode_midpoint_euler.c:43
exact_solution
void exact_solution(const double *x, double *y)
Exact solution of the problem. Used for solution comparison.
Definition: ode_forward_euler.c:68
test1
void test1()
Definition: kohonen_som_trace.c:252
min
#define min(a, b)
Definition: kohonen_som_trace.c:30
main
int main(int argc, char *argv[])
Definition: ode_forward_euler.c:137
fit_sample
double fit_sample(struct adaline *ada, const double *x, const int y)
Definition: adaline_learning.c:144
save_2d_data
int save_2d_data(const char *fname, double **X, int num_points, int num_features)
Definition: kohonen_som_topology.c:99
func
double complex func(double complex x)
Definition: newton_raphson_root.c:22
activation
int activation(double x)
Definition: adaline_learning.c:94
contour
Definition: cantor_set.c:6
test1
void test1(double eta)
Definition: adaline_learning.c:205
test2
void test2(double eta)
Definition: adaline_learning.c:253
ACCURACY
#define ACCURACY
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
Definition: newton_raphson_root.c:16
test_3d_classes
void test_3d_classes(double *const *data, int N)
Definition: kohonen_som_trace.c:401
swap
void swap(int *a, int *b)
Definition: shell_sort2.c:21
max
#define max(a, b)
Definition: kohonen_som_topology.c:31
fit
void fit(struct adaline *ada, double **X, const int *y, const int N)
Definition: adaline_learning.c:170
adaline
Definition: adaline_learning.c:37
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:43
test1
void test1()
Definition: kohonen_som_topology.c:398
array_3d::dim2
int dim2
Definition: kohonen_som_topology.c:47
_large_num::digits
char * 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)
Definition: kohonen_som_topology.c:311
problem
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition: ode_midpoint_euler.c:54
order
#define order
Definition: ode_forward_euler.c:44
adaline::num_weights
int num_weights
number of weights of the neural network
Definition: adaline_learning.c:40
add_digit
void add_digit(large_num *num, unsigned int value)
Definition: factorial_large_number.c:48
get_clock_diff
double get_clock_diff(clock_t start_t, clock_t end_t)
Definition: kohonen_som_topology.c:655
test_2d_classes
void test_2d_classes(double *const *data, int N)
Definition: kohonen_som_topology.c:358