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>
50 #include <blaze/math/DenseMatrix.h>
51 #include <blaze/math/shims/Real.h>
55 #include <blaze/util/Assert.h>
56 #include <blaze/util/FalseType.h>
57 #include <blaze/util/Random.h>
58 #include <blaze/util/TrueType.h>
59 #include <blaze/util/Types.h>
60 
61 
62 namespace blaze {
63 
64 //=================================================================================================
65 //
66 // RAND SPECIALIZATION
67 //
68 //=================================================================================================
69 
70 //*************************************************************************************************
77 template< typename MT // Type of the adapted matrix
78  , bool SO // Storage order of the adapted matrix
79  , bool DF > // Density flag
80 class Rand< HermitianMatrix<MT,SO,DF> >
81 {
82  public:
83  //**Generate functions**************************************************************************
86  inline const HermitianMatrix<MT,SO,DF> generate() const;
87  inline const HermitianMatrix<MT,SO,DF> generate( size_t n ) const;
88  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
89 
90  template< typename Arg >
91  inline const HermitianMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
98  const Arg& min, const Arg& max ) const;
100  //**********************************************************************************************
101 
102  //**Randomize functions*************************************************************************
105  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix ) const;
106  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
107 
108  template< typename Arg >
109  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
110 
111  template< typename Arg >
112  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros,
113  const Arg& min, const Arg& max ) const;
115  //**********************************************************************************************
116 
117  private:
118  //**Randomize functions*************************************************************************
121  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, TrueType ) const;
122  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, FalseType ) const;
123 
124  template< typename Arg >
125  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
126 
127  template< typename Arg >
128  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
130  //**********************************************************************************************
131 };
133 //*************************************************************************************************
134 
135 
136 //*************************************************************************************************
142 template< typename MT // Type of the adapted matrix
143  , bool SO // Storage order of the adapted matrix
144  , bool DF > // Density flag
145 inline const HermitianMatrix<MT,SO,DF> Rand< HermitianMatrix<MT,SO,DF> >::generate() const
146 {
148 
149  HermitianMatrix<MT,SO,DF> matrix;
150  randomize( matrix );
151  return matrix;
152 }
154 //*************************************************************************************************
155 
156 
157 //*************************************************************************************************
164 template< typename MT // Type of the adapted matrix
165  , bool SO // Storage order of the adapted matrix
166  , bool DF > // Density flag
167 inline const HermitianMatrix<MT,SO,DF>
168  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n ) const
169 {
171 
172  HermitianMatrix<MT,SO,DF> matrix( n );
173  randomize( matrix );
174  return matrix;
175 }
177 //*************************************************************************************************
178 
179 
180 //*************************************************************************************************
189 template< typename MT // Type of the adapted matrix
190  , bool SO // Storage order of the adapted matrix
191  , bool DF > // Density flag
192 inline const HermitianMatrix<MT,SO,DF>
193  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
194 {
197 
198  if( nonzeros > n*n )
199  throw std::invalid_argument( "Invalid number of non-zero elements" );
200 
201  HermitianMatrix<MT,SO,DF> matrix( n );
202  randomize( matrix, nonzeros );
203 
204  return matrix;
205 }
207 //*************************************************************************************************
208 
209 
210 //*************************************************************************************************
218 template< typename MT // Type of the adapted matrix
219  , bool SO // Storage order of the adapted matrix
220  , bool DF > // Density flag
221 template< typename Arg > // Min/max argument type
222 inline const HermitianMatrix<MT,SO,DF>
223  Rand< HermitianMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
224 {
226 
227  HermitianMatrix<MT,SO,DF> matrix;
228  randomize( matrix, min, max );
229  return matrix;
230 }
232 //*************************************************************************************************
233 
234 
235 //*************************************************************************************************
244 template< typename MT // Type of the adapted matrix
245  , bool SO // Storage order of the adapted matrix
246  , bool DF > // Density flag
247 template< typename Arg > // Min/max argument type
248 inline const HermitianMatrix<MT,SO,DF>
249  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
250 {
252 
253  HermitianMatrix<MT,SO,DF> matrix( n );
254  randomize( matrix, min, max );
255  return matrix;
256 }
258 //*************************************************************************************************
259 
260 
261 //*************************************************************************************************
272 template< typename MT // Type of the adapted matrix
273  , bool SO // Storage order of the adapted matrix
274  , bool DF > // Density flag
275 template< typename Arg > // Min/max argument type
276 inline const HermitianMatrix<MT,SO,DF>
277  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
278  const Arg& min, const Arg& max ) const
279 {
282 
283  if( nonzeros > n*n )
284  throw std::invalid_argument( "Invalid number of non-zero elements" );
285 
286  HermitianMatrix<MT,SO,DF> matrix( n );
287  randomize( matrix, nonzeros, min, max );
288 
289  return matrix;
290 }
292 //*************************************************************************************************
293 
294 
295 //*************************************************************************************************
302 template< typename MT // Type of the adapted matrix
303  , bool SO // Storage order of the adapted matrix
304  , bool DF > // Density flag
305 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix ) const
306 {
307  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
308 }
310 //*************************************************************************************************
311 
312 
313 //*************************************************************************************************
320 template< typename MT // Type of the adapted matrix
321  , bool SO // Storage order of the adapted matrix
322  , bool DF > // Density flag
323 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, TrueType ) const
324 {
326 
327  typedef ElementType_<MT> ET;
328  typedef UnderlyingBuiltin_<ET> BT;
329 
330  const size_t n( matrix.rows() );
331 
332  for( size_t i=0UL; i<n; ++i ) {
333  for( size_t j=0UL; j<i; ++j ) {
334  matrix(i,j) = rand<ET>();
335  }
336  matrix(i,i) = rand<BT>();
337  }
338 }
340 //*************************************************************************************************
341 
342 
343 //*************************************************************************************************
350 template< typename MT // Type of the adapted matrix
351  , bool SO // Storage order of the adapted matrix
352  , bool DF > // Density flag
353 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, FalseType ) const
354 {
356 
357  const size_t n( matrix.rows() );
358 
359  if( n == 0UL ) return;
360 
361  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
362 
363  randomize( matrix, nonzeros );
364 }
366 //*************************************************************************************************
367 
368 
369 //*************************************************************************************************
378 template< typename MT // Type of the adapted matrix
379  , bool SO // Storage order of the adapted matrix
380  , bool DF > // Density flag
381 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
382 {
384 
385  typedef ElementType_<MT> ET;
386  typedef UnderlyingBuiltin_<ET> BT;
387 
388  const size_t n( matrix.rows() );
389 
390  if( nonzeros > n*n )
391  throw std::invalid_argument( "Invalid number of non-zero elements" );
392 
393  if( n == 0UL ) return;
394 
395  matrix.reset();
396  matrix.reserve( nonzeros );
397 
398  while( matrix.nonZeros() < nonzeros )
399  {
400  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
401  const size_t column( rand<size_t>( 0UL, n-1UL ) );
402 
403  if( row == column )
404  matrix(row,column) = rand<BT>();
405  else
406  matrix(row,column) = rand<ET>();
407  }
408 }
410 //*************************************************************************************************
411 
412 
413 //*************************************************************************************************
422 template< typename MT // Type of the adapted matrix
423  , bool SO // Storage order of the adapted matrix
424  , bool DF > // Density flag
425 template< typename Arg > // Min/max argument type
426 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
427  const Arg& min, const Arg& max ) const
428 {
429  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
430 }
432 //*************************************************************************************************
433 
434 
435 //*************************************************************************************************
444 template< typename MT // Type of the adapted matrix
445  , bool SO // Storage order of the adapted matrix
446  , bool DF > // Density flag
447 template< typename Arg > // Min/max argument type
448 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
449  const Arg& min, const Arg& max, TrueType ) const
450 {
452 
453  typedef ElementType_<MT> ET;
454  typedef UnderlyingBuiltin_<ET> BT;
455 
456  const size_t n( matrix.rows() );
457 
458  for( size_t i=0UL; i<n; ++i ) {
459  for( size_t j=0UL; j<i; ++j ) {
460  matrix(i,j) = rand<ET>( min, max );
461  }
462  matrix(i,i) = rand<BT>( real( min ), real( max ) );
463  }
464 }
466 //*************************************************************************************************
467 
468 
469 //*************************************************************************************************
478 template< typename MT // Type of the adapted matrix
479  , bool SO // Storage order of the adapted matrix
480  , bool DF > // Density flag
481 template< typename Arg > // Min/max argument type
482 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
483  const Arg& min, const Arg& max, FalseType ) const
484 {
486 
487  const size_t n( matrix.rows() );
488 
489  if( n == 0UL ) return;
490 
491  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
492 
493  randomize( matrix, nonzeros, min, max );
494 }
496 //*************************************************************************************************
497 
498 
499 //*************************************************************************************************
510 template< typename MT // Type of the adapted matrix
511  , bool SO // Storage order of the adapted matrix
512  , bool DF > // Density flag
513 template< typename Arg > // Min/max argument type
514 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
515  size_t nonzeros, const Arg& min, const Arg& max ) const
516 {
518 
519  typedef ElementType_<MT> ET;
520  typedef UnderlyingBuiltin_<ET> BT;
521 
522  const size_t n( matrix.rows() );
523 
524  if( nonzeros > n*n )
525  throw std::invalid_argument( "Invalid number of non-zero elements" );
526 
527  if( n == 0UL ) return;
528 
529  matrix.reset();
530  matrix.reserve( nonzeros );
531 
532  while( matrix.nonZeros() < nonzeros )
533  {
534  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
535  const size_t column( rand<size_t>( 0UL, n-1UL ) );
536 
537  if( row == column )
538  matrix(row,column) = rand<BT>( real( min ), real( max ) );
539  else
540  matrix(row,column) = rand<ET>( min, max );
541  }
542 }
544 //*************************************************************************************************
545 
546 
547 
548 
549 //=================================================================================================
550 //
551 // MAKE FUNCTIONS
552 //
553 //=================================================================================================
554 
555 //*************************************************************************************************
562 template< typename MT // Type of the adapted matrix
563  , bool SO // Storage order of the adapted matrix
564  , bool DF > // Density flag
565 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix )
566 {
567  typedef UnderlyingBuiltin_< ElementType_<MT> > BT;
568 
569  const size_t n( matrix.rows() );
570 
571  for( size_t i=0UL; i<n; ++i ) {
572  for( size_t j=0UL; j<=i; ++j ) {
573  matrix(i,j) = rand<BT>();
574  }
575  }
576 
577  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
578 }
580 //*************************************************************************************************
581 
582 
583 //*************************************************************************************************
592 template< typename MT // Type of the adapted matrix
593  , bool SO // Storage order of the adapted matrix
594  , bool DF // Density flag
595  , typename Arg > // Min/max argument type
596 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
597 {
598  typedef UnderlyingBuiltin_< ElementType_<MT> > BT;
599 
600  const size_t n( matrix.rows() );
601 
602  for( size_t i=0UL; i<n; ++i ) {
603  for( size_t j=0UL; j<=i; ++j ) {
604  matrix(i,j) = rand<BT>( real( min ), real( max ) );
605  }
606  }
607 
608  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
609 }
611 //*************************************************************************************************
612 
613 
614 //*************************************************************************************************
621 template< typename MT // Type of the adapted matrix
622  , bool SO // Storage order of the adapted matrix
623  , bool DF > // Density flag
624 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix )
625 {
626  using blaze::randomize;
627 
628  randomize( matrix );
629 }
631 //*************************************************************************************************
632 
633 
634 //*************************************************************************************************
643 template< typename MT // Type of the adapted matrix
644  , bool SO // Storage order of the adapted matrix
645  , bool DF // Density flag
646  , typename Arg > // Min/max argument type
647 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
648 {
649  using blaze::randomize;
650 
651  randomize( matrix, min, max );
652 }
654 //*************************************************************************************************
655 
656 
657 //*************************************************************************************************
664 template< typename MT // Type of the adapted matrix
665  , bool SO // Storage order of the adapted matrix
666  , bool DF > // Density flag
667 void makePositiveDefinite( HermitianMatrix<MT,SO,DF>& matrix )
668 {
669  using blaze::randomize;
670 
671  typedef UnderlyingBuiltin_< ElementType_<MT> > BT;
672 
673  const size_t n( matrix.rows() );
674 
675  randomize( matrix );
676  matrix *= matrix;
677 
678  for( size_t i=0UL; i<n; ++i ) {
679  matrix(i,i) += BT(n);
680  }
681 }
683 //*************************************************************************************************
684 
685 } // namespace blaze
686 
687 #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.
#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:926
Constraint on the data type.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:689
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
Implementation of a random number generator.
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:1716
DisableIf_< Or< IsComputation< MT >, IsTransExpr< MT > >, ColumnExprTrait_< MT > > column(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific column of the given matrix.
Definition: Column.h:126
Header file for the implementation of a Hermitian matrix adaptor.
Constraint on the data type.
Constraint on the data type.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for all basic SparseMatrix functionality.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:289
Header file for the UnderlyingBuiltin type trait.
Header file for the IsDenseMatrix type trait.
const DMatForEachExpr< MT, Real, SO > real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatForEachExpr.h:1223
DisableIf_< Or< IsComputation< MT >, IsTransExpr< MT > >, RowExprTrait_< MT > > row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:126
Header file for run time assertion macros.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_RESIZABLE(T)
Constraint on the data type.In case the given data type T is resizable, i.e. has a 'resize' member fu...
Definition: Resizable.h:81
T generate() const
Generation of a random value in the range .
Definition: Random.h:249
Header file for all basic DenseMatrix functionality.
#define BLAZE_CONSTRAINT_MUST_BE_RESIZABLE(T)
Constraint on the data type.In case the given data type T is not resizable, i.e. does not have a 'res...
Definition: Resizable.h:61
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.
const DMatForEachExpr< MT, Ceil, SO > ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatForEachExpr.h:1130