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>
48 #include <blaze/math/DenseMatrix.h>
49 #include <blaze/math/LowerMatrix.h>
52 #include <blaze/util/Exception.h>
53 #include <blaze/util/FalseType.h>
54 #include <blaze/util/Random.h>
55 #include <blaze/util/TrueType.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< UpperMatrix<MT,SO,DF> >
78 {
79  public:
80  //**Generate functions**************************************************************************
83  inline const UpperMatrix<MT,SO,DF> generate() const;
84  inline const UpperMatrix<MT,SO,DF> generate( size_t n ) const;
85  inline const UpperMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
86 
87  template< typename Arg >
88  inline const UpperMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
89 
90  template< typename Arg >
91  inline const UpperMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const UpperMatrix<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( UpperMatrix<MT,SO,DF>& matrix ) const;
103  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
104 
105  template< typename Arg >
106  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
107 
108  template< typename Arg >
109  inline void randomize( UpperMatrix<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( UpperMatrix<MT,SO,DF>& matrix, TrueType ) const;
119  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, FalseType ) const;
120 
121  template< typename Arg >
122  inline void randomize( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
123 
124  template< typename Arg >
125  inline void randomize( UpperMatrix<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 UpperMatrix<MT,SO,DF> Rand< UpperMatrix<MT,SO,DF> >::generate() const
143 {
145 
146  UpperMatrix<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 UpperMatrix<MT,SO,DF>
165  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n ) const
166 {
168 
169  UpperMatrix<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 UpperMatrix<MT,SO,DF>
190  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
191 {
194 
195  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
196  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
197  }
198 
199  UpperMatrix<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 UpperMatrix<MT,SO,DF>
221  Rand< UpperMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
222 {
224 
225  UpperMatrix<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 UpperMatrix<MT,SO,DF>
247  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
248 {
250 
251  UpperMatrix<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 UpperMatrix<MT,SO,DF>
275  Rand< UpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
276  const Arg& min, const Arg& max ) const
277 {
280 
281  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
282  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
283  }
284 
285  UpperMatrix<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< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<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< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix, TrueType ) const
323 {
325 
326  typedef typename MT::ElementType ET;
327 
328  const size_t n( matrix.rows() );
329 
330  for( size_t i=0UL; i<n; ++i ) {
331  for( size_t j=i; j<n; ++j ) {
332  matrix(i,j) = rand<ET>();
333  }
334  }
335 }
337 //*************************************************************************************************
338 
339 
340 //*************************************************************************************************
347 template< typename MT // Type of the adapted matrix
348  , bool SO // Storage order of the adapted matrix
349  , bool DF > // Numeric flag
350 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix, FalseType ) const
351 {
353 
354  typedef typename MT::ElementType ET;
355 
356  const size_t n( matrix.rows() );
357 
358  if( n == 0UL ) return;
359 
360  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*n*n ) ) );
361 
362  matrix.reset();
363  matrix.reserve( nonzeros );
364 
365  while( matrix.nonZeros() < nonzeros ) {
366  const size_t row( rand<size_t>( 0UL, n-1UL ) );
367  const size_t col( rand<size_t>( row, n-1UL ) );
368  matrix(row,col) = rand<ET>();
369  }
370 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
384 template< typename MT // Type of the adapted matrix
385  , bool SO // Storage order of the adapted matrix
386  , bool DF > // Numeric flag
387 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
388 {
390 
391  typedef typename MT::ElementType ET;
392 
393  const size_t n( matrix.rows() );
394 
395  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
396  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
397  }
398 
399  if( n == 0UL ) return;
400 
401  matrix.reset();
402  matrix.reserve( nonzeros );
403 
404  while( matrix.nonZeros() < nonzeros ) {
405  const size_t row( rand<size_t>( 0UL, n-1UL ) );
406  const size_t col( rand<size_t>( row, n-1UL ) );
407  matrix(row,col) = rand<ET>();
408  }
409 }
411 //*************************************************************************************************
412 
413 
414 //*************************************************************************************************
423 template< typename MT // Type of the adapted matrix
424  , bool SO // Storage order of the adapted matrix
425  , bool DF > // Numeric flag
426 template< typename Arg > // Min/max argument type
427 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix,
428  const Arg& min, const Arg& max ) const
429 {
430  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
431 }
433 //*************************************************************************************************
434 
435 
436 //*************************************************************************************************
445 template< typename MT // Type of the adapted matrix
446  , bool SO // Storage order of the adapted matrix
447  , bool DF > // Numeric flag
448 template< typename Arg > // Min/max argument type
449 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix,
450  const Arg& min, const Arg& max, TrueType ) const
451 {
453 
454  typedef typename MT::ElementType ET;
455 
456  const size_t n( matrix.rows() );
457 
458  for( size_t i=0UL; i<n; ++i ) {
459  for( size_t j=i; j<n; ++j ) {
460  matrix(i,j) = rand<ET>( min, max );
461  }
462  }
463 }
465 //*************************************************************************************************
466 
467 
468 //*************************************************************************************************
477 template< typename MT // Type of the adapted matrix
478  , bool SO // Storage order of the adapted matrix
479  , bool DF > // Numeric flag
480 template< typename Arg > // Min/max argument type
481 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix,
482  const Arg& min, const Arg& max, FalseType ) const
483 {
485 
486  typedef typename MT::ElementType ET;
487 
488  const size_t n( matrix.rows() );
489 
490  if( n == 0UL ) return;
491 
492  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*n*n ) ) );
493 
494  matrix.reset();
495  matrix.reserve( nonzeros );
496 
497  while( matrix.nonZeros() < nonzeros ) {
498  const size_t row( rand<size_t>( 0UL, n-1UL ) );
499  const size_t col( rand<size_t>( row, n-1UL ) );
500  matrix(row,col) = rand<ET>( min, max );
501  }
502 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
518 template< typename MT // Type of the adapted matrix
519  , bool SO // Storage order of the adapted matrix
520  , bool DF > // Numeric flag
521 template< typename Arg > // Min/max argument type
522 inline void Rand< UpperMatrix<MT,SO,DF> >::randomize( UpperMatrix<MT,SO,DF>& matrix,
523  size_t nonzeros, const Arg& min, const Arg& max ) const
524 {
526 
527  typedef typename MT::ElementType ET;
528 
529  const size_t n( matrix.rows() );
530 
531  if( nonzeros > UpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
532  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
533  }
534 
535  if( n == 0UL ) return;
536 
537  matrix.reset();
538  matrix.reserve( nonzeros );
539 
540  while( matrix.nonZeros() < nonzeros ) {
541  const size_t row( rand<size_t>( 0UL, n-1UL ) );
542  const size_t col( rand<size_t>( row, n-1UL ) );
543  matrix(row,col) = rand<ET>( min, max );
544  }
545 }
547 //*************************************************************************************************
548 
549 
550 
551 
552 //=================================================================================================
553 //
554 // MAKE FUNCTIONS
555 //
556 //=================================================================================================
557 
558 //*************************************************************************************************
565 template< typename MT // Type of the adapted matrix
566  , bool SO // Storage order of the adapted matrix
567  , bool DF > // Density flag
568 void makeSymmetric( UpperMatrix<MT,SO,DF>& matrix )
569 {
570  const size_t n( matrix.rows() );
571 
572  reset( matrix );
573 
574  for( size_t i=0UL; i<n; ++i ) {
575  matrix(i,i) = rand<typename MT::ElementType>();
576  }
577 
578  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
579 }
581 //*************************************************************************************************
582 
583 
584 //*************************************************************************************************
593 template< typename MT // Type of the adapted matrix
594  , bool SO // Storage order of the adapted matrix
595  , bool DF // Density flag
596  , typename Arg > // Min/max argument type
597 void makeSymmetric( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
598 {
599  typedef typename MT::ElementType Type;
600 
601  const size_t n( matrix.rows() );
602 
603  reset( matrix );
604 
605  for( size_t i=0UL; i<n; ++i ) {
606  matrix(i,i) = rand<Type>( min, max );
607  }
608 
609  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
610 }
612 //*************************************************************************************************
613 
614 
615 //*************************************************************************************************
622 template< typename MT // Type of the adapted matrix
623  , bool SO // Storage order of the adapted matrix
624  , bool DF > // Density flag
625 void makeHermitian( UpperMatrix<MT,SO,DF>& matrix )
626 {
627  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type Type;
628 
629  const size_t n( matrix.rows() );
630 
631  reset( matrix );
632 
633  for( size_t i=0UL; i<n; ++i ) {
634  matrix(i,i) = rand<Type>();
635  }
636 
637  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
638 }
640 //*************************************************************************************************
641 
642 
643 //*************************************************************************************************
652 template< typename MT // Type of the adapted matrix
653  , bool SO // Storage order of the adapted matrix
654  , bool DF // Density flag
655  , typename Arg > // Min/max argument type
656 void makeHermitian( UpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
657 {
658  typedef typename UnderlyingBuiltin<typename MT::ElementType>::Type Type;
659 
660  const size_t n( matrix.rows() );
661 
662  reset( matrix );
663 
664  for( size_t i=0UL; i<n; ++i ) {
665  matrix(i,i) = rand<Type>( min, max );
666  }
667 
668  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
669 }
671 //*************************************************************************************************
672 
673 
674 //*************************************************************************************************
681 template< typename MT // Type of the adapted matrix
682  , bool SO // Storage order of the adapted matrix
683  , bool DF > // Density flag
684 void makePositiveDefinite( UpperMatrix<MT,SO,DF>& matrix )
685 {
686  makeHermitian( matrix );
687 }
689 //*************************************************************************************************
690 
691 } // namespace blaze
692 
693 #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
Header file for the complete LowerMatrix implementation.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
Header file for the IsDenseMatrix type trait.
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename RowExprTrait< MT >::Type >::Type row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:107
Header file for the implementation of a upper matrix adaptor.
#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.