QR.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_DENSE_QR_H_
36 #define _BLAZE_MATH_DENSE_QR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <memory>
44 #include <blaze/math/Aliases.h>
52 #include <blaze/math/Exception.h>
61 #include <blaze/util/EnableIf.h>
62 
63 
64 namespace blaze {
65 
66 //=================================================================================================
67 //
68 // QR DECOMPOSITION FUNCTIONS
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
75 template< typename MT1, bool SO1, typename MT2, bool SO2, typename MT3, bool SO3 >
76 void qr( const DenseMatrix<MT1,SO1>& A, DenseMatrix<MT2,SO2>& Q, DenseMatrix<MT3,SO3>& R );
78 //*************************************************************************************************
79 
80 
81 //*************************************************************************************************
93 template< typename MT1 > // Type of matrix A
94 inline EnableIf_<IsBuiltin< ElementType_<MT1> > >
95  qr_backend( MT1& A, const ElementType_<MT1>* tau )
96 {
97  orgqr( A, tau );
98 }
100 //*************************************************************************************************
101 
102 
103 //*************************************************************************************************
115 template< typename MT1 > // Type of matrix A
116 inline EnableIf_<IsComplex< ElementType_<MT1> > >
117  qr_backend( MT1& A, const ElementType_<MT1>* tau )
118 {
119  ungqr( A, tau );
120 }
122 //*************************************************************************************************
123 
124 
125 //*************************************************************************************************
173 template< typename MT1 // Type of matrix A
174  , bool SO1 // Storage order of matrix A
175  , typename MT2 // Type of matrix Q
176  , bool SO2 // Storage order of matrix Q
177  , typename MT3 // Type of matrix R
178  , bool SO3 > // Storage order of matrix R
180 {
183 
186 
192 
193  using ET1 = ElementType_<MT1>;
194 
195  const size_t m( (~A).rows() );
196  const size_t n( (~A).columns() );
197  const size_t mindim( min( m, n ) );
198 
199  if( ( !IsResizable<MT2>::value && ( (~Q).rows() != m || (~Q).columns() != mindim ) ) ||
200  ( !IsResizable<MT3>::value && ( (~R).rows() != mindim || (~R).columns() != n ) ) ) {
201  BLAZE_THROW_INVALID_ARGUMENT( "Dimensions of fixed size matrix do not match" );
202  }
203 
204  if( IsSquare<MT3>::value && mindim != n ) {
205  BLAZE_THROW_INVALID_ARGUMENT( "Square matrix cannot be resized to min(m,n)-by-n" );
206  }
207 
208  const std::unique_ptr<ET1[]> tau( new ET1[mindim] );
209  decltype(auto) r( derestrict( ~R ) );
210 
211  if( m < n )
212  {
213  r = A;
214  geqrf( r, tau.get() );
215  (~Q) = submatrix( r, 0UL, 0UL, m, m );
216  qr_backend( ~Q, tau.get() );
217 
218  for( size_t i=1UL; i<m; ++i ) {
219  for( size_t j=0UL; j<i; ++j ) {
220  reset( r(i,j) );
221  }
222  }
223  }
224  else
225  {
226  (~Q) = A;
227  geqrf( ~Q, tau.get() );
228 
229  resize( ~R, n, n, false );
230  reset( r );
231 
232  for( size_t i=0UL; i<n; ++i ) {
233  for( size_t j=i; j<n; ++j ) {
234  r(i,j) = (~Q)(i,j);
235  }
236  }
237 
238  qr_backend( ~Q, tau.get() );
239  }
240 }
241 //*************************************************************************************************
242 
243 } // namespace blaze
244 
245 #endif
Header file for the implementation of the Submatrix view.
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
Headerfile for the generic min algorithm.
void ungqr(int m, int n, int k, complex< float > *A, int lda, const complex< float > *tau, complex< float > *work, int lwork, int *info)
LAPACK kernel for the reconstruction of the orthogonal matrix Q from a QR decomposition.
Definition: ungqr.h:120
void orgqr(int m, int n, int k, float *A, int lda, const float *tau, float *work, int lwork, int *info)
LAPACK kernel for the reconstruction of the orthogonal matrix Q from a QR decomposition.
Definition: orgqr.h:120
Header file for the LAPACK functions to reconstruct Q from a QR decomposition (orgqr) ...
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:560
Submatrix< MT, AF > submatrix(Matrix< MT, SO > &matrix, size_t row, size_t column, size_t m, size_t n)
Creating a view on a specific submatrix of the given matrix.
Definition: Submatrix.h:352
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
#define BLAZE_CONSTRAINT_MUST_NOT_BE_UNITRIANGULAR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a lower or upper unitriangular matrix ty...
Definition: UniTriangular.h:81
Constraint on the data type.
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ADAPTOR_TYPE(T)
Constraint on the data type.In case the given data type T is an adaptor type (as for instance LowerMa...
Definition: Adaptor.h:81
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes...
Definition: DenseMatrix.h:78
Constraint on the data type.
Header file for the IsSquare type trait.
Constraint on the data type.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the LAPACK QR decomposition functions (geqrf)
Header file for the DenseMatrix base class.
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
Compile time check for square matrices.This type trait tests whether or not the given template parame...
Definition: IsSquare.h:88
BLAZE_ALWAYS_INLINE size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:340
Header file for the exception macros of the math module.
BLAZE_ALWAYS_INLINE void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:548
Header file for the EnableIf class template.
Constraint on the data type.
void qr(const DenseMatrix< MT1, SO1 > &A, DenseMatrix< MT2, SO2 > &Q, DenseMatrix< MT3, SO3 > &R)
QR decomposition of the given dense matrix.
Definition: QR.h:179
Compile time check for resizable data types.This type trait tests whether the given data type is a re...
Definition: IsResizable.h:75
#define BLAZE_CONSTRAINT_MUST_NOT_BE_SYMMETRIC_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a symmetric matrix type, a compilation error is created.
Definition: Symmetric.h:79
void geqrf(int m, int n, float *A, int lda, float *tau, float *work, int lwork, int *info)
LAPACK kernel for the QR decomposition of the given dense single precision column-major matrix...
Definition: geqrf.h:146
#define BLAZE_CONSTRAINT_MUST_NOT_BE_LOWER_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a lower triangular matrix type...
Definition: Lower.h:81
#define BLAZE_CONSTRAINT_MUST_BE_BLAS_COMPATIBLE_TYPE(T)
Constraint on the data type.In case the given data type T is not a BLAS compatible data type (i...
Definition: BLASCompatible.h:61
Constraint on the data type.
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_STRICTLY_TRIANGULAR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a strictly lower or upper triangular mat...
Definition: StrictlyTriangular.h:81
BLAZE_ALWAYS_INLINE size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:324
#define BLAZE_CONSTRAINT_MUST_NOT_BE_HERMITIAN_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is an Hermitian matrix type, a compilation error is created.
Definition: Hermitian.h:79
Header file for the LAPACK functions to reconstruct Q from a QR decomposition (ungqr) ...
Header file for the IsResizable type trait.