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>
49 #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_<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 {
225 
230 
231  using ET = ElementType_<MT>;
232 
233  const size_t N( (~A).rows() );
234 
235  if( !isSquare( ~A ) ) {
236  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
237  }
238 
239  if( uplo != 'L' && uplo != 'U' ) {
240  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
241  }
242 
243  resize( ~w, N, false );
244 
245  if( N == 0UL ) {
246  return 0;
247  }
248 
249  return syevx_backend( ~A, ~w, uplo, 'A', ET(), ET(), 0, 0 );
250 }
251 //*************************************************************************************************
252 
253 
254 //*************************************************************************************************
340 template< typename MT // Type of the matrix A
341  , bool SO // Storage order of the matrix A
342  , typename VT // Type of the vector w
343  , bool TF // Transpose flag of the vector w
344  , typename ST > // Type of the scalar boundary values
345 inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo, ST low, ST upp )
346 {
352 
357 
358  if( !isSquare( ~A ) ) {
359  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
360  }
361 
362  if( uplo != 'L' && uplo != 'U' ) {
363  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
364  }
365 
366  if( IsFloatingPoint<ST>::value && low >= upp ) {
367  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
368  }
369 
370  if( !IsFloatingPoint<ST>::value && low > upp ) {
371  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
372  }
373 
374  const size_t N( (~A).rows() );
375  const size_t num( IsFloatingPoint<ST>::value ? N : size_t( upp - low ) + 1UL );
376 
377  if( !IsFloatingPoint<ST>::value && num > N ) {
378  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
379  }
380 
381  resize( ~w, num, false );
382 
383  if( N == 0UL ) {
384  return 0;
385  }
386 
387  const char range( IsFloatingPoint<ST>::value ? 'V' : 'I' );
388  const ST vl ( IsFloatingPoint<ST>::value ? low : ST() );
389  const ST vu ( IsFloatingPoint<ST>::value ? upp : ST() );
390  const int il ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( low ) );
391  const int iu ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( upp ) );
392 
393  return syevx_backend( ~A, ~w, uplo, range, vl, vu, il, iu );
394 }
395 //*************************************************************************************************
396 
397 
398 //*************************************************************************************************
422 template< typename MT1 // Type of the matrix A
423  , bool SO1 // Storage order of the matrix A
424  , typename VT // Type of the vector w
425  , bool TF // Transpose flag of the vector w
426  , typename MT2 // Type of the matrix Z
427  , bool SO2 // Storage order of the matrix Z
428  , typename ST > // Type of the scalar boundary values
429 inline size_t syevx_backend( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
430  DenseMatrix<MT2,SO2>& Z, char uplo, char range,
431  ST vl, ST vu, int il, int iu )
432 {
433  BLAZE_INTERNAL_ASSERT( isSquare( ~A ), "Invalid non-square matrix detected" );
434  BLAZE_INTERNAL_ASSERT( range != 'A' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
435  BLAZE_INTERNAL_ASSERT( range != 'V' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
436  BLAZE_INTERNAL_ASSERT( range != 'I' || (~w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
437  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).rows() == (~w).size(), "Invalid matrix dimension detected" );
438  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).columns() == (~A).rows(), "Invalid matrix dimension detected" );
439  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).rows() == (~A).rows(), "Invalid matrix dimension detected" );
440  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).columns() == (~w).size(), "Invalid matrix dimension detected" );
441 
442  using ET = ElementType_<MT1>;
443 
445 
446  int n ( numeric_cast<int>( (~A).rows() ) );
447  int lda ( numeric_cast<int>( (~A).spacing() ) );
448  int m ( 0 );
449  int ldz ( numeric_cast<int>( (~Z).spacing() ) );
450  int info( 0 );
451 
452  int lwork( 12*n + 2 );
453  const std::unique_ptr<ET[]> work ( new ET[lwork] );
454  const std::unique_ptr<int[]> iwork( new int[12*n] );
455  const std::unique_ptr<int[]> ifail( new int[n] );
456 
457  syevx( 'N', range, uplo, n, (~A).data(), lda, vl, vu, il, iu, ET(0), &m,
458  (~w).data(), (~Z).data(), ldz, work.get(), lwork, iwork.get(), ifail.get(), &info );
459 
460  const size_t num( numeric_cast<size_t>( m ) );
461 
462  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
463  BLAZE_INTERNAL_ASSERT( num <= (~w).size(), "Invalid number of eigenvalues detected" );
464 
465  if( info > 0 ) {
466  BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
467  }
468 
469  return num;
470 }
472 //*************************************************************************************************
473 
474 
475 //*************************************************************************************************
538 template< typename MT1 // Type of the matrix A
539  , bool SO1 // Storage order of the matrix A
540  , typename VT // Type of the vector w
541  , bool TF // Transpose flag of the vector w
542  , typename MT2 // Type of the matrix Z
543  , bool SO2 > // Storage order of the matrix Z
545  DenseMatrix<MT2,SO2>& Z, char uplo )
546 {
552 
557 
563 
564  using ET = ElementType_<MT1>;
565 
566  const size_t N( (~A).rows() );
567 
568  if( !isSquare( ~A ) ) {
569  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
570  }
571 
572  if( uplo != 'L' && uplo != 'U' ) {
573  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
574  }
575 
576  resize( ~w, N, false );
577  resize( ~Z, N, N, false );
578 
579  if( N == 0UL ) {
580  return 0;
581  }
582 
583  return syevx_backend( ~A, ~w, ~Z, uplo, 'A', ET(), ET(), 0, 0 );
584 }
585 //*************************************************************************************************
586 
587 
588 //*************************************************************************************************
684 template< typename MT1 // Type of the matrix A
685  , bool SO1 // Storage order of the matrix A
686  , typename VT // Type of the vector w
687  , bool TF // Transpose flag of the vector w
688  , typename MT2 // Type of the matrix Z
689  , bool SO2 // Storage order of the matrix Z
690  , typename ST > // Type of the scalar boundary values
692  DenseMatrix<MT2,SO2>& Z, char uplo, ST low, ST upp )
693 {
699 
704 
710 
711  if( !isSquare( ~A ) ) {
712  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
713  }
714 
715  if( uplo != 'L' && uplo != 'U' ) {
716  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
717  }
718 
719  if( IsFloatingPoint<ST>::value && low >= upp ) {
720  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
721  }
722 
723  if( !IsFloatingPoint<ST>::value && low > upp ) {
724  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
725  }
726 
727  const size_t N( (~A).rows() );
728  const size_t num( IsFloatingPoint<ST>::value ? N : size_t( upp - low ) + 1UL );
729 
730  if( !IsFloatingPoint<ST>::value && num > N ) {
731  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
732  }
733 
734  resize( ~w, num, false );
735  resize( ~Z, ( IsRowMajorMatrix<MT2>::value ? num : N ),
736  ( IsRowMajorMatrix<MT2>::value ? N : num ), false );
737 
738  if( N == 0UL ) {
739  return 0;
740  }
741 
742  const char range( IsFloatingPoint<ST>::value ? 'V' : 'I' );
743  const ST vl ( IsFloatingPoint<ST>::value ? low : ST() );
744  const ST vu ( IsFloatingPoint<ST>::value ? upp : ST() );
745  const int il ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( low ) );
746  const int iu ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( upp ) );
747 
748  return syevx_backend( ~A, ~w, ~Z, uplo, range, vl, vu, il, iu );
749 }
750 //*************************************************************************************************
751 
752 } // namespace blaze
753 
754 #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.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
#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.
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
BLAZE_ALWAYS_INLINE size_t spacing(const DenseMatrix< MT, SO > &dm) noexcept
Returns the spacing between the beginning of two rows/columns.
Definition: DenseMatrix.h:110
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:57
Compile time check for row-major matrix types.This type trait tests whether or not the given template...
Definition: IsRowMajorMatrix.h:110
Header file for the IsFloatingPoint type trait.
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
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
Header file for the CLAPACK syevx wrapper functions.
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.
Compile time check for floating point data types.This type trait tests whether or not the given templ...
Definition: IsFloatingPoint.h:75
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.
Header file for run time assertion macros.
#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
#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
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
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
BLAZE_ALWAYS_INLINE bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:742
Header file for the IsResizable type trait.
#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.