This tutorial shows how the QR factorization of matrices from ViennaCL or Boost.uBLAS can be computed.
#define VIENNACL_WITH_UBLAS
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
  A helper function comparing two matrices and returning the maximum entry-wise relative error encountered. 
template<typename MatrixType>
double check(MatrixType 
const & qr, MatrixType 
const & ref)
 
{
  bool do_break = false;
  double max_error = 0;
  for (std::size_t i=0; i<ref.size1(); ++i)
  {
    for (std::size_t j=0; j<ref.size2(); ++j)
    {
      if (qr(i,j) != 0.0 && ref(i,j) != 0.0)
      {
        double rel_err = fabs(qr(i,j) - ref(i,j)) / fabs(ref(i,j) );
        if (rel_err > max_error)
          max_error = rel_err;
      }
      
    }
    if (do_break)
      break;
  }
  return max_error;
}
 We set up a random matrix using Boost.uBLAS and use it to initialize a ViennaCL matrix. Then we compute the QR factorization directly for the uBLAS matrix as well as the ViennaCL matrix. 
int main (
int, 
const char **)
 
{
  typedef boost::numeric::ublas::matrix<ScalarType>              MatrixType;
  std::size_t rows = 113;   
  std::size_t cols = 54;    
 Create uBLAS matrices with some random input data. 
MatrixType ublas_A(rows, cols);
MatrixType Q(rows, rows);
MatrixType R(rows, cols);
for (std::size_t i=0; i<rows; ++i)
{
  for (std::size_t j=0; j<cols; ++j)
  {
    if (i == j)
    R(i,j) = 0.0;
  }
  for (std::size_t j=0; j<rows; ++j)
}
MatrixType ublas_A_backup(ublas_A);
 Setup the matrix in ViennaCL and copy the data from the uBLAS matrix: 
VCLMatrixType vcl_A(ublas_A.size1(), ublas_A.size2());
QR Factorization with Boost.uBLAS Matrices
Compute QR factorization of A. A is overwritten with Householder vectors. Coefficients are returned and a block size of 3 is used. Note that at the moment the number of columns of A must be divisible by the block size 
std::cout << "--- Boost.uBLAS ---" << std::endl;
 Let us check for the correct result: 
MatrixType ublas_QR = 
prod(Q, R);
double ublas_error = 
check(ublas_QR, ublas_A_backup);
 
std::cout << "Maximum relative error (ublas): " << ublas_error << std::endl;
  QR Factorization with Boost.uBLAS Matrices
We now compute the QR factorization from a ViennaCL matrix. Internally it uses Boost.uBLAS for the panel factorization. 
std::cout << "--- Hybrid (default) ---" << std::endl;
 Let us check for the correct result: 
Q.clear(); R.clear();
double hybrid_error = 
check(ublas_QR, ublas_A_backup);
 
std::cout << "Maximum relative error (hybrid): " << hybrid_error << std::endl;
  That's it. Print a success message and exit. 
  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
  return EXIT_SUCCESS;
}
Full Example Code
#define VIENNACL_WITH_UBLAS
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
template<typename MatrixType>
double check(MatrixType 
const & qr, MatrixType 
const & ref)
 
{
  bool do_break = false;
  double max_error = 0;
  for (std::size_t i=0; i<ref.size1(); ++i)
  {
    for (std::size_t j=0; j<ref.size2(); ++j)
    {
      if (qr(i,j) != 0.0 && ref(i,j) != 0.0)
      {
        double rel_err = fabs(qr(i,j) - ref(i,j)) / fabs(ref(i,j) );
        if (rel_err > max_error)
          max_error = rel_err;
      }
      
    }
    if (do_break)
      break;
  }
  return max_error;
}
int main (
int, 
const char **)
 
{
  typedef boost::numeric::ublas::matrix<ScalarType>              MatrixType;
  std::size_t rows = 113;   
  std::size_t cols = 54;    
  MatrixType ublas_A(rows, cols);
  MatrixType Q(rows, rows);
  MatrixType R(rows, cols);
  
  for (std::size_t i=0; i<rows; ++i)
  {
    for (std::size_t j=0; j<cols; ++j)
    {
      if (i == j)
      R(i,j) = 0.0;
    }
    for (std::size_t j=0; j<rows; ++j)
  }
  
  MatrixType ublas_A_backup(ublas_A);
  VCLMatrixType vcl_A(ublas_A.size1(), ublas_A.size2());
  std::cout << "--- Boost.uBLAS ---" << std::endl;
  MatrixType ublas_QR = 
prod(Q, R);
  double ublas_error = 
check(ublas_QR, ublas_A_backup);
 
  std::cout << "Maximum relative error (ublas): " << ublas_error << std::endl;
  std::cout << "--- Hybrid (default) ---" << std::endl;
  Q.clear(); R.clear();
  double hybrid_error = 
check(ublas_QR, ublas_A_backup);
 
  std::cout << "Maximum relative error (hybrid): " << hybrid_error << std::endl;
  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
  return EXIT_SUCCESS;
}