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 
43 #include <blaze/math/Aliases.h>
48 #include <blaze/math/DenseMatrix.h>
49 #include <blaze/math/Exception.h>
53 #include <blaze/util/FalseType.h>
54 #include <blaze/util/Indices.h>
55 #include <blaze/util/Random.h>
56 #include <blaze/util/TrueType.h>
57 #include <blaze/util/Types.h>
58 
59 
60 namespace blaze {
61 
62 //=================================================================================================
63 //
64 // RAND SPECIALIZATION
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
75 template< typename MT // Type of the adapted matrix
76  , bool SO // Storage order of the adapted matrix
77  , bool DF > // Numeric flag
78 class Rand< DiagonalMatrix<MT,SO,DF> >
79 {
80  public:
81  //**Generate functions**************************************************************************
84  inline const DiagonalMatrix<MT,SO,DF> generate() const;
85  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n ) const;
86  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
87 
88  template< typename Arg >
89  inline const DiagonalMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
90 
91  template< typename Arg >
92  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
96  const Arg& min, const Arg& max ) const;
98  //**********************************************************************************************
99 
100  //**Randomize functions*************************************************************************
103  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const;
104  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
105 
106  template< typename Arg >
107  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
108 
109  template< typename Arg >
110  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros,
111  const Arg& min, const Arg& max ) const;
113  //**********************************************************************************************
114 
115  private:
116  //**Randomize functions*************************************************************************
119  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const;
120  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const;
121 
122  template< typename Arg >
123  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
124 
125  template< typename Arg >
126  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
128  //**********************************************************************************************
129 };
131 //*************************************************************************************************
132 
133 
134 //*************************************************************************************************
140 template< typename MT // Type of the adapted matrix
141  , bool SO // Storage order of the adapted matrix
142  , bool DF > // Numeric flag
143 inline const DiagonalMatrix<MT,SO,DF> Rand< DiagonalMatrix<MT,SO,DF> >::generate() const
144 {
146 
147  DiagonalMatrix<MT,SO,DF> matrix;
148  randomize( matrix );
149  return matrix;
150 }
152 //*************************************************************************************************
153 
154 
155 //*************************************************************************************************
162 template< typename MT // Type of the adapted matrix
163  , bool SO // Storage order of the adapted matrix
164  , bool DF > // Numeric flag
165 inline const DiagonalMatrix<MT,SO,DF>
166  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n ) const
167 {
169 
170  DiagonalMatrix<MT,SO,DF> matrix( n );
171  randomize( matrix );
172  return matrix;
173 }
175 //*************************************************************************************************
176 
177 
178 //*************************************************************************************************
187 template< typename MT // Type of the adapted matrix
188  , bool SO // Storage order of the adapted matrix
189  , bool DF > // Numeric flag
190 inline const DiagonalMatrix<MT,SO,DF>
191  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
192 {
195 
196  if( nonzeros > n ) {
197  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
198  }
199 
200  DiagonalMatrix<MT,SO,DF> matrix( n );
201  randomize( matrix, nonzeros );
202 
203  return matrix;
204 }
206 //*************************************************************************************************
207 
208 
209 //*************************************************************************************************
217 template< typename MT // Type of the adapted matrix
218  , bool SO // Storage order of the adapted matrix
219  , bool DF > // Numeric flag
220 template< typename Arg > // Min/max argument type
221 inline const DiagonalMatrix<MT,SO,DF>
222  Rand< DiagonalMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
223 {
225 
226  DiagonalMatrix<MT,SO,DF> matrix;
227  randomize( matrix, min, max );
228  return matrix;
229 }
231 //*************************************************************************************************
232 
233 
234 //*************************************************************************************************
243 template< typename MT // Type of the adapted matrix
244  , bool SO // Storage order of the adapted matrix
245  , bool DF > // Numeric flag
246 template< typename Arg > // Min/max argument type
247 inline const DiagonalMatrix<MT,SO,DF>
248  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
249 {
251 
252  DiagonalMatrix<MT,SO,DF> matrix( n );
253  randomize( matrix, min, max );
254  return matrix;
255 }
257 //*************************************************************************************************
258 
259 
260 //*************************************************************************************************
271 template< typename MT // Type of the adapted matrix
272  , bool SO // Storage order of the adapted matrix
273  , bool DF > // Numeric flag
274 template< typename Arg > // Min/max argument type
275 inline const DiagonalMatrix<MT,SO,DF>
276  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
277  const Arg& min, const Arg& max ) const
278 {
281 
282  if( nonzeros > DiagonalMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
283  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
284  }
285 
286  DiagonalMatrix<MT,SO,DF> matrix( n );
287  randomize( matrix, nonzeros, min, max );
288 
289  return matrix;
290 }
292 //*************************************************************************************************
293 
294 
295 //*************************************************************************************************
302 template< typename MT // Type of the adapted matrix
303  , bool SO // Storage order of the adapted matrix
304  , bool DF > // Numeric flag
305 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const
306 {
307  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
308 }
310 //*************************************************************************************************
311 
312 
313 //*************************************************************************************************
320 template< typename MT // Type of the adapted matrix
321  , bool SO // Storage order of the adapted matrix
322  , bool DF > // Numeric flag
323 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const
324 {
326 
327  using ET = ElementType_t<MT>;
328 
329  const size_t n( matrix.rows() );
330 
331  for( size_t i=0UL; i<n; ++i ) {
332  matrix(i,i) = rand<ET>();
333  }
334 }
336 //*************************************************************************************************
337 
338 
339 //*************************************************************************************************
346 template< typename MT // Type of the adapted matrix
347  , bool SO // Storage order of the adapted matrix
348  , bool DF > // Numeric flag
349 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const
350 {
352 
353  const size_t n( matrix.rows() );
354 
355  if( n == 0UL ) return;
356 
357  const size_t nonzeros( rand<size_t>( 1UL, n ) );
358 
359  randomize( matrix, nonzeros );
360 }
362 //*************************************************************************************************
363 
364 
365 //*************************************************************************************************
374 template< typename MT // Type of the adapted matrix
375  , bool SO // Storage order of the adapted matrix
376  , bool DF > // Numeric flag
377 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
378 {
380 
381  using ET = ElementType_t<MT>;
382 
383  const size_t n( matrix.rows() );
384 
385  if( nonzeros > n ) {
386  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
387  }
388 
389  if( n == 0UL ) return;
390 
391  matrix.reset();
392  matrix.reserve( nonzeros );
393 
394  Indices indices( 0UL, n-1UL, nonzeros );
395  size_t i( 0UL );
396 
397  for( size_t index : indices ) {
398  for( ; i<index; ++i )
399  matrix.finalize( i );
400  matrix.append( i, i, rand<ET>() );
401  }
402 
403  for( ; i<n; ++i ) {
404  matrix.finalize( i );
405  }
406 }
408 //*************************************************************************************************
409 
410 
411 //*************************************************************************************************
420 template< typename MT // Type of the adapted matrix
421  , bool SO // Storage order of the adapted matrix
422  , bool DF > // Numeric flag
423 template< typename Arg > // Min/max argument type
424 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
425  const Arg& min, const Arg& max ) const
426 {
427  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
428 }
430 //*************************************************************************************************
431 
432 
433 //*************************************************************************************************
442 template< typename MT // Type of the adapted matrix
443  , bool SO // Storage order of the adapted matrix
444  , bool DF > // Numeric flag
445 template< typename Arg > // Min/max argument type
446 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
447  const Arg& min, const Arg& max, TrueType ) const
448 {
450 
451  using ET = ElementType_t<MT>;
452 
453  const size_t n( matrix.rows() );
454 
455  for( size_t i=0UL; i<n; ++i ) {
456  matrix(i,i) = rand<ET>( min, max );
457  }
458 }
460 //*************************************************************************************************
461 
462 
463 //*************************************************************************************************
472 template< typename MT // Type of the adapted matrix
473  , bool SO // Storage order of the adapted matrix
474  , bool DF > // Numeric flag
475 template< typename Arg > // Min/max argument type
476 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
477  const Arg& min, const Arg& max, FalseType ) const
478 {
480 
481  const size_t n( matrix.rows() );
482 
483  if( n == 0UL ) return;
484 
485  const size_t nonzeros( rand<size_t>( 1UL, n ) );
486 
487  randomize( matrix, nonzeros, min, max );
488 }
490 //*************************************************************************************************
491 
492 
493 //*************************************************************************************************
504 template< typename MT // Type of the adapted matrix
505  , bool SO // Storage order of the adapted matrix
506  , bool DF > // Numeric flag
507 template< typename Arg > // Min/max argument type
508 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
509  size_t nonzeros, const Arg& min, const Arg& max ) const
510 {
512 
513  using ET = ElementType_t<MT>;
514 
515  const size_t n( matrix.rows() );
516 
517  if( nonzeros > n ) {
518  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
519  }
520 
521  if( n == 0UL ) return;
522 
523  matrix.reset();
524  matrix.reserve( nonzeros );
525 
526  Indices indices( 0UL, n-1UL, nonzeros );
527  size_t i( 0UL );
528 
529  for( size_t index : indices ) {
530  for( ; i<index; ++i )
531  matrix.finalize( i );
532  matrix.append( i, i, rand<ET>( min, max ) );
533  }
534 
535  for( ; i<n; ++i ) {
536  matrix.finalize( i );
537  }
538 }
540 //*************************************************************************************************
541 
542 
543 
544 
545 //=================================================================================================
546 //
547 // MAKE FUNCTIONS
548 //
549 //=================================================================================================
550 
551 //*************************************************************************************************
558 template< typename MT // Type of the adapted matrix
559  , bool SO // Storage order of the adapted matrix
560  , bool DF > // Density flag
561 void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix )
562 {
563  const size_t n( matrix.rows() );
564 
565  reset( matrix );
566 
567  for( size_t i=0UL; i<n; ++i ) {
568  matrix(i,i) = rand< ElementType_t<MT> >();
569  }
570 
571  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
572 }
574 //*************************************************************************************************
575 
576 
577 //*************************************************************************************************
586 template< typename MT // Type of the adapted matrix
587  , bool SO // Storage order of the adapted matrix
588  , bool DF // Density flag
589  , typename Arg > // Min/max argument type
590 void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
591 {
592  using Type = ElementType_t<MT>;
593 
594  const size_t n( matrix.rows() );
595 
596  reset( matrix );
597 
598  for( size_t i=0UL; i<n; ++i ) {
599  matrix(i,i) = rand<Type>( min, max );
600  }
601 
602  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
603 }
605 //*************************************************************************************************
606 
607 
608 //*************************************************************************************************
615 template< typename MT // Type of the adapted matrix
616  , bool SO // Storage order of the adapted matrix
617  , bool DF > // Density flag
618 void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix )
619 {
620  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
621 
622  const size_t n( matrix.rows() );
623 
624  reset( matrix );
625 
626  for( size_t i=0UL; i<n; ++i ) {
627  matrix(i,i) = rand<Type>();
628  }
629 
630  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
631 }
633 //*************************************************************************************************
634 
635 
636 //*************************************************************************************************
645 template< typename MT // Type of the adapted matrix
646  , bool SO // Storage order of the adapted matrix
647  , bool DF // Density flag
648  , typename Arg > // Min/max argument type
649 void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
650 {
651  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
652 
653  const size_t n( matrix.rows() );
654 
655  reset( matrix );
656 
657  for( size_t i=0UL; i<n; ++i ) {
658  matrix(i,i) = rand<Type>( min, max );
659  }
660 
661  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
662 }
664 //*************************************************************************************************
665 
666 
667 //*************************************************************************************************
674 template< typename MT // Type of the adapted matrix
675  , bool SO // Storage order of the adapted matrix
676  , bool DF > // Density flag
677 void makePositiveDefinite( DiagonalMatrix<MT,SO,DF>& matrix )
678 {
679  makeHermitian( matrix );
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
Constraint on the data type.
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:591
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: TrueType.h:61
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:1147
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:1179
Header file for the IsDenseMatrix type trait.
Header file for the Indices class.
#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:617
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:539
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.
#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.