Blaze  3.6
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/Indices.h>
55 #include <blaze/util/Random.h>
56 #include <blaze/util/Types.h>
57 
58 
59 namespace blaze {
60 
61 //=================================================================================================
62 //
63 // RAND SPECIALIZATION
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
74 template< typename MT // Type of the adapted matrix
75  , bool SO // Storage order of the adapted matrix
76  , bool DF > // Numeric flag
77 class Rand< DiagonalMatrix<MT,SO,DF> >
78 {
79  public:
80  //**Generate functions**************************************************************************
83  inline const DiagonalMatrix<MT,SO,DF> generate() const;
84  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n ) const;
85  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
86 
87  template< typename Arg >
88  inline const DiagonalMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
89 
90  template< typename Arg >
91  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
95  const Arg& min, const Arg& max ) const;
97  //**********************************************************************************************
98 
99  //**Randomize functions*************************************************************************
102  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const;
103  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
104 
105  template< typename Arg >
106  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
107 
108  template< typename Arg >
109  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros,
110  const Arg& min, const Arg& max ) const;
112  //**********************************************************************************************
113 
114  private:
115  //**Randomize functions*************************************************************************
118  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const;
119  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const;
120 
121  template< typename Arg >
122  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
123 
124  template< typename Arg >
125  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
127  //**********************************************************************************************
128 };
130 //*************************************************************************************************
131 
132 
133 //*************************************************************************************************
139 template< typename MT // Type of the adapted matrix
140  , bool SO // Storage order of the adapted matrix
141  , bool DF > // Numeric flag
142 inline const DiagonalMatrix<MT,SO,DF> Rand< DiagonalMatrix<MT,SO,DF> >::generate() const
143 {
145 
146  DiagonalMatrix<MT,SO,DF> matrix;
147  randomize( matrix );
148  return matrix;
149 }
151 //*************************************************************************************************
152 
153 
154 //*************************************************************************************************
161 template< typename MT // Type of the adapted matrix
162  , bool SO // Storage order of the adapted matrix
163  , bool DF > // Numeric flag
164 inline const DiagonalMatrix<MT,SO,DF>
165  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n ) const
166 {
168 
169  DiagonalMatrix<MT,SO,DF> matrix( n );
170  randomize( matrix );
171  return matrix;
172 }
174 //*************************************************************************************************
175 
176 
177 //*************************************************************************************************
186 template< typename MT // Type of the adapted matrix
187  , bool SO // Storage order of the adapted matrix
188  , bool DF > // Numeric flag
189 inline const DiagonalMatrix<MT,SO,DF>
190  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
191 {
194 
195  if( nonzeros > n ) {
196  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
197  }
198 
199  DiagonalMatrix<MT,SO,DF> matrix( n );
200  randomize( matrix, nonzeros );
201 
202  return matrix;
203 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
216 template< typename MT // Type of the adapted matrix
217  , bool SO // Storage order of the adapted matrix
218  , bool DF > // Numeric flag
219 template< typename Arg > // Min/max argument type
220 inline const DiagonalMatrix<MT,SO,DF>
221  Rand< DiagonalMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
222 {
224 
225  DiagonalMatrix<MT,SO,DF> matrix;
226  randomize( matrix, min, max );
227  return matrix;
228 }
230 //*************************************************************************************************
231 
232 
233 //*************************************************************************************************
242 template< typename MT // Type of the adapted matrix
243  , bool SO // Storage order of the adapted matrix
244  , bool DF > // Numeric flag
245 template< typename Arg > // Min/max argument type
246 inline const DiagonalMatrix<MT,SO,DF>
247  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
248 {
250 
251  DiagonalMatrix<MT,SO,DF> matrix( n );
252  randomize( matrix, min, max );
253  return matrix;
254 }
256 //*************************************************************************************************
257 
258 
259 //*************************************************************************************************
270 template< typename MT // Type of the adapted matrix
271  , bool SO // Storage order of the adapted matrix
272  , bool DF > // Numeric flag
273 template< typename Arg > // Min/max argument type
274 inline const DiagonalMatrix<MT,SO,DF>
275  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
276  const Arg& min, const Arg& max ) const
277 {
280 
281  if( nonzeros > DiagonalMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
282  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
283  }
284 
285  DiagonalMatrix<MT,SO,DF> matrix( n );
286  randomize( matrix, nonzeros, min, max );
287 
288  return matrix;
289 }
291 //*************************************************************************************************
292 
293 
294 //*************************************************************************************************
301 template< typename MT // Type of the adapted matrix
302  , bool SO // Storage order of the adapted matrix
303  , bool DF > // Numeric flag
304 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const
305 {
306  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
307 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
319 template< typename MT // Type of the adapted matrix
320  , bool SO // Storage order of the adapted matrix
321  , bool DF > // Numeric flag
322 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const
323 {
325 
326  using ET = ElementType_t<MT>;
327 
328  const size_t n( matrix.rows() );
329 
330  for( size_t i=0UL; i<n; ++i ) {
331  matrix(i,i) = rand<ET>();
332  }
333 }
335 //*************************************************************************************************
336 
337 
338 //*************************************************************************************************
345 template< typename MT // Type of the adapted matrix
346  , bool SO // Storage order of the adapted matrix
347  , bool DF > // Numeric flag
348 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const
349 {
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  randomize( matrix, nonzeros );
359 }
361 //*************************************************************************************************
362 
363 
364 //*************************************************************************************************
373 template< typename MT // Type of the adapted matrix
374  , bool SO // Storage order of the adapted matrix
375  , bool DF > // Numeric flag
376 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
377 {
379 
380  using ET = ElementType_t<MT>;
381 
382  const size_t n( matrix.rows() );
383 
384  if( nonzeros > n ) {
385  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
386  }
387 
388  if( n == 0UL ) return;
389 
390  matrix.reset();
391  matrix.reserve( nonzeros );
392 
393  Indices indices( 0UL, n-1UL, nonzeros );
394  size_t i( 0UL );
395 
396  for( size_t index : indices ) {
397  for( ; i<index; ++i )
398  matrix.finalize( i );
399  matrix.append( i, i, rand<ET>() );
400  }
401 
402  for( ; i<n; ++i ) {
403  matrix.finalize( i );
404  }
405 }
407 //*************************************************************************************************
408 
409 
410 //*************************************************************************************************
419 template< typename MT // Type of the adapted matrix
420  , bool SO // Storage order of the adapted matrix
421  , bool DF > // Numeric flag
422 template< typename Arg > // Min/max argument type
423 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
424  const Arg& min, const Arg& max ) const
425 {
426  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
427 }
429 //*************************************************************************************************
430 
431 
432 //*************************************************************************************************
441 template< typename MT // Type of the adapted matrix
442  , bool SO // Storage order of the adapted matrix
443  , bool DF > // Numeric flag
444 template< typename Arg > // Min/max argument type
445 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
446  const Arg& min, const Arg& max, TrueType ) const
447 {
449 
450  using ET = ElementType_t<MT>;
451 
452  const size_t n( matrix.rows() );
453 
454  for( size_t i=0UL; i<n; ++i ) {
455  matrix(i,i) = rand<ET>( min, max );
456  }
457 }
459 //*************************************************************************************************
460 
461 
462 //*************************************************************************************************
471 template< typename MT // Type of the adapted matrix
472  , bool SO // Storage order of the adapted matrix
473  , bool DF > // Numeric flag
474 template< typename Arg > // Min/max argument type
475 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
476  const Arg& min, const Arg& max, FalseType ) const
477 {
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  randomize( matrix, nonzeros, min, max );
487 }
489 //*************************************************************************************************
490 
491 
492 //*************************************************************************************************
503 template< typename MT // Type of the adapted matrix
504  , bool SO // Storage order of the adapted matrix
505  , bool DF > // Numeric flag
506 template< typename Arg > // Min/max argument type
507 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
508  size_t nonzeros, const Arg& min, const Arg& max ) const
509 {
511 
512  using ET = ElementType_t<MT>;
513 
514  const size_t n( matrix.rows() );
515 
516  if( nonzeros > n ) {
517  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
518  }
519 
520  if( n == 0UL ) return;
521 
522  matrix.reset();
523  matrix.reserve( nonzeros );
524 
525  Indices indices( 0UL, n-1UL, nonzeros );
526  size_t i( 0UL );
527 
528  for( size_t index : indices ) {
529  for( ; i<index; ++i )
530  matrix.finalize( i );
531  matrix.append( i, i, rand<ET>( min, max ) );
532  }
533 
534  for( ; i<n; ++i ) {
535  matrix.finalize( i );
536  }
537 }
539 //*************************************************************************************************
540 
541 
542 
543 
544 //=================================================================================================
545 //
546 // MAKE FUNCTIONS
547 //
548 //=================================================================================================
549 
550 //*************************************************************************************************
557 template< typename MT // Type of the adapted matrix
558  , bool SO // Storage order of the adapted matrix
559  , bool DF > // Density flag
560 void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix )
561 {
562  const size_t n( matrix.rows() );
563 
564  reset( matrix );
565 
566  for( size_t i=0UL; i<n; ++i ) {
567  matrix(i,i) = rand< ElementType_t<MT> >();
568  }
569 
570  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
571 }
573 //*************************************************************************************************
574 
575 
576 //*************************************************************************************************
585 template< typename MT // Type of the adapted matrix
586  , bool SO // Storage order of the adapted matrix
587  , bool DF // Density flag
588  , typename Arg > // Min/max argument type
589 void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
590 {
591  using Type = ElementType_t<MT>;
592 
593  const size_t n( matrix.rows() );
594 
595  reset( matrix );
596 
597  for( size_t i=0UL; i<n; ++i ) {
598  matrix(i,i) = rand<Type>( min, max );
599  }
600 
601  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
602 }
604 //*************************************************************************************************
605 
606 
607 //*************************************************************************************************
614 template< typename MT // Type of the adapted matrix
615  , bool SO // Storage order of the adapted matrix
616  , bool DF > // Density flag
617 void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix )
618 {
619  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
620 
621  const size_t n( matrix.rows() );
622 
623  reset( matrix );
624 
625  for( size_t i=0UL; i<n; ++i ) {
626  matrix(i,i) = rand<Type>();
627  }
628 
629  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
630 }
632 //*************************************************************************************************
633 
634 
635 //*************************************************************************************************
644 template< typename MT // Type of the adapted matrix
645  , bool SO // Storage order of the adapted matrix
646  , bool DF // Density flag
647  , typename Arg > // Min/max argument type
648 void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
649 {
650  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
651 
652  const size_t n( matrix.rows() );
653 
654  reset( matrix );
655 
656  for( size_t i=0UL; i<n; ++i ) {
657  matrix(i,i) = rand<Type>( min, max );
658  }
659 
660  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
661 }
663 //*************************************************************************************************
664 
665 
666 //*************************************************************************************************
673 template< typename MT // Type of the adapted matrix
674  , bool SO // Storage order of the adapted matrix
675  , bool DF > // Density flag
676 void makePositiveDefinite( DiagonalMatrix<MT,SO,DF>& matrix )
677 {
678  makeHermitian( matrix );
679 }
681 //*************************************************************************************************
682 
683 } // namespace blaze
684 
685 #endif
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: IntegralConstant.h:121
#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.
#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:595
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: IntegralConstant.h:132
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:1162
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:1198
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 'resize' member fu...
Definition: Resizable.h:81
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:1406
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:1328
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 'res...
Definition: Resizable.h:61
Header file for the implementation of a diagonal matrix adaptor.
Header file for the IntegralConstant class template.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
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