UpperMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_UPPERMATRIX_H_
36 #define _BLAZE_MATH_UPPERMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <vector>
45 #include <blaze/math/Aliases.h>
51 #include <blaze/math/DenseMatrix.h>
52 #include <blaze/math/Exception.h>
53 #include <blaze/math/LowerMatrix.h>
57 #include <blaze/util/FalseType.h>
58 #include <blaze/util/Indices.h>
59 #include <blaze/util/Random.h>
60 #include <blaze/util/TrueType.h>
61 #include <blaze/util/Types.h>
62 
63 
64 namespace blaze {
65 
66 //=================================================================================================
67 //
68 // RAND SPECIALIZATION
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
79 template< typename MT // Type of the adapted matrix
80  , bool SO // Storage order of the adapted matrix
81  , bool DF > // Numeric flag
82 class Rand< UpperMatrix<MT,SO,DF> >
83 {
84  public:
85  //**Generate functions**************************************************************************
88  inline const UpperMatrix<MT,SO,DF> generate() const;
89  inline const UpperMatrix<MT,SO,DF> generate( size_t n ) const;
90  inline const UpperMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
91 
92  template< typename Arg >
93  inline const UpperMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
94 
95  template< typename Arg >
96  inline const UpperMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
97 
98  template< typename Arg >
99  inline const UpperMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
100  const Arg& min, const Arg& max ) const;
102  //**********************************************************************************************
103 
104  //**Randomize functions*************************************************************************
107  inline void randomize( UpperMatrix<MT,SO,DF>& matrix ) const;
108  inline void randomize( UpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
109  inline void randomize( UpperMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
110 
111  template< typename Arg >
112  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
113 
114  template< typename Arg >
115  inline void randomize( UpperMatrix<MT,false,DF>& matrix, size_t nonzeros,
116  const Arg& min, const Arg& max ) const;
117 
118  template< typename Arg >
119  inline void randomize( UpperMatrix<MT,true,DF>& matrix, size_t nonzeros,
120  const Arg& min, const Arg& max ) const;
122  //**********************************************************************************************
123 
124  private:
125  //**Randomize functions*************************************************************************
128  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, TrueType ) const;
129  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, FalseType ) const;
130 
131  template< typename Arg >
132  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
133 
134  template< typename Arg >
135  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
137  //**********************************************************************************************
138 };
140 //*************************************************************************************************
141 
142 
143 //*************************************************************************************************
149 template< typename MT // Type of the adapted matrix
150  , bool SO // Storage order of the adapted matrix
151  , bool DF > // Numeric flag
152 inline const UpperMatrix<MT,SO,DF> Rand< UpperMatrix<MT,SO,DF> >::generate() const
153 {
155 
156  UpperMatrix<MT,SO,DF> matrix;
157  randomize( matrix );
158  return matrix;
159 }
161 //*************************************************************************************************
162 
163 
164 //*************************************************************************************************
171 template< typename MT // Type of the adapted matrix
172  , bool SO // Storage order of the adapted matrix
173  , bool DF > // Numeric flag
174 inline const UpperMatrix<MT,SO,DF>
175  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n ) const
176 {
178 
179  UpperMatrix<MT,SO,DF> matrix( n );
180  randomize( matrix );
181  return matrix;
182 }
184 //*************************************************************************************************
185 
186 
187 //*************************************************************************************************
196 template< typename MT // Type of the adapted matrix
197  , bool SO // Storage order of the adapted matrix
198  , bool DF > // Numeric flag
199 inline const UpperMatrix<MT,SO,DF>
200  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
201 {
204 
205  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
206  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
207  }
208 
209  UpperMatrix<MT,SO,DF> matrix( n );
210  randomize( matrix, nonzeros );
211 
212  return matrix;
213 }
215 //*************************************************************************************************
216 
217 
218 //*************************************************************************************************
226 template< typename MT // Type of the adapted matrix
227  , bool SO // Storage order of the adapted matrix
228  , bool DF > // Numeric flag
229 template< typename Arg > // Min/max argument type
230 inline const UpperMatrix<MT,SO,DF>
231  Rand< UpperMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
232 {
234 
235  UpperMatrix<MT,SO,DF> matrix;
236  randomize( matrix, min, max );
237  return matrix;
238 }
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
252 template< typename MT // Type of the adapted matrix
253  , bool SO // Storage order of the adapted matrix
254  , bool DF > // Numeric flag
255 template< typename Arg > // Min/max argument type
256 inline const UpperMatrix<MT,SO,DF>
257  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
258 {
260 
261  UpperMatrix<MT,SO,DF> matrix( n );
262  randomize( matrix, min, max );
263  return matrix;
264 }
266 //*************************************************************************************************
267 
268 
269 //*************************************************************************************************
280 template< typename MT // Type of the adapted matrix
281  , bool SO // Storage order of the adapted matrix
282  , bool DF > // Numeric flag
283 template< typename Arg > // Min/max argument type
284 inline const UpperMatrix<MT,SO,DF>
285  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
286  const Arg& min, const Arg& max ) const
287 {
290 
291  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
292  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
293  }
294 
295  UpperMatrix<MT,SO,DF> 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 > // Numeric flag
314 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& 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 > // Numeric flag
332 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix, TrueType ) const
333 {
335 
336  using ET = ElementType_t<MT>;
337 
338  const size_t n( matrix.rows() );
339 
340  for( size_t i=0UL; i<n; ++i ) {
341  for( size_t j=i; j<n; ++j ) {
342  matrix(i,j) = rand<ET>();
343  }
344  }
345 }
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
357 template< typename MT // Type of the adapted matrix
358  , bool SO // Storage order of the adapted matrix
359  , bool DF > // Numeric flag
360 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix, FalseType ) const
361 {
363 
364  const size_t n( matrix.rows() );
365 
366  if( n == 0UL ) return;
367 
368  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*n*n ) ) );
369 
370  randomize( matrix, nonzeros );
371 }
373 //*************************************************************************************************
374 
375 
376 //*************************************************************************************************
385 template< typename MT // Type of the adapted matrix
386  , bool SO // Storage order of the adapted matrix
387  , bool DF > // Numeric flag
388 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
389 {
391 
392  using ET = ElementType_t<MT>;
393 
394  const size_t n( matrix.rows() );
395 
396  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
397  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
398  }
399 
400  if( n == 0UL ) return;
401 
402  matrix.reset();
403  matrix.reserve( nonzeros );
404 
405  std::vector<size_t> dist( n );
406 
407  for( size_t nz=0UL; nz<nonzeros; ) {
408  const size_t index = rand<size_t>( 0UL, n-1UL );
409  if( dist[index] == n - index ) continue;
410  ++dist[index];
411  ++nz;
412  }
413 
414  for( size_t i=0UL; i<n; ++i ) {
415  const Indices indices( i, n-1UL, dist[i] );
416  for( size_t j : indices ) {
417  matrix.append( i, j, rand<ET>() );
418  }
419  matrix.finalize( i );
420  }
421 }
423 //*************************************************************************************************
424 
425 
426 //*************************************************************************************************
435 template< typename MT // Type of the adapted matrix
436  , bool SO // Storage order of the adapted matrix
437  , bool DF > // Numeric flag
438 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
439 {
441 
442  using ET = ElementType_t<MT>;
443 
444  const size_t n( matrix.rows() );
445 
446  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
447  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
448  }
449 
450  if( n == 0UL ) return;
451 
452  matrix.reset();
453  matrix.reserve( nonzeros );
454 
455  std::vector<size_t> dist( n );
456 
457  for( size_t nz=0UL; nz<nonzeros; ) {
458  const size_t index = rand<size_t>( 0UL, n-1UL );
459  if( dist[index] == index+1UL ) continue;
460  ++dist[index];
461  ++nz;
462  }
463 
464  for( size_t j=0UL; j<n; ++j ) {
465  const Indices indices( 0UL, j, dist[j] );
466  for( size_t i : indices ) {
467  matrix.append( i, j, rand<ET>() );
468  }
469  matrix.finalize( j );
470  }
471 }
473 //*************************************************************************************************
474 
475 
476 //*************************************************************************************************
485 template< typename MT // Type of the adapted matrix
486  , bool SO // Storage order of the adapted matrix
487  , bool DF > // Numeric flag
488 template< typename Arg > // Min/max argument type
489 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix,
490  const Arg& min, const Arg& max ) const
491 {
492  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
493 }
495 //*************************************************************************************************
496 
497 
498 //*************************************************************************************************
507 template< typename MT // Type of the adapted matrix
508  , bool SO // Storage order of the adapted matrix
509  , bool DF > // Numeric flag
510 template< typename Arg > // Min/max argument type
511 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix,
512  const Arg& min, const Arg& max, TrueType ) const
513 {
515 
516  using ET = ElementType_t<MT>;
517 
518  const size_t n( matrix.rows() );
519 
520  for( size_t i=0UL; i<n; ++i ) {
521  for( size_t j=i; j<n; ++j ) {
522  matrix(i,j) = rand<ET>( min, max );
523  }
524  }
525 }
527 //*************************************************************************************************
528 
529 
530 //*************************************************************************************************
539 template< typename MT // Type of the adapted matrix
540  , bool SO // Storage order of the adapted matrix
541  , bool DF > // Numeric flag
542 template< typename Arg > // Min/max argument type
543 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix,
544  const Arg& min, const Arg& max, FalseType ) const
545 {
547 
548  const size_t n( matrix.rows() );
549 
550  if( n == 0UL ) return;
551 
552  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*n*n ) ) );
553 
554  randomize( matrix, nonzeros, min, max );
555 }
557 //*************************************************************************************************
558 
559 
560 //*************************************************************************************************
571 template< typename MT // Type of the adapted matrix
572  , bool SO // Storage order of the adapted matrix
573  , bool DF > // Numeric flag
574 template< typename Arg > // Min/max argument type
575 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,false,DF>& matrix,
576  size_t nonzeros, const Arg& min, const Arg& max ) const
577 {
579 
580  using ET = ElementType_t<MT>;
581 
582  const size_t n( matrix.rows() );
583 
584  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
585  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
586  }
587 
588  if( n == 0UL ) return;
589 
590  matrix.reset();
591  matrix.reserve( nonzeros );
592 
593  std::vector<size_t> dist( n );
594 
595  for( size_t nz=0UL; nz<nonzeros; ) {
596  const size_t index = rand<size_t>( 0UL, n-1UL );
597  if( dist[index] == n - index ) continue;
598  ++dist[index];
599  ++nz;
600  }
601 
602  for( size_t i=0UL; i<n; ++i ) {
603  const Indices indices( i, n-1UL, dist[i] );
604  for( size_t j : indices ) {
605  matrix.append( i, j, rand<ET>( min, max ) );
606  }
607  matrix.finalize( i );
608  }
609 }
611 //*************************************************************************************************
612 
613 
614 //*************************************************************************************************
625 template< typename MT // Type of the adapted matrix
626  , bool SO // Storage order of the adapted matrix
627  , bool DF > // Numeric flag
628 template< typename Arg > // Min/max argument type
629 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,true,DF>& matrix,
630  size_t nonzeros, const Arg& min, const Arg& max ) const
631 {
633 
634  using ET = ElementType_t<MT>;
635 
636  const size_t n( matrix.rows() );
637 
638  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
639  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
640  }
641 
642  if( n == 0UL ) return;
643 
644  matrix.reset();
645  matrix.reserve( nonzeros );
646 
647  std::vector<size_t> dist( n );
648 
649  for( size_t nz=0UL; nz<nonzeros; ) {
650  const size_t index = rand<size_t>( 0UL, n-1UL );
651  if( dist[index] == index+1UL ) continue;
652  ++dist[index];
653  ++nz;
654  }
655 
656  for( size_t j=0UL; j<n; ++j ) {
657  const Indices indices( 0UL, j, dist[j] );
658  for( size_t i : indices ) {
659  matrix.append( i, j, rand<ET>( min, max ) );
660  }
661  matrix.finalize( j );
662  }
663 }
665 //*************************************************************************************************
666 
667 
668 
669 
670 //=================================================================================================
671 //
672 // MAKE FUNCTIONS
673 //
674 //=================================================================================================
675 
676 //*************************************************************************************************
683 template< typename MT // Type of the adapted matrix
684  , bool SO // Storage order of the adapted matrix
685  , bool DF > // Density flag
686 void makeSymmetric( UpperMatrix<MT,SO,DF>& matrix )
687 {
688  const size_t n( matrix.rows() );
689 
690  reset( matrix );
691 
692  for( size_t i=0UL; i<n; ++i ) {
693  matrix(i,i) = rand< ElementType_t<MT> >();
694  }
695 
696  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
697 }
699 //*************************************************************************************************
700 
701 
702 //*************************************************************************************************
711 template< typename MT // Type of the adapted matrix
712  , bool SO // Storage order of the adapted matrix
713  , bool DF // Density flag
714  , typename Arg > // Min/max argument type
715 void makeSymmetric( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
716 {
717  using Type = ElementType_t<MT>;
718 
719  const size_t n( matrix.rows() );
720 
721  reset( matrix );
722 
723  for( size_t i=0UL; i<n; ++i ) {
724  matrix(i,i) = rand<Type>( min, max );
725  }
726 
727  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
728 }
730 //*************************************************************************************************
731 
732 
733 //*************************************************************************************************
740 template< typename MT // Type of the adapted matrix
741  , bool SO // Storage order of the adapted matrix
742  , bool DF > // Density flag
743 void makeHermitian( UpperMatrix<MT,SO,DF>& matrix )
744 {
745  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
746 
747  const size_t n( matrix.rows() );
748 
749  reset( matrix );
750 
751  for( size_t i=0UL; i<n; ++i ) {
752  matrix(i,i) = rand<Type>();
753  }
754 
755  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
756 }
758 //*************************************************************************************************
759 
760 
761 //*************************************************************************************************
770 template< typename MT // Type of the adapted matrix
771  , bool SO // Storage order of the adapted matrix
772  , bool DF // Density flag
773  , typename Arg > // Min/max argument type
774 void makeHermitian( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
775 {
776  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
777 
778  const size_t n( matrix.rows() );
779 
780  reset( matrix );
781 
782  for( size_t i=0UL; i<n; ++i ) {
783  matrix(i,i) = rand<Type>( min, max );
784  }
785 
786  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
787 }
789 //*************************************************************************************************
790 
791 
792 //*************************************************************************************************
799 template< typename MT // Type of the adapted matrix
800  , bool SO // Storage order of the adapted matrix
801  , bool DF > // Density flag
802 void makePositiveDefinite( UpperMatrix<MT,SO,DF>& matrix )
803 {
804  makeHermitian( matrix );
805 }
807 //*************************************************************************************************
808 
809 } // namespace blaze
810 
811 #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.
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1239
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 complete LowerMatrix implementation.
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 implementation of a upper matrix adaptor.
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.