Blaze 3.9
syevx.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_LAPACK_SYEVX_H_
36#define _BLAZE_MATH_LAPACK_SYEVX_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <memory>
44#include <blaze/math/Aliases.h>
55#include <blaze/util/Assert.h>
58#include <blaze/util/Types.h>
60
61
62namespace blaze {
63
64//=================================================================================================
65//
66// LAPACK SYMMETRIC MATRIX EIGENVALUE FUNCTIONS (SYEVX)
67//
68//=================================================================================================
69
70//*************************************************************************************************
73template< typename MT, bool SO, typename VT, bool TF >
74size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo );
75
76template< typename MT, bool SO, typename VT, bool TF, typename ST >
77size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo, ST low, ST upp );
78
79template< typename MT1, bool SO1, typename VT, bool TF, typename MT2, bool SO2 >
80size_t syevx( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
81 DenseMatrix<MT2,SO2>& Z, char uplo );
82
83template< typename MT1, bool SO1, typename VT, bool TF, typename MT2, bool SO2, typename ST >
84size_t syevx( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
85 DenseMatrix<MT2,SO2>& Z, char uplo, ST low, ST upp );
87//*************************************************************************************************
88
89
90//*************************************************************************************************
113template< typename MT // Type of the matrix A
114 , bool SO // Storage order of the matrix A
115 , typename VT // Type of the vector w
116 , bool TF // Transpose flag of the vector w
117 , typename ST > // Type of the scalar boundary values
118inline size_t syevx_backend( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w,
119 char uplo, char range, ST vl, ST vu,
120 blas_int_t il, blas_int_t iu )
121{
122 BLAZE_INTERNAL_ASSERT( isSquare( *A ), "Invalid non-square matrix detected" );
123 BLAZE_INTERNAL_ASSERT( range != 'A' || (*w).size() == (*A).rows(), "Invalid vector dimension detected" );
124 BLAZE_INTERNAL_ASSERT( range != 'V' || (*w).size() == (*A).rows(), "Invalid vector dimension detected" );
125 BLAZE_INTERNAL_ASSERT( range != 'I' || (*w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
126
127 using ET = ElementType_t<MT>;
128
130
131 blas_int_t n ( numeric_cast<blas_int_t>( (*A).rows() ) );
132 blas_int_t lda ( numeric_cast<blas_int_t>( (*A).spacing() ) );
133 blas_int_t m ( 0 );
134 blas_int_t info( 0 );
135
136 blas_int_t lwork( 12*n + 2 );
137 const std::unique_ptr<ET[]> work ( new ET[lwork] );
138 const std::unique_ptr<blas_int_t[]> iwork( new blas_int_t[12*n] );
139 const std::unique_ptr<blas_int_t[]> ifail( new blas_int_t[n] );
140
141 syevx( 'N', range, uplo, n, (*A).data(), lda, vl, vu, il, iu, ET(0), &m,
142 (*w).data(), nullptr, 1, work.get(), lwork, iwork.get(), ifail.get(), &info );
143
144 const size_t num( numeric_cast<size_t>( m ) );
145
146 BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
147 BLAZE_INTERNAL_ASSERT( num <= (*w).size(), "Invalid number of eigenvalues detected" );
148
149 if( info > 0 ) {
150 BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
151 }
152
153 return num;
154}
156//*************************************************************************************************
157
158
159//*************************************************************************************************
215template< typename MT // Type of the matrix A
216 , bool SO // Storage order of the matrix A
217 , typename VT // Type of the vector w
218 , bool TF > // Transpose flag of the vector w
219inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo )
220{
227
233
234 using ET = ElementType_t<MT>;
235
236 const size_t N( (*A).rows() );
237
238 if( !isSquare( *A ) ) {
239 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
240 }
241
242 if( uplo != 'L' && uplo != 'U' ) {
243 BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
244 }
245
246 resize( *w, N, false );
247
248 if( N == 0UL ) {
249 return 0;
250 }
251
252 return syevx_backend( *A, *w, uplo, 'A', ET(), ET(), 0, 0 );
253}
254//*************************************************************************************************
255
256
257//*************************************************************************************************
343template< typename MT // Type of the matrix A
344 , bool SO // Storage order of the matrix A
345 , typename VT // Type of the vector w
346 , bool TF // Transpose flag of the vector w
347 , typename ST > // Type of the scalar boundary values
348inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo, ST low, ST upp )
349{
356
362
363 if( !isSquare( *A ) ) {
364 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
365 }
366
367 if( uplo != 'L' && uplo != 'U' ) {
368 BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
369 }
370
371 if( IsFloatingPoint_v<ST> && low >= upp ) {
372 BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
373 }
374
375 if( !IsFloatingPoint_v<ST> && low > upp ) {
376 BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
377 }
378
379 const size_t N( (*A).rows() );
380 const size_t num( IsFloatingPoint_v<ST> ? N : size_t( upp - low ) + 1UL );
381
382 if( !IsFloatingPoint_v<ST> && num > N ) {
383 BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
384 }
385
386 resize( *w, num, false );
387
388 if( N == 0UL ) {
389 return 0;
390 }
391
392 const char range( IsFloatingPoint_v<ST> ? 'V' : 'I' );
393 const ST vl ( IsFloatingPoint_v<ST> ? low : ST() );
394 const ST vu ( IsFloatingPoint_v<ST> ? upp : ST() );
395 const blas_int_t il ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<blas_int_t>( low ) );
396 const blas_int_t iu ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<blas_int_t>( upp ) );
397
398 return syevx_backend( *A, *w, uplo, range, vl, vu, il, iu );
399}
400//*************************************************************************************************
401
402
403//*************************************************************************************************
427template< typename MT1 // Type of the matrix A
428 , bool SO1 // Storage order of the matrix A
429 , typename VT // Type of the vector w
430 , bool TF // Transpose flag of the vector w
431 , typename MT2 // Type of the matrix Z
432 , bool SO2 // Storage order of the matrix Z
433 , typename ST > // Type of the scalar boundary values
434inline size_t syevx_backend( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
435 DenseMatrix<MT2,SO2>& Z, char uplo, char range,
436 ST vl, ST vu, blas_int_t il, blas_int_t iu )
437{
438 BLAZE_INTERNAL_ASSERT( isSquare( *A ), "Invalid non-square matrix detected" );
439 BLAZE_INTERNAL_ASSERT( range != 'A' || (*w).size() == (*A).rows(), "Invalid vector dimension detected" );
440 BLAZE_INTERNAL_ASSERT( range != 'V' || (*w).size() == (*A).rows(), "Invalid vector dimension detected" );
441 BLAZE_INTERNAL_ASSERT( range != 'I' || (*w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
442 BLAZE_INTERNAL_ASSERT( SO2 || (*Z).rows() == (*w).size(), "Invalid matrix dimension detected" );
443 BLAZE_INTERNAL_ASSERT( SO2 || (*Z).columns() == (*A).rows(), "Invalid matrix dimension detected" );
444 BLAZE_INTERNAL_ASSERT( !SO2 || (*Z).rows() == (*A).rows(), "Invalid matrix dimension detected" );
445 BLAZE_INTERNAL_ASSERT( !SO2 || (*Z).columns() == (*w).size(), "Invalid matrix dimension detected" );
446
447 using ET = ElementType_t<MT1>;
448
450
451 blas_int_t n ( numeric_cast<blas_int_t>( (*A).rows() ) );
452 blas_int_t lda ( numeric_cast<blas_int_t>( (*A).spacing() ) );
453 blas_int_t m ( 0 );
454 blas_int_t ldz ( numeric_cast<blas_int_t>( (*Z).spacing() ) );
455 blas_int_t info( 0 );
456
457 blas_int_t lwork( 12*n + 2 );
458 const std::unique_ptr<ET[]> work ( new ET[lwork] );
459 const std::unique_ptr<blas_int_t[]> iwork( new blas_int_t[12*n] );
460 const std::unique_ptr<blas_int_t[]> ifail( new blas_int_t[n] );
461
462 syevx( 'N', range, uplo, n, (*A).data(), lda, vl, vu, il, iu, ET(0), &m,
463 (*w).data(), (*Z).data(), ldz, work.get(), lwork, iwork.get(), ifail.get(), &info );
464
465 const size_t num( numeric_cast<size_t>( m ) );
466
467 BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
468 BLAZE_INTERNAL_ASSERT( num <= (*w).size(), "Invalid number of eigenvalues detected" );
469
470 if( info > 0 ) {
471 BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
472 }
473
474 return num;
475}
477//*************************************************************************************************
478
479
480//*************************************************************************************************
543template< typename MT1 // Type of the matrix A
544 , bool SO1 // Storage order of the matrix A
545 , typename VT // Type of the vector w
546 , bool TF // Transpose flag of the vector w
547 , typename MT2 // Type of the matrix Z
548 , bool SO2 > // Storage order of the matrix Z
550 DenseMatrix<MT2,SO2>& Z, char uplo )
551{
558
564
571
572 using ET = ElementType_t<MT1>;
573
574 const size_t N( (*A).rows() );
575
576 if( !isSquare( *A ) ) {
577 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
578 }
579
580 if( uplo != 'L' && uplo != 'U' ) {
581 BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
582 }
583
584 resize( *w, N, false );
585 resize( *Z, N, N, false );
586
587 if( N == 0UL ) {
588 return 0;
589 }
590
591 return syevx_backend( *A, *w, *Z, uplo, 'A', ET(), ET(), 0, 0 );
592}
593//*************************************************************************************************
594
595
596//*************************************************************************************************
692template< typename MT1 // Type of the matrix A
693 , bool SO1 // Storage order of the matrix A
694 , typename VT // Type of the vector w
695 , bool TF // Transpose flag of the vector w
696 , typename MT2 // Type of the matrix Z
697 , bool SO2 // Storage order of the matrix Z
698 , typename ST > // Type of the scalar boundary values
700 DenseMatrix<MT2,SO2>& Z, char uplo, ST low, ST upp )
701{
708
714
721
722 if( !isSquare( *A ) ) {
723 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
724 }
725
726 if( uplo != 'L' && uplo != 'U' ) {
727 BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
728 }
729
730 if( IsFloatingPoint_v<ST> && low >= upp ) {
731 BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
732 }
733
734 if( !IsFloatingPoint_v<ST> && low > upp ) {
735 BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
736 }
737
738 const size_t N( (*A).rows() );
739 const size_t num( IsFloatingPoint_v<ST> ? N : size_t( upp - low ) + 1UL );
740
741 if( !IsFloatingPoint_v<ST> && num > N ) {
742 BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
743 }
744
745 resize( *w, num, false );
746 resize( *Z, ( IsRowMajorMatrix_v<MT2> ? num : N ),
747 ( IsRowMajorMatrix_v<MT2> ? N : num ), false );
748
749 if( N == 0UL ) {
750 return 0;
751 }
752
753 const char range( IsFloatingPoint_v<ST> ? 'V' : 'I' );
754 const ST vl ( IsFloatingPoint_v<ST> ? low : ST() );
755 const ST vu ( IsFloatingPoint_v<ST> ? upp : ST() );
756 const blas_int_t il ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<blas_int_t>( low ) );
757 const blas_int_t iu ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<blas_int_t>( upp ) );
758
759 return syevx_backend( *A, *w, *Z, uplo, range, vl, vu, il, iu );
760}
761//*************************************************************************************************
762
763} // namespace blaze
764
765#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
Header file for run time assertion macros.
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
Header file for the IsFloatingPoint type trait.
Header file for the IsRowMajorMatrix type trait.
Constraint on the data type.
Cast operators for numeric types.
Header file for the CLAPACK syevx wrapper functions.
Base class for dense matrices.
Definition: DenseMatrix.h:82
Base class for N-dimensional dense vectors.
Definition: DenseVector.h:77
Constraint on the data type.
Header file for the DenseMatrix base class.
Header file for the DenseVector base class.
#define BLAZE_CONSTRAINT_MUST_BE_BUILTIN_TYPE(T)
Constraint on the data type.
Definition: Builtin.h:60
size_t syevx(DenseMatrix< MT1, SO1 > &A, DenseVector< VT, TF > &w, DenseMatrix< MT2, SO2 > &Z, char uplo, ST low, ST upp)
LAPACK kernel for computing the eigenvalues of the given dense symmetric matrix.
Definition: syevx.h:699
#define BLAZE_CONSTRAINT_MUST_BE_BLAS_COMPATIBLE_TYPE(T)
Constraint on the data type.
Definition: BLASCompatible.h:61
#define BLAZE_CONSTRAINT_MUST_BE_CONTIGUOUS_TYPE(T)
Constraint on the data type.
Definition: Contiguous.h:61
#define BLAZE_CONSTRAINT_MUST_NOT_BE_COMPUTATION_TYPE(T)
Constraint on the data type.
Definition: Computation.h:81
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ADAPTOR_TYPE(T)
Constraint on the data type.
Definition: Adaptor.h:81
#define BLAZE_CONSTRAINT_MUST_HAVE_MUTABLE_DATA_ACCESS(T)
Constraint on the data type.
Definition: MutableDataAccess.h:61
int32_t blas_int_t
Signed integer type used in the BLAS/LAPACK wrapper functions.
Definition: Types.h:64
#define BLAZE_THROW_LAPACK_ERROR(MESSAGE)
Macro for the emission of an exception on detection of a LAPACK error.
Definition: Exception.h:146
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
bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:1383
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
#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.
Header file for basic type definitions.