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/FalseType.h>
58 #include <blaze/util/Indices.h>
59 #include <blaze/util/Random.h>
60 #include <blaze/util/TrueType.h>
61 #include <blaze/util/Types.h>
62 #include <blaze/util/Unused.h>
63 
64 
65 namespace blaze {
66 
67 //=================================================================================================
68 //
69 // RAND SPECIALIZATION
70 //
71 //=================================================================================================
72 
73 //*************************************************************************************************
80 template< typename MT // Type of the adapted matrix
81  , bool SO // Storage order of the adapted matrix
82  , bool DF > // Numeric flag
83 class Rand< UniLowerMatrix<MT,SO,DF> >
84 {
85  public:
86  //**Generate functions**************************************************************************
89  inline const UniLowerMatrix<MT,SO,DF> generate() const;
90  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n ) const;
91  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
92 
93  template< typename Arg >
94  inline const UniLowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
98 
99  template< typename Arg >
100  inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
101  const Arg& min, const Arg& max ) const;
103  //**********************************************************************************************
104 
105  //**Randomize functions*************************************************************************
108  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix ) const;
109  inline void randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
110  inline void randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
111 
112  template< typename Arg >
113  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
114 
115  template< typename Arg >
116  inline void randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros,
117  const Arg& min, const Arg& max ) const;
118 
119  template< typename Arg >
120  inline void randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros,
121  const Arg& min, const Arg& max ) const;
123  //**********************************************************************************************
124 
125  private:
126  //**Randomize functions*************************************************************************
129  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
130  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
131 
132  template< typename Arg >
133  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
134 
135  template< typename Arg >
136  inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
138  //**********************************************************************************************
139 };
141 //*************************************************************************************************
142 
143 
144 //*************************************************************************************************
150 template< typename MT // Type of the adapted matrix
151  , bool SO // Storage order of the adapted matrix
152  , bool DF > // Numeric flag
153 inline const UniLowerMatrix<MT,SO,DF> Rand< UniLowerMatrix<MT,SO,DF> >::generate() const
154 {
156 
157  UniLowerMatrix<MT,SO,DF> matrix;
158  randomize( matrix );
159  return matrix;
160 }
162 //*************************************************************************************************
163 
164 
165 //*************************************************************************************************
172 template< typename MT // Type of the adapted matrix
173  , bool SO // Storage order of the adapted matrix
174  , bool DF > // Numeric flag
175 inline const UniLowerMatrix<MT,SO,DF>
176  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n ) const
177 {
179 
180  UniLowerMatrix<MT,SO,DF> matrix( n );
181  randomize( matrix );
182  return matrix;
183 }
185 //*************************************************************************************************
186 
187 
188 //*************************************************************************************************
197 template< typename MT // Type of the adapted matrix
198  , bool SO // Storage order of the adapted matrix
199  , bool DF > // Numeric flag
200 inline const UniLowerMatrix<MT,SO,DF>
201  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
202 {
205 
206  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
207  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
208  }
209 
210  UniLowerMatrix<MT,SO,DF> matrix( n );
211  randomize( matrix, nonzeros );
212 
213  return matrix;
214 }
216 //*************************************************************************************************
217 
218 
219 //*************************************************************************************************
227 template< typename MT // Type of the adapted matrix
228  , bool SO // Storage order of the adapted matrix
229  , bool DF > // Numeric flag
230 template< typename Arg > // Min/max argument type
231 inline const UniLowerMatrix<MT,SO,DF>
232  Rand< UniLowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
233 {
235 
236  UniLowerMatrix<MT,SO,DF> matrix;
237  randomize( matrix, min, max );
238  return matrix;
239 }
241 //*************************************************************************************************
242 
243 
244 //*************************************************************************************************
253 template< typename MT // Type of the adapted matrix
254  , bool SO // Storage order of the adapted matrix
255  , bool DF > // Numeric flag
256 template< typename Arg > // Min/max argument type
257 inline const UniLowerMatrix<MT,SO,DF>
258  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
259 {
261 
262  UniLowerMatrix<MT,SO,DF> matrix( n );
263  randomize( matrix, min, max );
264  return matrix;
265 }
267 //*************************************************************************************************
268 
269 
270 //*************************************************************************************************
281 template< typename MT // Type of the adapted matrix
282  , bool SO // Storage order of the adapted matrix
283  , bool DF > // Numeric flag
284 template< typename Arg > // Min/max argument type
285 inline const UniLowerMatrix<MT,SO,DF>
286  Rand< UniLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
287  const Arg& min, const Arg& max ) const
288 {
291 
292  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
293  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
294  }
295 
296  UniLowerMatrix<MT,SO,DF> matrix( n );
297  randomize( matrix, nonzeros, min, max );
298 
299  return matrix;
300 }
302 //*************************************************************************************************
303 
304 
305 //*************************************************************************************************
312 template< typename MT // Type of the adapted matrix
313  , bool SO // Storage order of the adapted matrix
314  , bool DF > // Numeric flag
315 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix ) const
316 {
317  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
318 }
320 //*************************************************************************************************
321 
322 
323 //*************************************************************************************************
330 template< typename MT // Type of the adapted matrix
331  , bool SO // Storage order of the adapted matrix
332  , bool DF > // Numeric flag
333 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const
334 {
336 
337  using ET = ElementType_<MT>;
338 
339  const size_t n( matrix.rows() );
340 
341  for( size_t i=1UL; i<n; ++i ) {
342  for( size_t j=0UL; j<i; ++j ) {
343  matrix(i,j) = rand<ET>();
344  }
345  }
346 }
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
358 template< typename MT // Type of the adapted matrix
359  , bool SO // Storage order of the adapted matrix
360  , bool DF > // Numeric flag
361 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const
362 {
364 
365  const size_t n( matrix.rows() );
366 
367  if( n == 0UL || n == 1UL ) return;
368 
369  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
370 
371  randomize( matrix, nonzeros );
372 }
374 //*************************************************************************************************
375 
376 
377 //*************************************************************************************************
386 template< typename MT // Type of the adapted matrix
387  , bool SO // Storage order of the adapted matrix
388  , bool DF > // Numeric flag
389 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
390 {
392 
393  using ET = ElementType_<MT>;
394 
395  const size_t n( matrix.rows() );
396 
397  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
398  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
399  }
400 
401  if( n == 0UL || n == 1UL ) return;
402 
403  matrix.reset();
404  matrix.reserve( nonzeros );
405  matrix.finalize( 0UL );
406 
407  std::vector<size_t> dist( n );
408 
409  for( size_t nz=0UL; nz<nonzeros; ) {
410  const size_t index = rand<size_t>( 1UL, n-1UL );
411  if( dist[index] == index ) continue;
412  ++dist[index];
413  ++nz;
414  }
415 
416  for( size_t i=1UL; i<n; ++i ) {
417  const Indices indices( 0UL, i-1UL, dist[i] );
418  for( size_t j : indices ) {
419  matrix.append( i, j, rand<ET>() );
420  }
421  matrix.finalize( i );
422  }
423 }
425 //*************************************************************************************************
426 
427 
428 //*************************************************************************************************
437 template< typename MT // Type of the adapted matrix
438  , bool SO // Storage order of the adapted matrix
439  , bool DF > // Numeric flag
440 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
441 {
443 
444  using ET = ElementType_<MT>;
445 
446  const size_t n( matrix.rows() );
447 
448  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
449  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
450  }
451 
452  if( n == 0UL || n == 1UL ) return;
453 
454  matrix.reset();
455  matrix.reserve( nonzeros );
456 
457  std::vector<size_t> dist( n-1UL );
458 
459  for( size_t nz=0UL; nz<nonzeros; ) {
460  const size_t index = rand<size_t>( 0UL, n-2UL );
461  if( dist[index] == n - index - 1UL ) continue;
462  ++dist[index];
463  ++nz;
464  }
465 
466  for( size_t j=0UL; j<n-1UL; ++j ) {
467  const Indices indices( j+1UL, n-1UL, dist[j] );
468  for( size_t i : indices ) {
469  matrix.append( i, j, rand<ET>() );
470  }
471  matrix.finalize( j );
472  }
473 
474  matrix.finalize( n-1UL );
475 }
477 //*************************************************************************************************
478 
479 
480 //*************************************************************************************************
489 template< typename MT // Type of the adapted matrix
490  , bool SO // Storage order of the adapted matrix
491  , bool DF > // Numeric flag
492 template< typename Arg > // Min/max argument type
493 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
494  const Arg& min, const Arg& max ) const
495 {
496  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
497 }
499 //*************************************************************************************************
500 
501 
502 //*************************************************************************************************
511 template< typename MT // Type of the adapted matrix
512  , bool SO // Storage order of the adapted matrix
513  , bool DF > // Numeric flag
514 template< typename Arg > // Min/max argument type
515 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
516  const Arg& min, const Arg& max, TrueType ) const
517 {
519 
520  using ET = ElementType_<MT>;
521 
522  const size_t n( matrix.rows() );
523 
524  for( size_t i=1UL; i<n; ++i ) {
525  for( size_t j=0UL; j<i; ++j ) {
526  matrix(i,j) = rand<ET>( min, max );
527  }
528  }
529 }
531 //*************************************************************************************************
532 
533 
534 //*************************************************************************************************
543 template< typename MT // Type of the adapted matrix
544  , bool SO // Storage order of the adapted matrix
545  , bool DF > // Numeric flag
546 template< typename Arg > // Min/max argument type
547 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,SO,DF>& matrix,
548  const Arg& min, const Arg& max, FalseType ) const
549 {
551 
552  const size_t n( matrix.rows() );
553 
554  if( n == 0UL || n == 1UL ) return;
555 
556  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
557 
558  randomize( matrix, nonzeros, min, max );
559 }
561 //*************************************************************************************************
562 
563 
564 //*************************************************************************************************
575 template< typename MT // Type of the adapted matrix
576  , bool SO // Storage order of the adapted matrix
577  , bool DF > // Numeric flag
578 template< typename Arg > // Min/max argument type
579 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,false,DF>& matrix,
580  size_t nonzeros, const Arg& min, const Arg& max ) const
581 {
583 
584  using ET = ElementType_<MT>;
585 
586  const size_t n( matrix.rows() );
587 
588  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
589  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
590  }
591 
592  if( n == 0UL || n == 1UL ) return;
593 
594  matrix.reset();
595  matrix.reserve( nonzeros );
596  matrix.finalize( 0UL );
597 
598  std::vector<size_t> dist( n );
599 
600  for( size_t nz=0UL; nz<nonzeros; ) {
601  const size_t index = rand<size_t>( 1UL, n-1UL );
602  if( dist[index] == index ) continue;
603  ++dist[index];
604  ++nz;
605  }
606 
607  for( size_t i=1UL; i<n; ++i ) {
608  const Indices indices( 0UL, i-1UL, dist[i] );
609  for( size_t j : indices ) {
610  matrix.append( i, j, rand<ET>( min, max ) );
611  }
612  matrix.finalize( i );
613  }
614 }
616 //*************************************************************************************************
617 
618 
619 //*************************************************************************************************
630 template< typename MT // Type of the adapted matrix
631  , bool SO // Storage order of the adapted matrix
632  , bool DF > // Numeric flag
633 template< typename Arg > // Min/max argument type
634 inline void Rand< UniLowerMatrix<MT,SO,DF> >::randomize( UniLowerMatrix<MT,true,DF>& matrix,
635  size_t nonzeros, const Arg& min, const Arg& max ) const
636 {
638 
639  using ET = ElementType_<MT>;
640 
641  const size_t n( matrix.rows() );
642 
643  if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
644  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
645  }
646 
647  if( n == 0UL || n == 1UL ) return;
648 
649  matrix.reset();
650  matrix.reserve( nonzeros );
651 
652  std::vector<size_t> dist( n-1UL );
653 
654  for( size_t nz=0UL; nz<nonzeros; ) {
655  const size_t index = rand<size_t>( 0UL, n-2UL );
656  if( dist[index] == n - index - 1UL ) continue;
657  ++dist[index];
658  ++nz;
659  }
660 
661  for( size_t j=0UL; j<n-1UL; ++j ) {
662  const Indices indices( j+1UL, n-1UL, dist[j] );
663  for( size_t i : indices ) {
664  matrix.append( i, j, rand<ET>( min, max ) );
665  }
666  matrix.finalize( j );
667  }
668 
669  matrix.finalize( n-1UL );
670 }
672 //*************************************************************************************************
673 
674 
675 
676 
677 //=================================================================================================
678 //
679 // MAKE FUNCTIONS
680 //
681 //=================================================================================================
682 
683 //*************************************************************************************************
690 template< typename MT // Type of the adapted matrix
691  , bool SO // Storage order of the adapted matrix
692  , bool DF > // Density flag
693 void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix )
694 {
695  reset( matrix );
696 
697  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
698 }
700 //*************************************************************************************************
701 
702 
703 //*************************************************************************************************
712 template< typename MT // Type of the adapted matrix
713  , bool SO // Storage order of the adapted matrix
714  , bool DF // Density flag
715  , typename Arg > // Min/max argument type
716 void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
717 {
718  UNUSED_PARAMETER( min, max );
719 
720  makeSymmetric( matrix );
721 }
723 //*************************************************************************************************
724 
725 
726 //*************************************************************************************************
733 template< typename MT // Type of the adapted matrix
734  , bool SO // Storage order of the adapted matrix
735  , bool DF > // Density flag
736 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix )
737 {
738  reset( matrix );
739 
740  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
741 }
743 //*************************************************************************************************
744 
745 
746 //*************************************************************************************************
755 template< typename MT // Type of the adapted matrix
756  , bool SO // Storage order of the adapted matrix
757  , bool DF // Density flag
758  , typename Arg > // Min/max argument type
759 void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
760 {
761  UNUSED_PARAMETER( min, max );
762 
763  makeHermitian( matrix );
764 }
766 //*************************************************************************************************
767 
768 
769 //*************************************************************************************************
776 template< typename MT // Type of the adapted matrix
777  , bool SO // Storage order of the adapted matrix
778  , bool DF > // Density flag
779 void makePositiveDefinite( UniLowerMatrix<MT,SO,DF>& matrix )
780 {
781  makeHermitian( matrix );
782 }
784 //*************************************************************************************************
785 
786 } // namespace blaze
787 
788 #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
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:1234
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:560
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
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:1809
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:290
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for all basic SparseMatrix functionality.
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 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 &#39;resize&#39; member fu...
Definition: Resizable.h:81
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:778
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:700
T generate() const
Generation of a random value in the range .
Definition: Random.h:250
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 &#39;res...
Definition: Resizable.h:61
Header file for the implementation of a strictly lower triangular matrix adaptor. ...
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.