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>
49 #include <blaze/math/DenseMatrix.h>
50 #include <blaze/math/shims/Real.h>
54 #include <blaze/util/Assert.h>
55 #include <blaze/util/FalseType.h>
56 #include <blaze/util/Random.h>
57 #include <blaze/util/TrueType.h>
58 #include <blaze/util/Types.h>
59 
60 
61 namespace blaze {
62 
63 //=================================================================================================
64 //
65 // RAND SPECIALIZATION
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
76 template< typename MT // Type of the adapted matrix
77  , bool SO // Storage order of the adapted matrix
78  , bool DF > // Density flag
79 class Rand< HermitianMatrix<MT,SO,DF> >
80 {
81  public:
82  //**Generate functions**************************************************************************
85  inline const HermitianMatrix<MT,SO,DF> generate() const;
86  inline const HermitianMatrix<MT,SO,DF> generate( size_t n ) const;
87  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
88 
89  template< typename Arg >
90  inline const HermitianMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
91 
92  template< typename Arg >
93  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
94 
95  template< typename Arg >
96  inline const HermitianMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
97  const Arg& min, const Arg& max ) const;
99  //**********************************************************************************************
100 
101  //**Randomize functions*************************************************************************
104  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix ) const;
105  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
106 
107  template< typename Arg >
108  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
109 
110  template< typename Arg >
111  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros,
112  const Arg& min, const Arg& max ) const;
114  //**********************************************************************************************
115 
116  private:
117  //**Randomize functions*************************************************************************
120  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, TrueType ) const;
121  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, FalseType ) const;
122 
123  template< typename Arg >
124  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
125 
126  template< typename Arg >
127  inline void randomize( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
129  //**********************************************************************************************
130 };
132 //*************************************************************************************************
133 
134 
135 //*************************************************************************************************
141 template< typename MT // Type of the adapted matrix
142  , bool SO // Storage order of the adapted matrix
143  , bool DF > // Density flag
144 inline const HermitianMatrix<MT,SO,DF> Rand< HermitianMatrix<MT,SO,DF> >::generate() const
145 {
147 
148  HermitianMatrix<MT,SO,DF> matrix;
149  randomize( matrix );
150  return matrix;
151 }
153 //*************************************************************************************************
154 
155 
156 //*************************************************************************************************
163 template< typename MT // Type of the adapted matrix
164  , bool SO // Storage order of the adapted matrix
165  , bool DF > // Density flag
166 inline const HermitianMatrix<MT,SO,DF>
167  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n ) const
168 {
170 
171  HermitianMatrix<MT,SO,DF> matrix( n );
172  randomize( matrix );
173  return matrix;
174 }
176 //*************************************************************************************************
177 
178 
179 //*************************************************************************************************
188 template< typename MT // Type of the adapted matrix
189  , bool SO // Storage order of the adapted matrix
190  , bool DF > // Density flag
191 inline const HermitianMatrix<MT,SO,DF>
192  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
193 {
196 
197  if( nonzeros > n*n )
198  throw std::invalid_argument( "Invalid number of non-zero elements" );
199 
200  HermitianMatrix<MT,SO,DF> matrix( n );
201  randomize( matrix, nonzeros );
202 
203  return matrix;
204 }
206 //*************************************************************************************************
207 
208 
209 //*************************************************************************************************
217 template< typename MT // Type of the adapted matrix
218  , bool SO // Storage order of the adapted matrix
219  , bool DF > // Density flag
220 template< typename Arg > // Min/max argument type
221 inline const HermitianMatrix<MT,SO,DF>
222  Rand< HermitianMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
223 {
225 
226  HermitianMatrix<MT,SO,DF> matrix;
227  randomize( matrix, min, max );
228  return matrix;
229 }
231 //*************************************************************************************************
232 
233 
234 //*************************************************************************************************
243 template< typename MT // Type of the adapted matrix
244  , bool SO // Storage order of the adapted matrix
245  , bool DF > // Density flag
246 template< typename Arg > // Min/max argument type
247 inline const HermitianMatrix<MT,SO,DF>
248  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
249 {
251 
252  HermitianMatrix<MT,SO,DF> matrix( n );
253  randomize( matrix, min, max );
254  return matrix;
255 }
257 //*************************************************************************************************
258 
259 
260 //*************************************************************************************************
271 template< typename MT // Type of the adapted matrix
272  , bool SO // Storage order of the adapted matrix
273  , bool DF > // Density flag
274 template< typename Arg > // Min/max argument type
275 inline const HermitianMatrix<MT,SO,DF>
276  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
277  const Arg& min, const Arg& max ) const
278 {
281 
282  if( nonzeros > n*n )
283  throw std::invalid_argument( "Invalid number of non-zero elements" );
284 
285  HermitianMatrix<MT,SO,DF> matrix( n );
286  randomize( matrix, nonzeros, min, max );
287 
288  return matrix;
289 }
291 //*************************************************************************************************
292 
293 
294 //*************************************************************************************************
301 template< typename MT // Type of the adapted matrix
302  , bool SO // Storage order of the adapted matrix
303  , bool DF > // Density flag
304 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix ) const
305 {
306  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
307 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
319 template< typename MT // Type of the adapted matrix
320  , bool SO // Storage order of the adapted matrix
321  , bool DF > // Density flag
322 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, TrueType ) const
323 {
325 
326  typedef typename MT::ElementType ET;
327  typedef typename UnderlyingBuiltin<ET>::Type BT;
328 
329  const size_t n( matrix.rows() );
330 
331  for( size_t i=0UL; i<n; ++i ) {
332  for( size_t j=0UL; j<i; ++j ) {
333  matrix(i,j) = rand<ET>();
334  }
335  matrix(i,i) = rand<BT>();
336  }
337 }
339 //*************************************************************************************************
340 
341 
342 //*************************************************************************************************
349 template< typename MT // Type of the adapted matrix
350  , bool SO // Storage order of the adapted matrix
351  , bool DF > // Density flag
352 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, FalseType ) const
353 {
355 
356  typedef typename MT::ElementType ET;
357  typedef typename UnderlyingBuiltin<ET>::Type BT;
358 
359  const size_t n( matrix.rows() );
360 
361  if( n == 0UL ) return;
362 
363  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
364 
365  matrix.reset();
366  matrix.reserve( nonzeros );
367 
368  while( matrix.nonZeros() < nonzeros )
369  {
370  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
371  const size_t column( rand<size_t>( 0UL, n-1UL ) );
372 
373  if( row == column )
374  matrix(row,column) = rand<BT>();
375  else
376  matrix(row,column) = rand<ET>();
377  }
378 }
380 //*************************************************************************************************
381 
382 
383 //*************************************************************************************************
392 template< typename MT // Type of the adapted matrix
393  , bool SO // Storage order of the adapted matrix
394  , bool DF > // Density flag
395 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
396 {
398 
399  typedef typename MT::ElementType ET;
400  typedef typename UnderlyingBuiltin<ET>::Type BT;
401 
402  const size_t n( matrix.rows() );
403 
404  if( nonzeros > n*n )
405  throw std::invalid_argument( "Invalid number of non-zero elements" );
406 
407  if( n == 0UL ) return;
408 
409  matrix.reset();
410  matrix.reserve( nonzeros );
411 
412  while( matrix.nonZeros() < nonzeros )
413  {
414  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
415  const size_t column( rand<size_t>( 0UL, n-1UL ) );
416 
417  if( row == column )
418  matrix(row,column) = rand<BT>();
419  else
420  matrix(row,column) = rand<ET>();
421  }
422 }
424 //*************************************************************************************************
425 
426 
427 //*************************************************************************************************
436 template< typename MT // Type of the adapted matrix
437  , bool SO // Storage order of the adapted matrix
438  , bool DF > // Density flag
439 template< typename Arg > // Min/max argument type
440 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
441  const Arg& min, const Arg& max ) const
442 {
443  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
444 }
446 //*************************************************************************************************
447 
448 
449 //*************************************************************************************************
458 template< typename MT // Type of the adapted matrix
459  , bool SO // Storage order of the adapted matrix
460  , bool DF > // Density flag
461 template< typename Arg > // Min/max argument type
462 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
463  const Arg& min, const Arg& max, TrueType ) const
464 {
466 
467  typedef typename MT::ElementType ET;
468  typedef typename UnderlyingBuiltin<ET>::Type BT;
469 
470  const size_t n( matrix.rows() );
471 
472  for( size_t i=0UL; i<n; ++i ) {
473  for( size_t j=0UL; j<i; ++j ) {
474  matrix(i,j) = rand<ET>( min, max );
475  }
476  matrix(i,i) = rand<BT>( real( min ), real( max ) );
477  }
478 }
480 //*************************************************************************************************
481 
482 
483 //*************************************************************************************************
492 template< typename MT // Type of the adapted matrix
493  , bool SO // Storage order of the adapted matrix
494  , bool DF > // Density flag
495 template< typename Arg > // Min/max argument type
496 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
497  const Arg& min, const Arg& max, FalseType ) const
498 {
500 
501  typedef typename MT::ElementType ET;
502  typedef typename UnderlyingBuiltin<ET>::Type BT;
503 
504  const size_t n( matrix.rows() );
505 
506  if( n == 0UL ) return;
507 
508  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
509 
510  matrix.reset();
511  matrix.reserve( nonzeros );
512 
513  while( matrix.nonZeros() < nonzeros )
514  {
515  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
516  const size_t column( rand<size_t>( 0UL, n-1UL ) );
517 
518  if( row == column )
519  matrix(row,column) = rand<BT>( real( min ), real( max ) );
520  else
521  matrix(row,column) = rand<ET>( min, max );
522  }
523 }
525 //*************************************************************************************************
526 
527 
528 //*************************************************************************************************
539 template< typename MT // Type of the adapted matrix
540  , bool SO // Storage order of the adapted matrix
541  , bool DF > // Density flag
542 template< typename Arg > // Min/max argument type
543 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
544  size_t nonzeros, const Arg& min, const Arg& max ) const
545 {
547 
548  typedef typename MT::ElementType ET;
549  typedef typename UnderlyingBuiltin<ET>::Type BT;
550 
551  const size_t n( matrix.rows() );
552 
553  if( nonzeros > n*n )
554  throw std::invalid_argument( "Invalid number of non-zero elements" );
555 
556  if( n == 0UL ) return;
557 
558  matrix.reset();
559  matrix.reserve( nonzeros );
560 
561  while( matrix.nonZeros() < nonzeros )
562  {
563  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
564  const size_t column( rand<size_t>( 0UL, n-1UL ) );
565 
566  if( row == column )
567  matrix(row,column) = rand<BT>( real( min ), real( max ) );
568  else
569  matrix(row,column) = rand<ET>( min, max );
570  }
571 }
573 //*************************************************************************************************
574 
575 
576 
577 
578 //=================================================================================================
579 //
580 // MAKE FUNCTIONS
581 //
582 //=================================================================================================
583 
584 //*************************************************************************************************
591 template< typename MT // Type of the adapted matrix
592  , bool SO // Storage order of the adapted matrix
593  , bool DF > // Density flag
594 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix )
595 {
596  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type BT;
597 
598  const size_t n( matrix.rows() );
599 
600  for( size_t i=0UL; i<n; ++i ) {
601  for( size_t j=0UL; j<=i; ++j ) {
602  matrix(i,j) = rand<BT>();
603  }
604  }
605 
606  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
607 }
609 //*************************************************************************************************
610 
611 
612 //*************************************************************************************************
621 template< typename MT // Type of the adapted matrix
622  , bool SO // Storage order of the adapted matrix
623  , bool DF // Density flag
624  , typename Arg > // Min/max argument type
625 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
626 {
627  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type BT;
628 
629  const size_t n( matrix.rows() );
630 
631  for( size_t i=0UL; i<n; ++i ) {
632  for( size_t j=0UL; j<=i; ++j ) {
633  matrix(i,j) = rand<BT>( real( min ), real( max ) );
634  }
635  }
636 
637  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
638 }
640 //*************************************************************************************************
641 
642 
643 //*************************************************************************************************
650 template< typename MT // Type of the adapted matrix
651  , bool SO // Storage order of the adapted matrix
652  , bool DF > // Density flag
653 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix )
654 {
655  using blaze::randomize;
656 
657  randomize( matrix );
658 }
660 //*************************************************************************************************
661 
662 
663 //*************************************************************************************************
672 template< typename MT // Type of the adapted matrix
673  , bool SO // Storage order of the adapted matrix
674  , bool DF // Density flag
675  , typename Arg > // Min/max argument type
676 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
677 {
678  using blaze::randomize;
679 
680  randomize( matrix, min, max );
681 }
683 //*************************************************************************************************
684 
685 
686 //*************************************************************************************************
693 template< typename MT // Type of the adapted matrix
694  , bool SO // Storage order of the adapted matrix
695  , bool DF > // Density flag
696 void makePositiveDefinite( HermitianMatrix<MT,SO,DF>& matrix )
697 {
698  using blaze::randomize;
699 
700  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type BT;
701 
702  const size_t n( matrix.rows() );
703 
704  randomize( matrix );
705  matrix *= matrix;
706 
707  for( size_t i=0UL; i<n; ++i ) {
708  matrix(i,i) += BT(n);
709  }
710 }
712 //*************************************************************************************************
713 
714 } // namespace blaze
715 
716 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
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:79
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1041
Constraint on the data type.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:697
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename ColumnExprTrait< MT >::Type >::Type column(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific column of the given matrix.
Definition: Column.h:107
Implementation of a random number generator.
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:260
Header file for the UnderlyingBuiltin type trait.
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
const RealExprTrait< MT >::Type real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatRealExpr.h:920
Header file for the real shim.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
Header file for the IsDenseMatrix type trait.
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename RowExprTrait< MT >::Type >::Type row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:107
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:118
T generate() const
Generation of a random value in the range .
Definition: Random.h:220
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:79
boost::false_type FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: FalseType.h:61
boost::true_type TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
#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:79
Header file for the TrueType type/value trait base class.