Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
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 */
22void 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 */
43double 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 */
64double 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 */
76double *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 */
101double *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 */
142void 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
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
double * vector_proj(double *a, double *b, double *out, int L)
Compute projection of vector on defined as.
Definition qr_decompose.h:76
double vector_dot(double *a, double *b, int L)
Compute dot product of two vectors of equal lengths.
Definition qr_decompose.h:43
void qr_decompose(double **A, double **Q, double **R, int M, int N)
Decompose matrix using Gram-Schmidt process.
Definition qr_decompose.h:142
double * vector_sub(double *a, double *b, double *out, int L)
Compute vector subtraction.
Definition qr_decompose.h:101
void print_matrix(double **A, int M, int N)
function to display matrix on stdout
Definition qr_decompose.h:22
double vector_mag(double *vector, int L)
Compute magnitude of vector.
Definition qr_decompose.h:64
Definition list.h:8