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>
48 #include <blaze/math/DenseMatrix.h>
49 #include <blaze/math/shims/Real.h>
53 #include <blaze/util/Assert.h>
54 #include <blaze/util/Exception.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  , bool NF > // Numeric flag
80 class Rand< SymmetricMatrix<MT,SO,DF,NF> >
81 {
82  public:
83  //**Generate functions**************************************************************************
86  inline const SymmetricMatrix<MT,SO,DF,NF> generate() const;
87  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n ) const;
88  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros ) const;
89 
90  template< typename Arg >
91  inline const SymmetricMatrix<MT,SO,DF,NF> generate( const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros,
98  const Arg& min, const Arg& max ) const;
100  //**********************************************************************************************
101 
102  //**Randomize functions*************************************************************************
105  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const;
106  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const;
107 
108  template< typename Arg >
109  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max ) const;
110 
111  template< typename Arg >
112  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros,
113  const Arg& min, const Arg& max ) const;
115  //**********************************************************************************************
116 
117  private:
118  //**Randomize functions*************************************************************************
121  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const;
122  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const;
123 
124  template< typename Arg >
125  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
126 
127  template< typename Arg >
128  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& 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  , bool NF > // Numeric flag
146 inline const SymmetricMatrix<MT,SO,DF,NF> Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate() const
147 {
149 
150  SymmetricMatrix<MT,SO,DF,NF> 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  , bool NF > // Numeric flag
169 inline const SymmetricMatrix<MT,SO,DF,NF>
170  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n ) const
171 {
173 
174  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
175  randomize( matrix );
176  return matrix;
177 }
179 //*************************************************************************************************
180 
181 
182 //*************************************************************************************************
191 template< typename MT // Type of the adapted matrix
192  , bool SO // Storage order of the adapted matrix
193  , bool DF // Density flag
194  , bool NF > // Numeric flag
195 inline const SymmetricMatrix<MT,SO,DF,NF>
196  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros ) const
197 {
200 
201  if( nonzeros > n*n ) {
202  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
203  }
204 
205  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
206  randomize( matrix, nonzeros );
207 
208  return matrix;
209 }
211 //*************************************************************************************************
212 
213 
214 //*************************************************************************************************
222 template< typename MT // Type of the adapted matrix
223  , bool SO // Storage order of the adapted matrix
224  , bool DF // Density flag
225  , bool NF > // Numeric flag
226 template< typename Arg > // Min/max argument type
227 inline const SymmetricMatrix<MT,SO,DF,NF>
228  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( const Arg& min, const Arg& max ) const
229 {
231 
232  SymmetricMatrix<MT,SO,DF,NF> matrix;
233  randomize( matrix, min, max );
234  return matrix;
235 }
237 //*************************************************************************************************
238 
239 
240 //*************************************************************************************************
249 template< typename MT // Type of the adapted matrix
250  , bool SO // Storage order of the adapted matrix
251  , bool DF // Density flag
252  , bool NF > // Numeric flag
253 template< typename Arg > // Min/max argument type
254 inline const SymmetricMatrix<MT,SO,DF,NF>
255  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, const Arg& min, const Arg& max ) const
256 {
258 
259  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
260  randomize( matrix, min, max );
261  return matrix;
262 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
278 template< typename MT // Type of the adapted matrix
279  , bool SO // Storage order of the adapted matrix
280  , bool DF // Density flag
281  , bool NF > // Numeric flag
282 template< typename Arg > // Min/max argument type
283 inline const SymmetricMatrix<MT,SO,DF,NF>
284  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros,
285  const Arg& min, const Arg& max ) const
286 {
289 
290  if( nonzeros > n*n ) {
291  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
292  }
293 
294  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
295  randomize( matrix, nonzeros, min, max );
296 
297  return matrix;
298 }
300 //*************************************************************************************************
301 
302 
303 //*************************************************************************************************
310 template< typename MT // Type of the adapted matrix
311  , bool SO // Storage order of the adapted matrix
312  , bool DF // Density flag
313  , bool NF > // Numeric flag
314 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const
315 {
316  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
317 }
319 //*************************************************************************************************
320 
321 
322 //*************************************************************************************************
329 template< typename MT // Type of the adapted matrix
330  , bool SO // Storage order of the adapted matrix
331  , bool DF // Density flag
332  , bool NF > // Numeric flag
333 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const
334 {
336 
337  typedef typename MT::ElementType ET;
338 
339  const size_t n( matrix.rows() );
340 
341  for( size_t i=0UL; i<n; ++i ) {
342  for( size_t j=0UL; j<=i; ++j ) {
343  matrix(i,j) = rand<ET>();
344  }
345  }
346 }
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
358 template< typename MT // Type of the adapted matrix
359  , bool SO // Storage order of the adapted matrix
360  , bool DF // Density flag
361  , bool NF > // Numeric flag
362 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const
363 {
365 
366  typedef typename MT::ElementType ET;
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  matrix.reset();
375  matrix.reserve( nonzeros );
376 
377  while( matrix.nonZeros() < nonzeros ) {
378  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>();
379  }
380 }
382 //*************************************************************************************************
383 
384 
385 //*************************************************************************************************
394 template< typename MT // Type of the adapted matrix
395  , bool SO // Storage order of the adapted matrix
396  , bool DF // Density flag
397  , bool NF > // Numeric flag
398 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const
399 {
401 
402  typedef typename MT::ElementType ET;
403 
404  const size_t n( matrix.rows() );
405 
406  if( nonzeros > n*n ) {
407  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
408  }
409 
410  if( n == 0UL ) return;
411 
412  matrix.reset();
413  matrix.reserve( nonzeros );
414 
415  while( matrix.nonZeros() < nonzeros ) {
416  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>();
417  }
418 }
420 //*************************************************************************************************
421 
422 
423 //*************************************************************************************************
432 template< typename MT // Type of the adapted matrix
433  , bool SO // Storage order of the adapted matrix
434  , bool DF // Density flag
435  , bool NF > // Numeric flag
436 template< typename Arg > // Min/max argument type
437 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
438  const Arg& min, const Arg& max ) const
439 {
440  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
441 }
443 //*************************************************************************************************
444 
445 
446 //*************************************************************************************************
455 template< typename MT // Type of the adapted matrix
456  , bool SO // Storage order of the adapted matrix
457  , bool DF // Density flag
458  , bool NF > // Numeric flag
459 template< typename Arg > // Min/max argument type
460 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
461  const Arg& min, const Arg& max, TrueType ) const
462 {
464 
465  typedef typename MT::ElementType ET;
466 
467  const size_t n( matrix.rows() );
468 
469  for( size_t i=0UL; i<n; ++i ) {
470  for( size_t j=0UL; j<=i; ++j ) {
471  matrix(i,j) = rand<ET>( min, max );
472  }
473  }
474 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
488 template< typename MT // Type of the adapted matrix
489  , bool SO // Storage order of the adapted matrix
490  , bool DF // Density flag
491  , bool NF > // Numeric flag
492 template< typename Arg > // Min/max argument type
493 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
494  const Arg& min, const Arg& max, FalseType ) const
495 {
497 
498  typedef typename MT::ElementType ET;
499 
500  const size_t n( matrix.rows() );
501 
502  if( n == 0UL ) return;
503 
504  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
505 
506  matrix.reset();
507  matrix.reserve( nonzeros );
508 
509  while( matrix.nonZeros() < nonzeros ) {
510  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>( min, max );
511  }
512 }
514 //*************************************************************************************************
515 
516 
517 //*************************************************************************************************
528 template< typename MT // Type of the adapted matrix
529  , bool SO // Storage order of the adapted matrix
530  , bool DF // Density flag
531  , bool NF > // Numeric flag
532 template< typename Arg > // Min/max argument type
533 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
534  size_t nonzeros, const Arg& min, const Arg& max ) const
535 {
537 
538  typedef typename MT::ElementType ET;
539 
540  const size_t n( matrix.rows() );
541 
542  if( nonzeros > n*n ) {
543  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
544  }
545 
546  if( n == 0UL ) return;
547 
548  matrix.reset();
549  matrix.reserve( nonzeros );
550 
551  while( matrix.nonZeros() < nonzeros ) {
552  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>( min, max );
553  }
554 }
556 //*************************************************************************************************
557 
558 
559 
560 
561 //=================================================================================================
562 //
563 // MAKE FUNCTIONS
564 //
565 //=================================================================================================
566 
567 //*************************************************************************************************
574 template< typename MT // Type of the adapted matrix
575  , bool SO // Storage order of the adapted matrix
576  , bool NF > // Numeric flag
577 void makeSymmetric( SymmetricMatrix<MT,SO,true,NF>& matrix )
578 {
579  using blaze::randomize;
580 
581  randomize( matrix );
582 }
584 //*************************************************************************************************
585 
586 
587 //*************************************************************************************************
596 template< typename MT // Type of the adapted matrix
597  , bool SO // Storage order of the adapted matrix
598  , bool NF // Numeric flag
599  , typename Arg > // Min/max argument type
600 void makeSymmetric( SymmetricMatrix<MT,SO,true,NF>& matrix, const Arg& min, const Arg& max )
601 {
602  using blaze::randomize;
603 
604  randomize( matrix, min, max );
605 }
607 //*************************************************************************************************
608 
609 
610 //*************************************************************************************************
617 template< typename MT // Type of the adapted matrix
618  , bool SO // Storage order of the adapted matrix
619  , bool NF > // Numeric flag
620 void makeHermitian( SymmetricMatrix<MT,SO,true,NF>& matrix )
621 {
622  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type BT;
623 
624  const size_t n( matrix.rows() );
625 
626  for( size_t i=0UL; i<n; ++i ) {
627  for( size_t j=0UL; j<=i; ++j ) {
628  matrix(i,j) = rand<BT>();
629  }
630  }
631 
632  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
633 }
635 //*************************************************************************************************
636 
637 
638 //*************************************************************************************************
647 template< typename MT // Type of the adapted matrix
648  , bool SO // Storage order of the adapted matrix
649  , bool NF // Numeric flag
650  , typename Arg > // Min/max argument type
651 void makeHermitian( SymmetricMatrix<MT,SO,true,NF>& matrix, const Arg& min, const Arg& max )
652 {
653  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type BT;
654 
655  const size_t n( matrix.rows() );
656 
657  for( size_t i=0UL; i<n; ++i ) {
658  for( size_t j=0UL; j<=i; ++j ) {
659  matrix(i,j) = rand<BT>( real( min ), real( max ) );
660  }
661  }
662 
663  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
664 }
666 //*************************************************************************************************
667 
668 
669 //*************************************************************************************************
676 template< typename MT // Type of the adapted matrix
677  , bool SO // Storage order of the adapted matrix
678  , bool NF > // Numeric flag
679 void makePositiveDefinite( SymmetricMatrix<MT,SO,true,NF>& matrix )
680 {
681  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type BT;
682 
683  const size_t n( matrix.rows() );
684 
685  makeHermitian( matrix );
686  matrix *= matrix;
687 
688  for( size_t i=0UL; i<n; ++i ) {
689  matrix(i,i) += BT(n);
690  }
691 
692  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
693 }
695 //*************************************************************************************************
696 
697 } // namespace blaze
698 
699 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exceptionThis macro encapsulates the default way of...
Definition: Exception.h:187
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.
Implementation of a random number generator.
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: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.
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
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:767
Header file for exception macros.
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.