DiagonalMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_DIAGONALMATRIX_H_
36 #define _BLAZE_MATH_DIAGONALMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
48 #include <blaze/math/DenseMatrix.h>
51 #include <blaze/util/FalseType.h>
52 #include <blaze/util/Random.h>
53 #include <blaze/util/TrueType.h>
54 #include <blaze/util/Types.h>
55 
56 
57 namespace blaze {
58 
59 //=================================================================================================
60 //
61 // RAND SPECIALIZATION
62 //
63 //=================================================================================================
64 
65 //*************************************************************************************************
72 template< typename MT // Type of the adapted matrix
73  , bool SO // Storage order of the adapted matrix
74  , bool DF > // Numeric flag
75 class Rand< DiagonalMatrix<MT,SO,DF> >
76 {
77  public:
78  //**Generate functions**************************************************************************
81  inline const DiagonalMatrix<MT,SO,DF> generate() const;
82  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n ) const;
83  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const;
84 
85  template< typename Arg >
86  inline const DiagonalMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const;
87 
88  template< typename Arg >
89  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, const Arg& min, const Arg& max ) const;
90 
91  template< typename Arg >
92  inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros,
93  const Arg& min, const Arg& max ) const;
95  //**********************************************************************************************
96 
97  //**Randomize functions*************************************************************************
100  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const;
101  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const;
102 
103  template< typename Arg >
104  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const;
105 
106  template< typename Arg >
107  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros,
108  const Arg& min, const Arg& max ) const;
110  //**********************************************************************************************
111 
112  private:
113  //**Randomize functions*************************************************************************
116  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const;
117  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const;
118 
119  template< typename Arg >
120  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
121 
122  template< typename Arg >
123  inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max, FalseType ) const;
125  //**********************************************************************************************
126 };
128 //*************************************************************************************************
129 
130 
131 //*************************************************************************************************
137 template< typename MT // Type of the adapted matrix
138  , bool SO // Storage order of the adapted matrix
139  , bool DF > // Numeric flag
140 inline const DiagonalMatrix<MT,SO,DF> Rand< DiagonalMatrix<MT,SO,DF> >::generate() const
141 {
143 
144  DiagonalMatrix<MT,SO,DF> matrix;
145  randomize( matrix );
146  return matrix;
147 }
149 //*************************************************************************************************
150 
151 
152 //*************************************************************************************************
159 template< typename MT // Type of the adapted matrix
160  , bool SO // Storage order of the adapted matrix
161  , bool DF > // Numeric flag
162 inline const DiagonalMatrix<MT,SO,DF>
163  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n ) const
164 {
166 
167  DiagonalMatrix<MT,SO,DF> matrix( n );
168  randomize( matrix );
169  return matrix;
170 }
172 //*************************************************************************************************
173 
174 
175 //*************************************************************************************************
184 template< typename MT // Type of the adapted matrix
185  , bool SO // Storage order of the adapted matrix
186  , bool DF > // Numeric flag
187 inline const DiagonalMatrix<MT,SO,DF>
188  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros ) const
189 {
192 
193  if( nonzeros > n )
194  throw std::invalid_argument( "Invalid number of non-zero elements" );
195 
196  DiagonalMatrix<MT,SO,DF> matrix( n );
197  randomize( matrix, nonzeros );
198 
199  return matrix;
200 }
202 //*************************************************************************************************
203 
204 
205 //*************************************************************************************************
213 template< typename MT // Type of the adapted matrix
214  , bool SO // Storage order of the adapted matrix
215  , bool DF > // Numeric flag
216 template< typename Arg > // Min/max argument type
217 inline const DiagonalMatrix<MT,SO,DF>
218  Rand< DiagonalMatrix<MT,SO,DF> >::generate( const Arg& min, const Arg& max ) const
219 {
221 
222  DiagonalMatrix<MT,SO,DF> matrix;
223  randomize( matrix, min, max );
224  return matrix;
225 }
227 //*************************************************************************************************
228 
229 
230 //*************************************************************************************************
239 template< typename MT // Type of the adapted matrix
240  , bool SO // Storage order of the adapted matrix
241  , bool DF > // Numeric flag
242 template< typename Arg > // Min/max argument type
243 inline const DiagonalMatrix<MT,SO,DF>
244  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, const Arg& min, const Arg& max ) const
245 {
247 
248  DiagonalMatrix<MT,SO,DF> matrix( n );
249  randomize( matrix, min, max );
250  return matrix;
251 }
253 //*************************************************************************************************
254 
255 
256 //*************************************************************************************************
267 template< typename MT // Type of the adapted matrix
268  , bool SO // Storage order of the adapted matrix
269  , bool DF > // Numeric flag
270 template< typename Arg > // Min/max argument type
271 inline const DiagonalMatrix<MT,SO,DF>
272  Rand< DiagonalMatrix<MT,SO,DF> >::generate( size_t n, size_t nonzeros,
273  const Arg& min, const Arg& max ) const
274 {
277 
278  if( nonzeros > DiagonalMatrix<MT,SO,DF>::maxNonZeros( n ) )
279  throw std::invalid_argument( "Invalid number of non-zero elements" );
280 
281  DiagonalMatrix<MT,SO,DF> matrix( n );
282  randomize( matrix, nonzeros, min, max );
283 
284  return matrix;
285 }
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
297 template< typename MT // Type of the adapted matrix
298  , bool SO // Storage order of the adapted matrix
299  , bool DF > // Numeric flag
300 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const
301 {
302  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
303 }
305 //*************************************************************************************************
306 
307 
308 //*************************************************************************************************
315 template< typename MT // Type of the adapted matrix
316  , bool SO // Storage order of the adapted matrix
317  , bool DF > // Numeric flag
318 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const
319 {
321 
322  typedef typename MT::ElementType ET;
323 
324  const size_t n( matrix.rows() );
325 
326  for( size_t i=0UL; i<n; ++i ) {
327  matrix(i,i) = rand<ET>();
328  }
329 }
331 //*************************************************************************************************
332 
333 
334 //*************************************************************************************************
341 template< typename MT // Type of the adapted matrix
342  , bool SO // Storage order of the adapted matrix
343  , bool DF > // Numeric flag
344 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const
345 {
347 
348  typedef typename MT::ElementType ET;
349 
350  const size_t n( matrix.rows() );
351 
352  if( n == 0UL ) return;
353 
354  const size_t nonzeros( rand<size_t>( 1UL, n ) );
355 
356  matrix.reset();
357  matrix.reserve( nonzeros );
358 
359  while( matrix.nonZeros() < nonzeros ) {
360  const size_t i( rand<size_t>( 0UL, n-1UL ) );
361  matrix(i,i) = rand<ET>();
362  }
363 }
365 //*************************************************************************************************
366 
367 
368 //*************************************************************************************************
377 template< typename MT // Type of the adapted matrix
378  , bool SO // Storage order of the adapted matrix
379  , bool DF > // Numeric flag
380 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
381 {
383 
384  typedef typename MT::ElementType ET;
385 
386  const size_t n( matrix.rows() );
387 
388  if( nonzeros > n )
389  throw std::invalid_argument( "Invalid number of non-zero elements" );
390 
391  if( n == 0UL ) return;
392 
393  matrix.reset();
394  matrix.reserve( nonzeros );
395 
396  while( matrix.nonZeros() < nonzeros ) {
397  const size_t i( rand<size_t>( 0UL, n-1UL ) );
398  matrix(i,i) = rand<ET>();
399  }
400 }
402 //*************************************************************************************************
403 
404 
405 //*************************************************************************************************
414 template< typename MT // Type of the adapted matrix
415  , bool SO // Storage order of the adapted matrix
416  , bool DF > // Numeric flag
417 template< typename Arg > // Min/max argument type
418 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
419  const Arg& min, const Arg& max ) const
420 {
421  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
422 }
424 //*************************************************************************************************
425 
426 
427 //*************************************************************************************************
436 template< typename MT // Type of the adapted matrix
437  , bool SO // Storage order of the adapted matrix
438  , bool DF > // Numeric flag
439 template< typename Arg > // Min/max argument type
440 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
441  const Arg& min, const Arg& max, TrueType ) const
442 {
444 
445  typedef typename MT::ElementType ET;
446 
447  const size_t n( matrix.rows() );
448 
449  for( size_t i=0UL; i<n; ++i ) {
450  matrix(i,i) = rand<ET>( min, max );
451  }
452 }
454 //*************************************************************************************************
455 
456 
457 //*************************************************************************************************
466 template< typename MT // Type of the adapted matrix
467  , bool SO // Storage order of the adapted matrix
468  , bool DF > // Numeric flag
469 template< typename Arg > // Min/max argument type
470 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
471  const Arg& min, const Arg& max, FalseType ) const
472 {
474 
475  typedef typename MT::ElementType ET;
476 
477  const size_t n( matrix.rows() );
478 
479  if( n == 0UL ) return;
480 
481  const size_t nonzeros( rand<size_t>( 1UL, n ) );
482 
483  matrix.reset();
484  matrix.reserve( nonzeros );
485 
486  while( matrix.nonZeros() < nonzeros ) {
487  const size_t i( rand<size_t>( 0UL, n-1UL ) );
488  matrix(i,i) = rand<ET>( min, max );
489  }
490 }
492 //*************************************************************************************************
493 
494 
495 //*************************************************************************************************
506 template< typename MT // Type of the adapted matrix
507  , bool SO // Storage order of the adapted matrix
508  , bool DF > // Numeric flag
509 template< typename Arg > // Min/max argument type
510 inline void Rand< DiagonalMatrix<MT,SO,DF> >::randomize( DiagonalMatrix<MT,SO,DF>& matrix,
511  size_t nonzeros, const Arg& min, const Arg& max ) const
512 {
514 
515  typedef typename MT::ElementType ET;
516 
517  const size_t n( matrix.rows() );
518 
519  if( nonzeros > n )
520  throw std::invalid_argument( "Invalid number of non-zero elements" );
521 
522  if( n == 0UL ) return;
523 
524  matrix.reset();
525  matrix.reserve( nonzeros );
526 
527  while( matrix.nonZeros() < nonzeros ) {
528  const size_t i( rand<size_t>( 0UL, n-1UL ) );
529  matrix(i,i) = rand<ET>( min, max );
530  }
531 }
533 //*************************************************************************************************
534 
535 } // namespace blaze
536 
537 #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.
#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 implementation of a diagonal matrix adaptor.
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.