Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
qr_decompose.h File Reference

Library functions to compute QR decomposition of a given matrix. More...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for qr_decompose.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void print_matrix (double **A, int M, int N)
 function to display matrix on stdout More...
 
double vector_dot (double *a, double *b, int L)
 Compute dot product of two vectors of equal lengths. More...
 
double vector_mag (double *vector, int L)
 Compute magnitude of vector. More...
 
double * vector_proj (double *a, double *b, double *out, int L)
 Compute projection of vector \(\vec{a}\) on \(\vec{b}\) defined as. More...
 
double * vector_sub (double *a, double *b, double *out, int L)
 Compute vector subtraction. More...
 
void qr_decompose (double **A, double **Q, double **R, int M, int N)
 Decompose matrix \(A\) using Gram-Schmidt process. More...
 

Detailed Description

Library functions to compute QR decomposition of a given matrix.

Author
Krishna Vedala

Function Documentation

◆ print_matrix()

void print_matrix ( double **  A,
int  M,
int  N 
)

function to display matrix on stdout

Parameters
Amatrix to print
Mnumber of rows of matrix
Nnumber 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 }

◆ qr_decompose()

void qr_decompose ( double **  A,
double **  Q,
double **  R,
int  M,
int  N 
)

Decompose matrix \(A\) using Gram-Schmidt process.

\begin{eqnarray*} \text{given that}\quad A &=& *\left[\mathbf{a}_1,\mathbf{a}_2,\ldots,\mathbf{a}_{N-1},\right]\\ \text{where}\quad\mathbf{a}_i &=& *\left[a_{0i},a_{1i},a_{2i},\ldots,a_{(M-1)i}\right]^T\quad\ldots\mbox{(column *vectors)}\\ \text{then}\quad\mathbf{u}_i &=& \mathbf{a}_i *-\sum_{j=0}^{i-1}\text{proj}_{\mathbf{u}_j}\mathbf{a}_i\\ \mathbf{e}_i &=&\frac{\mathbf{u}_i}{\left|\mathbf{u}_i\right|}\\ Q &=& \begin{bmatrix}\mathbf{e}_0 & \mathbf{e}_1 & \mathbf{e}_2 & \dots & *\mathbf{e}_{N-1}\end{bmatrix}\\ R &=& \begin{bmatrix}\langle\mathbf{e}_0\,,\mathbf{a}_0\rangle & *\langle\mathbf{e}_1\,,\mathbf{a}_1\rangle & *\langle\mathbf{e}_2\,,\mathbf{a}_2\rangle & \dots \\ 0 & \langle\mathbf{e}_1\,,\mathbf{a}_1\rangle & *\langle\mathbf{e}_2\,,\mathbf{a}_2\rangle & \dots\\ 0 & 0 & \langle\mathbf{e}_2\,,\mathbf{a}_2\rangle & \dots\\ \vdots & \vdots & \vdots & \ddots \end{bmatrix}\\ \end{eqnarray*}

Parameters
Ainput matrix to decompose
Qoutput decomposed matrix
Routput decomposed matrix
Mnumber of rows of matrix A
Nnumber of columns of matrix A
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 }
Here is the call graph for this function:

◆ vector_dot()

double vector_dot ( double *  a,
double *  b,
int  L 
)

Compute dot product of two vectors of equal lengths.

If \(\vec{a}=\left[a_0,a_1,a_2,...,a_L\right]\) and \(\vec{b}=\left[b_0,b_1,b_1,...,b_L\right]\) then \(\vec{a}\cdot\vec{b}=\displaystyle\sum_{i=0}^L a_i\times b_i\)

Returns
\(\vec{a}\cdot\vec{b}\)
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 }

◆ vector_mag()

double vector_mag ( double *  vector,
int  L 
)

Compute magnitude of vector.

If \(\vec{a}=\left[a_0,a_1,a_2,...,a_L\right]\) then \(\left|\vec{a}\right|=\sqrt{\displaystyle\sum_{i=0}^L a_i^2}\)

Returns
\(\left|\vec{a}\right|\)
65 {
66  double dot = vector_dot(vector, vector, L);
67  return sqrt(dot);
68 }
Here is the call graph for this function:

◆ vector_proj()

double* vector_proj ( double *  a,
double *  b,
double *  out,
int  L 
)

Compute projection of vector \(\vec{a}\) on \(\vec{b}\) defined as.

\[\text{proj}_\vec{b}\vec{a}=\frac{\vec{a}\cdot\vec{b}}{\left|\vec{b}\right|^2}\vec{b}\]

Returns
NULL if error, otherwise pointer to output

check for division by zero

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 }
Here is the call graph for this function:

◆ vector_sub()

double* vector_sub ( double *  a,
double *  b,
double *  out,
int  L 
)

Compute vector subtraction.

\(\vec{c}=\vec{a}-\vec{b}\)

Returns
pointer to output vector
Parameters
aminuend
bsubtrahend
outresultant vector
Llength of vectors
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 }
L
Definition: list.h:8
vector_mag
double vector_mag(double *vector, int L)
Compute magnitude of vector.
Definition: qr_decompose.h:64
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
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
vector_sub
double * vector_sub(double *a, double *b, double *out, int L)
Compute vector subtraction.
Definition: qr_decompose.h:101