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>
50 #include <blaze/math/DenseMatrix.h>
51 #include <blaze/math/Exception.h>
55 #include <blaze/util/FalseType.h>
56 #include <blaze/util/Indices.h>
57 #include <blaze/util/Random.h>
58 #include <blaze/util/TrueType.h>
59 #include <blaze/util/Types.h>
60 
61 
62 namespace blaze {
63 
64 //=================================================================================================
65 //
66 // RAND SPECIALIZATION
67 //
68 //=================================================================================================
69 
70 //*************************************************************************************************
77 template< typename MT // Type of the adapted matrix
78  , bool SO // Storage order of the adapted matrix
79  , bool DF > // Numeric flag
80 class Rand< StrictlyUpperMatrix<MT,SO,DF> >
81 {
82  public:
83  //**Generate functions**************************************************************************
86  inline const StrictlyUpperMatrix<MT,SO,DF> generate() const;
87  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n ) const;
88  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
89 
90  template< typename Arg >
91  inline const StrictlyUpperMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
98  const Arg& min, const Arg& max ) const;
100  //**********************************************************************************************
101 
102  //**Randomize functions*************************************************************************
105  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix ) const;
106  inline void randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
107  inline void randomize( StrictlyUpperMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
108 
109  template< typename Arg >
110  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
111 
112  template< typename Arg >
113  inline void randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros,
114  const Arg& min, const Arg& max ) const;
115 
116  template< typename Arg >
117  inline void randomize( StrictlyUpperMatrix<MT,true,DF>& matrix, size_t nonzeros,
118  const Arg& min, const Arg& max ) const;
120  //**********************************************************************************************
121 
122  private:
123  //**Randomize functions*************************************************************************
126  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, TrueType ) const;
127  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, FalseType ) const;
128 
129  template< typename Arg >
130  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
131 
132  template< typename Arg >
133  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
135  //**********************************************************************************************
136 };
138 //*************************************************************************************************
139 
140 
141 //*************************************************************************************************
147 template< typename MT // Type of the adapted matrix
148  , bool SO // Storage order of the adapted matrix
149  , bool DF > // Numeric flag
150 inline const StrictlyUpperMatrix<MT,SO,DF> Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate() const
151 {
153 
154  StrictlyUpperMatrix<MT,SO,DF> matrix;
155  randomize( matrix );
156  return matrix;
157 }
159 //*************************************************************************************************
160 
161 
162 //*************************************************************************************************
169 template< typename MT // Type of the adapted matrix
170  , bool SO // Storage order of the adapted matrix
171  , bool DF > // Numeric flag
172 inline const StrictlyUpperMatrix<MT,SO,DF>
173  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n ) const
174 {
176 
177  StrictlyUpperMatrix<MT,SO,DF> matrix( n );
178  randomize( matrix );
179  return matrix;
180 }
182 //*************************************************************************************************
183 
184 
185 //*************************************************************************************************
194 template< typename MT // Type of the adapted matrix
195  , bool SO // Storage order of the adapted matrix
196  , bool DF > // Numeric flag
197 inline const StrictlyUpperMatrix<MT,SO,DF>
198  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
199 {
202 
203  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
204  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
205  }
206 
207  StrictlyUpperMatrix<MT,SO,DF> matrix( n );
208  randomize( matrix, nonzeros );
209 
210  return matrix;
211 }
213 //*************************************************************************************************
214 
215 
216 //*************************************************************************************************
224 template< typename MT // Type of the adapted matrix
225  , bool SO // Storage order of the adapted matrix
226  , bool DF > // Numeric flag
227 template< typename Arg > // Min/max argument type
228 inline const StrictlyUpperMatrix<MT,SO,DF>
229  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
230 {
232 
233  StrictlyUpperMatrix<MT,SO,DF> matrix;
234  randomize( matrix, min, max );
235  return matrix;
236 }
238 //*************************************************************************************************
239 
240 
241 //*************************************************************************************************
250 template< typename MT // Type of the adapted matrix
251  , bool SO // Storage order of the adapted matrix
252  , bool DF > // Numeric flag
253 template< typename Arg > // Min/max argument type
254 inline const StrictlyUpperMatrix<MT,SO,DF>
255  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
256 {
258 
259  StrictlyUpperMatrix<MT,SO,DF> matrix( n );
260  randomize( matrix, min, max );
261  return matrix;
262 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
278 template< typename MT // Type of the adapted matrix
279  , bool SO // Storage order of the adapted matrix
280  , bool DF > // Numeric flag
281 template< typename Arg > // Min/max argument type
282 inline const StrictlyUpperMatrix<MT,SO,DF>
283  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
284  const Arg& min, const Arg& max ) const
285 {
288 
289  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
290  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
291  }
292 
293  StrictlyUpperMatrix<MT,SO,DF> matrix( n );
294  randomize( matrix, nonzeros, min, max );
295 
296  return matrix;
297 }
299 //*************************************************************************************************
300 
301 
302 //*************************************************************************************************
309 template< typename MT // Type of the adapted matrix
310  , bool SO // Storage order of the adapted matrix
311  , bool DF > // Numeric flag
312 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix ) const
313 {
314  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
315 }
317 //*************************************************************************************************
318 
319 
320 //*************************************************************************************************
327 template< typename MT // Type of the adapted matrix
328  , bool SO // Storage order of the adapted matrix
329  , bool DF > // Numeric flag
330 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, TrueType ) const
331 {
333 
334  typedef ElementType_<MT> ET;
335 
336  const size_t n( matrix.rows() );
337 
338  for( size_t i=0UL; i<n; ++i ) {
339  for( size_t j=i+1UL; j<n; ++j ) {
340  matrix(i,j) = rand<ET>();
341  }
342  }
343 }
345 //*************************************************************************************************
346 
347 
348 //*************************************************************************************************
355 template< typename MT // Type of the adapted matrix
356  , bool SO // Storage order of the adapted matrix
357  , bool DF > // Numeric flag
358 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, FalseType ) const
359 {
361 
362  const size_t n( matrix.rows() );
363 
364  if( n == 0UL || n == 1UL ) return;
365 
366  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
367 
368  randomize( matrix, nonzeros );
369 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
383 template< typename MT // Type of the adapted matrix
384  , bool SO // Storage order of the adapted matrix
385  , bool DF > // Numeric flag
386 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
387 {
389 
390  typedef ElementType_<MT> ET;
391 
392  const size_t n( matrix.rows() );
393 
394  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
395  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
396  }
397 
398  if( n == 0UL || n == 1UL ) return;
399 
400  matrix.reset();
401  matrix.reserve( nonzeros );
402 
403  std::vector<size_t> dist( n-1UL );
404 
405  for( size_t nz=0UL; nz<nonzeros; ) {
406  const size_t index = rand<size_t>( 0UL, n-2UL );
407  if( dist[index] == n - index - 1UL ) continue;
408  ++dist[index];
409  ++nz;
410  }
411 
412  for( size_t i=0UL; i<n-1UL; ++i ) {
413  const Indices indices( i+1UL, n-1UL, dist[i] );
414  for( size_t j : indices ) {
415  matrix.append( i, j, rand<ET>() );
416  }
417  matrix.finalize( i );
418  }
419 
420  matrix.finalize( n-1UL );
421 }
423 //*************************************************************************************************
424 
425 
426 //*************************************************************************************************
435 template< typename MT // Type of the adapted matrix
436  , bool SO // Storage order of the adapted matrix
437  , bool DF > // Numeric flag
438 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
439 {
441 
442  typedef ElementType_<MT> ET;
443 
444  const size_t n( matrix.rows() );
445 
446  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
447  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
448  }
449 
450  if( n == 0UL || n == 1UL ) return;
451 
452  matrix.reset();
453  matrix.reserve( nonzeros );
454  matrix.finalize( 0UL );
455 
456  std::vector<size_t> dist( n );
457 
458  for( size_t nz=0UL; nz<nonzeros; ) {
459  const size_t index = rand<size_t>( 1UL, n-1UL );
460  if( dist[index] == index ) continue;
461  ++dist[index];
462  ++nz;
463  }
464 
465  for( size_t j=1UL; j<n; ++j ) {
466  const Indices indices( 0UL, j-1UL, dist[j] );
467  for( size_t i : indices ) {
468  matrix.append( i, j, rand<ET>() );
469  }
470  matrix.finalize( j );
471  }
472 }
474 //*************************************************************************************************
475 
476 
477 //*************************************************************************************************
486 template< typename MT // Type of the adapted matrix
487  , bool SO // Storage order of the adapted matrix
488  , bool DF > // Numeric flag
489 template< typename Arg > // Min/max argument type
490 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix,
491  const Arg& min, const Arg& max ) const
492 {
493  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
494 }
496 //*************************************************************************************************
497 
498 
499 //*************************************************************************************************
508 template< typename MT // Type of the adapted matrix
509  , bool SO // Storage order of the adapted matrix
510  , bool DF > // Numeric flag
511 template< typename Arg > // Min/max argument type
512 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix,
513  const Arg& min, const Arg& max, TrueType ) const
514 {
516 
517  typedef ElementType_<MT> ET;
518 
519  const size_t n( matrix.rows() );
520 
521  for( size_t i=0UL; i<n; ++i ) {
522  for( size_t j=i+1UL; j<n; ++j ) {
523  matrix(i,j) = rand<ET>( min, max );
524  }
525  }
526 }
528 //*************************************************************************************************
529 
530 
531 //*************************************************************************************************
540 template< typename MT // Type of the adapted matrix
541  , bool SO // Storage order of the adapted matrix
542  , bool DF > // Numeric flag
543 template< typename Arg > // Min/max argument type
544 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix,
545  const Arg& min, const Arg& max, FalseType ) const
546 {
548 
549  const size_t n( matrix.rows() );
550 
551  if( n == 0UL || n == 1UL ) return;
552 
553  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
554 
555  randomize( matrix, nonzeros, min, max );
556 }
558 //*************************************************************************************************
559 
560 
561 //*************************************************************************************************
572 template< typename MT // Type of the adapted matrix
573  , bool SO // Storage order of the adapted matrix
574  , bool DF > // Numeric flag
575 template< typename Arg > // Min/max argument type
576 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,false,DF>& matrix,
577  size_t nonzeros, const Arg& min, const Arg& max ) const
578 {
580 
581  typedef ElementType_<MT> ET;
582 
583  const size_t n( matrix.rows() );
584 
585  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
586  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
587  }
588 
589  if( n == 0UL || n == 1UL ) return;
590 
591  matrix.reset();
592  matrix.reserve( nonzeros );
593 
594  std::vector<size_t> dist( n-1UL );
595 
596  for( size_t nz=0UL; nz<nonzeros; ) {
597  const size_t index = rand<size_t>( 0UL, n-2UL );
598  if( dist[index] == n - index - 1UL ) continue;
599  ++dist[index];
600  ++nz;
601  }
602 
603  for( size_t i=0UL; i<n-1UL; ++i ) {
604  const Indices indices( i+1UL, n-1UL, dist[i] );
605  for( size_t j : indices ) {
606  matrix.append( i, j, rand<ET>( min, max ) );
607  }
608  matrix.finalize( i );
609  }
610 
611  matrix.finalize( n-1UL );
612 }
614 //*************************************************************************************************
615 
616 
617 //*************************************************************************************************
628 template< typename MT // Type of the adapted matrix
629  , bool SO // Storage order of the adapted matrix
630  , bool DF > // Numeric flag
631 template< typename Arg > // Min/max argument type
632 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,true,DF>& matrix,
633  size_t nonzeros, const Arg& min, const Arg& max ) const
634 {
636 
637  typedef ElementType_<MT> ET;
638 
639  const size_t n( matrix.rows() );
640 
641  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
642  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
643  }
644 
645  if( n == 0UL || n == 1UL ) return;
646 
647  matrix.reset();
648  matrix.reserve( nonzeros );
649  matrix.finalize( 0UL );
650 
651  std::vector<size_t> dist( n );
652 
653  for( size_t nz=0UL; nz<nonzeros; ) {
654  const size_t index = rand<size_t>( 1UL, n-1UL );
655  if( dist[index] == index ) continue;
656  ++dist[index];
657  ++nz;
658  }
659 
660  for( size_t j=1UL; j<n; ++j ) {
661  const Indices indices( 0UL, j-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 }
669 //*************************************************************************************************
670 
671 } // namespace blaze
672 
673 #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
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:926
Constraint on the data type.
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
Implementation of a random number generator.
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
Constraint on the data type.
Constraint on the data type.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for all basic SparseMatrix functionality.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:289
Header file for the implementation of a 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 Indices class.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_RESIZABLE(T)
Constraint on the data type.In case the given data type T is resizable, i.e. has a 'resize' member fu...
Definition: Resizable.h:81
T generate() const
Generation of a random value in the range .
Definition: Random.h:249
Header file for all basic DenseMatrix functionality.
#define BLAZE_CONSTRAINT_MUST_BE_RESIZABLE(T)
Constraint on the data type.In case the given data type T is not resizable, i.e. does not have a 'res...
Definition: Resizable.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type...
Definition: SparseMatrix.h:61
Header file for the TrueType type/value trait base class.
const DMatForEachExpr< MT, Ceil, SO > ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatForEachExpr.h:1130