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>
49 #include <blaze/math/DenseMatrix.h>
50 #include <blaze/math/Exception.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  , bool NF > // Numeric flag
81 class Rand< SymmetricMatrix<MT,SO,DF,NF> >
82 {
83  public:
84  //**Generate functions**************************************************************************
87  inline const SymmetricMatrix<MT,SO,DF,NF> generate() const;
88  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n ) const;
89  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros ) const;
90 
91  template< typename Arg >
92  inline const SymmetricMatrix<MT,SO,DF,NF> generate( const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, const Arg& min, const Arg& max ) const;
96 
97  template< typename Arg >
98  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros,
99  const Arg& min, const Arg& max ) const;
101  //**********************************************************************************************
102 
103  //**Randomize functions*************************************************************************
106  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const;
107  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const;
108 
109  template< typename Arg >
110  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max ) const;
111 
112  template< typename Arg >
113  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros,
114  const Arg& min, const Arg& max ) const;
116  //**********************************************************************************************
117 
118  private:
119  //**Randomize functions*************************************************************************
122  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const;
123  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const;
124 
125  template< typename Arg >
126  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
127 
128  template< typename Arg >
129  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& 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  , bool NF > // Numeric flag
147 inline const SymmetricMatrix<MT,SO,DF,NF> Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate() const
148 {
150 
151  SymmetricMatrix<MT,SO,DF,NF> matrix;
152  randomize( matrix );
153  return matrix;
154 }
156 //*************************************************************************************************
157 
158 
159 //*************************************************************************************************
166 template< typename MT // Type of the adapted matrix
167  , bool SO // Storage order of the adapted matrix
168  , bool DF // Density flag
169  , bool NF > // Numeric flag
170 inline const SymmetricMatrix<MT,SO,DF,NF>
171  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n ) const
172 {
174 
175  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
176  randomize( matrix );
177  return matrix;
178 }
180 //*************************************************************************************************
181 
182 
183 //*************************************************************************************************
192 template< typename MT // Type of the adapted matrix
193  , bool SO // Storage order of the adapted matrix
194  , bool DF // Density flag
195  , bool NF > // Numeric flag
196 inline const SymmetricMatrix<MT,SO,DF,NF>
197  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros ) const
198 {
201 
202  if( nonzeros > n*n ) {
203  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
204  }
205 
206  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
207  randomize( matrix, nonzeros );
208 
209  return matrix;
210 }
212 //*************************************************************************************************
213 
214 
215 //*************************************************************************************************
223 template< typename MT // Type of the adapted matrix
224  , bool SO // Storage order of the adapted matrix
225  , bool DF // Density flag
226  , bool NF > // Numeric flag
227 template< typename Arg > // Min/max argument type
228 inline const SymmetricMatrix<MT,SO,DF,NF>
229  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( const Arg& min, const Arg& max ) const
230 {
232 
233  SymmetricMatrix<MT,SO,DF,NF> matrix;
234  randomize( matrix, min, max );
235  return matrix;
236 }
238 //*************************************************************************************************
239 
240 
241 //*************************************************************************************************
250 template< typename MT // Type of the adapted matrix
251  , bool SO // Storage order of the adapted matrix
252  , bool DF // Density flag
253  , bool NF > // Numeric flag
254 template< typename Arg > // Min/max argument type
255 inline const SymmetricMatrix<MT,SO,DF,NF>
256  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, const Arg& min, const Arg& max ) const
257 {
259 
260  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
261  randomize( matrix, min, max );
262  return matrix;
263 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
279 template< typename MT // Type of the adapted matrix
280  , bool SO // Storage order of the adapted matrix
281  , bool DF // Density flag
282  , bool NF > // Numeric flag
283 template< typename Arg > // Min/max argument type
284 inline const SymmetricMatrix<MT,SO,DF,NF>
285  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros,
286  const Arg& min, const Arg& max ) const
287 {
290 
291  if( nonzeros > n*n ) {
292  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
293  }
294 
295  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
296  randomize( matrix, nonzeros, min, max );
297 
298  return matrix;
299 }
301 //*************************************************************************************************
302 
303 
304 //*************************************************************************************************
311 template< typename MT // Type of the adapted matrix
312  , bool SO // Storage order of the adapted matrix
313  , bool DF // Density flag
314  , bool NF > // Numeric flag
315 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const
316 {
317  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
318 }
320 //*************************************************************************************************
321 
322 
323 //*************************************************************************************************
330 template< typename MT // Type of the adapted matrix
331  , bool SO // Storage order of the adapted matrix
332  , bool DF // Density flag
333  , bool NF > // Numeric flag
334 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const
335 {
337 
338  typedef ElementType_<MT> ET;
339 
340  const size_t n( matrix.rows() );
341 
342  for( size_t i=0UL; i<n; ++i ) {
343  for( size_t j=0UL; j<=i; ++j ) {
344  matrix(i,j) = rand<ET>();
345  }
346  }
347 }
349 //*************************************************************************************************
350 
351 
352 //*************************************************************************************************
359 template< typename MT // Type of the adapted matrix
360  , bool SO // Storage order of the adapted matrix
361  , bool DF // Density flag
362  , bool NF > // Numeric flag
363 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const
364 {
366 
367  const size_t n( matrix.rows() );
368 
369  if( n == 0UL ) return;
370 
371  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
372 
373  randomize( matrix, nonzeros );
374 }
376 //*************************************************************************************************
377 
378 
379 //*************************************************************************************************
388 template< typename MT // Type of the adapted matrix
389  , bool SO // Storage order of the adapted matrix
390  , bool DF // Density flag
391  , bool NF > // Numeric flag
392 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const
393 {
395 
396  typedef ElementType_<MT> ET;
397 
398  const size_t n( matrix.rows() );
399 
400  if( nonzeros > n*n ) {
401  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
402  }
403 
404  if( n == 0UL ) return;
405 
406  matrix.reset();
407  matrix.reserve( nonzeros+1UL );
408 
409  while( matrix.nonZeros() < nonzeros ) {
410  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = 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  , bool NF > // Numeric flag
430 template< typename Arg > // Min/max argument type
431 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
432  const Arg& min, const Arg& max ) const
433 {
434  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
435 }
437 //*************************************************************************************************
438 
439 
440 //*************************************************************************************************
449 template< typename MT // Type of the adapted matrix
450  , bool SO // Storage order of the adapted matrix
451  , bool DF // Density flag
452  , bool NF > // Numeric flag
453 template< typename Arg > // Min/max argument type
454 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
455  const Arg& min, const Arg& max, TrueType ) const
456 {
458 
459  typedef ElementType_<MT> ET;
460 
461  const size_t n( matrix.rows() );
462 
463  for( size_t i=0UL; i<n; ++i ) {
464  for( size_t j=0UL; j<=i; ++j ) {
465  matrix(i,j) = rand<ET>( min, max );
466  }
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  , bool NF > // Numeric flag
486 template< typename Arg > // Min/max argument type
487 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
488  const Arg& min, const Arg& max, FalseType ) const
489 {
491 
492  const size_t n( matrix.rows() );
493 
494  if( n == 0UL ) return;
495 
496  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
497 
498  randomize( matrix, nonzeros, min, max );
499 }
501 //*************************************************************************************************
502 
503 
504 //*************************************************************************************************
515 template< typename MT // Type of the adapted matrix
516  , bool SO // Storage order of the adapted matrix
517  , bool DF // Density flag
518  , bool NF > // Numeric flag
519 template< typename Arg > // Min/max argument type
520 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
521  size_t nonzeros, const Arg& min, const Arg& max ) const
522 {
524 
525  typedef ElementType_<MT> ET;
526 
527  const size_t n( matrix.rows() );
528 
529  if( nonzeros > n*n ) {
530  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
531  }
532 
533  if( n == 0UL ) return;
534 
535  matrix.reset();
536  matrix.reserve( nonzeros+1UL );
537 
538  while( matrix.nonZeros() < nonzeros ) {
539  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>( min, max );
540  }
541 }
543 //*************************************************************************************************
544 
545 
546 
547 
548 //=================================================================================================
549 //
550 // MAKE FUNCTIONS
551 //
552 //=================================================================================================
553 
554 //*************************************************************************************************
561 template< typename MT // Type of the adapted matrix
562  , bool SO // Storage order of the adapted matrix
563  , bool NF > // Numeric flag
564 void makeSymmetric( SymmetricMatrix<MT,SO,true,NF>& matrix )
565 {
566  using blaze::randomize;
567 
568  randomize( matrix );
569 }
571 //*************************************************************************************************
572 
573 
574 //*************************************************************************************************
583 template< typename MT // Type of the adapted matrix
584  , bool SO // Storage order of the adapted matrix
585  , bool NF // Numeric flag
586  , typename Arg > // Min/max argument type
587 void makeSymmetric( SymmetricMatrix<MT,SO,true,NF>& matrix, const Arg& min, const Arg& max )
588 {
589  using blaze::randomize;
590 
591  randomize( matrix, min, max );
592 }
594 //*************************************************************************************************
595 
596 
597 //*************************************************************************************************
604 template< typename MT // Type of the adapted matrix
605  , bool SO // Storage order of the adapted matrix
606  , bool NF > // Numeric flag
607 void makeHermitian( SymmetricMatrix<MT,SO,true,NF>& matrix )
608 {
609  typedef UnderlyingBuiltin_< ElementType_<MT> > BT;
610 
611  const size_t n( matrix.rows() );
612 
613  for( size_t i=0UL; i<n; ++i ) {
614  for( size_t j=0UL; j<=i; ++j ) {
615  matrix(i,j) = rand<BT>();
616  }
617  }
618 
619  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
620 }
622 //*************************************************************************************************
623 
624 
625 //*************************************************************************************************
634 template< typename MT // Type of the adapted matrix
635  , bool SO // Storage order of the adapted matrix
636  , bool NF // Numeric flag
637  , typename Arg > // Min/max argument type
638 void makeHermitian( SymmetricMatrix<MT,SO,true,NF>& matrix, const Arg& min, const Arg& max )
639 {
640  typedef UnderlyingBuiltin_< ElementType_<MT> > BT;
641 
642  const size_t n( matrix.rows() );
643 
644  for( size_t i=0UL; i<n; ++i ) {
645  for( size_t j=0UL; j<=i; ++j ) {
646  matrix(i,j) = rand<BT>( real( min ), real( max ) );
647  }
648  }
649 
650  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
651 }
653 //*************************************************************************************************
654 
655 
656 //*************************************************************************************************
663 template< typename MT // Type of the adapted matrix
664  , bool SO // Storage order of the adapted matrix
665  , bool NF > // Numeric flag
666 void makePositiveDefinite( SymmetricMatrix<MT,SO,true,NF>& matrix )
667 {
668  typedef UnderlyingBuiltin_< ElementType_<MT> > BT;
669 
670  const size_t n( matrix.rows() );
671 
672  makeHermitian( matrix );
673  matrix *= matrix;
674 
675  for( size_t i=0UL; i<n; ++i ) {
676  matrix(i,i) += BT(n);
677  }
678 
679  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
680 }
682 //*************************************************************************************************
683 
684 } // namespace blaze
685 
686 #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.
#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.
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
Constraint on the data type.
Constraint on the data type.
Header file for the implementation of a symmetric matrix adaptor.
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 exception macros of the math module.
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
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
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:759
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