SymmetricMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SYMMETRICMATRIX_H_
36 #define _BLAZE_MATH_SYMMETRICMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <blaze/math/Aliases.h>
50 #include <blaze/math/DenseMatrix.h>
51 #include <blaze/math/Exception.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  , bool NF > // Numeric flag
82 class Rand< SymmetricMatrix<MT,SO,DF,NF> >
83 {
84  public:
85  //**Generate functions**************************************************************************
88  inline const SymmetricMatrix<MT,SO,DF,NF> generate() const;
89  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n ) const;
90  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros ) const;
91 
92  template< typename Arg >
93  inline const SymmetricMatrix<MT,SO,DF,NF> generate( const Arg& min, const Arg& max ) const;
94 
95  template< typename Arg >
96  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, const Arg& min, const Arg& max ) const;
97 
98  template< typename Arg >
99  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros,
100  const Arg& min, const Arg& max ) const;
102  //**********************************************************************************************
103 
104  //**Randomize functions*************************************************************************
107  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const;
108  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const;
109 
110  template< typename Arg >
111  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max ) const;
112 
113  template< typename Arg >
114  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros,
115  const Arg& min, const Arg& max ) const;
117  //**********************************************************************************************
118 
119  private:
120  //**Randomize functions*************************************************************************
123  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const;
124  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const;
125 
126  template< typename Arg >
127  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
128 
129  template< typename Arg >
130  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
132  //**********************************************************************************************
133 };
135 //*************************************************************************************************
136 
137 
138 //*************************************************************************************************
144 template< typename MT // Type of the adapted matrix
145  , bool SO // Storage order of the adapted matrix
146  , bool DF // Density flag
147  , bool NF > // Numeric flag
148 inline const SymmetricMatrix<MT,SO,DF,NF> Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate() const
149 {
151 
152  SymmetricMatrix<MT,SO,DF,NF> matrix;
153  randomize( matrix );
154  return matrix;
155 }
157 //*************************************************************************************************
158 
159 
160 //*************************************************************************************************
167 template< typename MT // Type of the adapted matrix
168  , bool SO // Storage order of the adapted matrix
169  , bool DF // Density flag
170  , bool NF > // Numeric flag
171 inline const SymmetricMatrix<MT,SO,DF,NF>
172  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n ) const
173 {
175 
176  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
177  randomize( matrix );
178  return matrix;
179 }
181 //*************************************************************************************************
182 
183 
184 //*************************************************************************************************
193 template< typename MT // Type of the adapted matrix
194  , bool SO // Storage order of the adapted matrix
195  , bool DF // Density flag
196  , bool NF > // Numeric flag
197 inline const SymmetricMatrix<MT,SO,DF,NF>
198  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros ) const
199 {
202 
203  if( nonzeros > n*n ) {
204  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
205  }
206 
207  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
208  randomize( matrix, nonzeros );
209 
210  return matrix;
211 }
213 //*************************************************************************************************
214 
215 
216 //*************************************************************************************************
224 template< typename MT // Type of the adapted matrix
225  , bool SO // Storage order of the adapted matrix
226  , bool DF // Density flag
227  , bool NF > // Numeric flag
228 template< typename Arg > // Min/max argument type
229 inline const SymmetricMatrix<MT,SO,DF,NF>
230  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( const Arg& min, const Arg& max ) const
231 {
233 
234  SymmetricMatrix<MT,SO,DF,NF> matrix;
235  randomize( matrix, min, max );
236  return matrix;
237 }
239 //*************************************************************************************************
240 
241 
242 //*************************************************************************************************
251 template< typename MT // Type of the adapted matrix
252  , bool SO // Storage order of the adapted matrix
253  , bool DF // Density flag
254  , bool NF > // Numeric flag
255 template< typename Arg > // Min/max argument type
256 inline const SymmetricMatrix<MT,SO,DF,NF>
257  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, const Arg& min, const Arg& max ) const
258 {
260 
261  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
262  randomize( matrix, min, max );
263  return matrix;
264 }
266 //*************************************************************************************************
267 
268 
269 //*************************************************************************************************
280 template< typename MT // Type of the adapted matrix
281  , bool SO // Storage order of the adapted matrix
282  , bool DF // Density flag
283  , bool NF > // Numeric flag
284 template< typename Arg > // Min/max argument type
285 inline const SymmetricMatrix<MT,SO,DF,NF>
286  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros,
287  const Arg& min, const Arg& max ) const
288 {
291 
292  if( nonzeros > n*n ) {
293  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
294  }
295 
296  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
297  randomize( matrix, nonzeros, min, max );
298 
299  return matrix;
300 }
302 //*************************************************************************************************
303 
304 
305 //*************************************************************************************************
312 template< typename MT // Type of the adapted matrix
313  , bool SO // Storage order of the adapted matrix
314  , bool DF // Density flag
315  , bool NF > // Numeric flag
316 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const
317 {
318  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
319 }
321 //*************************************************************************************************
322 
323 
324 //*************************************************************************************************
331 template< typename MT // Type of the adapted matrix
332  , bool SO // Storage order of the adapted matrix
333  , bool DF // Density flag
334  , bool NF > // Numeric flag
335 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const
336 {
338 
339  using ET = ElementType_<MT>;
340 
341  const size_t n( matrix.rows() );
342 
343  for( size_t i=0UL; i<n; ++i ) {
344  for( size_t j=0UL; j<=i; ++j ) {
345  matrix(i,j) = rand<ET>();
346  }
347  }
348 }
350 //*************************************************************************************************
351 
352 
353 //*************************************************************************************************
360 template< typename MT // Type of the adapted matrix
361  , bool SO // Storage order of the adapted matrix
362  , bool DF // Density flag
363  , bool NF > // Numeric flag
364 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const
365 {
367 
368  const size_t n( matrix.rows() );
369 
370  if( n == 0UL ) return;
371 
372  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
373 
374  randomize( matrix, nonzeros );
375 }
377 //*************************************************************************************************
378 
379 
380 //*************************************************************************************************
389 template< typename MT // Type of the adapted matrix
390  , bool SO // Storage order of the adapted matrix
391  , bool DF // Density flag
392  , bool NF > // Numeric flag
393 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const
394 {
396 
397  using ET = ElementType_<MT>;
398 
399  const size_t n( matrix.rows() );
400 
401  if( nonzeros > n*n ) {
402  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
403  }
404 
405  if( n == 0UL ) return;
406 
407  matrix.reset();
408  matrix.reserve( nonzeros+1UL );
409 
410  while( matrix.nonZeros() < nonzeros ) {
411  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>();
412  }
413 }
415 //*************************************************************************************************
416 
417 
418 //*************************************************************************************************
427 template< typename MT // Type of the adapted matrix
428  , bool SO // Storage order of the adapted matrix
429  , bool DF // Density flag
430  , bool NF > // Numeric flag
431 template< typename Arg > // Min/max argument type
432 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
433  const Arg& min, const Arg& max ) const
434 {
435  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
436 }
438 //*************************************************************************************************
439 
440 
441 //*************************************************************************************************
450 template< typename MT // Type of the adapted matrix
451  , bool SO // Storage order of the adapted matrix
452  , bool DF // Density flag
453  , bool NF > // Numeric flag
454 template< typename Arg > // Min/max argument type
455 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
456  const Arg& min, const Arg& max, TrueType ) const
457 {
459 
460  using ET = ElementType_<MT>;
461 
462  const size_t n( matrix.rows() );
463 
464  for( size_t i=0UL; i<n; ++i ) {
465  for( size_t j=0UL; j<=i; ++j ) {
466  matrix(i,j) = rand<ET>( min, max );
467  }
468  }
469 }
471 //*************************************************************************************************
472 
473 
474 //*************************************************************************************************
483 template< typename MT // Type of the adapted matrix
484  , bool SO // Storage order of the adapted matrix
485  , bool DF // Density flag
486  , bool NF > // Numeric flag
487 template< typename Arg > // Min/max argument type
488 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
489  const Arg& min, const Arg& max, FalseType ) const
490 {
492 
493  const size_t n( matrix.rows() );
494 
495  if( n == 0UL ) return;
496 
497  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
498 
499  randomize( matrix, nonzeros, min, max );
500 }
502 //*************************************************************************************************
503 
504 
505 //*************************************************************************************************
516 template< typename MT // Type of the adapted matrix
517  , bool SO // Storage order of the adapted matrix
518  , bool DF // Density flag
519  , bool NF > // Numeric flag
520 template< typename Arg > // Min/max argument type
521 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
522  size_t nonzeros, const Arg& min, const Arg& max ) const
523 {
525 
526  using ET = ElementType_<MT>;
527 
528  const size_t n( matrix.rows() );
529 
530  if( nonzeros > n*n ) {
531  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
532  }
533 
534  if( n == 0UL ) return;
535 
536  matrix.reset();
537  matrix.reserve( nonzeros+1UL );
538 
539  while( matrix.nonZeros() < nonzeros ) {
540  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = 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 NF > // Numeric flag
565 void makeSymmetric( SymmetricMatrix<MT,SO,true,NF>& matrix )
566 {
567  using blaze::randomize;
568 
569  randomize( matrix );
570 }
572 //*************************************************************************************************
573 
574 
575 //*************************************************************************************************
584 template< typename MT // Type of the adapted matrix
585  , bool SO // Storage order of the adapted matrix
586  , bool NF // Numeric flag
587  , typename Arg > // Min/max argument type
588 void makeSymmetric( SymmetricMatrix<MT,SO,true,NF>& matrix, const Arg& min, const Arg& max )
589 {
590  using blaze::randomize;
591 
592  randomize( matrix, min, max );
593 }
595 //*************************************************************************************************
596 
597 
598 //*************************************************************************************************
605 template< typename MT // Type of the adapted matrix
606  , bool SO // Storage order of the adapted matrix
607  , bool NF > // Numeric flag
608 void makeHermitian( SymmetricMatrix<MT,SO,true,NF>& matrix )
609 {
610  using BT = UnderlyingBuiltin_< ElementType_<MT> >;
611 
612  const size_t n( matrix.rows() );
613 
614  for( size_t i=0UL; i<n; ++i ) {
615  for( size_t j=0UL; j<=i; ++j ) {
616  matrix(i,j) = rand<BT>();
617  }
618  }
619 
620  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
621 }
623 //*************************************************************************************************
624 
625 
626 //*************************************************************************************************
635 template< typename MT // Type of the adapted matrix
636  , bool SO // Storage order of the adapted matrix
637  , bool NF // Numeric flag
638  , typename Arg > // Min/max argument type
639 void makeHermitian( SymmetricMatrix<MT,SO,true,NF>& matrix, const Arg& min, const Arg& max )
640 {
641  using BT = UnderlyingBuiltin_< ElementType_<MT> >;
642 
643  const size_t n( matrix.rows() );
644 
645  for( size_t i=0UL; i<n; ++i ) {
646  for( size_t j=0UL; j<=i; ++j ) {
647  matrix(i,j) = rand<BT>( real( min ), real( max ) );
648  }
649  }
650 
651  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
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 NF > // Numeric flag
667 void makePositiveDefinite( SymmetricMatrix<MT,SO,true,NF>& matrix )
668 {
669  using BT = UnderlyingBuiltin_< ElementType_<MT> >;
670 
671  const size_t n( matrix.rows() );
672 
673  makeHermitian( matrix );
674  matrix *= matrix;
675 
676  for( size_t i=0UL; i<n; ++i ) {
677  matrix(i,i) += BT(n);
678  }
679 
680  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
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
#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.
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
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
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1903
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:1950
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
Header file for the implementation of a symmetric matrix adaptor.
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.
Header file for the exception macros of the math module.
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 isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:919
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 &#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.