Blaze  3.6
LowerMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_LOWERMATRIX_H_
36 #define _BLAZE_MATH_LOWERMATRIX_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>
56 #include <blaze/math/UpperMatrix.h>
57 #include <blaze/util/Indices.h>
59 #include <blaze/util/Random.h>
60 #include <blaze/util/Types.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< LowerMatrix<MT,SO,DF> >
82 {
83  public:
84  //**Generate functions**************************************************************************
87  inline const LowerMatrix<MT,SO,DF> generate() const;
88  inline const LowerMatrix<MT,SO,DF> generate( size_t n ) const;
89  inline const LowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
90 
91  template< typename Arg >
92  inline const LowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline const LowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
96 
97  template< typename Arg >
98  inline const LowerMatrix<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( LowerMatrix<MT,SO,DF>& matrix ) const;
107  inline void randomize( LowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
108  inline void randomize( LowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
109 
110  template< typename Arg >
111  inline void randomize( LowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
112 
113  template< typename Arg >
114  inline void randomize( LowerMatrix<MT,false,DF>& matrix, size_t nonzeros,
115  const Arg& min, const Arg& max ) const;
116 
117  template< typename Arg >
118  inline void randomize( LowerMatrix<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( LowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
128  inline void randomize( LowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
129 
130  template< typename Arg >
131  inline void randomize( LowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
132 
133  template< typename Arg >
134  inline void randomize( LowerMatrix<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 LowerMatrix<MT,SO,DF> Rand< LowerMatrix<MT,SO,DF> >::generate() const
152 {
154 
155  LowerMatrix<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 LowerMatrix<MT,SO,DF>
174  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n ) const
175 {
177 
178  LowerMatrix<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 LowerMatrix<MT,SO,DF>
199  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
200 {
203 
204  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
205  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
206  }
207 
208  LowerMatrix<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 LowerMatrix<MT,SO,DF>
230  Rand< LowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
231 {
233 
234  LowerMatrix<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 LowerMatrix<MT,SO,DF>
256  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
257 {
259 
260  LowerMatrix<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 LowerMatrix<MT,SO,DF>
284  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
285  const Arg& min, const Arg& max ) const
286 {
289 
290  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
291  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
292  }
293 
294  LowerMatrix<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< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<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< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix, TrueType ) const
332 {
334 
335  using ET = ElementType_t<MT>;
336 
337  const size_t n( matrix.rows() );
338 
339  for( size_t i=0UL; 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< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix, FalseType ) const
360 {
362 
363  const size_t n( matrix.rows() );
364 
365  if( n == 0UL ) return;
366 
367  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*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< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
388 {
390 
391  using ET = ElementType_t<MT>;
392 
393  const size_t n( matrix.rows() );
394 
395  if( nonzeros > LowerMatrix<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  std::vector<size_t> dist( n );
405 
406  for( size_t nz=0UL; nz<nonzeros; ) {
407  const size_t index = rand<size_t>( 0UL, n-1UL );
408  if( dist[index] == index+1UL ) continue;
409  ++dist[index];
410  ++nz;
411  }
412 
413  for( size_t i=0UL; i<n; ++i ) {
414  const Indices indices( 0UL, i, dist[i] );
415  for( size_t j : indices ) {
416  matrix.append( i, j, rand<ET>() );
417  }
418  matrix.finalize( i );
419  }
420 }
422 //*************************************************************************************************
423 
424 
425 //*************************************************************************************************
434 template< typename MT // Type of the adapted matrix
435  , bool SO // Storage order of the adapted matrix
436  , bool DF > // Numeric flag
437 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
438 {
440 
441  using ET = ElementType_t<MT>;
442 
443  const size_t n( matrix.rows() );
444 
445  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
446  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
447  }
448 
449  if( n == 0UL ) return;
450 
451  matrix.reset();
452  matrix.reserve( nonzeros );
453 
454  std::vector<size_t> dist( n );
455 
456  for( size_t nz=0UL; nz<nonzeros; ) {
457  const size_t index = rand<size_t>( 0UL, n-1UL );
458  if( dist[index] == n - index ) continue;
459  ++dist[index];
460  ++nz;
461  }
462 
463  for( size_t j=0UL; j<n; ++j ) {
464  const Indices indices( j, n-1UL, dist[j] );
465  for( size_t i : indices ) {
466  matrix.append( i, j, rand<ET>() );
467  }
468  matrix.finalize( j );
469  }
470 }
472 //*************************************************************************************************
473 
474 
475 //*************************************************************************************************
484 template< typename MT // Type of the adapted matrix
485  , bool SO // Storage order of the adapted matrix
486  , bool DF > // Numeric flag
487 template< typename Arg > // Min/max argument type
488 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix,
489  const Arg& min, const Arg& max ) const
490 {
491  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
492 }
494 //*************************************************************************************************
495 
496 
497 //*************************************************************************************************
506 template< typename MT // Type of the adapted matrix
507  , bool SO // Storage order of the adapted matrix
508  , bool DF > // Numeric flag
509 template< typename Arg > // Min/max argument type
510 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix,
511  const Arg& min, const Arg& max, TrueType ) const
512 {
514 
515  using ET = ElementType_t<MT>;
516 
517  const size_t n( matrix.rows() );
518 
519  for( size_t i=0UL; i<n; ++i ) {
520  for( size_t j=0UL; j<=i; ++j ) {
521  matrix(i,j) = rand<ET>( min, max );
522  }
523  }
524 }
526 //*************************************************************************************************
527 
528 
529 //*************************************************************************************************
538 template< typename MT // Type of the adapted matrix
539  , bool SO // Storage order of the adapted matrix
540  , bool DF > // Numeric flag
541 template< typename Arg > // Min/max argument type
542 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix,
543  const Arg& min, const Arg& max, FalseType ) const
544 {
546 
547  const size_t n( matrix.rows() );
548 
549  if( n == 0UL ) return;
550 
551  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*n*n ) ) );
552 
553  randomize( matrix, nonzeros, min, max );
554 }
556 //*************************************************************************************************
557 
558 
559 //*************************************************************************************************
570 template< typename MT // Type of the adapted matrix
571  , bool SO // Storage order of the adapted matrix
572  , bool DF > // Numeric flag
573 template< typename Arg > // Min/max argument type
574 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,false,DF>& matrix,
575  size_t nonzeros, const Arg& min, const Arg& max ) const
576 {
578 
579  using ET = ElementType_t<MT>;
580 
581  const size_t n( matrix.rows() );
582 
583  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
584  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
585  }
586 
587  if( n == 0UL ) return;
588 
589  matrix.reset();
590  matrix.reserve( nonzeros );
591 
592  std::vector<size_t> dist( n );
593 
594  for( size_t nz=0UL; nz<nonzeros; ) {
595  const size_t index = rand<size_t>( 0UL, n-1UL );
596  if( dist[index] == index+1UL ) continue;
597  ++dist[index];
598  ++nz;
599  }
600 
601  for( size_t i=0UL; i<n; ++i ) {
602  const Indices indices( 0UL, i, dist[i] );
603  for( size_t j : indices ) {
604  matrix.append( i, j, rand<ET>( min, max ) );
605  }
606  matrix.finalize( i );
607  }
608 }
610 //*************************************************************************************************
611 
612 
613 //*************************************************************************************************
624 template< typename MT // Type of the adapted matrix
625  , bool SO // Storage order of the adapted matrix
626  , bool DF > // Numeric flag
627 template< typename Arg > // Min/max argument type
628 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,true,DF>& matrix,
629  size_t nonzeros, const Arg& min, const Arg& max ) const
630 {
632 
633  using ET = ElementType_t<MT>;
634 
635  const size_t n( matrix.rows() );
636 
637  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
638  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
639  }
640 
641  if( n == 0UL ) return;
642 
643  matrix.reset();
644  matrix.reserve( nonzeros );
645 
646  std::vector<size_t> dist( n );
647 
648  for( size_t nz=0UL; nz<nonzeros; ) {
649  const size_t index = rand<size_t>( 0UL, n-1UL );
650  if( dist[index] == n - index ) continue;
651  ++dist[index];
652  ++nz;
653  }
654 
655  for( size_t j=0UL; j<n; ++j ) {
656  const Indices indices( j, n-1UL, dist[j] );
657  for( size_t i : indices ) {
658  matrix.append( i, j, rand<ET>( min, max ) );
659  }
660  matrix.finalize( j );
661  }
662 }
664 //*************************************************************************************************
665 
666 
667 
668 
669 //=================================================================================================
670 //
671 // MAKE FUNCTIONS
672 //
673 //=================================================================================================
674 
675 //*************************************************************************************************
682 template< typename MT // Type of the adapted matrix
683  , bool SO // Storage order of the adapted matrix
684  , bool DF > // Density flag
685 void makeSymmetric( LowerMatrix<MT,SO,DF>& matrix )
686 {
687  const size_t n( matrix.rows() );
688 
689  reset( matrix );
690 
691  for( size_t i=0UL; i<n; ++i ) {
692  matrix(i,i) = rand< ElementType_t<MT> >();
693  }
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( LowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
715 {
716  using Type = ElementType_t<MT>;
717 
718  const size_t n( matrix.rows() );
719 
720  reset( matrix );
721 
722  for( size_t i=0UL; i<n; ++i ) {
723  matrix(i,i) = rand<Type>( min, max );
724  }
725 
726  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
727 }
729 //*************************************************************************************************
730 
731 
732 //*************************************************************************************************
739 template< typename MT // Type of the adapted matrix
740  , bool SO // Storage order of the adapted matrix
741  , bool DF > // Density flag
742 void makeHermitian( LowerMatrix<MT,SO,DF>& matrix )
743 {
744  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
745 
746  const size_t n( matrix.rows() );
747 
748  reset( matrix );
749 
750  for( size_t i=0UL; i<n; ++i ) {
751  matrix(i,i) = rand<Type>();
752  }
753 
754  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
755 }
757 //*************************************************************************************************
758 
759 
760 //*************************************************************************************************
769 template< typename MT // Type of the adapted matrix
770  , bool SO // Storage order of the adapted matrix
771  , bool DF // Density flag
772  , typename Arg > // Min/max argument type
773 void makeHermitian( LowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
774 {
775  using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
776 
777  const size_t n( matrix.rows() );
778 
779  reset( matrix );
780 
781  for( size_t i=0UL; i<n; ++i ) {
782  matrix(i,i) = rand<Type>( min, max );
783  }
784 
785  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
786 }
788 //*************************************************************************************************
789 
790 
791 //*************************************************************************************************
798 template< typename MT // Type of the adapted matrix
799  , bool SO // Storage order of the adapted matrix
800  , bool DF > // Density flag
801 void makePositiveDefinite( LowerMatrix<MT,SO,DF>& matrix )
802 {
803  makeHermitian( matrix );
804 }
806 //*************************************************************************************************
807 
808 } // namespace blaze
809 
810 #endif
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: IntegralConstant.h:121
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
Header file for basic type definitions.
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional matrix type,...
Definition: DenseMatrix.h:61
Constraint on the data type.
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1240
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:595
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
Implementation of a random number generator.
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: IntegralConstant.h:132
Constraint on the data type.
Constraint on the data type.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for all basic SparseMatrix functionality.
Header file for the UnderlyingBuiltin type trait.
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1162
Header file for the exception macros of the math module.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1198
Header file for the IsDenseMatrix type trait.
Header file for the implementation of a lower 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 'resize' member fu...
Definition: Resizable.h:81
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:1406
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:1328
T generate() const
Generation of a random value in the range .
Definition: Random.h:252
Header file for all basic DenseMatrix functionality.
#define BLAZE_CONSTRAINT_MUST_BE_RESIZABLE_TYPE(T)
Constraint on the data type.In case the given data type T is not resizable, i.e. does not have a 'res...
Definition: Resizable.h:61
Header file for the implementation of a diagonal matrix adaptor.
Header file for the complete UpperMatrix implementation.
Header file for the IntegralConstant class template.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type,...
Definition: SparseMatrix.h:61