HermitianMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_HERMITIANMATRIX_H_
36 #define _BLAZE_MATH_HERMITIANMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <stdexcept>
45 #include <blaze/math/Aliases.h>
51 #include <blaze/math/DenseMatrix.h>
52 #include <blaze/math/shims/Real.h>
56 #include <blaze/util/Assert.h>
57 #include <blaze/util/FalseType.h>
58 #include <blaze/util/Random.h>
59 #include <blaze/util/TrueType.h>
60 #include <blaze/util/Types.h>
61 
62 
63 namespace blaze {
64 
65 //=================================================================================================
66 //
67 // RAND SPECIALIZATION
68 //
69 //=================================================================================================
70 
71 //*************************************************************************************************
78 template< typename MT // Type of the adapted matrix
79  , bool SO // Storage order of the adapted matrix
80  , bool DF > // Density flag
81 class Rand< HermitianMatrix<MT,SO,DF> >
82 {
83  public:
84  //**Generate functions**************************************************************************
87  inline const HermitianMatrix<MT,SO,DF> generate() const;
88  inline const HermitianMatrix<MT,SO,DF> generate( size_t n ) const;
89  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
90 
91  template< typename Arg >
92  inline const HermitianMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
96 
97  template< typename Arg >
98  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
99  const Arg& min, const Arg& max ) const;
101  //**********************************************************************************************
102 
103  //**Randomize functions*************************************************************************
106  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix ) const;
107  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
108 
109  template< typename Arg >
110  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
111 
112  template< typename Arg >
113  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros,
114  const Arg& min, const Arg& max ) const;
116  //**********************************************************************************************
117 
118  private:
119  //**Randomize functions*************************************************************************
122  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, TrueType ) const;
123  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, FalseType ) const;
124 
125  template< typename Arg >
126  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
127 
128  template< typename Arg >
129  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
131  //**********************************************************************************************
132 };
134 //*************************************************************************************************
135 
136 
137 //*************************************************************************************************
143 template< typename MT // Type of the adapted matrix
144  , bool SO // Storage order of the adapted matrix
145  , bool DF > // Density flag
146 inline const HermitianMatrix<MT,SO,DF> Rand< HermitianMatrix<MT,SO,DF> >::generate() const
147 {
149 
150  HermitianMatrix<MT,SO,DF> matrix;
151  randomize( matrix );
152  return matrix;
153 }
155 //*************************************************************************************************
156 
157 
158 //*************************************************************************************************
165 template< typename MT // Type of the adapted matrix
166  , bool SO // Storage order of the adapted matrix
167  , bool DF > // Density flag
168 inline const HermitianMatrix<MT,SO,DF>
169  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n ) const
170 {
172 
173  HermitianMatrix<MT,SO,DF> matrix( n );
174  randomize( matrix );
175  return matrix;
176 }
178 //*************************************************************************************************
179 
180 
181 //*************************************************************************************************
190 template< typename MT // Type of the adapted matrix
191  , bool SO // Storage order of the adapted matrix
192  , bool DF > // Density flag
193 inline const HermitianMatrix<MT,SO,DF>
194  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
195 {
198 
199  if( nonzeros > n*n )
200  throw std::invalid_argument( "Invalid number of non-zero elements" );
201 
202  HermitianMatrix<MT,SO,DF> matrix( n );
203  randomize( matrix, nonzeros );
204 
205  return matrix;
206 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
219 template< typename MT // Type of the adapted matrix
220  , bool SO // Storage order of the adapted matrix
221  , bool DF > // Density flag
222 template< typename Arg > // Min/max argument type
223 inline const HermitianMatrix<MT,SO,DF>
224  Rand< HermitianMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
225 {
227 
228  HermitianMatrix<MT,SO,DF> matrix;
229  randomize( matrix, min, max );
230  return matrix;
231 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
245 template< typename MT // Type of the adapted matrix
246  , bool SO // Storage order of the adapted matrix
247  , bool DF > // Density flag
248 template< typename Arg > // Min/max argument type
249 inline const HermitianMatrix<MT,SO,DF>
250  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
251 {
253 
254  HermitianMatrix<MT,SO,DF> matrix( n );
255  randomize( matrix, min, max );
256  return matrix;
257 }
259 //*************************************************************************************************
260 
261 
262 //*************************************************************************************************
273 template< typename MT // Type of the adapted matrix
274  , bool SO // Storage order of the adapted matrix
275  , bool DF > // Density flag
276 template< typename Arg > // Min/max argument type
277 inline const HermitianMatrix<MT,SO,DF>
278  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
279  const Arg& min, const Arg& max ) const
280 {
283 
284  if( nonzeros > n*n )
285  throw std::invalid_argument( "Invalid number of non-zero elements" );
286 
287  HermitianMatrix<MT,SO,DF> matrix( n );
288  randomize( matrix, nonzeros, min, max );
289 
290  return matrix;
291 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
303 template< typename MT // Type of the adapted matrix
304  , bool SO // Storage order of the adapted matrix
305  , bool DF > // Density flag
306 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix ) const
307 {
308  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
309 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
321 template< typename MT // Type of the adapted matrix
322  , bool SO // Storage order of the adapted matrix
323  , bool DF > // Density flag
324 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, TrueType ) const
325 {
327 
328  using ET = ElementType_<MT>;
329  using BT = UnderlyingBuiltin_<ET>;
330 
331  const size_t n( matrix.rows() );
332 
333  for( size_t i=0UL; i<n; ++i ) {
334  for( size_t j=0UL; j<i; ++j ) {
335  matrix(i,j) = rand<ET>();
336  }
337  matrix(i,i) = rand<BT>();
338  }
339 }
341 //*************************************************************************************************
342 
343 
344 //*************************************************************************************************
351 template< typename MT // Type of the adapted matrix
352  , bool SO // Storage order of the adapted matrix
353  , bool DF > // Density flag
354 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, FalseType ) const
355 {
357 
358  const size_t n( matrix.rows() );
359 
360  if( n == 0UL ) return;
361 
362  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
363 
364  randomize( matrix, nonzeros );
365 }
367 //*************************************************************************************************
368 
369 
370 //*************************************************************************************************
379 template< typename MT // Type of the adapted matrix
380  , bool SO // Storage order of the adapted matrix
381  , bool DF > // Density flag
382 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
383 {
385 
386  using ET = ElementType_<MT>;
387  using BT = UnderlyingBuiltin_<ET>;
388 
389  const size_t n( matrix.rows() );
390 
391  if( nonzeros > n*n )
392  throw std::invalid_argument( "Invalid number of non-zero elements" );
393 
394  if( n == 0UL ) return;
395 
396  matrix.reset();
397  matrix.reserve( nonzeros );
398 
399  while( matrix.nonZeros() < nonzeros )
400  {
401  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
402  const size_t column( rand<size_t>( 0UL, n-1UL ) );
403 
404  if( row == column )
405  matrix(row,column) = rand<BT>();
406  else
407  matrix(row,column) = rand<ET>();
408  }
409 }
411 //*************************************************************************************************
412 
413 
414 //*************************************************************************************************
423 template< typename MT // Type of the adapted matrix
424  , bool SO // Storage order of the adapted matrix
425  , bool DF > // Density flag
426 template< typename Arg > // Min/max argument type
427 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
428  const Arg& min, const Arg& max ) const
429 {
430  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
431 }
433 //*************************************************************************************************
434 
435 
436 //*************************************************************************************************
445 template< typename MT // Type of the adapted matrix
446  , bool SO // Storage order of the adapted matrix
447  , bool DF > // Density flag
448 template< typename Arg > // Min/max argument type
449 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
450  const Arg& min, const Arg& max, TrueType ) const
451 {
453 
454  using ET = ElementType_<MT>;
455  using BT = UnderlyingBuiltin_<ET>;
456 
457  const size_t n( matrix.rows() );
458 
459  for( size_t i=0UL; i<n; ++i ) {
460  for( size_t j=0UL; j<i; ++j ) {
461  matrix(i,j) = rand<ET>( min, max );
462  }
463  matrix(i,i) = rand<BT>( real( min ), real( max ) );
464  }
465 }
467 //*************************************************************************************************
468 
469 
470 //*************************************************************************************************
479 template< typename MT // Type of the adapted matrix
480  , bool SO // Storage order of the adapted matrix
481  , bool DF > // Density flag
482 template< typename Arg > // Min/max argument type
483 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
484  const Arg& min, const Arg& max, FalseType ) const
485 {
487 
488  const size_t n( matrix.rows() );
489 
490  if( n == 0UL ) return;
491 
492  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
493 
494  randomize( matrix, nonzeros, min, max );
495 }
497 //*************************************************************************************************
498 
499 
500 //*************************************************************************************************
511 template< typename MT // Type of the adapted matrix
512  , bool SO // Storage order of the adapted matrix
513  , bool DF > // Density flag
514 template< typename Arg > // Min/max argument type
515 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
516  size_t nonzeros, const Arg& min, const Arg& max ) const
517 {
519 
520  using ET = ElementType_<MT>;
521  using BT = UnderlyingBuiltin_<ET>;
522 
523  const size_t n( matrix.rows() );
524 
525  if( nonzeros > n*n )
526  throw std::invalid_argument( "Invalid number of non-zero elements" );
527 
528  if( n == 0UL ) return;
529 
530  matrix.reset();
531  matrix.reserve( nonzeros );
532 
533  while( matrix.nonZeros() < nonzeros )
534  {
535  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
536  const size_t column( rand<size_t>( 0UL, n-1UL ) );
537 
538  if( row == column )
539  matrix(row,column) = rand<BT>( real( min ), real( max ) );
540  else
541  matrix(row,column) = rand<ET>( min, max );
542  }
543 }
545 //*************************************************************************************************
546 
547 
548 
549 
550 //=================================================================================================
551 //
552 // MAKE FUNCTIONS
553 //
554 //=================================================================================================
555 
556 //*************************************************************************************************
563 template< typename MT // Type of the adapted matrix
564  , bool SO // Storage order of the adapted matrix
565  , bool DF > // Density flag
566 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix )
567 {
568  using BT = UnderlyingBuiltin_< ElementType_<MT> >;
569 
570  const size_t n( matrix.rows() );
571 
572  for( size_t i=0UL; i<n; ++i ) {
573  for( size_t j=0UL; j<=i; ++j ) {
574  matrix(i,j) = rand<BT>();
575  }
576  }
577 
578  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
579 }
581 //*************************************************************************************************
582 
583 
584 //*************************************************************************************************
593 template< typename MT // Type of the adapted matrix
594  , bool SO // Storage order of the adapted matrix
595  , bool DF // Density flag
596  , typename Arg > // Min/max argument type
597 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
598 {
599  using BT = UnderlyingBuiltin_< ElementType_<MT> >;
600 
601  const size_t n( matrix.rows() );
602 
603  for( size_t i=0UL; i<n; ++i ) {
604  for( size_t j=0UL; j<=i; ++j ) {
605  matrix(i,j) = rand<BT>( real( min ), real( max ) );
606  }
607  }
608 
609  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
610 }
612 //*************************************************************************************************
613 
614 
615 //*************************************************************************************************
622 template< typename MT // Type of the adapted matrix
623  , bool SO // Storage order of the adapted matrix
624  , bool DF > // Density flag
625 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix )
626 {
627  using blaze::randomize;
628 
629  randomize( matrix );
630 }
632 //*************************************************************************************************
633 
634 
635 //*************************************************************************************************
644 template< typename MT // Type of the adapted matrix
645  , bool SO // Storage order of the adapted matrix
646  , bool DF // Density flag
647  , typename Arg > // Min/max argument type
648 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
649 {
650  using blaze::randomize;
651 
652  randomize( matrix, min, max );
653 }
655 //*************************************************************************************************
656 
657 
658 //*************************************************************************************************
665 template< typename MT // Type of the adapted matrix
666  , bool SO // Storage order of the adapted matrix
667  , bool DF > // Density flag
668 void makePositiveDefinite( HermitianMatrix<MT,SO,DF>& matrix )
669 {
670  using blaze::randomize;
671 
672  using BT = UnderlyingBuiltin_< ElementType_<MT> >;
673 
674  const size_t n( matrix.rows() );
675 
676  randomize( matrix );
677  matrix *= matrix;
678 
679  for( size_t i=0UL; i<n; ++i ) {
680  matrix(i,i) += BT(n);
681  }
682 }
684 //*************************************************************************************************
685 
686 } // namespace blaze
687 
688 #endif
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: FalseType.h:61
Header file for auxiliary alias declarations.
Header file for basic type definitions.
Header file for the FalseType type/value trait base class.
decltype(auto) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1387
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional matrix type...
Definition: DenseMatrix.h:61
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:927
Constraint on the data type.
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1234
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
Implementation of a random number generator.
Column< MT > column(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific column of the given matrix.
Definition: Column.h:124
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
Header file for the implementation of a Hermitian matrix adaptor.
Row< MT > row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:124
Constraint on the data type.
Constraint on the data type.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:290
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for all basic SparseMatrix functionality.
Header file for the UnderlyingBuiltin type trait.
Header file for the IsDenseMatrix type trait.
Header file for run time assertion macros.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_RESIZABLE_TYPE(T)
Constraint on the data type.In case the given data type T is resizable, i.e. has a &#39;resize&#39; member fu...
Definition: Resizable.h:81
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:700
T generate() const
Generation of a random value in the range .
Definition: Random.h:250
Header file for all basic DenseMatrix functionality.
#define BLAZE_CONSTRAINT_MUST_BE_RESIZABLE_TYPE(T)
Constraint on the data type.In case the given data type T is not resizable, i.e. does not have a &#39;res...
Definition: Resizable.h:61
Header file for the implementation of a diagonal matrix adaptor.
Header file for the real shim.
#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
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type...
Definition: SparseMatrix.h:61
Header file for the TrueType type/value trait base class.