qr_algorithm Namespace Reference

Functions to compute QR decomposition of any rectangular matrix. More...

## Functions

template<typename T >
std::ostream & | operator<< (std::ostream &out, std::valarray< std::valarray< T >> const &v)
template<typename T >
std::ostream & | operator<< (std::ostream &out, std::valarray< T > const &v)
template<typename T >
double | vector_dot (const std::valarray< T > &a, const std::valarray< T > &b)
template<typename T >
double | vector_mag (const std::valarray< T > &a)
template<typename T >
std::valarray< T > | vector_proj (const std::valarray< T > &a, const std::valarray< T > &b)
template<typename T >
void | qr_decompose (const std::valarray< std::valarray< T >> &A, std::valarray< std::valarray< T >> *Q, std::valarray< std::valarray< T >> *R)
std::valarray< double > | eigen_values (std::valarray< std::valarray< double >> *A, bool print_intermediates=false)

## Detailed Description

Functions to compute QR decomposition of any rectangular matrix.

## Function Documentation

## eigen_values()

std::valarray<double> qr_algorithm::eigen_values | ( | std::valarray< std::valarray< double >> * | A,
|  | bool | print_intermediates = `false`
| ) |  |

Compute eigen values using iterative shifted QR decomposition algorithm as follows:

1. Use last diagonal element of A as eigen value approximation \(c\)
2. Shift diagonals of matrix \(A' = A - cI\)
3. Decompose matrix \(A'=QR\)
4. Compute next approximation \(A'_1 = RQ \)
5. Shift diagonals back \(A_1 = A'_1 + cI\)
6. Termination condition check: last element below diagonal is almost 0
   1. If not 0, go back to step 1 with the new approximation \(A_1\)
   2. If 0, continue to step 7
7. Save last known \(c\) as the eigen value.
8. Are all eigen values found?
   1. If not, remove last row and column of \(A_1\) and go back to step 1.
   2. If yes, stop.

Note
  The matrix \(A\) gets modified

Parameters

  [in,out] | A | matrix to compute eigen values for
  [in] | print_intermediates | (optional) whether to print intermediate A, Q and R matrices (default = `false`)

{
int rows = A->size();
int columns = rows;
int counter = 0, num_eigs = rows - 1;
double last_eig = 0;
std::valarray<std::valarray<double>> Q(rows);
std::valarray<std::valarray<double>> R(columns);
/* number of eigen values = matrix size */
std::valarray<double> eigen_vals(rows);
for (int i = 0; i < rows; i++) {
Q[i] = std::valarray<double>(columns);
R[i] = std::valarray<double>(columns);
}
/* continue till all eigen values are found */
while (num_eigs > 0) {
/* iterate with QR decomposition */
while (std::abs(A[0][num_eigs][num_eigs - 1]) >
std::numeric_limits<double>::epsilon()) {
// initial approximation = last diagonal element
last_eig = A[0][num_eigs][num_eigs];
for (int i = 0; i < rows; i++) {
A[0][i][i] -= last_eig; /* A - cI */
}
qr_decompose(*A, &Q, &R);
if (print_intermediates) {
std::cout << *A << "\n";
std::cout << Q << "\n";
std::cout << R << "\n";
printf("-------------------- %d ---------------------\n",
++counter);
}
// new approximation A' = R * Q
mat_mul(R, Q, A);
for (int i = 0; i < rows; i++) {
A[0][i][i] += last_eig; /* A + cI */
}
}
/* store the converged eigen value */
eigen_vals[num_eigs] = last_eig;
// A[0][num_eigs][num_eigs];
if (print_intermediates) {
std::cout << "========================\n";
std::cout << "Eigen value: " << last_eig << ",\n";
std::cout << "========================\n";
}
num_eigs--;
rows--;
columns--;
}
eigen_vals[0] = A[0][0][0];
if (print_intermediates) {
std::cout << Q << "\n";
std::cout << R << "\n";
}
return eigen_vals;
}
std::cout
std::printf
T printf(T... args)
qr_algorithm::qr_decompose
void qr_decompose(const std::valarray< std::valarray< T >> &A, std::valarray< std::valarray< T >> *Q, std::valarray< std::valarray< T >> *R)
Definition: qr_decompose.h:146
std::numeric_limits
mat_mul
void mat_mul(const std::valarray< std::valarray< double >> &A, const std::valarray< std::valarray< double >> &B, std::valarray< std::valarray< double >> *OUT)
Definition: qr_eigen_values.cpp:54
std::valarray
Here is the call graph for this function:

## operator<<() [1/2]

template<typename T >

std::ostream& qr_algorithm::operator<< | ( | std::ostream & | out,
|  | std::valarray< std::valarray< T >> const & | v
| ) |  |

operator to print a matrix

{
const int width = 12;
const char separator = ' ';
out.precision(4);
for (size_t row = 0; row < v.size(); row++) {
for (size_t col = 0; col < v[row].size(); col++)
out << std::right << std::setw(width) << std::setfill(separator)
<< v[row][col];
out << std::endl;
}
return out;
}
std::endl
T endl(T... args)
std::right
T right(T... args)
std::ostream::precision
T precision(T... args)
std::setfill
T setfill(T... args)
std::setw
T setw(T... args)
Here is the call graph for this function:

## operator<<() [2/2]

template<typename T >

std::ostream& qr_algorithm::operator<< | ( | std::ostream & | out,
|  | std::valarray< T > const & | v
| ) |  |

operator to print a vector

{
const int width = 10;
const char separator = ' ';
out.precision(4);
for (size_t row = 0; row < v.size(); row++) {
out << std::right << std::setw(width) << std::setfill(separator)
<< v[row];
}
return out;
}
Here is the call graph for this function:

## qr_decompose()

template<typename T >

void qr_algorithm::qr_decompose | ( | const std::valarray< std::valarray< T >> & | A,
|  | std::valarray< std::valarray< T >> * | Q,
|  | std::valarray< std::valarray< T >> * | R
| ) |  |

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

  A | input matrix to decompose
  Q | output decomposed matrix
  R | output decomposed matrix

{
std::size_t ROWS = A.size(); // number of rows of A
std::size_t COLUMNS = A[0].size(); // number of columns of A
std::valarray<T> col_vector(ROWS);
std::valarray<T> col_vector2(ROWS);
std::valarray<T> tmp_vector(ROWS);
for (int i = 0; i < COLUMNS; i++) {
/* for each column => R is a square matrix of NxN */
int j;
R[0][i] = 0.; /* make R upper triangular */
/* get corresponding Q vector */
#ifdef _OPENMP
// parallelize on threads
#pragma omp for
#endif
for (j = 0; j < ROWS; j++) {
tmp_vector[j] = A[j][i]; /* accumulator for uk */
col_vector[j] = A[j][i];
}
for (j = 0; j < i; j++) {
for (int k = 0; k < ROWS; k++) {
col_vector2[k] = Q[0][k][j];
}
col_vector2 = vector_proj(col_vector, col_vector2);
tmp_vector -= col_vector2;
}
double mag = vector_mag(tmp_vector);
#ifdef _OPENMP
// parallelize on threads
#pragma omp for
#endif
for (j = 0; j < ROWS; j++) Q[0][j][i] = tmp_vector[j] / mag;
/* compute upper triangular values of R */
#ifdef _OPENMP
// parallelize on threads
#pragma omp for
#endif
for (int kk = 0; kk < ROWS; kk++) {
col_vector[kk] = Q[0][kk][i];
}
#ifdef _OPENMP
// parallelize on threads
#pragma omp for
#endif
for (int k = i; k < COLUMNS; k++) {
for (int kk = 0; kk < ROWS; kk++) {
col_vector2[kk] = A[kk][k];
}
R[0][i][k] = (col_vector * col_vector2).sum();
}
}
}
machine_learning::sum
T sum(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:232
qr_algorithm::vector_proj
std::valarray< T > vector_proj(const std::valarray< T > &a, const std::valarray< T > &b)
Definition: qr_decompose.h:104
qr_algorithm::vector_mag
double vector_mag(const std::valarray< T > &a)
Definition: qr_decompose.h:92
std::size_t
math::vector_cross::mag
double mag(const std::array< double, 3 > &vec)
Calculates the magnitude of the mathematical vector from it's direction ratios.
Definition: vector_cross_product.cpp:83
Here is the call graph for this function:

## vector_dot()

template<typename T >

double qr_algorithm::vector_dot | ( | const std::valarray< T > & | a,
|  | const std::valarray< T > & | b
| ) |  |

inline

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}\)

{
return (a * b).sum();
// could also use following
// return std::inner_product(std::begin(a), std::end(a), std::begin(b),
// 0.f);
}

## vector_mag()

template<typename T >

double qr_algorithm::vector_mag | ( | const std::valarray< T > & | a | ) |

inline

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|\)

{
double dot = vector_dot(a, a);
return std::sqrt(dot);
}
qr_algorithm::vector_dot
double vector_dot(const std::valarray< T > &a, const std::valarray< T > &b)
Definition: qr_decompose.h:76
std::sqrt
T sqrt(T... args)
Here is the call graph for this function:

## vector_proj()

template<typename T >

std::valarray<T> qr_algorithm::vector_proj | ( | const std::valarray< T > & | a,
|  | const std::valarray< T > & | b
| ) |  |

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 using machine epsilon

{
double num = vector_dot(a, b);
double deno = vector_dot(b, b);
/*! check for division by zero using machine epsilon */
if (deno <= std::numeric_limits<double>::epsilon()) {
std::cerr << "[" << __func__ << "] Possible division by zero\n";
return a; // return vector a back
}
double scalar = num / deno;
return b * scalar;
}
Here is the call graph for this function: