StrictlyUpperMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_STRICTLYUPPERMATRIX_H_
36 #define _BLAZE_MATH_STRICTLYUPPERMATRIX_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 
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< StrictlyUpperMatrix<MT,SO,DF> >
83 {
84  public:
85  //**Generate functions**************************************************************************
88  inline const StrictlyUpperMatrix<MT,SO,DF> generate() const;
89  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n ) const;
90  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
91 
92  template< typename Arg >
93  inline const StrictlyUpperMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
94 
95  template< typename Arg >
96  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
97 
98  template< typename Arg >
99  inline const StrictlyUpperMatrix<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( StrictlyUpperMatrix<MT,SO,DF>& matrix ) const;
108  inline void randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
109  inline void randomize( StrictlyUpperMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
110 
111  template< typename Arg >
112  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
113 
114  template< typename Arg >
115  inline void randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros,
116  const Arg& min, const Arg& max ) const;
117 
118  template< typename Arg >
119  inline void randomize( StrictlyUpperMatrix<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( StrictlyUpperMatrix<MT,SO,DF>& matrix, TrueType ) const;
129  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, FalseType ) const;
130 
131  template< typename Arg >
132  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
133 
134  template< typename Arg >
135  inline void randomize( StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF> Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate() const
153 {
155 
156  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
175  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n ) const
176 {
178 
179  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
200  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
201 {
204 
205  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
206  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
207  }
208 
209  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
231  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
232 {
234 
235  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
257  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
258 {
260 
261  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
285  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
286  const Arg& min, const Arg& max ) const
287 {
290 
291  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
292  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
293  }
294 
295  StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, TrueType ) const
333 {
335 
336  using ET = ElementType_<MT>;
337 
338  const size_t n( matrix.rows() );
339 
340  for( size_t i=0UL; i<n; ++i ) {
341  for( size_t j=i+1UL; j<n; ++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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
389 {
391 
392  using ET = ElementType_<MT>;
393 
394  const size_t n( matrix.rows() );
395 
396  if( nonzeros > StrictlyUpperMatrix<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 
405  std::vector<size_t> dist( n-1UL );
406 
407  for( size_t nz=0UL; nz<nonzeros; ) {
408  const size_t index = rand<size_t>( 0UL, n-2UL );
409  if( dist[index] == n - index - 1UL ) continue;
410  ++dist[index];
411  ++nz;
412  }
413 
414  for( size_t i=0UL; i<n-1UL; ++i ) {
415  const Indices indices( i+1UL, n-1UL, dist[i] );
416  for( size_t j : indices ) {
417  matrix.append( i, j, rand<ET>() );
418  }
419  matrix.finalize( i );
420  }
421 
422  matrix.finalize( n-1UL );
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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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 > StrictlyUpperMatrix<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  matrix.finalize( 0UL );
457 
458  std::vector<size_t> dist( n );
459 
460  for( size_t nz=0UL; nz<nonzeros; ) {
461  const size_t index = rand<size_t>( 1UL, n-1UL );
462  if( dist[index] == index ) continue;
463  ++dist[index];
464  ++nz;
465  }
466 
467  for( size_t j=1UL; j<n; ++j ) {
468  const Indices indices( 0UL, j-1UL, dist[j] );
469  for( size_t i : indices ) {
470  matrix.append( i, j, rand<ET>() );
471  }
472  matrix.finalize( j );
473  }
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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix,
515  const Arg& min, const Arg& max, TrueType ) const
516 {
518 
519  using ET = ElementType_<MT>;
520 
521  const size_t n( matrix.rows() );
522 
523  for( size_t i=0UL; i<n; ++i ) {
524  for( size_t j=i+1UL; j<n; ++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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,false,DF>& matrix,
579  size_t nonzeros, const Arg& min, const Arg& max ) const
580 {
582 
583  using ET = ElementType_<MT>;
584 
585  const size_t n( matrix.rows() );
586 
587  if( nonzeros > StrictlyUpperMatrix<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 
596  std::vector<size_t> dist( n-1UL );
597 
598  for( size_t nz=0UL; nz<nonzeros; ) {
599  const size_t index = rand<size_t>( 0UL, n-2UL );
600  if( dist[index] == n - index - 1UL ) continue;
601  ++dist[index];
602  ++nz;
603  }
604 
605  for( size_t i=0UL; i<n-1UL; ++i ) {
606  const Indices indices( i+1UL, n-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 
613  matrix.finalize( n-1UL );
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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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 > StrictlyUpperMatrix<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  matrix.finalize( 0UL );
652 
653  std::vector<size_t> dist( n );
654 
655  for( size_t nz=0UL; nz<nonzeros; ) {
656  const size_t index = rand<size_t>( 1UL, n-1UL );
657  if( dist[index] == index ) continue;
658  ++dist[index];
659  ++nz;
660  }
661 
662  for( size_t j=1UL; j<n; ++j ) {
663  const Indices indices( 0UL, j-1UL, dist[j] );
664  for( size_t i : indices ) {
665  matrix.append( i, j, rand<ET>( min, max ) );
666  }
667  matrix.finalize( j );
668  }
669 }
671 //*************************************************************************************************
672 
673 } // namespace blaze
674 
675 #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 basic type definitions.
Header file for the FalseType type/value trait base class.
Header file for the complete StrictlyLowerMatrix implementation.
#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
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
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 strictly upper triangular 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 upper 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
T generate() const
Generation of a random value in the range .
Definition: Random.h:250
Header file for all basic DenseMatrix functionality.
Header file for the implementation of a upper unitriangular matrix adaptor.
#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
#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.