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>
44 #include <vector>
45 #include <blaze/math/Aliases.h>
50 #include <blaze/math/DenseMatrix.h>
51 #include <blaze/math/Exception.h>
55 #include <blaze/util/FalseType.h>
56 #include <blaze/util/Indices.h>
57 #include <blaze/util/Random.h>
58 #include <blaze/util/TrueType.h>
59 #include <blaze/util/Types.h>
60 #include <blaze/util/Unused.h>
61 
62 
63 namespace blaze {
64 
65 //=================================================================================================
66 //
67 // RAND SPECIALIZATION
68 //
69 //=================================================================================================
70 
71 //*************************************************************************************************
78 template< typename MT // Type of the adapted matrix
79  , bool SO // Storage order of the adapted matrix
80  , bool DF > // Numeric flag
81 class Rand< UniLowerMatrix<MT,SO,DF> >
82 {
83  public:
84  //**Generate functions**************************************************************************
87  inline const UniLowerMatrix<MT,SO,DF> generate() const;
88  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n ) const;
89  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
90 
91  template< typename Arg >
92  inline const UniLowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
96 
97  template< typename Arg >
98  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
99  const Arg& min, const Arg& max ) const;
101  //**********************************************************************************************
102 
103  //**Randomize functions*************************************************************************
106  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix ) const;
107  inline void randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
108  inline void randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
109 
110  template< typename Arg >
111  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
112 
113  template< typename Arg >
114  inline void randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros,
115  const Arg& min, const Arg& max ) const;
116 
117  template< typename Arg >
118  inline void randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros,
119  const Arg& min, const Arg& max ) const;
121  //**********************************************************************************************
122 
123  private:
124  //**Randomize functions*************************************************************************
127  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
128  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
129 
130  template< typename Arg >
131  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
132 
133  template< typename Arg >
134  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
136  //**********************************************************************************************
137 };
139 //*************************************************************************************************
140 
141 
142 //*************************************************************************************************
148 template< typename MT // Type of the adapted matrix
149  , bool SO // Storage order of the adapted matrix
150  , bool DF > // Numeric flag
151 inline const UniLowerMatrix<MT,SO,DF> Rand< UniLowerMatrix<MT,SO,DF> >::generate() const
152 {
154 
155  UniLowerMatrix<MT,SO,DF> matrix;
156  randomize( matrix );
157  return matrix;
158 }
160 //*************************************************************************************************
161 
162 
163 //*************************************************************************************************
170 template< typename MT // Type of the adapted matrix
171  , bool SO // Storage order of the adapted matrix
172  , bool DF > // Numeric flag
173 inline const UniLowerMatrix<MT,SO,DF>
174  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n ) const
175 {
177 
178  UniLowerMatrix<MT,SO,DF> matrix( n );
179  randomize( matrix );
180  return matrix;
181 }
183 //*************************************************************************************************
184 
185 
186 //*************************************************************************************************
195 template< typename MT // Type of the adapted matrix
196  , bool SO // Storage order of the adapted matrix
197  , bool DF > // Numeric flag
198 inline const UniLowerMatrix<MT,SO,DF>
199  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
200 {
203 
204  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
205  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
206  }
207 
208  UniLowerMatrix<MT,SO,DF> matrix( n );
209  randomize( matrix, nonzeros );
210 
211  return matrix;
212 }
214 //*************************************************************************************************
215 
216 
217 //*************************************************************************************************
225 template< typename MT // Type of the adapted matrix
226  , bool SO // Storage order of the adapted matrix
227  , bool DF > // Numeric flag
228 template< typename Arg > // Min/max argument type
229 inline const UniLowerMatrix<MT,SO,DF>
230  Rand< UniLowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
231 {
233 
234  UniLowerMatrix<MT,SO,DF> matrix;
235  randomize( matrix, min, max );
236  return matrix;
237 }
239 //*************************************************************************************************
240 
241 
242 //*************************************************************************************************
251 template< typename MT // Type of the adapted matrix
252  , bool SO // Storage order of the adapted matrix
253  , bool DF > // Numeric flag
254 template< typename Arg > // Min/max argument type
255 inline const UniLowerMatrix<MT,SO,DF>
256  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
257 {
259 
260  UniLowerMatrix<MT,SO,DF> matrix( n );
261  randomize( matrix, min, max );
262  return matrix;
263 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
279 template< typename MT // Type of the adapted matrix
280  , bool SO // Storage order of the adapted matrix
281  , bool DF > // Numeric flag
282 template< typename Arg > // Min/max argument type
283 inline const UniLowerMatrix<MT,SO,DF>
284  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
285  const Arg& min, const Arg& max ) const
286 {
289 
290  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
291  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
292  }
293 
294  UniLowerMatrix<MT,SO,DF> matrix( n );
295  randomize( matrix, nonzeros, min, max );
296 
297  return matrix;
298 }
300 //*************************************************************************************************
301 
302 
303 //*************************************************************************************************
310 template< typename MT // Type of the adapted matrix
311  , bool SO // Storage order of the adapted matrix
312  , bool DF > // Numeric flag
313 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix ) const
314 {
315  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
316 }
318 //*************************************************************************************************
319 
320 
321 //*************************************************************************************************
328 template< typename MT // Type of the adapted matrix
329  , bool SO // Storage order of the adapted matrix
330  , bool DF > // Numeric flag
331 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const
332 {
334 
335  typedef ElementType_<MT> ET;
336 
337  const size_t n( matrix.rows() );
338 
339  for( size_t i=1UL; i<n; ++i ) {
340  for( size_t j=0UL; j<i; ++j ) {
341  matrix(i,j) = rand<ET>();
342  }
343  }
344 }
346 //*************************************************************************************************
347 
348 
349 //*************************************************************************************************
356 template< typename MT // Type of the adapted matrix
357  , bool SO // Storage order of the adapted matrix
358  , bool DF > // Numeric flag
359 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const
360 {
362 
363  const size_t n( matrix.rows() );
364 
365  if( n == 0UL || n == 1UL ) return;
366 
367  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
368 
369  randomize( matrix, nonzeros );
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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
388 {
390 
391  typedef ElementType_<MT> ET;
392 
393  const size_t n( matrix.rows() );
394 
395  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
396  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
397  }
398 
399  if( n == 0UL || n == 1UL ) return;
400 
401  matrix.reset();
402  matrix.reserve( nonzeros );
403  matrix.finalize( 0UL );
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>( 1UL, n-1UL );
409  if( dist[index] == index ) continue;
410  ++dist[index];
411  ++nz;
412  }
413 
414  for( size_t i=1UL; i<n; ++i ) {
415  const Indices indices( 0UL, i-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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
439 {
441 
442  typedef ElementType_<MT> ET;
443 
444  const size_t n( matrix.rows() );
445 
446  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
447  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
448  }
449 
450  if( n == 0UL || n == 1UL ) return;
451 
452  matrix.reset();
453  matrix.reserve( nonzeros );
454 
455  std::vector<size_t> dist( n-1UL );
456 
457  for( size_t nz=0UL; nz<nonzeros; ) {
458  const size_t index = rand<size_t>( 0UL, n-2UL );
459  if( dist[index] == n - index - 1UL ) continue;
460  ++dist[index];
461  ++nz;
462  }
463 
464  for( size_t j=0UL; j<n-1UL; ++j ) {
465  const Indices indices( j+1UL, n-1UL, dist[j] );
466  for( size_t i : indices ) {
467  matrix.append( i, j, rand<ET>() );
468  }
469  matrix.finalize( j );
470  }
471 
472  matrix.finalize( n-1UL );
473 }
475 //*************************************************************************************************
476 
477 
478 //*************************************************************************************************
487 template< typename MT // Type of the adapted matrix
488  , bool SO // Storage order of the adapted matrix
489  , bool DF > // Numeric flag
490 template< typename Arg > // Min/max argument type
491 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
492  const Arg& min, const Arg& max ) const
493 {
494  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
495 }
497 //*************************************************************************************************
498 
499 
500 //*************************************************************************************************
509 template< typename MT // Type of the adapted matrix
510  , bool SO // Storage order of the adapted matrix
511  , bool DF > // Numeric flag
512 template< typename Arg > // Min/max argument type
513 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
514  const Arg& min, const Arg& max, TrueType ) const
515 {
517 
518  typedef ElementType_<MT> ET;
519 
520  const size_t n( matrix.rows() );
521 
522  for( size_t i=1UL; i<n; ++i ) {
523  for( size_t j=0UL; j<i; ++j ) {
524  matrix(i,j) = rand<ET>( min, max );
525  }
526  }
527 }
529 //*************************************************************************************************
530 
531 
532 //*************************************************************************************************
541 template< typename MT // Type of the adapted matrix
542  , bool SO // Storage order of the adapted matrix
543  , bool DF > // Numeric flag
544 template< typename Arg > // Min/max argument type
545 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
546  const Arg& min, const Arg& max, FalseType ) const
547 {
549 
550  const size_t n( matrix.rows() );
551 
552  if( n == 0UL || n == 1UL ) return;
553 
554  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
555 
556  randomize( matrix, nonzeros, min, max );
557 }
559 //*************************************************************************************************
560 
561 
562 //*************************************************************************************************
573 template< typename MT // Type of the adapted matrix
574  , bool SO // Storage order of the adapted matrix
575  , bool DF > // Numeric flag
576 template< typename Arg > // Min/max argument type
577 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,false,DF>& matrix,
578  size_t nonzeros, const Arg& min, const Arg& max ) const
579 {
581 
582  typedef ElementType_<MT> ET;
583 
584  const size_t n( matrix.rows() );
585 
586  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
587  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
588  }
589 
590  if( n == 0UL || n == 1UL ) return;
591 
592  matrix.reset();
593  matrix.reserve( nonzeros );
594  matrix.finalize( 0UL );
595 
596  std::vector<size_t> dist( n );
597 
598  for( size_t nz=0UL; nz<nonzeros; ) {
599  const size_t index = rand<size_t>( 1UL, n-1UL );
600  if( dist[index] == index ) continue;
601  ++dist[index];
602  ++nz;
603  }
604 
605  for( size_t i=1UL; i<n; ++i ) {
606  const Indices indices( 0UL, i-1UL, dist[i] );
607  for( size_t j : indices ) {
608  matrix.append( i, j, rand<ET>( min, max ) );
609  }
610  matrix.finalize( i );
611  }
612 }
614 //*************************************************************************************************
615 
616 
617 //*************************************************************************************************
628 template< typename MT // Type of the adapted matrix
629  , bool SO // Storage order of the adapted matrix
630  , bool DF > // Numeric flag
631 template< typename Arg > // Min/max argument type
632 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,true,DF>& matrix,
633  size_t nonzeros, const Arg& min, const Arg& max ) const
634 {
636 
637  typedef ElementType_<MT> ET;
638 
639  const size_t n( matrix.rows() );
640 
641  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
642  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
643  }
644 
645  if( n == 0UL || n == 1UL ) return;
646 
647  matrix.reset();
648  matrix.reserve( nonzeros );
649 
650  std::vector<size_t> dist( n-1UL );
651 
652  for( size_t nz=0UL; nz<nonzeros; ) {
653  const size_t index = rand<size_t>( 0UL, n-2UL );
654  if( dist[index] == n - index - 1UL ) continue;
655  ++dist[index];
656  ++nz;
657  }
658 
659  for( size_t j=0UL; j<n-1UL; ++j ) {
660  const Indices indices( j+1UL, n-1UL, dist[j] );
661  for( size_t i : indices ) {
662  matrix.append( i, j, rand<ET>( min, max ) );
663  }
664  matrix.finalize( j );
665  }
666 
667  matrix.finalize( n-1UL );
668 }
670 //*************************************************************************************************
671 
672 
673 
674 
675 //=================================================================================================
676 //
677 // MAKE FUNCTIONS
678 //
679 //=================================================================================================
680 
681 //*************************************************************************************************
688 template< typename MT // Type of the adapted matrix
689  , bool SO // Storage order of the adapted matrix
690  , bool DF > // Density flag
691 void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix )
692 {
693  reset( matrix );
694 
695  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
696 }
698 //*************************************************************************************************
699 
700 
701 //*************************************************************************************************
710 template< typename MT // Type of the adapted matrix
711  , bool SO // Storage order of the adapted matrix
712  , bool DF // Density flag
713  , typename Arg > // Min/max argument type
714 void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
715 {
716  UNUSED_PARAMETER( min, max );
717 
718  makeSymmetric( matrix );
719 }
721 //*************************************************************************************************
722 
723 
724 //*************************************************************************************************
731 template< typename MT // Type of the adapted matrix
732  , bool SO // Storage order of the adapted matrix
733  , bool DF > // Density flag
734 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix )
735 {
736  reset( matrix );
737 
738  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
739 }
741 //*************************************************************************************************
742 
743 
744 //*************************************************************************************************
753 template< typename MT // Type of the adapted matrix
754  , bool SO // Storage order of the adapted matrix
755  , bool DF // Density flag
756  , typename Arg > // Min/max argument type
757 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
758 {
759  UNUSED_PARAMETER( min, max );
760 
761  makeHermitian( matrix );
762 }
764 //*************************************************************************************************
765 
766 
767 //*************************************************************************************************
774 template< typename MT // Type of the adapted matrix
775  , bool SO // Storage order of the adapted matrix
776  , bool DF > // Density flag
777 void makePositiveDefinite( UniLowerMatrix<MT,SO,DF>& matrix )
778 {
779  makeHermitian( matrix );
780 }
782 //*************************************************************************************************
783 
784 } // namespace blaze
785 
786 #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 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:61
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:926
Constraint on the data type.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:689
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:533
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
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
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
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:289
Header file for the implementation of a lower unitriangular matrix adaptor.
Header file for the exception macros of the math module.
Header file for the IsDenseMatrix type trait.
Header file for the Indices class.
#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:81
T generate() const
Generation of a random value in the range .
Definition: Random.h:249
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:61
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:759
void UNUSED_PARAMETER(const Args &...)
Suppression of unused parameter warnings.
Definition: Unused.h:81
#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.
const DMatForEachExpr< MT, Ceil, SO > ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatForEachExpr.h:1130