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 <boost/cast.hpp>
45 #include <blaze/math/Aliases.h>
50 #include <blaze/math/Exception.h>
56 #include <blaze/util/Assert.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  using boost::numeric_cast;
122 
123  BLAZE_INTERNAL_ASSERT( isSquare( ~A ), "Invalid non-square matrix detected" );
124  BLAZE_INTERNAL_ASSERT( range != 'A' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
125  BLAZE_INTERNAL_ASSERT( range != 'V' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
126  BLAZE_INTERNAL_ASSERT( range != 'I' || (~w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
127 
128  using ET = ElementType_<MT>;
129 
131 
132  int n ( numeric_cast<int>( (~A).rows() ) );
133  int lda ( numeric_cast<int>( (~A).spacing() ) );
134  int m ( 0 );
135  int info( 0 );
136 
137  int lwork( 12*n + 2 );
138  const std::unique_ptr<ET[]> work ( new ET[lwork] );
139  const std::unique_ptr<int[]> iwork( new int[12*n] );
140  const std::unique_ptr<int[]> ifail( new int[n] );
141 
142  syevx( 'N', range, uplo, n, (~A).data(), lda, vl, vu, il, iu, ET(0), &m,
143  (~w).data(), nullptr, 1, work.get(), lwork, iwork.get(), ifail.get(), &info );
144 
145  const size_t num( numeric_cast<size_t>( m ) );
146 
147  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
148  BLAZE_INTERNAL_ASSERT( num <= (~w).size(), "Invalid number of eigenvalues detected" );
149 
150  if( info > 0 ) {
151  BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
152  }
153 
154  return num;
155 }
157 //*************************************************************************************************
158 
159 
160 //*************************************************************************************************
216 template< typename MT // Type of the matrix A
217  , bool SO // Storage order of the matrix A
218  , typename VT // Type of the vector w
219  , bool TF > // Transpose flag of the vector w
220 inline size_t syevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo )
221 {
227 
232 
233  using ET = ElementType_<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 {
349  using boost::numeric_cast;
350 
356 
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<ST>::value && low >= upp ) {
371  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
372  }
373 
374  if( !IsFloatingPoint<ST>::value && 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<ST>::value ? N : size_t( upp - low ) + 1UL );
380 
381  if( !IsFloatingPoint<ST>::value && 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<ST>::value ? 'V' : 'I' );
392  const ST vl ( IsFloatingPoint<ST>::value ? low : ST() );
393  const ST vu ( IsFloatingPoint<ST>::value ? upp : ST() );
394  const int il ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( low ) );
395  const int iu ( IsFloatingPoint<ST>::value ? 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  using boost::numeric_cast;
438 
439  BLAZE_INTERNAL_ASSERT( isSquare( ~A ), "Invalid non-square matrix detected" );
440  BLAZE_INTERNAL_ASSERT( range != 'A' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
441  BLAZE_INTERNAL_ASSERT( range != 'V' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
442  BLAZE_INTERNAL_ASSERT( range != 'I' || (~w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
443  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).rows() == (~w).size(), "Invalid matrix dimension detected" );
444  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).columns() == (~A).rows(), "Invalid matrix dimension detected" );
445  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).rows() == (~A).rows(), "Invalid matrix dimension detected" );
446  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).columns() == (~w).size(), "Invalid matrix dimension detected" );
447 
448  using ET = ElementType_<MT1>;
449 
451 
452  int n ( numeric_cast<int>( (~A).rows() ) );
453  int lda ( numeric_cast<int>( (~A).spacing() ) );
454  int m ( 0 );
455  int ldz ( numeric_cast<int>( (~Z).spacing() ) );
456  int info( 0 );
457 
458  int lwork( 12*n + 2 );
459  const std::unique_ptr<ET[]> work ( new ET[lwork] );
460  const std::unique_ptr<int[]> iwork( new int[12*n] );
461  const std::unique_ptr<int[]> ifail( new int[n] );
462 
463  syevx( 'N', range, uplo, n, (~A).data(), lda, vl, vu, il, iu, ET(0), &m,
464  (~w).data(), (~Z).data(), ldz, work.get(), lwork, iwork.get(), ifail.get(), &info );
465 
466  const size_t num( numeric_cast<size_t>( m ) );
467 
468  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
469  BLAZE_INTERNAL_ASSERT( num <= (~w).size(), "Invalid number of eigenvalues detected" );
470 
471  if( info > 0 ) {
472  BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
473  }
474 
475  return num;
476 }
478 //*************************************************************************************************
479 
480 
481 //*************************************************************************************************
544 template< typename MT1 // Type of the matrix A
545  , bool SO1 // Storage order of the matrix A
546  , typename VT // Type of the vector w
547  , bool TF // Transpose flag of the vector w
548  , typename MT2 // Type of the matrix Z
549  , bool SO2 > // Storage order of the matrix Z
551  DenseMatrix<MT2,SO2>& Z, char uplo )
552 {
558 
563 
569 
570  using ET = ElementType_<MT1>;
571 
572  const size_t N( (~A).rows() );
573 
574  if( !isSquare( ~A ) ) {
575  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
576  }
577 
578  if( uplo != 'L' && uplo != 'U' ) {
579  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
580  }
581 
582  resize( ~w, N, false );
583  resize( ~Z, N, N, false );
584 
585  if( N == 0UL ) {
586  return 0;
587  }
588 
589  return syevx_backend( ~A, ~w, ~Z, uplo, 'A', ET(), ET(), 0, 0 );
590 }
591 //*************************************************************************************************
592 
593 
594 //*************************************************************************************************
690 template< typename MT1 // Type of the matrix A
691  , bool SO1 // Storage order of the matrix A
692  , typename VT // Type of the vector w
693  , bool TF // Transpose flag of the vector w
694  , typename MT2 // Type of the matrix Z
695  , bool SO2 // Storage order of the matrix Z
696  , typename ST > // Type of the scalar boundary values
698  DenseMatrix<MT2,SO2>& Z, char uplo, ST low, ST upp )
699 {
700  using boost::numeric_cast;
701 
707 
712 
718 
719  if( !isSquare( ~A ) ) {
720  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
721  }
722 
723  if( uplo != 'L' && uplo != 'U' ) {
724  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
725  }
726 
727  if( IsFloatingPoint<ST>::value && low >= upp ) {
728  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
729  }
730 
731  if( !IsFloatingPoint<ST>::value && low > upp ) {
732  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
733  }
734 
735  const size_t N( (~A).rows() );
736  const size_t num( IsFloatingPoint<ST>::value ? N : size_t( upp - low ) + 1UL );
737 
738  if( !IsFloatingPoint<ST>::value && num > N ) {
739  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
740  }
741 
742  resize( ~w, num, false );
743  resize( ~Z, ( IsRowMajorMatrix<MT2>::value ? num : N ),
744  ( IsRowMajorMatrix<MT2>::value ? N : num ), false );
745 
746  if( N == 0UL ) {
747  return 0;
748  }
749 
750  const char range( IsFloatingPoint<ST>::value ? 'V' : 'I' );
751  const ST vl ( IsFloatingPoint<ST>::value ? low : ST() );
752  const ST vu ( IsFloatingPoint<ST>::value ? upp : ST() );
753  const int il ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( low ) );
754  const int iu ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( upp ) );
755 
756  return syevx_backend( ~A, ~w, ~Z, uplo, range, vl, vu, il, iu );
757 }
758 //*************************************************************************************************
759 
760 } // namespace blaze
761 
762 #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.
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:102
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:261
#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.
#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:71
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:146
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:83
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:336
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:544
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:320
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:677
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.