Heidelberg Educational Numerics Library Version 0.27 (from 15 March 2021)
Functions
qr.hh File Reference

This file implements QR decomposition using Gram-Schmidt method. More...

#include <cmath>
#include <utility>
#include "densematrix.hh"
#include "vector.hh"

Go to the source code of this file.

Functions

template<class T >
DenseMatrix< Thdnum::gram_schmidt (const DenseMatrix< T > &A)
 computes orthonormal basis of Im(A) using classical Gram-Schmidt
 
template<class T >
DenseMatrix< Thdnum::modified_gram_schmidt (const DenseMatrix< T > &A)
 computes orthonormal basis of Im(A) using modified Gram-Schmidt
 
template<class T >
DenseMatrix< Thdnum::qr_gram_schmidt_simple (DenseMatrix< T > &Q)
 computes qr decomposition using modified Gram-Schmidt - works only with small (m>n) and square matrices
 
template<class T >
DenseMatrix< Thdnum::qr_gram_schmidt (DenseMatrix< T > &Q)
 computes qr decomposition using modified Gram-Schmidt - works only with small (m>n) and square matrices
 
template<class T >
DenseMatrix< Thdnum::qr_gram_schmidt_pivoting (DenseMatrix< T > &Q, Vector< int > &p, int &rank, T threshold=0.00000000001)
 computes qr decomposition using modified Gram-Schmidt and pivoting - works with all types of matrices
 
template<typename T >
void hdnum::permute_forward (DenseMatrix< T > &A, Vector< int > &p)
 applies a permutation vector to a matrix
 

Detailed Description

This file implements QR decomposition using Gram-Schmidt method.

Function Documentation

◆ gram_schmidt()

template<class T >
DenseMatrix< T > hdnum::gram_schmidt ( const DenseMatrix< T > & A)

computes orthonormal basis of Im(A) using classical Gram-Schmidt

Template Parameters
hdnum::DenseMatrix<T>A

Example:

{1, -5}});
hdnum::DenseMatrix<double> Q(hdnum::gram_schmidt(A));
std::cout << "A = " << A << std::endl;
std::cout << "Q = " << Q << std::endl;
Class with mathematical matrix operations.
Definition densematrix.hh:33

Output:

A = 
                  0          1 
      0   2.000e+00  9.000e+00 
      1   1.000e+00 -5.000e+00 

Q = 
                  0          1 
      0   8.944e-01  4.472e-01 
      1   4.472e-01 -8.944e-01 

◆ modified_gram_schmidt()

template<class T >
DenseMatrix< T > hdnum::modified_gram_schmidt ( const DenseMatrix< T > & A)

computes orthonormal basis of Im(A) using modified Gram-Schmidt

Template Parameters
hdnum::DenseMatrix<T>A

Example:

{1, -5}});
hdnum::DenseMatrix<double> Q(hdnum::modified_gram_schmidt(A));
std::cout << "A = " << A << std::endl;
std::cout << "Q = " << Q << std::endl;

Output:

A = 
                  0          1 
      0   2.000e+00  9.000e+00 
      1   1.000e+00 -5.000e+00 

Q = 
                  0          1 
      0   8.944e-01  4.472e-01 
      1   4.472e-01 -8.944e-01 

◆ permute_forward()

template<typename T >
void hdnum::permute_forward ( DenseMatrix< T > & A,
Vector< int > & p )

applies a permutation vector to a matrix

Template Parameters
hdnum::DenseMatrix<T>A
Parameters
hdnum::Vector<int>p

Example:

{1, -5}});
std::cout << "A = " << A << std::endl;
std::cout << "p = " << p << std::endl;
void permute_forward(const Vector< std::size_t > &p, Vector< T > &b)
apply permutations to a right hand side vector
Definition lr.hh:160

Output:

A = 
                  0          1 
      0   9.000e+00  2.000e+00 
      1  -5.000e+00  1.000e+00 

p = 
        [ 0]              0
        [ 1]              1

◆ qr_gram_schmidt()

template<class T >
DenseMatrix< T > hdnum::qr_gram_schmidt ( DenseMatrix< T > & Q)

computes qr decomposition using modified Gram-Schmidt - works only with small (m>n) and square matrices

Template Parameters
hdnum::DenseMatrix<T>Q

Example:

{1, -5}});
hdnum::DenseMatrix<double> R(hdnum::qr_gram_schmidt(Q));
std::cout << "A = " << A << std::endl;
std::cout << "Q = " << Q << std::endl;
std::cout << "R = " << R << std::endl;
std::cout << "QR = " << Q*R << std::endl;

Output:

A = 
                  0          1 
      0   2.000e+00  9.000e+00 
      1   1.000e+00 -5.000e+00 

Q = 
                  0          1 
      0   8.944e-01  4.472e-01 
      1   4.472e-01 -8.944e-01 

R = 
                  0          1 
      0   2.236e+00  5.814e+00 
      1   0.000e+00  8.497e+00 

QR = 
                  0          1 
      0   2.000e+00  9.000e+00 
      1   1.000e+00 -5.000e+00 

◆ qr_gram_schmidt_pivoting()

template<class T >
DenseMatrix< T > hdnum::qr_gram_schmidt_pivoting ( DenseMatrix< T > & Q,
Vector< int > & p,
int & rank,
T threshold = 0.00000000001 )

computes qr decomposition using modified Gram-Schmidt and pivoting - works with all types of matrices

Template Parameters
hdnum::DenseMatrix<T>Q
Tthreshold (optional)
Parameters
hdnum::Vector<int>p
intrank

Example:

{11, 9, 2}});
hdnum::Vector<int> p(A.colsize());
int rank;
hdnum::DenseMatrix<double> R(hdnum::qr_gram_schmidt_pivoting(Q, p, rank));
hdnum::DenseMatrix<double> Q_right_dimension(A.rowsize(), rank);
hdnum::DenseMatrix<double> R_right_dimension(rank, A.colsize());
for (int i = 0; i < Q_right_dimension.rowsize(); i++) {
for (int j = 0; j < Q_right_dimension.colsize(); j++) {
Q_right_dimension(i, j) = Q(i, j);
}
}
for (int i = 0; i < R_right_dimension.rowsize(); i++) {
for (int j = 0; j < R_right_dimension.colsize(); j++) {
R_right_dimension(i, j) = R(i, j);
}
}
hdnum::DenseMatrix<double> QR(Q_right_dimension*R_right_dimension);
std::cout << "A = " << A << std::endl;
std::cout << "Q = " << Q_right_dimension << std::endl;
std::cout << "R = " << R_right_dimension << std::endl;
std::cout << "QR = " << QR << std::endl;

Output:

A = 
                  0          1          2 
      0   5.000e+00  2.000e+00  3.000e+00 
      1   1.100e+01  9.000e+00  2.000e+00 

Q = 
                  0          1 
      0   4.138e-01 -9.104e-01 
      1   9.104e-01  4.138e-01 

R = 
                  0          1          2 
      0   1.208e+01  9.021e+00  3.062e+00 
      1   0.000e+00  1.903e+00 -1.903e+00 

QR = 
                  0          1          2 
      0   5.000e+00  2.000e+00  3.000e+00 
      1   1.100e+01  9.000e+00  2.000e+00

◆ qr_gram_schmidt_simple()

template<class T >
DenseMatrix< T > hdnum::qr_gram_schmidt_simple ( DenseMatrix< T > & Q)

computes qr decomposition using modified Gram-Schmidt - works only with small (m>n) and square matrices

Template Parameters
hdnum::DenseMatrix<T>Q

Example:

{1, -5}});
hdnum::DenseMatrix<double> R(hdnum::qr_gram_schmidt_simple(Q));
std::cout << "A = " << A << std::endl;
std::cout << "Q = " << Q << std::endl;
std::cout << "R = " << R << std::endl;
std::cout << "QR = " << Q*R << std::endl;

Output:

A = 
                  0          1 
      0   2.000e+00  9.000e+00 
      1   1.000e+00 -5.000e+00 

Q = 
                  0          1 
      0   8.944e-01  4.472e-01 
      1   4.472e-01 -8.944e-01 

R = 
                  0          1 
      0   2.236e+00  5.814e+00 
      1   0.000e+00  8.497e+00 

QR = 
                  0          1 
      0   2.000e+00  9.000e+00 
      1   1.000e+00 -5.000e+00