32 #ifndef __VMML__QR_DECOMPOSITION__HPP__
33 #define __VMML__QR_DECOMPOSITION__HPP__
35 #include <vmmlib/matrix.hpp>
36 #include <vmmlib/vector.hpp>
37 #include <vmmlib/exception.hpp>
52 template<
size_t M,
size_t N,
typename T >
53 void qr_decompose_gram_schmidt(
54 const matrix< M, N, T >& A_,
63 matrix< M, N, T > A( A_ );
65 vector< M, T > a_column, q_column;
69 for(
size_t k = 0; k < N; ++k )
72 A.get_column( k, a_column );
73 const T a_norm = a_column.length();
75 R.at( k, k ) = a_norm;
77 if ( a_norm == static_cast< T >( 0.0 ) )
80 Q.set_column( k, a_column / a_norm );
82 for(
size_t j = k+1; j < N; ++j )
84 Q.get_column( k, q_column );
85 A.get_column( j, a_column );
87 R.at( k, j ) = a_column.dot( q_column );
89 for(
size_t i = 0; i < M; ++i )
91 A( i, j ) = A( i, j ) - R( k, j ) * Q( i, k );
97 for(
unsigned j = 0; j < N; ++j )
101 for(
unsigned i = 0; i < j; ++i )
103 Q.get_column( i, q_column );
104 A.get_column( j, a_column );
106 R( i, j ) = dot( q_column, a_column );
107 v -= q_column * R(i,j);
110 Q.set_column( j, v / R(j,j) );