Blaze 3.9
Loading...
Searching...
No Matches
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>
61
62
63namespace blaze {
64
65//=================================================================================================
66//
67// LU DECOMPOSITION FUNCTIONS
68//
69//=================================================================================================
70
71//*************************************************************************************************
74template< typename MT1, bool SO1, typename MT2, typename MT3, typename MT4, bool SO2 >
78//*************************************************************************************************
79
80
81//*************************************************************************************************
94template< typename MT1 // Type of matrix A
95 , bool SO1 // Storage order of dense matrix A
96 , typename MT2 // Type of matrix P
97 , bool SO2 > // Storage order of matrix P
99{
100 using std::swap;
101
105
106 using ET = ElementType_t<MT2>;
107
108 const blas_int_t m( numeric_cast<blas_int_t>( (*A).rows() ) );
109 const blas_int_t n( numeric_cast<blas_int_t>( (*A).columns() ) );
110 const blas_int_t mindim( min( m, n ) );
111 const blas_int_t size( SO1 ? m : n );
112
113 const std::unique_ptr<blas_int_t[]> helper( new blas_int_t[mindim + size] );
114 blas_int_t* ipiv ( helper.get() );
115 blas_int_t* permut( ipiv + mindim );
116
117 getrf( *A, ipiv );
118
119 for( int i=0; i<size; ++i ) {
120 permut[i] = i;
121 }
122
123 for( int i=0; i<mindim; ++i ) {
124 --ipiv[i];
125 if( ipiv[i] != i ) {
126 swap( permut[ipiv[i]], permut[i] );
127 }
128 }
129
130 resize( *P, size, size, false );
131 reset( *P );
132 for( int i=0; i<size; ++i ) {
133 (*P)( ( SO1 ? permut[i] : i ), ( SO1 ? i : permut[i] ) ) = ET(1);
134 }
135}
137//*************************************************************************************************
138
139
140//*************************************************************************************************
216template< typename MT1 // Type of matrix A
217 , bool SO1 // Storage order of matrix A, L and U
218 , typename MT2 // Type of matrix L
219 , typename MT3 // Type of matrix U
220 , typename MT4 // Type of matrix P
221 , bool SO2 > // Storage order of matrix P
224{
227
232
237
238 using ET2 = ElementType_t<MT2>;
239 using ET3 = ElementType_t<MT3>;
240
241 const size_t m( (*A).rows() );
242 const size_t n( (*A).columns() );
243 const size_t mindim( min( m, n ) );
244 const size_t size( SO1 ? m : n );
245
246 if( ( !IsResizable_v<MT2> && ( (*L).rows() != m || (*L).columns() != mindim ) ) ||
247 ( !IsResizable_v<MT3> && ( (*U).rows() != mindim || (*U).columns() != n ) ) ||
248 ( !IsResizable_v<MT4> && ( (*P).rows() != size || (*P).columns() != size ) ) ) {
249 BLAZE_THROW_INVALID_ARGUMENT( "Dimensions of fixed size matrix do not match" );
250 }
251
252 if( ( IsSquare_v<MT2> && n < m ) || ( IsSquare_v<MT3> && m < n ) ) {
253 BLAZE_THROW_INVALID_ARGUMENT( "Square matrix cannot be resized to m-by-n" );
254 }
255
256 decltype(auto) l( derestrict( *L ) );
257 decltype(auto) u( derestrict( *U ) );
258
259 if( m < n )
260 {
261 u = (*A);
262 lu( u, *P );
263
264 resize( *L, m, m, false );
265 reset( l );
266
267 if( SO1 == rowMajor )
268 {
269 for( size_t i=0UL; i<m; ++i )
270 {
271 for( size_t j=0UL; j<i; ++j ) {
272 l(i,j) = u(i,j);
273 reset( u(i,j) );
274 }
275
276 l(i,i) = u(i,i);
277 u(i,i) = ET3(1);
278 }
279 }
280 else
281 {
282 for( size_t j=0UL; j<m; ++j )
283 {
284 l(j,j) = ET2(1);
285
286 for( size_t i=j+1UL; i<m; ++i ) {
287 l(i,j) = u(i,j);
288 reset( u(i,j) );
289 }
290 }
291 }
292 }
293 else
294 {
295 l = (*A);
296 lu( l, *P );
297
298 resize( *U, n, n, false );
299 reset( u );
300
301 if( SO1 == rowMajor )
302 {
303 for( size_t i=0UL; i<n; ++i )
304 {
305 u(i,i) = ET3(1);
306
307 for( size_t j=i+1UL; j<n; ++j ) {
308 u(i,j) = l(i,j);
309 reset( l(i,j) );
310 }
311 }
312 }
313 else
314 {
315 for( size_t j=0UL; j<n; ++j )
316 {
317 for( size_t i=0UL; i<j; ++i ) {
318 u(i,j) = l(i,j);
319 reset( l(i,j) );
320 }
321
322 u(j,j) = l(j,j);
323 l(j,j) = ET2(1);
324 }
325 }
326 }
327}
328//*************************************************************************************************
329
330} // namespace blaze
331
332#endif
Constraint on the data type.
Header file for auxiliary alias declarations.
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition Aliases.h:190
Constraint on the data type.
Constraint on the data type.
Header file for the IsResizable type trait.
Header file for the IsSquare type trait.
Constraint on the data type.
Cast operators for numeric types.
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
Base class for dense matrices.
Definition DenseMatrix.h:82
Base class for matrices.
Definition Matrix.h:85
Header file for the DenseMatrix base class.
Header file for the LAPACK LU decomposition functions (getrf).
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:222
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition DMatDMatMapExpr.h:1339
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b) noexcept
Swapping the contents of two matrices.
Definition DiagonalMatrix.h:225
void getrf(blas_int_t m, blas_int_t n, float *A, blas_int_t lda, blas_int_t *ipiv, blas_int_t *info)
LAPACK kernel for the LU decomposition of the given dense general single precision column-major matri...
Definition getrf.h:140
#define BLAZE_CONSTRAINT_MUST_NOT_BE_SYMMETRIC_MATRIX_TYPE(T)
Constraint on the data type.
Definition Symmetric.h:79
#define BLAZE_CONSTRAINT_MUST_BE_BLAS_COMPATIBLE_TYPE(T)
Constraint on the data type.
Definition BLASCompatible.h:61
#define BLAZE_CONSTRAINT_MUST_NOT_BE_HERMITIAN_MATRIX_TYPE(T)
Constraint on the data type.
Definition Hermitian.h:79
#define BLAZE_CONSTRAINT_MUST_NOT_BE_UPPER_MATRIX_TYPE(T)
Constraint on the data type.
Definition Upper.h:81
#define BLAZE_CONSTRAINT_MUST_NOT_BE_STRICTLY_TRIANGULAR_MATRIX_TYPE(T)
Constraint on the data type.
Definition StrictlyTriangular.h:81
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ADAPTOR_TYPE(T)
Constraint on the data type.
Definition Adaptor.h:81
#define BLAZE_CONSTRAINT_MUST_NOT_BE_LOWER_MATRIX_TYPE(T)
Constraint on the data type.
Definition Lower.h:81
#define BLAZE_CONSTRAINT_MUST_NOT_BE_UNITRIANGULAR_MATRIX_TYPE(T)
Constraint on the data type.
Definition UniTriangular.h:81
constexpr bool IsSquare_v
Auxiliary variable template for the IsSquare type trait.
Definition IsSquare.h:146
constexpr bool IsResizable_v
Auxiliary variable template for the IsResizable type trait.
Definition IsResizable.h:136
int32_t blas_int_t
Signed integer type used in the BLAS/LAPACK wrapper functions.
Definition Types.h:64
constexpr void reset(Matrix< MT, SO > &matrix)
Resetting the given matrix.
Definition Matrix.h:806
void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition Matrix.h:1108
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition Matrix.h:676
To numeric_cast(From from)
Checked conversion of values of numeric type.
Definition NumericCast.h:275
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition Exception.h:235
Header file for the exception macros of the math module.
constexpr bool rowMajor
Storage order flag for row-major matrices.
Definition StorageOrder.h:71
Header file for the generic min algorithm.