DiagonalMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_DIAGONALMATRIX_H_
36 #define _BLAZE_MATH_DIAGONALMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
47 #include <blaze/math/DenseMatrix.h>
50 #include <blaze/util/Exception.h>
51 #include <blaze/util/FalseType.h>
52 #include <blaze/util/Random.h>
53 #include <blaze/util/TrueType.h>
54 #include <blaze/util/Types.h>
55 
56 
57 namespace blaze {
58 
59 //=================================================================================================
60 //
61 // RAND SPECIALIZATION
62 //
63 //=================================================================================================
64 
65 //*************************************************************************************************
72 template< typename MT // Type of the adapted matrix
73  , bool SO // Storage order of the adapted matrix
74  , bool DF > // Numeric flag
75 class Rand< DiagonalMatrix<MT,SO,DF> >
76 {
77  public:
78  //**Generate functions**************************************************************************
81  inline const DiagonalMatrix<MT,SO,DF> generate() const;
82  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n ) const;
83  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
84 
85  template< typename Arg >
86  inline const DiagonalMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
87 
88  template< typename Arg >
89  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
90 
91  template< typename Arg >
92  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
93  const Arg& min, const Arg& max ) const;
95  //**********************************************************************************************
96 
97  //**Randomize functions*************************************************************************
100  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const;
101  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
102 
103  template< typename Arg >
104  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
105 
106  template< typename Arg >
107  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros,
108  const Arg& min, const Arg& max ) const;
110  //**********************************************************************************************
111 
112  private:
113  //**Randomize functions*************************************************************************
116  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const;
117  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const;
118 
119  template< typename Arg >
120  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
121 
122  template< typename Arg >
123  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
125  //**********************************************************************************************
126 };
128 //*************************************************************************************************
129 
130 
131 //*************************************************************************************************
137 template< typename MT // Type of the adapted matrix
138  , bool SO // Storage order of the adapted matrix
139  , bool DF > // Numeric flag
140 inline const DiagonalMatrix<MT,SO,DF> Rand< DiagonalMatrix<MT,SO,DF> >::generate() const
141 {
143 
144  DiagonalMatrix<MT,SO,DF> matrix;
145  randomize( matrix );
146  return matrix;
147 }
149 //*************************************************************************************************
150 
151 
152 //*************************************************************************************************
159 template< typename MT // Type of the adapted matrix
160  , bool SO // Storage order of the adapted matrix
161  , bool DF > // Numeric flag
162 inline const DiagonalMatrix<MT,SO,DF>
163  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n ) const
164 {
166 
167  DiagonalMatrix<MT,SO,DF> matrix( n );
168  randomize( matrix );
169  return matrix;
170 }
172 //*************************************************************************************************
173 
174 
175 //*************************************************************************************************
184 template< typename MT // Type of the adapted matrix
185  , bool SO // Storage order of the adapted matrix
186  , bool DF > // Numeric flag
187 inline const DiagonalMatrix<MT,SO,DF>
188  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
189 {
192 
193  if( nonzeros > n ) {
194  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
195  }
196 
197  DiagonalMatrix<MT,SO,DF> matrix( n );
198  randomize( matrix, nonzeros );
199 
200  return matrix;
201 }
203 //*************************************************************************************************
204 
205 
206 //*************************************************************************************************
214 template< typename MT // Type of the adapted matrix
215  , bool SO // Storage order of the adapted matrix
216  , bool DF > // Numeric flag
217 template< typename Arg > // Min/max argument type
218 inline const DiagonalMatrix<MT,SO,DF>
219  Rand< DiagonalMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
220 {
222 
223  DiagonalMatrix<MT,SO,DF> matrix;
224  randomize( matrix, min, max );
225  return matrix;
226 }
228 //*************************************************************************************************
229 
230 
231 //*************************************************************************************************
240 template< typename MT // Type of the adapted matrix
241  , bool SO // Storage order of the adapted matrix
242  , bool DF > // Numeric flag
243 template< typename Arg > // Min/max argument type
244 inline const DiagonalMatrix<MT,SO,DF>
245  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
246 {
248 
249  DiagonalMatrix<MT,SO,DF> matrix( n );
250  randomize( matrix, min, max );
251  return matrix;
252 }
254 //*************************************************************************************************
255 
256 
257 //*************************************************************************************************
268 template< typename MT // Type of the adapted matrix
269  , bool SO // Storage order of the adapted matrix
270  , bool DF > // Numeric flag
271 template< typename Arg > // Min/max argument type
272 inline const DiagonalMatrix<MT,SO,DF>
273  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
274  const Arg& min, const Arg& max ) const
275 {
278 
279  if( nonzeros > DiagonalMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
280  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
281  }
282 
283  DiagonalMatrix<MT,SO,DF> matrix( n );
284  randomize( matrix, nonzeros, min, max );
285 
286  return matrix;
287 }
289 //*************************************************************************************************
290 
291 
292 //*************************************************************************************************
299 template< typename MT // Type of the adapted matrix
300  , bool SO // Storage order of the adapted matrix
301  , bool DF > // Numeric flag
302 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const
303 {
304  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
305 }
307 //*************************************************************************************************
308 
309 
310 //*************************************************************************************************
317 template< typename MT // Type of the adapted matrix
318  , bool SO // Storage order of the adapted matrix
319  , bool DF > // Numeric flag
320 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const
321 {
323 
324  typedef typename MT::ElementType ET;
325 
326  const size_t n( matrix.rows() );
327 
328  for( size_t i=0UL; i<n; ++i ) {
329  matrix(i,i) = rand<ET>();
330  }
331 }
333 //*************************************************************************************************
334 
335 
336 //*************************************************************************************************
343 template< typename MT // Type of the adapted matrix
344  , bool SO // Storage order of the adapted matrix
345  , bool DF > // Numeric flag
346 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const
347 {
349 
350  typedef typename MT::ElementType ET;
351 
352  const size_t n( matrix.rows() );
353 
354  if( n == 0UL ) return;
355 
356  const size_t nonzeros( rand<size_t>( 1UL, n ) );
357 
358  matrix.reset();
359  matrix.reserve( nonzeros );
360 
361  while( matrix.nonZeros() < nonzeros ) {
362  const size_t i( rand<size_t>( 0UL, n-1UL ) );
363  matrix(i,i) = rand<ET>();
364  }
365 }
367 //*************************************************************************************************
368 
369 
370 //*************************************************************************************************
379 template< typename MT // Type of the adapted matrix
380  , bool SO // Storage order of the adapted matrix
381  , bool DF > // Numeric flag
382 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
383 {
385 
386  typedef typename MT::ElementType ET;
387 
388  const size_t n( matrix.rows() );
389 
390  if( nonzeros > n ) {
391  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
392  }
393 
394  if( n == 0UL ) return;
395 
396  matrix.reset();
397  matrix.reserve( nonzeros );
398 
399  while( matrix.nonZeros() < nonzeros ) {
400  const size_t i( rand<size_t>( 0UL, n-1UL ) );
401  matrix(i,i) = rand<ET>();
402  }
403 }
405 //*************************************************************************************************
406 
407 
408 //*************************************************************************************************
417 template< typename MT // Type of the adapted matrix
418  , bool SO // Storage order of the adapted matrix
419  , bool DF > // Numeric flag
420 template< typename Arg > // Min/max argument type
421 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
422  const Arg& min, const Arg& max ) const
423 {
424  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
425 }
427 //*************************************************************************************************
428 
429 
430 //*************************************************************************************************
439 template< typename MT // Type of the adapted matrix
440  , bool SO // Storage order of the adapted matrix
441  , bool DF > // Numeric flag
442 template< typename Arg > // Min/max argument type
443 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
444  const Arg& min, const Arg& max, TrueType ) const
445 {
447 
448  typedef typename MT::ElementType ET;
449 
450  const size_t n( matrix.rows() );
451 
452  for( size_t i=0UL; i<n; ++i ) {
453  matrix(i,i) = rand<ET>( min, max );
454  }
455 }
457 //*************************************************************************************************
458 
459 
460 //*************************************************************************************************
469 template< typename MT // Type of the adapted matrix
470  , bool SO // Storage order of the adapted matrix
471  , bool DF > // Numeric flag
472 template< typename Arg > // Min/max argument type
473 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
474  const Arg& min, const Arg& max, FalseType ) const
475 {
477 
478  typedef typename MT::ElementType ET;
479 
480  const size_t n( matrix.rows() );
481 
482  if( n == 0UL ) return;
483 
484  const size_t nonzeros( rand<size_t>( 1UL, n ) );
485 
486  matrix.reset();
487  matrix.reserve( nonzeros );
488 
489  while( matrix.nonZeros() < nonzeros ) {
490  const size_t i( rand<size_t>( 0UL, n-1UL ) );
491  matrix(i,i) = rand<ET>( min, max );
492  }
493 }
495 //*************************************************************************************************
496 
497 
498 //*************************************************************************************************
509 template< typename MT // Type of the adapted matrix
510  , bool SO // Storage order of the adapted matrix
511  , bool DF > // Numeric flag
512 template< typename Arg > // Min/max argument type
513 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
514  size_t nonzeros, const Arg& min, const Arg& max ) const
515 {
517 
518  typedef typename MT::ElementType ET;
519 
520  const size_t n( matrix.rows() );
521 
522  if( nonzeros > n ) {
523  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
524  }
525 
526  if( n == 0UL ) return;
527 
528  matrix.reset();
529  matrix.reserve( nonzeros );
530 
531  while( matrix.nonZeros() < nonzeros ) {
532  const size_t i( rand<size_t>( 0UL, n-1UL ) );
533  matrix(i,i) = rand<ET>( min, max );
534  }
535 }
537 //*************************************************************************************************
538 
539 
540 
541 
542 //=================================================================================================
543 //
544 // MAKE FUNCTIONS
545 //
546 //=================================================================================================
547 
548 //*************************************************************************************************
555 template< typename MT // Type of the adapted matrix
556  , bool SO // Storage order of the adapted matrix
557  , bool DF > // Density flag
558 void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix )
559 {
560  const size_t n( matrix.rows() );
561 
562  reset( matrix );
563 
564  for( size_t i=0UL; i<n; ++i ) {
565  matrix(i,i) = rand<typename MT::ElementType>();
566  }
567 
568  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
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 DF // Density flag
586  , typename Arg > // Min/max argument type
587 void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
588 {
589  typedef typename MT::ElementType Type;
590 
591  const size_t n( matrix.rows() );
592 
593  reset( matrix );
594 
595  for( size_t i=0UL; i<n; ++i ) {
596  matrix(i,i) = rand<Type>( min, max );
597  }
598 
599  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
600 }
602 //*************************************************************************************************
603 
604 
605 //*************************************************************************************************
612 template< typename MT // Type of the adapted matrix
613  , bool SO // Storage order of the adapted matrix
614  , bool DF > // Density flag
615 void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix )
616 {
617  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type Type;
618 
619  const size_t n( matrix.rows() );
620 
621  reset( matrix );
622 
623  for( size_t i=0UL; i<n; ++i ) {
624  matrix(i,i) = rand<Type>();
625  }
626 
627  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
628 }
630 //*************************************************************************************************
631 
632 
633 //*************************************************************************************************
642 template< typename MT // Type of the adapted matrix
643  , bool SO // Storage order of the adapted matrix
644  , bool DF // Density flag
645  , typename Arg > // Min/max argument type
646 void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
647 {
648  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type Type;
649 
650  const size_t n( matrix.rows() );
651 
652  reset( matrix );
653 
654  for( size_t i=0UL; i<n; ++i ) {
655  matrix(i,i) = rand<Type>( min, max );
656  }
657 
658  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
659 }
661 //*************************************************************************************************
662 
663 
664 //*************************************************************************************************
671 template< typename MT // Type of the adapted matrix
672  , bool SO // Storage order of the adapted matrix
673  , bool DF > // Density flag
674 void makePositiveDefinite( DiagonalMatrix<MT,SO,DF>& matrix )
675 {
676  makeHermitian( matrix );
677 }
679 //*************************************************************************************************
680 
681 } // namespace blaze
682 
683 #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.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:697
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:507
Implementation of a random number generator.
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
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
Header file for the IsDenseMatrix type trait.
#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
Header file for the implementation of a diagonal matrix adaptor.
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.