UniLowerMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_UNILOWERMATRIX_H_
36 #define _BLAZE_MATH_UNILOWERMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
48 #include <blaze/math/DenseMatrix.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 #include <blaze/util/Unused.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< UniLowerMatrix<MT,SO,DF> >
79 {
80  public:
81  //**Generate functions**************************************************************************
84  inline const UniLowerMatrix<MT,SO,DF> generate() const;
85  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n ) const;
86  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
87 
88  template< typename Arg >
89  inline const UniLowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
90 
91  template< typename Arg >
92  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline const UniLowerMatrix<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( UniLowerMatrix<MT,SO,DF>& matrix ) const;
104  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
105 
106  template< typename Arg >
107  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
108 
109  template< typename Arg >
110  inline void randomize( UniLowerMatrix<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( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
120  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
121 
122  template< typename Arg >
123  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
124 
125  template< typename Arg >
126  inline void randomize( UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF> Rand< UniLowerMatrix<MT,SO,DF> >::generate() const
144 {
146 
147  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
166  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n ) const
167 {
169 
170  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
191  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
192 {
195 
196  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
197  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
198  }
199 
200  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
222  Rand< UniLowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
223 {
225 
226  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
248  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
249 {
251 
252  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
276  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
277  const Arg& min, const Arg& max ) const
278 {
281 
282  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
283  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
284  }
285 
286  UniLowerMatrix<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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const
324 {
326 
327  typedef typename MT::ElementType ET;
328 
329  const size_t n( matrix.rows() );
330 
331  for( size_t i=1UL; i<n; ++i ) {
332  for( size_t j=0UL; j<i; ++j ) {
333  matrix(i,j) = rand<ET>();
334  }
335  }
336 }
338 //*************************************************************************************************
339 
340 
341 //*************************************************************************************************
348 template< typename MT // Type of the adapted matrix
349  , bool SO // Storage order of the adapted matrix
350  , bool DF > // Numeric flag
351 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const
352 {
354 
355  typedef typename MT::ElementType ET;
356 
357  const size_t n( matrix.rows() );
358 
359  if( n == 0UL ) return;
360 
361  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
362 
363  matrix.reset();
364  matrix.reserve( nonzeros );
365 
366  while( matrix.nonZeros() < nonzeros ) {
367  const size_t row( rand<size_t>( 1UL, n-1UL ) );
368  const size_t col( rand<size_t>( 0UL, row-1UL ) );
369  matrix(row,col) = rand<ET>();
370  }
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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
389 {
391 
392  typedef typename MT::ElementType ET;
393 
394  const size_t n( matrix.rows() );
395 
396  if( nonzeros > UniLowerMatrix<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  while( matrix.nonZeros() < nonzeros ) {
406  const size_t row( rand<size_t>( 1UL, n-1UL ) );
407  const size_t col( rand<size_t>( 0UL, row-1UL ) );
408  matrix(row,col) = rand<ET>();
409  }
410 }
412 //*************************************************************************************************
413 
414 
415 //*************************************************************************************************
424 template< typename MT // Type of the adapted matrix
425  , bool SO // Storage order of the adapted matrix
426  , bool DF > // Numeric flag
427 template< typename Arg > // Min/max argument type
428 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
429  const Arg& min, const Arg& max ) const
430 {
431  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
432 }
434 //*************************************************************************************************
435 
436 
437 //*************************************************************************************************
446 template< typename MT // Type of the adapted matrix
447  , bool SO // Storage order of the adapted matrix
448  , bool DF > // Numeric flag
449 template< typename Arg > // Min/max argument type
450 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
451  const Arg& min, const Arg& max, TrueType ) const
452 {
454 
455  typedef typename MT::ElementType ET;
456 
457  const size_t n( matrix.rows() );
458 
459  for( size_t i=1UL; i<n; ++i ) {
460  for( size_t j=0UL; j<i; ++j ) {
461  matrix(i,j) = rand<ET>( min, max );
462  }
463  }
464 }
466 //*************************************************************************************************
467 
468 
469 //*************************************************************************************************
478 template< typename MT // Type of the adapted matrix
479  , bool SO // Storage order of the adapted matrix
480  , bool DF > // Numeric flag
481 template< typename Arg > // Min/max argument type
482 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
483  const Arg& min, const Arg& max, FalseType ) const
484 {
486 
487  typedef typename MT::ElementType ET;
488 
489  const size_t n( matrix.rows() );
490 
491  if( n == 0UL ) return;
492 
493  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
494 
495  matrix.reset();
496  matrix.reserve( nonzeros );
497 
498  while( matrix.nonZeros() < nonzeros ) {
499  const size_t row( rand<size_t>( 1UL, n-1UL ) );
500  const size_t col( rand<size_t>( 0UL, row-1UL ) );
501  matrix(row,col) = rand<ET>( min, max );
502  }
503 }
505 //*************************************************************************************************
506 
507 
508 //*************************************************************************************************
519 template< typename MT // Type of the adapted matrix
520  , bool SO // Storage order of the adapted matrix
521  , bool DF > // Numeric flag
522 template< typename Arg > // Min/max argument type
523 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
524  size_t nonzeros, const Arg& min, const Arg& max ) const
525 {
527 
528  typedef typename MT::ElementType ET;
529 
530  const size_t n( matrix.rows() );
531 
532  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
533  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
534  }
535 
536  if( n == 0UL ) return;
537 
538  matrix.reset();
539  matrix.reserve( nonzeros );
540 
541  while( matrix.nonZeros() < nonzeros ) {
542  const size_t row( rand<size_t>( 1UL, n-1UL ) );
543  const size_t col( rand<size_t>( 0UL, row-1UL ) );
544  matrix(row,col) = rand<ET>( min, max );
545  }
546 }
548 //*************************************************************************************************
549 
550 
551 
552 
553 //=================================================================================================
554 //
555 // MAKE FUNCTIONS
556 //
557 //=================================================================================================
558 
559 //*************************************************************************************************
566 template< typename MT // Type of the adapted matrix
567  , bool SO // Storage order of the adapted matrix
568  , bool DF > // Density flag
569 void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix )
570 {
571  reset( matrix );
572 
573  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
574 }
576 //*************************************************************************************************
577 
578 
579 //*************************************************************************************************
588 template< typename MT // Type of the adapted matrix
589  , bool SO // Storage order of the adapted matrix
590  , bool DF // Density flag
591  , typename Arg > // Min/max argument type
592 void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
593 {
594  UNUSED_PARAMETER( min, max );
595 
596  makeSymmetric( matrix );
597 }
599 //*************************************************************************************************
600 
601 
602 //*************************************************************************************************
609 template< typename MT // Type of the adapted matrix
610  , bool SO // Storage order of the adapted matrix
611  , bool DF > // Density flag
612 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix )
613 {
614  reset( matrix );
615 
616  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
617 }
619 //*************************************************************************************************
620 
621 
622 //*************************************************************************************************
631 template< typename MT // Type of the adapted matrix
632  , bool SO // Storage order of the adapted matrix
633  , bool DF // Density flag
634  , typename Arg > // Min/max argument type
635 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
636 {
637  UNUSED_PARAMETER( min, max );
638 
639  makeHermitian( matrix );
640 }
642 //*************************************************************************************************
643 
644 
645 //*************************************************************************************************
652 template< typename MT // Type of the adapted matrix
653  , bool SO // Storage order of the adapted matrix
654  , bool DF > // Density flag
655 void makePositiveDefinite( UniLowerMatrix<MT,SO,DF>& matrix )
656 {
657  makeHermitian( matrix );
658 }
660 //*************************************************************************************************
661 
662 } // namespace blaze
663 
664 #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 the UNUSED_PARAMETER function template.
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
void UNUSED_PARAMETER(const T1 &)
Suppression of unused parameter warnings.
Definition: Unused.h:81
Implementation of a random number generator.
Header file for the complete UniUpperMatrix implementation.
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 implementation of a lower unitriangular matrix adaptor.
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
#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.