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>
50 #include <blaze/math/Exception.h>
55 #include <blaze/util/Assert.h>
57 #include <blaze/util/NumericCast.h>
58 #include <blaze/util/Types.h>
60 
61 
62 namespace blaze {
63 
64 //=================================================================================================
65 //
66 // LAPACK SYMMETRIC MATRIX EIGENVALUE FUNCTIONS (SYEVX)
67 //
68 //=================================================================================================
69 
70 //*************************************************************************************************
73 template< typename MT, bool SO, typename VT, bool TF >
74 inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo );
75 
76 template< typename MT, bool SO, typename VT, bool TF, typename ST >
77 inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo, ST low, ST upp );
78 
79 template< typename MT1, bool SO1, typename VT, bool TF, typename MT2, bool SO2 >
80 inline size_t syevx( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
81  DenseMatrix<MT2,SO2>& Z, char uplo );
82 
83 template< typename MT1, bool SO1, typename VT, bool TF, typename MT2, bool SO2, typename ST >
84 inline size_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 //*************************************************************************************************
113 template< 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
118 inline size_t syevx_backend( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w,
119  char uplo, char range, ST vl, ST vu, int il, int iu )
120 {
121  BLAZE_INTERNAL_ASSERT( isSquare( ~A ), "Invalid non-square matrix detected" );
122  BLAZE_INTERNAL_ASSERT( range != 'A' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
123  BLAZE_INTERNAL_ASSERT( range != 'V' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
124  BLAZE_INTERNAL_ASSERT( range != 'I' || (~w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
125 
126  using ET = ElementType_t<MT>;
127 
129 
130  int n ( numeric_cast<int>( (~A).rows() ) );
131  int lda ( numeric_cast<int>( (~A).spacing() ) );
132  int m ( 0 );
133  int info( 0 );
134 
135  int lwork( 12*n + 2 );
136  const std::unique_ptr<ET[]> work ( new ET[lwork] );
137  const std::unique_ptr<int[]> iwork( new int[12*n] );
138  const std::unique_ptr<int[]> ifail( new int[n] );
139 
140  syevx( 'N', range, uplo, n, (~A).data(), lda, vl, vu, il, iu, ET(0), &m,
141  (~w).data(), nullptr, 1, work.get(), lwork, iwork.get(), ifail.get(), &info );
142 
143  const size_t num( numeric_cast<size_t>( m ) );
144 
145  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
146  BLAZE_INTERNAL_ASSERT( num <= (~w).size(), "Invalid number of eigenvalues detected" );
147 
148  if( info > 0 ) {
149  BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
150  }
151 
152  return num;
153 }
155 //*************************************************************************************************
156 
157 
158 //*************************************************************************************************
214 template< typename MT // Type of the matrix A
215  , bool SO // Storage order of the matrix A
216  , typename VT // Type of the vector w
217  , bool TF > // Transpose flag of the vector w
218 inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo )
219 {
226 
232 
233  using ET = ElementType_t<MT>;
234 
235  const size_t N( (~A).rows() );
236 
237  if( !isSquare( ~A ) ) {
238  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
239  }
240 
241  if( uplo != 'L' && uplo != 'U' ) {
242  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
243  }
244 
245  resize( ~w, N, false );
246 
247  if( N == 0UL ) {
248  return 0;
249  }
250 
251  return syevx_backend( ~A, ~w, uplo, 'A', ET(), ET(), 0, 0 );
252 }
253 //*************************************************************************************************
254 
255 
256 //*************************************************************************************************
342 template< typename MT // Type of the matrix A
343  , bool SO // Storage order of the matrix A
344  , typename VT // Type of the vector w
345  , bool TF // Transpose flag of the vector w
346  , typename ST > // Type of the scalar boundary values
347 inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo, ST low, ST upp )
348 {
355 
361 
362  if( !isSquare( ~A ) ) {
363  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
364  }
365 
366  if( uplo != 'L' && uplo != 'U' ) {
367  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
368  }
369 
370  if( IsFloatingPoint_v<ST> && low >= upp ) {
371  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
372  }
373 
374  if( !IsFloatingPoint_v<ST> && low > upp ) {
375  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
376  }
377 
378  const size_t N( (~A).rows() );
379  const size_t num( IsFloatingPoint_v<ST> ? N : size_t( upp - low ) + 1UL );
380 
381  if( !IsFloatingPoint_v<ST> && num > N ) {
382  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
383  }
384 
385  resize( ~w, num, false );
386 
387  if( N == 0UL ) {
388  return 0;
389  }
390 
391  const char range( IsFloatingPoint_v<ST> ? 'V' : 'I' );
392  const ST vl ( IsFloatingPoint_v<ST> ? low : ST() );
393  const ST vu ( IsFloatingPoint_v<ST> ? upp : ST() );
394  const int il ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<int>( low ) );
395  const int iu ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<int>( upp ) );
396 
397  return syevx_backend( ~A, ~w, uplo, range, vl, vu, il, iu );
398 }
399 //*************************************************************************************************
400 
401 
402 //*************************************************************************************************
426 template< typename MT1 // Type of the matrix A
427  , bool SO1 // Storage order of the matrix A
428  , typename VT // Type of the vector w
429  , bool TF // Transpose flag of the vector w
430  , typename MT2 // Type of the matrix Z
431  , bool SO2 // Storage order of the matrix Z
432  , typename ST > // Type of the scalar boundary values
433 inline size_t syevx_backend( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
434  DenseMatrix<MT2,SO2>& Z, char uplo, char range,
435  ST vl, ST vu, int il, int iu )
436 {
437  BLAZE_INTERNAL_ASSERT( isSquare( ~A ), "Invalid non-square matrix detected" );
438  BLAZE_INTERNAL_ASSERT( range != 'A' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
439  BLAZE_INTERNAL_ASSERT( range != 'V' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
440  BLAZE_INTERNAL_ASSERT( range != 'I' || (~w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
441  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).rows() == (~w).size(), "Invalid matrix dimension detected" );
442  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).columns() == (~A).rows(), "Invalid matrix dimension detected" );
443  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).rows() == (~A).rows(), "Invalid matrix dimension detected" );
444  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).columns() == (~w).size(), "Invalid matrix dimension detected" );
445 
446  using ET = ElementType_t<MT1>;
447 
449 
450  int n ( numeric_cast<int>( (~A).rows() ) );
451  int lda ( numeric_cast<int>( (~A).spacing() ) );
452  int m ( 0 );
453  int ldz ( numeric_cast<int>( (~Z).spacing() ) );
454  int info( 0 );
455 
456  int lwork( 12*n + 2 );
457  const std::unique_ptr<ET[]> work ( new ET[lwork] );
458  const std::unique_ptr<int[]> iwork( new int[12*n] );
459  const std::unique_ptr<int[]> ifail( new int[n] );
460 
461  syevx( 'N', range, uplo, n, (~A).data(), lda, vl, vu, il, iu, ET(0), &m,
462  (~w).data(), (~Z).data(), ldz, work.get(), lwork, iwork.get(), ifail.get(), &info );
463 
464  const size_t num( numeric_cast<size_t>( m ) );
465 
466  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
467  BLAZE_INTERNAL_ASSERT( num <= (~w).size(), "Invalid number of eigenvalues detected" );
468 
469  if( info > 0 ) {
470  BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
471  }
472 
473  return num;
474 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
542 template< typename MT1 // Type of the matrix A
543  , bool SO1 // Storage order of the matrix A
544  , typename VT // Type of the vector w
545  , bool TF // Transpose flag of the vector w
546  , typename MT2 // Type of the matrix Z
547  , bool SO2 > // Storage order of the matrix Z
549  DenseMatrix<MT2,SO2>& Z, char uplo )
550 {
557 
563 
570 
571  using ET = ElementType_t<MT1>;
572 
573  const size_t N( (~A).rows() );
574 
575  if( !isSquare( ~A ) ) {
576  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
577  }
578 
579  if( uplo != 'L' && uplo != 'U' ) {
580  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
581  }
582 
583  resize( ~w, N, false );
584  resize( ~Z, N, N, false );
585 
586  if( N == 0UL ) {
587  return 0;
588  }
589 
590  return syevx_backend( ~A, ~w, ~Z, uplo, 'A', ET(), ET(), 0, 0 );
591 }
592 //*************************************************************************************************
593 
594 
595 //*************************************************************************************************
691 template< typename MT1 // Type of the matrix A
692  , bool SO1 // Storage order of the matrix A
693  , typename VT // Type of the vector w
694  , bool TF // Transpose flag of the vector w
695  , typename MT2 // Type of the matrix Z
696  , bool SO2 // Storage order of the matrix Z
697  , typename ST > // Type of the scalar boundary values
699  DenseMatrix<MT2,SO2>& Z, char uplo, ST low, ST upp )
700 {
707 
713 
720 
721  if( !isSquare( ~A ) ) {
722  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
723  }
724 
725  if( uplo != 'L' && uplo != 'U' ) {
726  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
727  }
728 
729  if( IsFloatingPoint_v<ST> && low >= upp ) {
730  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
731  }
732 
733  if( !IsFloatingPoint_v<ST> && low > upp ) {
734  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
735  }
736 
737  const size_t N( (~A).rows() );
738  const size_t num( IsFloatingPoint_v<ST> ? N : size_t( upp - low ) + 1UL );
739 
740  if( !IsFloatingPoint_v<ST> && num > N ) {
741  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
742  }
743 
744  resize( ~w, num, false );
745  resize( ~Z, ( IsRowMajorMatrix_v<MT2> ? num : N ),
746  ( IsRowMajorMatrix_v<MT2> ? N : num ), false );
747 
748  if( N == 0UL ) {
749  return 0;
750  }
751 
752  const char range( IsFloatingPoint_v<ST> ? 'V' : 'I' );
753  const ST vl ( IsFloatingPoint_v<ST> ? low : ST() );
754  const ST vu ( IsFloatingPoint_v<ST> ? upp : ST() );
755  const int il ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<int>( low ) );
756  const int iu ( IsFloatingPoint_v<ST> ? 0 : numeric_cast<int>( upp ) );
757 
758  return syevx_backend( ~A, ~w, ~Z, uplo, range, vl, vu, il, iu );
759 }
760 //*************************************************************************************************
761 
762 } // namespace blaze
763 
764 #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
Constraint on the data type.
Header file for auxiliary alias declarations.
#define BLAZE_CONSTRAINT_MUST_HAVE_MUTABLE_DATA_ACCESS(T)
Constraint on the data type.In case the given data type T does not provide low-level data access to m...
Definition: MutableDataAccess.h:61
Header file for basic type definitions.
MT::ElementType * data(DenseMatrix< MT, SO > &dm) noexcept
Low-level data access to the dense matrix elements.
Definition: DenseMatrix.h:169
#define BLAZE_CONSTRAINT_MUST_NOT_BE_COMPUTATION_TYPE(T)
Constraint on the data type.In case the given data type T is a computational expression (i...
Definition: Computation.h:81
Header file for the DenseVector base class.
Constraint on the data type.
Constraint on the data type.
Cast operators for numeric types.
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
#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:80
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
size_t spacing(const DenseMatrix< MT, SO > &dm) noexcept
Returns the spacing between the beginning of two rows/columns.
Definition: DenseMatrix.h:252
Constraint on the data type.
void syevx(char jobz, char range, char uplo, int n, float *A, int lda, float vl, float vu, int il, int iu, float abstol, int *m, float *w, float *Z, int ldz, float *work, int lwork, int *iwork, int *ifail, int *info)
LAPACK kernel for computing the eigenvalues of the given dense symmetric single precision column-majo...
Definition: syevx.h:148
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the IsFloatingPoint type trait.
#define BLAZE_CONSTRAINT_MUST_BE_CONTIGUOUS_TYPE(T)
Constraint on the data type.In case the given data type T is not an array-like data type with contigu...
Definition: Contiguous.h:61
Header file for the DenseMatrix base class.
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
Header file for the CLAPACK syevx wrapper functions.
Header file for the exception macros of the math module.
void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:738
Constraint on the data type.
Header file for run time assertion macros.
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
#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
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
#define BLAZE_CONSTRAINT_MUST_BE_BUILTIN_TYPE(T)
Constraint on the data type.In case the given data type T is not a built-in data type, a compilation error is created.
Definition: Builtin.h:60
Header file for the IsRowMajorMatrix type trait.
#define BLAZE_THROW_LAPACK_ERROR(MESSAGE)
Macro for the emission of an exception on detection of a LAPACK error.This macro encapsulates the def...
Definition: Exception.h:146
bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:951
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Constraint on the data type.