Blaze  3.6
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 <vector>
45 #include <blaze/math/Aliases.h>
51 #include <blaze/math/DenseMatrix.h>
52 #include <blaze/math/Exception.h>
53 #include <blaze/math/shims/Real.h>
57 #include <blaze/util/Assert.h>
59 #include <blaze/util/Random.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  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
201  }
202 
203  HermitianMatrix<MT,SO,DF> matrix( n );
204  randomize( matrix, nonzeros );
205 
206  return matrix;
207 }
209 //*************************************************************************************************
210 
211 
212 //*************************************************************************************************
220 template< typename MT // Type of the adapted matrix
221  , bool SO // Storage order of the adapted matrix
222  , bool DF > // Density flag
223 template< typename Arg > // Min/max argument type
224 inline const HermitianMatrix<MT,SO,DF>
225  Rand< HermitianMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
226 {
228 
229  HermitianMatrix<MT,SO,DF> matrix;
230  randomize( matrix, min, max );
231  return matrix;
232 }
234 //*************************************************************************************************
235 
236 
237 //*************************************************************************************************
246 template< typename MT // Type of the adapted matrix
247  , bool SO // Storage order of the adapted matrix
248  , bool DF > // Density flag
249 template< typename Arg > // Min/max argument type
250 inline const HermitianMatrix<MT,SO,DF>
251  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
252 {
254 
255  HermitianMatrix<MT,SO,DF> matrix( n );
256  randomize( matrix, min, max );
257  return matrix;
258 }
260 //*************************************************************************************************
261 
262 
263 //*************************************************************************************************
274 template< typename MT // Type of the adapted matrix
275  , bool SO // Storage order of the adapted matrix
276  , bool DF > // Density flag
277 template< typename Arg > // Min/max argument type
278 inline const HermitianMatrix<MT,SO,DF>
279  Rand< HermitianMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
280  const Arg& min, const Arg& max ) const
281 {
284 
285  if( nonzeros > n*n ) {
286  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
287  }
288 
289  HermitianMatrix<MT,SO,DF> matrix( n );
290  randomize( matrix, nonzeros, min, max );
291 
292  return matrix;
293 }
295 //*************************************************************************************************
296 
297 
298 //*************************************************************************************************
305 template< typename MT // Type of the adapted matrix
306  , bool SO // Storage order of the adapted matrix
307  , bool DF > // Density flag
308 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix ) const
309 {
310  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
311 }
313 //*************************************************************************************************
314 
315 
316 //*************************************************************************************************
323 template< typename MT // Type of the adapted matrix
324  , bool SO // Storage order of the adapted matrix
325  , bool DF > // Density flag
326 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, TrueType ) const
327 {
329 
330  using ET = ElementType_t<MT>;
331  using BT = UnderlyingBuiltin_t<ET>;
332 
333  const size_t n( matrix.rows() );
334 
335  for( size_t i=0UL; i<n; ++i ) {
336  for( size_t j=0UL; j<i; ++j ) {
337  matrix(i,j) = rand<ET>();
338  }
339  matrix(i,i) = rand<BT>();
340  }
341 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
353 template< typename MT // Type of the adapted matrix
354  , bool SO // Storage order of the adapted matrix
355  , bool DF > // Density flag
356 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, FalseType ) const
357 {
359 
360  const size_t n( matrix.rows() );
361 
362  if( n == 0UL ) return;
363 
364  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
365 
366  randomize( matrix, nonzeros );
367 }
369 //*************************************************************************************************
370 
371 
372 //*************************************************************************************************
381 template< typename MT // Type of the adapted matrix
382  , bool SO // Storage order of the adapted matrix
383  , bool DF > // Density flag
384 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
385 {
387 
388  using ET = ElementType_t<MT>;
389  using BT = UnderlyingBuiltin_t<ET>;
390 
391  const size_t n( matrix.rows() );
392 
393  if( nonzeros > n*n ) {
394  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
395  }
396 
397  if( n == 0UL ) return;
398 
399  matrix.reset();
400  matrix.reserve( nonzeros );
401 
402  while( matrix.nonZeros() < nonzeros )
403  {
404  const size_t row ( rand<size_t>( 0UL, n-1UL ) );
405  const size_t column( rand<size_t>( 0UL, n-1UL ) );
406 
407  if( row == column )
408  matrix(row,column) = rand<BT>();
409  else
410  matrix(row,column) = rand<ET>();
411  }
412 }
414 //*************************************************************************************************
415 
416 
417 //*************************************************************************************************
426 template< typename MT // Type of the adapted matrix
427  , bool SO // Storage order of the adapted matrix
428  , bool DF > // Density flag
429 template< typename Arg > // Min/max argument type
430 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
431  const Arg& min, const Arg& max ) const
432 {
433  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
434 }
436 //*************************************************************************************************
437 
438 
439 //*************************************************************************************************
448 template< typename MT // Type of the adapted matrix
449  , bool SO // Storage order of the adapted matrix
450  , bool DF > // Density flag
451 template< typename Arg > // Min/max argument type
452 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
453  const Arg& min, const Arg& max, TrueType ) const
454 {
456 
457  using ET = ElementType_t<MT>;
458  using BT = UnderlyingBuiltin_t<ET>;
459 
460  const size_t n( matrix.rows() );
461 
462  for( size_t i=0UL; i<n; ++i ) {
463  for( size_t j=0UL; j<i; ++j ) {
464  matrix(i,j) = rand<ET>( min, max );
465  }
466  matrix(i,i) = rand<BT>( real( min ), real( max ) );
467  }
468 }
470 //*************************************************************************************************
471 
472 
473 //*************************************************************************************************
482 template< typename MT // Type of the adapted matrix
483  , bool SO // Storage order of the adapted matrix
484  , bool DF > // Density flag
485 template< typename Arg > // Min/max argument type
486 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
487  const Arg& min, const Arg& max, FalseType ) const
488 {
490 
491  const size_t n( matrix.rows() );
492 
493  if( n == 0UL ) return;
494 
495  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
496 
497  randomize( matrix, nonzeros, min, max );
498 }
500 //*************************************************************************************************
501 
502 
503 //*************************************************************************************************
514 template< typename MT // Type of the adapted matrix
515  , bool SO // Storage order of the adapted matrix
516  , bool DF > // Density flag
517 template< typename Arg > // Min/max argument type
518 inline void Rand< HermitianMatrix<MT,SO,DF> >::randomize( HermitianMatrix<MT,SO,DF>& matrix,
519  size_t nonzeros, const Arg& min, const Arg& max ) const
520 {
522 
523  using ET = ElementType_t<MT>;
524  using BT = UnderlyingBuiltin_t<ET>;
525 
526  const size_t n( matrix.rows() );
527 
528  if( nonzeros > n*n ) {
529  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
530  }
531 
532  if( n == 0UL ) return;
533 
534  std::vector<size_t> dist( n );
535  std::vector<bool> structure( n*n );
536  size_t nz( 0UL );
537 
538  while( nz < nonzeros )
539  {
540  const size_t row = rand<size_t>( 0UL, n-1UL );
541  const size_t col = rand<size_t>( 0UL, n-1UL );
542 
543  if( structure[row*n+col] ) continue;
544 
545  ++dist[row];
546  structure[row*n+col] = true;
547  ++nz;
548 
549  if( row != col ) {
550  ++dist[col];
551  structure[col*n+row] = true;
552  ++nz;
553  }
554  }
555 
556  matrix.reset();
557  matrix.reserve( nz );
558 
559  for( size_t i=0UL; i<n; ++i ) {
560  matrix.reserve( i, dist[i] );
561  }
562 
563  for( size_t i=0UL; i<n; ++i ) {
564  for( size_t j=i; j<n; ++j ) {
565  if( structure[i*n+j] ) {
566  if( i == j )
567  matrix.append( i, j, rand<BT>( real( min ), real( max ) ) );
568  else
569  matrix.append( i, j, rand<ET>( min, max ) );
570  }
571  }
572  }
573 }
575 //*************************************************************************************************
576 
577 
578 
579 
580 //=================================================================================================
581 //
582 // MAKE FUNCTIONS
583 //
584 //=================================================================================================
585 
586 //*************************************************************************************************
593 template< typename MT // Type of the adapted matrix
594  , bool SO // Storage order of the adapted matrix
595  , bool DF > // Density flag
596 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix )
597 {
598  using BT = UnderlyingBuiltin_t< ElementType_t<MT> >;
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>();
605  }
606  }
607 
608  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
609 }
611 //*************************************************************************************************
612 
613 
614 //*************************************************************************************************
623 template< typename MT // Type of the adapted matrix
624  , bool SO // Storage order of the adapted matrix
625  , bool DF // Density flag
626  , typename Arg > // Min/max argument type
627 void makeSymmetric( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
628 {
629  using BT = UnderlyingBuiltin_t< ElementType_t<MT> >;
630 
631  const size_t n( matrix.rows() );
632 
633  for( size_t i=0UL; i<n; ++i ) {
634  for( size_t j=0UL; j<=i; ++j ) {
635  matrix(i,j) = rand<BT>( real( min ), real( max ) );
636  }
637  }
638 
639  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
640 }
642 //*************************************************************************************************
643 
644 
645 //*************************************************************************************************
652 template< typename MT // Type of the adapted matrix
653  , bool SO // Storage order of the adapted matrix
654  , bool DF > // Density flag
655 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix )
656 {
657  using blaze::randomize;
658 
659  randomize( matrix );
660 }
662 //*************************************************************************************************
663 
664 
665 //*************************************************************************************************
674 template< typename MT // Type of the adapted matrix
675  , bool SO // Storage order of the adapted matrix
676  , bool DF // Density flag
677  , typename Arg > // Min/max argument type
678 void makeHermitian( HermitianMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
679 {
680  using blaze::randomize;
681 
682  randomize( matrix, min, max );
683 }
685 //*************************************************************************************************
686 
687 
688 //*************************************************************************************************
695 template< typename MT // Type of the adapted matrix
696  , bool SO // Storage order of the adapted matrix
697  , bool DF > // Density flag
698 void makePositiveDefinite( HermitianMatrix<MT,SO,DF>& matrix )
699 {
700  using blaze::randomize;
701 
702  using BT = UnderlyingBuiltin_t< ElementType_t<MT> >;
703 
704  const size_t n( matrix.rows() );
705 
706  randomize( matrix );
707  matrix *= matrix;
708 
709  for( size_t i=0UL; i<n; ++i ) {
710  matrix(i,i) += BT(n);
711  }
712 }
714 //*************************************************************************************************
715 
716 } // namespace blaze
717 
718 #endif
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: IntegralConstant.h:121
#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
Header file for auxiliary alias declarations.
decltype(auto) column(Matrix< MT, SO > &matrix, RCAs... args)
Creating a view on a specific column of the given matrix.
Definition: Column.h:133
Header file for basic type definitions.
decltype(auto) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1389
#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
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:1240
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
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: IntegralConstant.h:132
Header file for the implementation of a Hermitian matrix adaptor.
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:292
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for all basic SparseMatrix functionality.
Header file for the UnderlyingBuiltin type trait.
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1162
Header file for the exception macros of the math module.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1198
Header file for the IsDenseMatrix type trait.
Header file for run time assertion macros.
decltype(auto) row(Matrix< MT, SO > &, RRAs...)
Creating a view on a specific row of the given matrix.
Definition: Row.h:133
#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 'resize' member fu...
Definition: Resizable.h:81
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:1328
T generate() const
Generation of a random value in the range .
Definition: Random.h:252
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 'res...
Definition: Resizable.h:61
Header file for the implementation of a diagonal matrix adaptor.
Header file for the IntegralConstant class template.
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,...
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