LowerMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_LOWERMATRIX_H_
36 #define _BLAZE_MATH_LOWERMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <stdexcept>
49 #include <blaze/math/DenseMatrix.h>
52 #include <blaze/math/UpperMatrix.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< LowerMatrix<MT,SO,DF> >
78 {
79  public:
80  //**Generate functions**************************************************************************
83  inline const LowerMatrix<MT,SO,DF> generate() const;
84  inline const LowerMatrix<MT,SO,DF> generate( size_t n ) const;
85  inline const LowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
86 
87  template< typename Arg >
88  inline const LowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
89 
90  template< typename Arg >
91  inline const LowerMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const LowerMatrix<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( LowerMatrix<MT,SO,DF>& matrix ) const;
103  inline void randomize( LowerMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
104 
105  template< typename Arg >
106  inline void randomize( LowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
107 
108  template< typename Arg >
109  inline void randomize( LowerMatrix<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( LowerMatrix<MT,SO,DF>& matrix, TrueType ) const;
119  inline void randomize( LowerMatrix<MT,SO,DF>& matrix, FalseType ) const;
120 
121  template< typename Arg >
122  inline void randomize( LowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
123 
124  template< typename Arg >
125  inline void randomize( LowerMatrix<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 LowerMatrix<MT,SO,DF> Rand< LowerMatrix<MT,SO,DF> >::generate() const
143 {
145 
146  LowerMatrix<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 LowerMatrix<MT,SO,DF>
165  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n ) const
166 {
168 
169  LowerMatrix<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 LowerMatrix<MT,SO,DF>
190  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
191 {
194 
195  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) )
196  throw std::invalid_argument( "Invalid number of non-zero elements" );
197 
198  LowerMatrix<MT,SO,DF> matrix( n );
199  randomize( matrix, nonzeros );
200 
201  return matrix;
202 }
204 //*************************************************************************************************
205 
206 
207 //*************************************************************************************************
215 template< typename MT // Type of the adapted matrix
216  , bool SO // Storage order of the adapted matrix
217  , bool DF > // Numeric flag
218 template< typename Arg > // Min/max argument type
219 inline const LowerMatrix<MT,SO,DF>
220  Rand< LowerMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
221 {
223 
224  LowerMatrix<MT,SO,DF> matrix;
225  randomize( matrix, min, max );
226  return matrix;
227 }
229 //*************************************************************************************************
230 
231 
232 //*************************************************************************************************
241 template< typename MT // Type of the adapted matrix
242  , bool SO // Storage order of the adapted matrix
243  , bool DF > // Numeric flag
244 template< typename Arg > // Min/max argument type
245 inline const LowerMatrix<MT,SO,DF>
246  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
247 {
249 
250  LowerMatrix<MT,SO,DF> matrix( n );
251  randomize( matrix, min, max );
252  return matrix;
253 }
255 //*************************************************************************************************
256 
257 
258 //*************************************************************************************************
269 template< typename MT // Type of the adapted matrix
270  , bool SO // Storage order of the adapted matrix
271  , bool DF > // Numeric flag
272 template< typename Arg > // Min/max argument type
273 inline const LowerMatrix<MT,SO,DF>
274  Rand< LowerMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
275  const Arg& min, const Arg& max ) const
276 {
279 
280  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) )
281  throw std::invalid_argument( "Invalid number of non-zero elements" );
282 
283  LowerMatrix<MT,SO,DF> matrix( n );
284  randomize( matrix, nonzeros, min, max );
285 
286  return matrix;
287 }
289 //*************************************************************************************************
290 
291 
292 //*************************************************************************************************
299 template< typename MT // Type of the adapted matrix
300  , bool SO // Storage order of the adapted matrix
301  , bool DF > // Numeric flag
302 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix ) const
303 {
304  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
305 }
307 //*************************************************************************************************
308 
309 
310 //*************************************************************************************************
317 template< typename MT // Type of the adapted matrix
318  , bool SO // Storage order of the adapted matrix
319  , bool DF > // Numeric flag
320 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix, TrueType ) const
321 {
323 
324  typedef typename MT::ElementType ET;
325 
326  const size_t n( matrix.rows() );
327 
328  for( size_t i=0UL; i<n; ++i ) {
329  for( size_t j=0UL; j<=i; ++j ) {
330  matrix(i,j) = rand<ET>();
331  }
332  }
333 }
335 //*************************************************************************************************
336 
337 
338 //*************************************************************************************************
345 template< typename MT // Type of the adapted matrix
346  , bool SO // Storage order of the adapted matrix
347  , bool DF > // Numeric flag
348 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix, FalseType ) const
349 {
351 
352  typedef typename MT::ElementType ET;
353 
354  const size_t n( matrix.rows() );
355 
356  if( n == 0UL ) return;
357 
358  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*n*n ) ) );
359 
360  matrix.reset();
361  matrix.reserve( nonzeros );
362 
363  while( matrix.nonZeros() < nonzeros ) {
364  const size_t row( rand<size_t>( 0UL, n-1UL ) );
365  const size_t col( rand<size_t>( 0UL, row ) );
366  matrix(row,col) = rand<ET>();
367  }
368 }
370 //*************************************************************************************************
371 
372 
373 //*************************************************************************************************
382 template< typename MT // Type of the adapted matrix
383  , bool SO // Storage order of the adapted matrix
384  , bool DF > // Numeric flag
385 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
386 {
388 
389  typedef typename MT::ElementType ET;
390 
391  const size_t n( matrix.rows() );
392 
393  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) )
394  throw std::invalid_argument( "Invalid number of non-zero elements" );
395 
396  if( n == 0UL ) return;
397 
398  matrix.reset();
399  matrix.reserve( nonzeros );
400 
401  while( matrix.nonZeros() < nonzeros ) {
402  const size_t row( rand<size_t>( 0UL, n-1UL ) );
403  const size_t col( rand<size_t>( 0UL, row ) );
404  matrix(row,col) = rand<ET>();
405  }
406 }
408 //*************************************************************************************************
409 
410 
411 //*************************************************************************************************
420 template< typename MT // Type of the adapted matrix
421  , bool SO // Storage order of the adapted matrix
422  , bool DF > // Numeric flag
423 template< typename Arg > // Min/max argument type
424 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix,
425  const Arg& min, const Arg& max ) const
426 {
427  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
428 }
430 //*************************************************************************************************
431 
432 
433 //*************************************************************************************************
442 template< typename MT // Type of the adapted matrix
443  , bool SO // Storage order of the adapted matrix
444  , bool DF > // Numeric flag
445 template< typename Arg > // Min/max argument type
446 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix,
447  const Arg& min, const Arg& max, TrueType ) const
448 {
450 
451  typedef typename MT::ElementType ET;
452 
453  const size_t n( matrix.rows() );
454 
455  for( size_t i=0UL; i<n; ++i ) {
456  for( size_t j=0UL; j<=i; ++j ) {
457  matrix(i,j) = rand<ET>( min, max );
458  }
459  }
460 }
462 //*************************************************************************************************
463 
464 
465 //*************************************************************************************************
474 template< typename MT // Type of the adapted matrix
475  , bool SO // Storage order of the adapted matrix
476  , bool DF > // Numeric flag
477 template< typename Arg > // Min/max argument type
478 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix,
479  const Arg& min, const Arg& max, FalseType ) const
480 {
482 
483  typedef typename MT::ElementType ET;
484 
485  const size_t n( matrix.rows() );
486 
487  if( n == 0UL ) return;
488 
489  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.3*n*n ) ) );
490 
491  matrix.reset();
492  matrix.reserve( nonzeros );
493 
494  while( matrix.nonZeros() < nonzeros ) {
495  const size_t row( rand<size_t>( 0UL, n-1UL ) );
496  const size_t col( rand<size_t>( 0UL, row ) );
497  matrix(row,col) = rand<ET>( min, max );
498  }
499 }
501 //*************************************************************************************************
502 
503 
504 //*************************************************************************************************
515 template< typename MT // Type of the adapted matrix
516  , bool SO // Storage order of the adapted matrix
517  , bool DF > // Numeric flag
518 template< typename Arg > // Min/max argument type
519 inline void Rand< LowerMatrix<MT,SO,DF> >::randomize( LowerMatrix<MT,SO,DF>& matrix,
520  size_t nonzeros, const Arg& min, const Arg& max ) const
521 {
523 
524  typedef typename MT::ElementType ET;
525 
526  const size_t n( matrix.rows() );
527 
528  if( nonzeros > LowerMatrix<MT,SO,DF>::maxNonZeros( n ) )
529  throw std::invalid_argument( "Invalid number of non-zero elements" );
530 
531  if( n == 0UL ) return;
532 
533  matrix.reset();
534  matrix.reserve( nonzeros );
535 
536  while( matrix.nonZeros() < nonzeros ) {
537  const size_t row( rand<size_t>( 0UL, n-1UL ) );
538  const size_t col( rand<size_t>( 0UL, row ) );
539  matrix(row,col) = rand<ET>( min, max );
540  }
541 }
543 //*************************************************************************************************
544 
545 } // namespace blaze
546 
547 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1649
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:1043
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:262
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1602
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2505
Header file for the IsDenseMatrix type trait.
Header file for the implementation of a lower matrix adaptor.
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:103
#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:222
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
Header file for the complete UpperMatrix implementation.
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
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.