LU.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_DENSE_LU_H_
36 #define _BLAZE_MATH_DENSE_LU_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <memory>
44 #include <utility>
45 #include <blaze/math/Aliases.h>
54 #include <blaze/math/Exception.h>
59 #include <blaze/util/NumericCast.h>
60 
61 
62 namespace blaze {
63 
64 //=================================================================================================
65 //
66 // LU DECOMPOSITION FUNCTIONS
67 //
68 //=================================================================================================
69 
70 //*************************************************************************************************
73 template< typename MT1, bool SO1, typename MT2, typename MT3, typename MT4, bool SO2 >
74 void lu( const DenseMatrix<MT1,SO1>& A, DenseMatrix<MT2,SO1>& L,
75  DenseMatrix<MT3,SO1>& U, Matrix<MT4,SO2>& P );
77 //*************************************************************************************************
78 
79 
80 //*************************************************************************************************
93 template< typename MT1 // Type of matrix A
94  , bool SO1 // Storage order of dense matrix A
95  , typename MT2 // Type of matrix P
96  , bool SO2 > // Storage order of matrix P
97 void lu( DenseMatrix<MT1,SO1>& A, Matrix<MT2,SO2>& P )
98 {
102 
103  using ET = ElementType_<MT2>;
104 
105  const int m( numeric_cast<int>( (~A).rows() ) );
106  const int n( numeric_cast<int>( (~A).columns() ) );
107  const int mindim( min( m, n ) );
108  const int size( SO1 ? m : n );
109 
110  const std::unique_ptr<int[]> helper( new int[mindim + size] );
111  int* ipiv ( helper.get() );
112  int* permut( ipiv + mindim );
113 
114  getrf( ~A, ipiv );
115 
116  for( int i=0; i<size; ++i ) {
117  permut[i] = i;
118  }
119 
120  for( int i=0; i<mindim; ++i ) {
121  --ipiv[i];
122  if( ipiv[i] != i ) {
123  std::swap( permut[ipiv[i]], permut[i] );
124  }
125  }
126 
127  resize( ~P, size, size, false );
128  reset( ~P );
129  for( int i=0; i<size; ++i ) {
130  (~P)( ( SO1 ? permut[i] : i ), ( SO1 ? i : permut[i] ) ) = ET(1);
131  }
132 }
134 //*************************************************************************************************
135 
136 
137 //*************************************************************************************************
213 template< typename MT1 // Type of matrix A
214  , bool SO1 // Storage order of matrix A, L and U
215  , typename MT2 // Type of matrix L
216  , typename MT3 // Type of matrix U
217  , typename MT4 // Type of matrix P
218  , bool SO2 > // Storage order of matrix P
221 {
224 
229 
234 
235  using ET2 = ElementType_<MT2>;
236  using ET3 = ElementType_<MT3>;
237 
238  const size_t m( (~A).rows() );
239  const size_t n( (~A).columns() );
240  const size_t mindim( min( m, n ) );
241  const size_t size( SO1 ? m : n );
242 
243  if( ( !IsResizable<MT2>::value && ( (~L).rows() != m || (~L).columns() != mindim ) ) ||
244  ( !IsResizable<MT3>::value && ( (~U).rows() != mindim || (~U).columns() != n ) ) ||
245  ( !IsResizable<MT4>::value && ( (~P).rows() != size || (~P).columns() != size ) ) ) {
246  BLAZE_THROW_INVALID_ARGUMENT( "Dimensions of fixed size matrix do not match" );
247  }
248 
249  if( ( IsSquare<MT2>::value && n < m ) || ( IsSquare<MT3>::value && m < n ) ) {
250  BLAZE_THROW_INVALID_ARGUMENT( "Square matrix cannot be resized to m-by-n" );
251  }
252 
253  decltype(auto) l( derestrict( ~L ) );
254  decltype(auto) u( derestrict( ~U ) );
255 
256  if( m < n )
257  {
258  u = (~A);
259  lu( u, ~P );
260 
261  resize( ~L, m, m, false );
262  reset( l );
263 
264  if( SO1 == rowMajor )
265  {
266  for( size_t i=0UL; i<m; ++i )
267  {
268  for( size_t j=0UL; j<i; ++j ) {
269  l(i,j) = u(i,j);
270  reset( u(i,j) );
271  }
272 
273  l(i,i) = u(i,i);
274  u(i,i) = ET3(1);
275  }
276  }
277  else
278  {
279  for( size_t j=0UL; j<m; ++j )
280  {
281  l(j,j) = ET2(1);
282 
283  for( size_t i=j+1UL; i<m; ++i ) {
284  l(i,j) = u(i,j);
285  reset( u(i,j) );
286  }
287  }
288  }
289  }
290  else
291  {
292  l = (~A);
293  lu( l, ~P );
294 
295  resize( ~U, n, n, false );
296  reset( u );
297 
298  if( SO1 == rowMajor )
299  {
300  for( size_t i=0UL; i<n; ++i )
301  {
302  u(i,i) = ET3(1);
303 
304  for( size_t j=i+1UL; j<n; ++j ) {
305  u(i,j) = l(i,j);
306  reset( l(i,j) );
307  }
308  }
309  }
310  else
311  {
312  for( size_t j=0UL; j<n; ++j )
313  {
314  for( size_t i=0UL; i<j; ++i ) {
315  u(i,j) = l(i,j);
316  reset( l(i,j) );
317  }
318 
319  u(j,j) = l(j,j);
320  l(j,j) = ET2(1);
321  }
322  }
323  }
324 }
325 //*************************************************************************************************
326 
327 } // namespace blaze
328 
329 #endif
#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.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:560
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.
Cast operators for numeric types.
#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.
Constraint on the data type.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b) noexcept
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:5924
Header file for the DenseMatrix base class.
void lu(const DenseMatrix< MT1, SO1 > &A, DenseMatrix< MT2, SO1 > &L, DenseMatrix< MT3, SO1 > &U, Matrix< MT4, SO2 > &P)
LU decomposition of the given dense matrix.
Definition: LU.h:219
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.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_UPPER_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a upper triangular matrix type...
Definition: Upper.h:81
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
Constraint on the data type.
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 getrf(int m, int n, float *A, int lda, int *ipiv, int *info)
LAPACK kernel for the LU decomposition of the given dense general single precision column-major matri...
Definition: getrf.h:131
Constraint on the data type.
#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
Base class for matrices.The Matrix class is a base class for all dense and sparse matrix classes with...
Definition: Forward.h:101
#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
const bool rowMajor
Storage order flag for row-major matrices.
Definition: StorageOrder.h:71
#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 IsResizable type trait.
Header file for the LAPACK LU decomposition functions (getrf)