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>
48 #include <blaze/math/DenseMatrix.h>
52 #include <blaze/util/Exception.h>
53 #include <blaze/util/FalseType.h>
54 #include <blaze/util/Random.h>
55 #include <blaze/util/TrueType.h>
56 #include <blaze/util/Types.h>
57 
58 
59 namespace blaze {
60 
61 //=================================================================================================
62 //
63 // RAND SPECIALIZATION
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
74 template< typename MT // Type of the adapted matrix
75  , bool SO // Storage order of the adapted matrix
76  , bool DF > // Numeric flag
77 class Rand< StrictlyLowerMatrix<MT,SO,DF> >
78 {
79  public:
80  //**Generate functions**************************************************************************
83  inline const StrictlyLowerMatrix<MT,SO,DF> generate() const;
84  inline const StrictlyLowerMatrix<MT,SO,DF> generate( size_t n ) const;
85  inline const StrictlyLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
86 
87  template< typename Arg >
88  inline const StrictlyLowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
89 
90  template< typename Arg >
91  inline const StrictlyLowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const StrictlyLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
95  const Arg& min, const Arg& max ) const;
97  //**********************************************************************************************
98 
99  //**Randomize functions*************************************************************************
102  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix ) const;
103  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
104 
105  template< typename Arg >
106  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
107 
108  template< typename Arg >
109  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, size_t nonzeros,
110  const Arg& min, const Arg& max ) const;
112  //**********************************************************************************************
113 
114  private:
115  //**Randomize functions*************************************************************************
118  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
119  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
120 
121  template< typename Arg >
122  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
123 
124  template< typename Arg >
125  inline void randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
127  //**********************************************************************************************
128 };
130 //*************************************************************************************************
131 
132 
133 //*************************************************************************************************
139 template< typename MT // Type of the adapted matrix
140  , bool SO // Storage order of the adapted matrix
141  , bool DF > // Numeric flag
142 inline const StrictlyLowerMatrix<MT,SO,DF> Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate() const
143 {
145 
146  StrictlyLowerMatrix<MT,SO,DF> matrix;
147  randomize( matrix );
148  return matrix;
149 }
151 //*************************************************************************************************
152 
153 
154 //*************************************************************************************************
161 template< typename MT // Type of the adapted matrix
162  , bool SO // Storage order of the adapted matrix
163  , bool DF > // Numeric flag
164 inline const StrictlyLowerMatrix<MT,SO,DF>
165  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n ) const
166 {
168 
169  StrictlyLowerMatrix<MT,SO,DF> matrix( n );
170  randomize( matrix );
171  return matrix;
172 }
174 //*************************************************************************************************
175 
176 
177 //*************************************************************************************************
186 template< typename MT // Type of the adapted matrix
187  , bool SO // Storage order of the adapted matrix
188  , bool DF > // Numeric flag
189 inline const StrictlyLowerMatrix<MT,SO,DF>
190  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
191 {
194 
195  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
196  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
197  }
198 
199  StrictlyLowerMatrix<MT,SO,DF> matrix( n );
200  randomize( matrix, nonzeros );
201 
202  return matrix;
203 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
216 template< typename MT // Type of the adapted matrix
217  , bool SO // Storage order of the adapted matrix
218  , bool DF > // Numeric flag
219 template< typename Arg > // Min/max argument type
220 inline const StrictlyLowerMatrix<MT,SO,DF>
221  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
222 {
224 
225  StrictlyLowerMatrix<MT,SO,DF> matrix;
226  randomize( matrix, min, max );
227  return matrix;
228 }
230 //*************************************************************************************************
231 
232 
233 //*************************************************************************************************
242 template< typename MT // Type of the adapted matrix
243  , bool SO // Storage order of the adapted matrix
244  , bool DF > // Numeric flag
245 template< typename Arg > // Min/max argument type
246 inline const StrictlyLowerMatrix<MT,SO,DF>
247  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
248 {
250 
251  StrictlyLowerMatrix<MT,SO,DF> matrix( n );
252  randomize( matrix, min, max );
253  return matrix;
254 }
256 //*************************************************************************************************
257 
258 
259 //*************************************************************************************************
270 template< typename MT // Type of the adapted matrix
271  , bool SO // Storage order of the adapted matrix
272  , bool DF > // Numeric flag
273 template< typename Arg > // Min/max argument type
274 inline const StrictlyLowerMatrix<MT,SO,DF>
275  Rand< StrictlyLowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
276  const Arg& min, const Arg& max ) const
277 {
280 
281  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
282  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
283  }
284 
285  StrictlyLowerMatrix<MT,SO,DF> matrix( n );
286  randomize( matrix, nonzeros, min, max );
287 
288  return matrix;
289 }
291 //*************************************************************************************************
292 
293 
294 //*************************************************************************************************
301 template< typename MT // Type of the adapted matrix
302  , bool SO // Storage order of the adapted matrix
303  , bool DF > // Numeric flag
304 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix ) const
305 {
306  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
307 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
319 template< typename MT // Type of the adapted matrix
320  , bool SO // Storage order of the adapted matrix
321  , bool DF > // Numeric flag
322 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, TrueType ) const
323 {
325 
326  typedef typename MT::ElementType ET;
327 
328  const size_t n( matrix.rows() );
329 
330  for( size_t i=1UL; i<n; ++i ) {
331  for( size_t j=0UL; j<i; ++j ) {
332  matrix(i,j) = rand<ET>();
333  }
334  }
335 }
337 //*************************************************************************************************
338 
339 
340 //*************************************************************************************************
347 template< typename MT // Type of the adapted matrix
348  , bool SO // Storage order of the adapted matrix
349  , bool DF > // Numeric flag
350 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, FalseType ) const
351 {
353 
354  typedef typename MT::ElementType ET;
355 
356  const size_t n( matrix.rows() );
357 
358  if( n == 0UL ) return;
359 
360  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
361 
362  matrix.reset();
363  matrix.reserve( nonzeros );
364 
365  while( matrix.nonZeros() < nonzeros ) {
366  const size_t row( rand<size_t>( 1UL, n-1UL ) );
367  const size_t col( rand<size_t>( 0UL, row-1UL ) );
368  matrix(row,col) = rand<ET>();
369  }
370 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
384 template< typename MT // Type of the adapted matrix
385  , bool SO // Storage order of the adapted matrix
386  , bool DF > // Numeric flag
387 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
388 {
390 
391  typedef typename MT::ElementType ET;
392 
393  const size_t n( matrix.rows() );
394 
395  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
396  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
397  }
398 
399  if( n == 0UL ) return;
400 
401  matrix.reset();
402  matrix.reserve( nonzeros );
403 
404  while( matrix.nonZeros() < nonzeros ) {
405  const size_t row( rand<size_t>( 1UL, n-1UL ) );
406  const size_t col( rand<size_t>( 0UL, row-1UL ) );
407  matrix(row,col) = rand<ET>();
408  }
409 }
411 //*************************************************************************************************
412 
413 
414 //*************************************************************************************************
423 template< typename MT // Type of the adapted matrix
424  , bool SO // Storage order of the adapted matrix
425  , bool DF > // Numeric flag
426 template< typename Arg > // Min/max argument type
427 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix,
428  const Arg& min, const Arg& max ) const
429 {
430  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
431 }
433 //*************************************************************************************************
434 
435 
436 //*************************************************************************************************
445 template< typename MT // Type of the adapted matrix
446  , bool SO // Storage order of the adapted matrix
447  , bool DF > // Numeric flag
448 template< typename Arg > // Min/max argument type
449 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix,
450  const Arg& min, const Arg& max, TrueType ) const
451 {
453 
454  typedef typename MT::ElementType ET;
455 
456  const size_t n( matrix.rows() );
457 
458  for( size_t i=1UL; i<n; ++i ) {
459  for( size_t j=0UL; j<i; ++j ) {
460  matrix(i,j) = rand<ET>( min, max );
461  }
462  }
463 }
465 //*************************************************************************************************
466 
467 
468 //*************************************************************************************************
477 template< typename MT // Type of the adapted matrix
478  , bool SO // Storage order of the adapted matrix
479  , bool DF > // Numeric flag
480 template< typename Arg > // Min/max argument type
481 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix,
482  const Arg& min, const Arg& max, FalseType ) const
483 {
485 
486  typedef typename MT::ElementType ET;
487 
488  const size_t n( matrix.rows() );
489 
490  if( n == 0UL ) return;
491 
492  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
493 
494  matrix.reset();
495  matrix.reserve( nonzeros );
496 
497  while( matrix.nonZeros() < nonzeros ) {
498  const size_t row( rand<size_t>( 1UL, n-1UL ) );
499  const size_t col( rand<size_t>( 0UL, row-1UL ) );
500  matrix(row,col) = rand<ET>( min, max );
501  }
502 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
518 template< typename MT // Type of the adapted matrix
519  , bool SO // Storage order of the adapted matrix
520  , bool DF > // Numeric flag
521 template< typename Arg > // Min/max argument type
522 inline void Rand< StrictlyLowerMatrix<MT,SO,DF> >::randomize( StrictlyLowerMatrix<MT,SO,DF>& matrix,
523  size_t nonzeros, const Arg& min, const Arg& max ) const
524 {
526 
527  typedef typename MT::ElementType ET;
528 
529  const size_t n( matrix.rows() );
530 
531  if( nonzeros > StrictlyLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
532  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
533  }
534 
535  if( n == 0UL ) return;
536 
537  matrix.reset();
538  matrix.reserve( nonzeros );
539 
540  while( matrix.nonZeros() < nonzeros ) {
541  const size_t row( rand<size_t>( 1UL, n-1UL ) );
542  const size_t col( rand<size_t>( 0UL, row-1UL ) );
543  matrix(row,col) = rand<ET>( min, max );
544  }
545 }
547 //*************************************************************************************************
548 
549 } // namespace blaze
550 
551 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exceptionThis macro encapsulates the default way of...
Definition: Exception.h:187
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
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:79
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1041
Constraint on the data type.
Implementation of a random number generator.
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:260
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
Header file for the complete StrictlyUpperMatrix implementation.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
Header file for the IsDenseMatrix type trait.
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename RowExprTrait< MT >::Type >::Type row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:107
#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:118
T generate() const
Generation of a random value in the range .
Definition: Random.h:220
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:79
boost::false_type FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: FalseType.h:61
Header file for the implementation of a strictly lower triangular matrix adaptor. ...
Header file for exception macros.
boost::true_type TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.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:79
Header file for the TrueType type/value trait base class.