heevx.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_LAPACK_HEEVX_H_
36 #define _BLAZE_MATH_LAPACK_HEEVX_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>
59 #include <blaze/util/Types.h>
61 
62 
63 namespace blaze {
64 
65 //=================================================================================================
66 //
67 // LAPACK HERMITIAN MATRIX EIGENVALUE FUNCTIONS (HEEVX)
68 //
69 //=================================================================================================
70 
71 //*************************************************************************************************
74 template< typename MT, bool SO, typename VT, bool TF >
75 inline size_t heevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo );
76 
77 template< typename MT, bool SO, typename VT, bool TF, typename ST >
78 inline size_t heevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo, ST low, ST upp );
79 
80 template< typename MT1, bool SO1, typename VT, bool TF, typename MT2, bool SO2 >
81 inline size_t heevx( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
82  DenseMatrix<MT2,SO2>& Z, char uplo );
83 
84 template< typename MT1, bool SO1, typename VT, bool TF, typename MT2, bool SO2, typename ST >
85 inline size_t heevx( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
86  DenseMatrix<MT2,SO2>& Z, char uplo, ST low, ST upp );
88 //*************************************************************************************************
89 
90 
91 //*************************************************************************************************
114 template< typename MT // Type of the matrix A
115  , bool SO // Storage order of the matrix A
116  , typename VT // Type of the vector w
117  , bool TF // Transpose flag of the vector w
118  , typename ST > // Type of the scalar boundary values
119 inline size_t heevx_backend( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w,
120  char uplo, char range, ST vl, ST vu, int il, int iu )
121 {
122  using boost::numeric_cast;
123 
124  BLAZE_INTERNAL_ASSERT( isSquare( ~A ), "Invalid non-square matrix detected" );
125  BLAZE_INTERNAL_ASSERT( range != 'A' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
126  BLAZE_INTERNAL_ASSERT( range != 'V' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
127  BLAZE_INTERNAL_ASSERT( range != 'I' || (~w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
128 
129  using CT = ElementType_<MT>;
130  using BT = UnderlyingElement_<CT>;
131 
134 
135  int n ( numeric_cast<int>( (~A).rows() ) );
136  int lda ( numeric_cast<int>( (~A).spacing() ) );
137  int m ( 0 );
138  int info( 0 );
139 
140  int lwork( 12*n + 2 );
141  const std::unique_ptr<CT[]> work ( new CT[lwork] );
142  const std::unique_ptr<BT[]> rwork( new BT[7*n] );
143  const std::unique_ptr<int[]> iwork( new int[5*n] );
144  const std::unique_ptr<int[]> ifail( new int[n] );
145 
146  heevx( 'N', range, uplo, n, (~A).data(), lda, vl, vu, il, iu, BT(0), &m, (~w).data(),
147  nullptr, 1, work.get(), lwork, rwork.get(), iwork.get(), ifail.get(), &info );
148 
149  const size_t num( numeric_cast<size_t>( m ) );
150 
151  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
152  BLAZE_INTERNAL_ASSERT( num <= (~w).size(), "Invalid number of eigenvalues detected" );
153 
154  if( info > 0 ) {
155  BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
156  }
157 
158  return num;
159 }
161 //*************************************************************************************************
162 
163 
164 //*************************************************************************************************
220 template< typename MT // Type of the matrix A
221  , bool SO // Storage order of the matrix A
222  , typename VT // Type of the vector w
223  , bool TF > // Transpose flag of the vector w
224 inline size_t heevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo )
225 {
231 
236 
237  using CT = ElementType_<MT>;
238  using BT = UnderlyingElement_<CT>;
239 
240  const size_t N( (~A).rows() );
241 
242  if( !isSquare( ~A ) ) {
243  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
244  }
245 
246  if( uplo != 'L' && uplo != 'U' ) {
247  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
248  }
249 
250  resize( ~w, N, false );
251 
252  if( N == 0UL ) {
253  return 0;
254  }
255 
256  return heevx_backend( ~A, ~w, uplo, 'A', BT(), BT(), 0, 0 );
257 }
258 //*************************************************************************************************
259 
260 
261 //*************************************************************************************************
347 template< typename MT // Type of the matrix A
348  , bool SO // Storage order of the matrix A
349  , typename VT // Type of the vector w
350  , bool TF // Transpose flag of the vector w
351  , typename ST > // Type of the scalar boundary values
352 inline size_t heevx( DenseMatrix<MT,SO>& A, DenseVector<VT,TF>& w, char uplo, ST low, ST upp )
353 {
354  using boost::numeric_cast;
355 
361 
366 
367  if( !isSquare( ~A ) ) {
368  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
369  }
370 
371  if( IsFloatingPoint<ST>::value && low >= upp ) {
372  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
373  }
374 
375  if( !IsFloatingPoint<ST>::value && 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<ST>::value ? N : size_t( upp - low ) + 1UL );
381 
382  if( !IsFloatingPoint<ST>::value && num > N ) {
383  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
384  }
385 
386  if( uplo != 'L' && uplo != 'U' ) {
387  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
388  }
389 
390  resize( ~w, num, false );
391 
392  if( N == 0UL ) {
393  return 0;
394  }
395 
396  const char range( IsFloatingPoint<ST>::value ? 'V' : 'I' );
397  const ST vl ( IsFloatingPoint<ST>::value ? low : ST() );
398  const ST vu ( IsFloatingPoint<ST>::value ? upp : ST() );
399  const int il ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( low ) );
400  const int iu ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( upp ) );
401 
402  return heevx_backend( ~A, ~w, uplo, range, vl, vu, il, iu );
403 }
404 //*************************************************************************************************
405 
406 
407 //*************************************************************************************************
431 template< typename MT1 // Type of the matrix A
432  , bool SO1 // Storage order of the matrix A
433  , typename VT // Type of the vector w
434  , bool TF // Transpose flag of the vector w
435  , typename MT2 // Type of the matrix Z
436  , bool SO2 // Storage order of the matrix Z
437  , typename ST > // Type of the scalar boundary values
438 inline size_t heevx_backend( DenseMatrix<MT1,SO1>& A, DenseVector<VT,TF>& w,
439  DenseMatrix<MT2,SO2>& Z, char uplo, char range,
440  ST vl, ST vu, int il, int iu )
441 {
442  using boost::numeric_cast;
443 
444  BLAZE_INTERNAL_ASSERT( isSquare( ~A ), "Invalid non-square matrix detected" );
445  BLAZE_INTERNAL_ASSERT( range != 'A' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
446  BLAZE_INTERNAL_ASSERT( range != 'V' || (~w).size() == (~A).rows(), "Invalid vector dimension detected" );
447  BLAZE_INTERNAL_ASSERT( range != 'I' || (~w).size() == size_t( iu-il+1 ), "Invalid vector dimension detected" );
448  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).rows() == (~w).size(), "Invalid matrix dimension detected" );
449  BLAZE_INTERNAL_ASSERT( SO2 || (~Z).columns() == (~A).rows(), "Invalid matrix dimension detected" );
450  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).rows() == (~A).rows(), "Invalid matrix dimension detected" );
451  BLAZE_INTERNAL_ASSERT( !SO2 || (~Z).columns() == (~w).size(), "Invalid matrix dimension detected" );
452 
453  using CT = ElementType_<MT1>;
454  using BT = UnderlyingElement_<CT>;
455 
458 
459  int n ( numeric_cast<int>( (~A).rows() ) );
460  int lda ( numeric_cast<int>( (~A).spacing() ) );
461  int m ( 0 );
462  int ldz ( numeric_cast<int>( (~Z).spacing() ) );
463  int info( 0 );
464 
465  int lwork( 12*n + 2 );
466  const std::unique_ptr<CT[]> work ( new CT[lwork] );
467  const std::unique_ptr<BT[]> rwork( new BT[7*n] );
468  const std::unique_ptr<int[]> iwork( new int[5*n] );
469  const std::unique_ptr<int[]> ifail( new int[n] );
470 
471  heevx( 'N', range, uplo, n, (~A).data(), lda, vl, vu, il, iu, BT(0), &m, (~w).data(),
472  (~Z).data(), ldz, work.get(), lwork, rwork.get(), iwork.get(), ifail.get(), &info );
473 
474  const size_t num( numeric_cast<size_t>( m ) );
475 
476  BLAZE_INTERNAL_ASSERT( info >= 0, "Invalid argument for eigenvalue computation" );
477  BLAZE_INTERNAL_ASSERT( num <= (~w).size(), "Invalid number of eigenvalues detected" );
478 
479  if( info > 0 ) {
480  BLAZE_THROW_LAPACK_ERROR( "Eigenvalue computation failed" );
481  }
482 
483  return num;
484 }
486 //*************************************************************************************************
487 
488 
489 //*************************************************************************************************
552 template< typename MT1 // Type of the matrix A
553  , bool SO1 // Storage order of the matrix A
554  , typename VT // Type of the vector w
555  , bool TF // Transpose flag of the vector w
556  , typename MT2 // Type of the matrix Z
557  , bool SO2 > // Storage order of the matrix Z
559  DenseMatrix<MT2,SO2>& Z, char uplo )
560 {
566 
571 
577 
578  using CT = ElementType_<MT1>;
579  using BT = UnderlyingElement_<CT>;
580 
581  const size_t N( (~A).rows() );
582 
583  if( !isSquare( ~A ) ) {
584  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
585  }
586 
587  if( uplo != 'L' && uplo != 'U' ) {
588  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
589  }
590 
591  resize( ~w, N, false );
592  resize( ~Z, N, N, false );
593 
594  if( N == 0UL ) {
595  return 0;
596  }
597 
598  return heevx_backend( ~A, ~w, ~Z, uplo, 'A', BT(), BT(), 0, 0 );
599 }
600 //*************************************************************************************************
601 
602 
603 //*************************************************************************************************
699 template< typename MT1 // Type of the matrix A
700  , bool SO1 // Storage order of the matrix A
701  , typename VT // Type of the vector w
702  , bool TF // Transpose flag of the vector w
703  , typename MT2 // Type of the matrix Z
704  , bool SO2 // Storage order of the matrix Z
705  , typename ST > // Type of the scalar boundary values
707  DenseMatrix<MT2,SO2>& Z, char uplo, ST low, ST upp )
708 {
709  using boost::numeric_cast;
710 
716 
721 
727 
728  if( !isSquare( ~A ) ) {
729  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
730  }
731 
732  if( IsFloatingPoint<ST>::value && low >= upp ) {
733  BLAZE_THROW_INVALID_ARGUMENT( "Invalid value range provided" );
734  }
735 
736  if( !IsFloatingPoint<ST>::value && low > upp ) {
737  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
738  }
739 
740  const size_t N( (~A).rows() );
741  const size_t num( IsFloatingPoint<ST>::value ? N : size_t( upp - low ) + 1UL );
742 
743  if( !IsFloatingPoint<ST>::value && num > N ) {
744  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range provided" );
745  }
746 
747  if( uplo != 'L' && uplo != 'U' ) {
748  BLAZE_THROW_INVALID_ARGUMENT( "Invalid uplo argument provided" );
749  }
750 
751  resize( ~w, num, false );
752  resize( ~Z, ( IsRowMajorMatrix<MT2>::value ? num : N ),
753  ( IsRowMajorMatrix<MT2>::value ? N : num ), false );
754 
755  if( N == 0UL ) {
756  return 0;
757  }
758 
759  const char range( IsFloatingPoint<ST>::value ? 'V' : 'I' );
760  const ST vl ( IsFloatingPoint<ST>::value ? low : ST() );
761  const ST vu ( IsFloatingPoint<ST>::value ? upp : ST() );
762  const int il ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( low ) );
763  const int iu ( IsFloatingPoint<ST>::value ? 0 : numeric_cast<int>( upp ) );
764 
765  return heevx_backend( ~A, ~w, ~Z, uplo, range, vl, vu, il, iu );
766 }
767 //*************************************************************************************************
768 
769 } // namespace blaze
770 
771 #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
Constraint on the data type.
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 heevx(char jobz, char range, char uplo, int n, complex< float > *A, int lda, float vl, float vu, int il, int iu, float abstol, int *m, float *w, complex< float > *Z, int ldz, complex< float > *work, int lwork, float *rwork, int *iwork, int *ifail, int *info)
LAPACK kernel for computing the eigenvalues of the given dense Hermitian single precision complex col...
Definition: heevx.h:149
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 UnderlyingElement< T >::Type UnderlyingElement_
Auxiliary alias declaration for the UnderlyingElement type trait.The UnderlyingElement_ alias declara...
Definition: UnderlyingElement.h:133
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
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 CLAPACK heevx wrapper functions.
#define BLAZE_CONSTRAINT_MUST_BE_COMPLEX_TYPE(T)
Constraint on the data type.This compile time constraint checks that the given data type T is a compl...
Definition: Complex.h:62
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.