StrictlyLowerMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_STRICTLYLOWERMATRIX_H_
36 #define _BLAZE_MATH_STRICTLYLOWERMATRIX_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< StrictlyLowerMatrix<MT,SO,DF> >
81 {
82  public:
83  //**Generate functions**************************************************************************
86  inline const StrictlyLowerMatrix<MT,SO,DF> generate() const;
87  inline const StrictlyLowerMatrix<MT,SO,DF> generate( size_t n ) const;
88  inline const StrictlyLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
89 
90  template< typename Arg >
91  inline const StrictlyLowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const StrictlyLowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline const StrictlyLowerMatrix<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( StrictlyLowerMatrix<MT,SO,DF>& matrix ) const;
106  inline void randomize( StrictlyLowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const;
107  inline void randomize( StrictlyLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const;
108 
109  template< typename Arg >
110  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
111 
112  template< typename Arg >
113  inline void randomize( StrictlyLowerMatrix<MT,false,DF>& matrix, size_t nonzeros,
114  const Arg& min, const Arg& max ) const;
115 
116  template< typename Arg >
117  inline void randomize( StrictlyLowerMatrix<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( StrictlyLowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
127  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
128 
129  template< typename Arg >
130  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
131 
132  template< typename Arg >
133  inline void randomize( StrictlyLowerMatrix<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 StrictlyLowerMatrix<MT,SO,DF> Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate() const
151 {
153 
154  StrictlyLowerMatrix<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 StrictlyLowerMatrix<MT,SO,DF>
173  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n ) const
174 {
176 
177  StrictlyLowerMatrix<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 StrictlyLowerMatrix<MT,SO,DF>
198  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
199 {
202 
203  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
204  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
205  }
206 
207  StrictlyLowerMatrix<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 StrictlyLowerMatrix<MT,SO,DF>
229  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
230 {
232 
233  StrictlyLowerMatrix<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 StrictlyLowerMatrix<MT,SO,DF>
255  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
256 {
258 
259  StrictlyLowerMatrix<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 StrictlyLowerMatrix<MT,SO,DF>
283  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
284  const Arg& min, const Arg& max ) const
285 {
288 
289  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
290  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
291  }
292 
293  StrictlyLowerMatrix<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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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=1UL; i<n; ++i ) {
339  for( size_t j=0UL; j<i; ++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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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 > StrictlyLowerMatrix<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  matrix.finalize( 0UL );
403 
404  std::vector<size_t> dist( n );
405 
406  for( size_t nz=0UL; nz<nonzeros; ) {
407  const size_t index = rand<size_t>( 1UL, n-1UL );
408  if( dist[index] == index ) continue;
409  ++dist[index];
410  ++nz;
411  }
412 
413  for( size_t i=1UL; i<n; ++i ) {
414  const Indices indices( 0UL, i-1UL, dist[i] );
415  for( size_t j : indices ) {
416  matrix.append( i, j, rand<ET>() );
417  }
418  matrix.finalize( i );
419  }
420 }
422 //*************************************************************************************************
423 
424 
425 //*************************************************************************************************
434 template< typename MT // Type of the adapted matrix
435  , bool SO // Storage order of the adapted matrix
436  , bool DF > // Numeric flag
437 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
438 {
440 
441  typedef ElementType_<MT> ET;
442 
443  const size_t n( matrix.rows() );
444 
445  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
446  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
447  }
448 
449  if( n == 0UL || n == 1UL ) return;
450 
451  matrix.reset();
452  matrix.reserve( nonzeros );
453 
454  std::vector<size_t> dist( n-1UL );
455 
456  for( size_t nz=0UL; nz<nonzeros; ) {
457  const size_t index = rand<size_t>( 0UL, n-2UL );
458  if( dist[index] == n - index - 1UL ) continue;
459  ++dist[index];
460  ++nz;
461  }
462 
463  for( size_t j=0UL; j<n-1UL; ++j ) {
464  const Indices indices( j+1UL, n-1UL, dist[j] );
465  for( size_t i : indices ) {
466  matrix.append( i, j, rand<ET>() );
467  }
468  matrix.finalize( j );
469  }
470 
471  matrix.finalize( n-1UL );
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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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=1UL; i<n; ++i ) {
522  for( size_t j=0UL; j<i; ++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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<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 > StrictlyLowerMatrix<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  matrix.finalize( 0UL );
594 
595  std::vector<size_t> dist( n );
596 
597  for( size_t nz=0UL; nz<nonzeros; ) {
598  const size_t index = rand<size_t>( 1UL, n-1UL );
599  if( dist[index] == index ) continue;
600  ++dist[index];
601  ++nz;
602  }
603 
604  for( size_t i=1UL; i<n; ++i ) {
605  const Indices indices( 0UL, i-1UL, dist[i] );
606  for( size_t j : indices ) {
607  matrix.append( i, j, rand<ET>( min, max ) );
608  }
609  matrix.finalize( i );
610  }
611 }
613 //*************************************************************************************************
614 
615 
616 //*************************************************************************************************
627 template< typename MT // Type of the adapted matrix
628  , bool SO // Storage order of the adapted matrix
629  , bool DF > // Numeric flag
630 template< typename Arg > // Min/max argument type
631 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,true,DF>& matrix,
632  size_t nonzeros, const Arg& min, const Arg& max ) const
633 {
635 
636  typedef ElementType_<MT> ET;
637 
638  const size_t n( matrix.rows() );
639 
640  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
641  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
642  }
643 
644  if( n == 0UL || n == 1UL ) return;
645 
646  matrix.reset();
647  matrix.reserve( nonzeros );
648 
649  std::vector<size_t> dist( n-1UL );
650 
651  for( size_t nz=0UL; nz<nonzeros; ) {
652  const size_t index = rand<size_t>( 0UL, n-2UL );
653  if( dist[index] == n - index - 1UL ) continue;
654  ++dist[index];
655  ++nz;
656  }
657 
658  for( size_t j=0UL; j<n-1UL; ++j ) {
659  const Indices indices( j+1UL, n-1UL, dist[j] );
660  for( size_t i : indices ) {
661  matrix.append( i, j, rand<ET>( min, max ) );
662  }
663  matrix.finalize( j );
664  }
665 
666  matrix.finalize( n-1UL );
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.
#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 complete StrictlyUpperMatrix implementation.
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
Header file for the implementation of a strictly lower triangular matrix adaptor. ...
#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