Blaze  3.6
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>
52 #include <blaze/math/DenseMatrix.h>
53 #include <blaze/math/Exception.h>
57 #include <blaze/util/Indices.h>
59 #include <blaze/util/MaybeUnused.h>
60 #include <blaze/util/Random.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< UniLowerMatrix<MT,SO,DF> >
83 {
84  public:
85  //**Generate functions**************************************************************************
88  inline const UniLowerMatrix<MT,SO,DF> generate() const;
89  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n ) const;
90  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
91 
92  template< typename Arg >
93  inline const UniLowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
94 
95  template< typename Arg >
96  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
97 
98  template< typename Arg >
99  inline const UniLowerMatrix<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( UniLowerMatrix<MT,SO,DF>& matrix ) const;
108  inline void randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
109  inline void randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
110 
111  template< typename Arg >
112  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
113 
114  template< typename Arg >
115  inline void randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros,
116  const Arg& min, const Arg& max ) const;
117 
118  template< typename Arg >
119  inline void randomize( UniLowerMatrix<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( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
129  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
130 
131  template< typename Arg >
132  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
133 
134  template< typename Arg >
135  inline void randomize( UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF> Rand< UniLowerMatrix<MT,SO,DF> >::generate() const
153 {
155 
156  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
175  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n ) const
176 {
178 
179  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
200  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
201 {
204 
205  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
206  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
207  }
208 
209  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
231  Rand< UniLowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
232 {
234 
235  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
257  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
258 {
260 
261  UniLowerMatrix<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 UniLowerMatrix<MT,SO,DF>
285  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
286  const Arg& min, const Arg& max ) const
287 {
290 
291  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
292  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
293  }
294 
295  UniLowerMatrix<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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<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=1UL; i<n; ++i ) {
341  for( size_t j=0UL; j<i; ++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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const
361 {
363 
364  const size_t n( matrix.rows() );
365 
366  if( n == 0UL || n == 1UL ) return;
367 
368  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*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< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<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 > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
397  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
398  }
399 
400  if( n == 0UL || n == 1UL ) return;
401 
402  matrix.reset();
403  matrix.reserve( nonzeros );
404  matrix.finalize( 0UL );
405 
406  std::vector<size_t> dist( n );
407 
408  for( size_t nz=0UL; nz<nonzeros; ) {
409  const size_t index = rand<size_t>( 1UL, n-1UL );
410  if( dist[index] == index ) continue;
411  ++dist[index];
412  ++nz;
413  }
414 
415  for( size_t i=1UL; i<n; ++i ) {
416  const Indices indices( 0UL, i-1UL, dist[i] );
417  for( size_t j : indices ) {
418  matrix.append( i, j, rand<ET>() );
419  }
420  matrix.finalize( i );
421  }
422 }
424 //*************************************************************************************************
425 
426 
427 //*************************************************************************************************
436 template< typename MT // Type of the adapted matrix
437  , bool SO // Storage order of the adapted matrix
438  , bool DF > // Numeric flag
439 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
440 {
442 
443  using ET = ElementType_t<MT>;
444 
445  const size_t n( matrix.rows() );
446 
447  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
448  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
449  }
450 
451  if( n == 0UL || n == 1UL ) return;
452 
453  matrix.reset();
454  matrix.reserve( nonzeros );
455 
456  std::vector<size_t> dist( n-1UL );
457 
458  for( size_t nz=0UL; nz<nonzeros; ) {
459  const size_t index = rand<size_t>( 0UL, n-2UL );
460  if( dist[index] == n - index - 1UL ) continue;
461  ++dist[index];
462  ++nz;
463  }
464 
465  for( size_t j=0UL; j<n-1UL; ++j ) {
466  const Indices indices( j+1UL, n-1UL, dist[j] );
467  for( size_t i : indices ) {
468  matrix.append( i, j, rand<ET>() );
469  }
470  matrix.finalize( j );
471  }
472 
473  matrix.finalize( n-1UL );
474 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
488 template< typename MT // Type of the adapted matrix
489  , bool SO // Storage order of the adapted matrix
490  , bool DF > // Numeric flag
491 template< typename Arg > // Min/max argument type
492 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
493  const Arg& min, const Arg& max ) const
494 {
495  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
496 }
498 //*************************************************************************************************
499 
500 
501 //*************************************************************************************************
510 template< typename MT // Type of the adapted matrix
511  , bool SO // Storage order of the adapted matrix
512  , bool DF > // Numeric flag
513 template< typename Arg > // Min/max argument type
514 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
515  const Arg& min, const Arg& max, TrueType ) const
516 {
518 
519  using ET = ElementType_t<MT>;
520 
521  const size_t n( matrix.rows() );
522 
523  for( size_t i=1UL; i<n; ++i ) {
524  for( size_t j=0UL; j<i; ++j ) {
525  matrix(i,j) = rand<ET>( min, max );
526  }
527  }
528 }
530 //*************************************************************************************************
531 
532 
533 //*************************************************************************************************
542 template< typename MT // Type of the adapted matrix
543  , bool SO // Storage order of the adapted matrix
544  , bool DF > // Numeric flag
545 template< typename Arg > // Min/max argument type
546 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
547  const Arg& min, const Arg& max, FalseType ) const
548 {
550 
551  const size_t n( matrix.rows() );
552 
553  if( n == 0UL || n == 1UL ) return;
554 
555  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
556 
557  randomize( matrix, nonzeros, min, max );
558 }
560 //*************************************************************************************************
561 
562 
563 //*************************************************************************************************
574 template< typename MT // Type of the adapted matrix
575  , bool SO // Storage order of the adapted matrix
576  , bool DF > // Numeric flag
577 template< typename Arg > // Min/max argument type
578 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,false,DF>& matrix,
579  size_t nonzeros, const Arg& min, const Arg& max ) const
580 {
582 
583  using ET = ElementType_t<MT>;
584 
585  const size_t n( matrix.rows() );
586 
587  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
588  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
589  }
590 
591  if( n == 0UL || n == 1UL ) return;
592 
593  matrix.reset();
594  matrix.reserve( nonzeros );
595  matrix.finalize( 0UL );
596 
597  std::vector<size_t> dist( n );
598 
599  for( size_t nz=0UL; nz<nonzeros; ) {
600  const size_t index = rand<size_t>( 1UL, n-1UL );
601  if( dist[index] == index ) continue;
602  ++dist[index];
603  ++nz;
604  }
605 
606  for( size_t i=1UL; i<n; ++i ) {
607  const Indices indices( 0UL, i-1UL, dist[i] );
608  for( size_t j : indices ) {
609  matrix.append( i, j, rand<ET>( min, max ) );
610  }
611  matrix.finalize( i );
612  }
613 }
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
629 template< typename MT // Type of the adapted matrix
630  , bool SO // Storage order of the adapted matrix
631  , bool DF > // Numeric flag
632 template< typename Arg > // Min/max argument type
633 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,true,DF>& matrix,
634  size_t nonzeros, const Arg& min, const Arg& max ) const
635 {
637 
638  using ET = ElementType_t<MT>;
639 
640  const size_t n( matrix.rows() );
641 
642  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
643  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
644  }
645 
646  if( n == 0UL || n == 1UL ) return;
647 
648  matrix.reset();
649  matrix.reserve( nonzeros );
650 
651  std::vector<size_t> dist( n-1UL );
652 
653  for( size_t nz=0UL; nz<nonzeros; ) {
654  const size_t index = rand<size_t>( 0UL, n-2UL );
655  if( dist[index] == n - index - 1UL ) continue;
656  ++dist[index];
657  ++nz;
658  }
659 
660  for( size_t j=0UL; j<n-1UL; ++j ) {
661  const Indices indices( j+1UL, n-1UL, dist[j] );
662  for( size_t i : indices ) {
663  matrix.append( i, j, rand<ET>( min, max ) );
664  }
665  matrix.finalize( j );
666  }
667 
668  matrix.finalize( n-1UL );
669 }
671 //*************************************************************************************************
672 
673 
674 
675 
676 //=================================================================================================
677 //
678 // MAKE FUNCTIONS
679 //
680 //=================================================================================================
681 
682 //*************************************************************************************************
689 template< typename MT // Type of the adapted matrix
690  , bool SO // Storage order of the adapted matrix
691  , bool DF > // Density flag
692 void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix )
693 {
694  reset( matrix );
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( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
716 {
717  MAYBE_UNUSED( min, max );
718 
719  makeSymmetric( matrix );
720 }
722 //*************************************************************************************************
723 
724 
725 //*************************************************************************************************
732 template< typename MT // Type of the adapted matrix
733  , bool SO // Storage order of the adapted matrix
734  , bool DF > // Density flag
735 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix )
736 {
737  reset( matrix );
738 
739  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
740 }
742 //*************************************************************************************************
743 
744 
745 //*************************************************************************************************
754 template< typename MT // Type of the adapted matrix
755  , bool SO // Storage order of the adapted matrix
756  , bool DF // Density flag
757  , typename Arg > // Min/max argument type
758 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
759 {
760  MAYBE_UNUSED( min, max );
761 
762  makeHermitian( matrix );
763 }
765 //*************************************************************************************************
766 
767 
768 //*************************************************************************************************
775 template< typename MT // Type of the adapted matrix
776  , bool SO // Storage order of the adapted matrix
777  , bool DF > // Density flag
778 void makePositiveDefinite( UniLowerMatrix<MT,SO,DF>& matrix )
779 {
780  makeHermitian( matrix );
781 }
783 //*************************************************************************************************
784 
785 } // namespace blaze
786 
787 #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
Header file for the MAYBE_UNUSED function template.
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
Header file for the complete UniUpperMatrix implementation.
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.
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 implementation of a lower unitriangular matrix adaptor.
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
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 strictly lower triangular matrix adaptor.
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