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/math/ZeroMatrix.h>
58 #include <blaze/util/FalseType.h>
59 #include <blaze/util/Indices.h>
60 #include <blaze/util/Random.h>
61 #include <blaze/util/TrueType.h>
62 #include <blaze/util/Types.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< StrictlyUpperMatrix<MT,SO,DF> >
84 {
85  public:
86  //**Generate functions**************************************************************************
89  inline const StrictlyUpperMatrix<MT,SO,DF> generate() const;
90  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n ) const;
91  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
92 
93  template< typename Arg >
94  inline const StrictlyUpperMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline const StrictlyUpperMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
98 
99  template< typename Arg >
100  inline const StrictlyUpperMatrix<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( StrictlyUpperMatrix<MT,SO,DF>& matrix ) const;
109  inline void randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
110  inline void randomize( StrictlyUpperMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
111 
112  template< typename Arg >
113  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
114 
115  template< typename Arg >
116  inline void randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros,
117  const Arg& min, const Arg& max ) const;
118 
119  template< typename Arg >
120  inline void randomize( StrictlyUpperMatrix<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( StrictlyUpperMatrix<MT,SO,DF>& matrix, TrueType ) const;
130  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, FalseType ) const;
131 
132  template< typename Arg >
133  inline void randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
134 
135  template< typename Arg >
136  inline void randomize( StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF> Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate() const
154 {
156 
157  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
176  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n ) const
177 {
179 
180  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
201  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
202 {
205 
206  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
207  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
208  }
209 
210  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
232  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
233 {
235 
236  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
258  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
259 {
261 
262  StrictlyUpperMatrix<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 StrictlyUpperMatrix<MT,SO,DF>
286  Rand< StrictlyUpperMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
287  const Arg& min, const Arg& max ) const
288 {
291 
292  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
293  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
294  }
295 
296  StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix, TrueType ) const
334 {
336 
337  using ET = ElementType_t<MT>;
338 
339  const size_t n( matrix.rows() );
340 
341  for( size_t i=0UL; i<n; ++i ) {
342  for( size_t j=i+1UL; j<n; ++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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
390 {
392 
393  using ET = ElementType_t<MT>;
394 
395  const size_t n( matrix.rows() );
396 
397  if( nonzeros > StrictlyUpperMatrix<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 
406  std::vector<size_t> dist( n-1UL );
407 
408  for( size_t nz=0UL; nz<nonzeros; ) {
409  const size_t index = rand<size_t>( 0UL, n-2UL );
410  if( dist[index] == n - index - 1UL ) continue;
411  ++dist[index];
412  ++nz;
413  }
414 
415  for( size_t i=0UL; i<n-1UL; ++i ) {
416  const Indices indices( i+1UL, n-1UL, dist[i] );
417  for( size_t j : indices ) {
418  matrix.append( i, j, rand<ET>() );
419  }
420  matrix.finalize( i );
421  }
422 
423  matrix.finalize( n-1UL );
424 }
426 //*************************************************************************************************
427 
428 
429 //*************************************************************************************************
438 template< typename MT // Type of the adapted matrix
439  , bool SO // Storage order of the adapted matrix
440  , bool DF > // Numeric flag
441 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
442 {
444 
445  using ET = ElementType_t<MT>;
446 
447  const size_t n( matrix.rows() );
448 
449  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
450  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
451  }
452 
453  if( n == 0UL || n == 1UL ) return;
454 
455  matrix.reset();
456  matrix.reserve( nonzeros );
457  matrix.finalize( 0UL );
458 
459  std::vector<size_t> dist( n );
460 
461  for( size_t nz=0UL; nz<nonzeros; ) {
462  const size_t index = rand<size_t>( 1UL, n-1UL );
463  if( dist[index] == index ) continue;
464  ++dist[index];
465  ++nz;
466  }
467 
468  for( size_t j=1UL; j<n; ++j ) {
469  const Indices indices( 0UL, j-1UL, dist[j] );
470  for( size_t i : indices ) {
471  matrix.append( i, j, rand<ET>() );
472  }
473  matrix.finalize( j );
474  }
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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,SO,DF>& matrix,
516  const Arg& min, const Arg& max, TrueType ) const
517 {
519 
520  using ET = ElementType_t<MT>;
521 
522  const size_t n( matrix.rows() );
523 
524  for( size_t i=0UL; i<n; ++i ) {
525  for( size_t j=i+1UL; j<n; ++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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<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< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,false,DF>& matrix,
580  size_t nonzeros, const Arg& min, const Arg& max ) const
581 {
583 
584  using ET = ElementType_t<MT>;
585 
586  const size_t n( matrix.rows() );
587 
588  if( nonzeros > StrictlyUpperMatrix<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 
597  std::vector<size_t> dist( n-1UL );
598 
599  for( size_t nz=0UL; nz<nonzeros; ) {
600  const size_t index = rand<size_t>( 0UL, n-2UL );
601  if( dist[index] == n - index - 1UL ) continue;
602  ++dist[index];
603  ++nz;
604  }
605 
606  for( size_t i=0UL; i<n-1UL; ++i ) {
607  const Indices indices( i+1UL, n-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 
614  matrix.finalize( n-1UL );
615 }
617 //*************************************************************************************************
618 
619 
620 //*************************************************************************************************
631 template< typename MT // Type of the adapted matrix
632  , bool SO // Storage order of the adapted matrix
633  , bool DF > // Numeric flag
634 template< typename Arg > // Min/max argument type
635 inline void Rand< StrictlyUpperMatrix<MT,SO,DF> >::randomize( StrictlyUpperMatrix<MT,true,DF>& matrix,
636  size_t nonzeros, const Arg& min, const Arg& max ) const
637 {
639 
640  using ET = ElementType_t<MT>;
641 
642  const size_t n( matrix.rows() );
643 
644  if( nonzeros > StrictlyUpperMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
645  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
646  }
647 
648  if( n == 0UL || n == 1UL ) return;
649 
650  matrix.reset();
651  matrix.reserve( nonzeros );
652  matrix.finalize( 0UL );
653 
654  std::vector<size_t> dist( n );
655 
656  for( size_t nz=0UL; nz<nonzeros; ) {
657  const size_t index = rand<size_t>( 1UL, n-1UL );
658  if( dist[index] == index ) continue;
659  ++dist[index];
660  ++nz;
661  }
662 
663  for( size_t j=1UL; j<n; ++j ) {
664  const Indices indices( 0UL, j-1UL, dist[j] );
665  for( size_t i : indices ) {
666  matrix.append( i, j, rand<ET>( min, max ) );
667  }
668  matrix.finalize( j );
669  }
670 }
672 //*************************************************************************************************
673 
674 } // namespace blaze
675 
676 #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:1239
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: TrueType.h:61
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:1147
Header file for the implementation of a strictly upper triangular matrix adaptor. ...
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:1179
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:252
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 complete ZeroMatrix implementation.
Header file for the TrueType type/value trait base class.