All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SymmetricMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SYMMETRICMATRIX_H_
36 #define _BLAZE_MATH_SYMMETRICMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <stdexcept>
49 #include <blaze/math/DenseMatrix.h>
52 #include <blaze/util/FalseType.h>
53 #include <blaze/util/Random.h>
54 #include <blaze/util/TrueType.h>
55 #include <blaze/util/Types.h>
56 
57 
58 namespace blaze {
59 
60 //=================================================================================================
61 //
62 // RAND SPECIALIZATION
63 //
64 //=================================================================================================
65 
66 //*************************************************************************************************
73 template< typename MT // Type of the adapted matrix
74  , bool SO // Storage order of the adapted matrix
75  , bool DF // Density flag
76  , bool NF > // Numeric flag
77 class Rand< SymmetricMatrix<MT,SO,DF,NF> >
78 {
79  public:
80  //**Generate functions**************************************************************************
83  inline const SymmetricMatrix<MT,SO,DF,NF> generate() const;
84  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n ) const;
85  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros ) const;
86 
87  template< typename Arg >
88  inline const SymmetricMatrix<MT,SO,DF,NF> generate( const Arg& min, const Arg& max ) const;
89 
90  template< typename Arg >
91  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, const Arg& min, const Arg& max ) const;
92 
93  template< typename Arg >
94  inline const SymmetricMatrix<MT,SO,DF,NF> generate( size_t n, size_t nonzeros,
95  const Arg& min, const Arg& max ) const;
97  //**********************************************************************************************
98 
99  //**Randomize functions*************************************************************************
102  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const;
103  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const;
104 
105  template< typename Arg >
106  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max ) const;
107 
108  template< typename Arg >
109  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros,
110  const Arg& min, const Arg& max ) const;
112  //**********************************************************************************************
113 
114  private:
115  //**Randomize functions*************************************************************************
118  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const;
119  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const;
120 
121  template< typename Arg >
122  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, const Arg& min, const Arg& max, TrueType ) const;
123 
124  template< typename Arg >
125  inline void randomize( SymmetricMatrix<MT,SO,DF,NF>& 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 // Density flag
142  , bool NF > // Numeric flag
143 inline const SymmetricMatrix<MT,SO,DF,NF> Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate() const
144 {
146 
147  SymmetricMatrix<MT,SO,DF,NF> matrix;
148  randomize( matrix );
149  return matrix;
150 }
152 //*************************************************************************************************
153 
154 
155 //*************************************************************************************************
162 template< typename MT // Type of the adapted matrix
163  , bool SO // Storage order of the adapted matrix
164  , bool DF // Density flag
165  , bool NF > // Numeric flag
166 inline const SymmetricMatrix<MT,SO,DF,NF>
167  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n ) const
168 {
170 
171  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
172  randomize( matrix );
173  return matrix;
174 }
176 //*************************************************************************************************
177 
178 
179 //*************************************************************************************************
188 template< typename MT // Type of the adapted matrix
189  , bool SO // Storage order of the adapted matrix
190  , bool DF // Density flag
191  , bool NF > // Numeric flag
192 inline const SymmetricMatrix<MT,SO,DF,NF>
193  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros ) const
194 {
197 
198  if( nonzeros > n*n )
199  throw std::invalid_argument( "Invalid number of non-zero elements" );
200 
201  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
202  randomize( matrix, nonzeros );
203 
204  return matrix;
205 }
207 //*************************************************************************************************
208 
209 
210 //*************************************************************************************************
218 template< typename MT // Type of the adapted matrix
219  , bool SO // Storage order of the adapted matrix
220  , bool DF // Density flag
221  , bool NF > // Numeric flag
222 template< typename Arg > // Min/max argument type
223 inline const SymmetricMatrix<MT,SO,DF,NF>
224  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( const Arg& min, const Arg& max ) const
225 {
227 
228  SymmetricMatrix<MT,SO,DF,NF> matrix;
229  randomize( matrix, min, max );
230  return matrix;
231 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
245 template< typename MT // Type of the adapted matrix
246  , bool SO // Storage order of the adapted matrix
247  , bool DF // Density flag
248  , bool NF > // Numeric flag
249 template< typename Arg > // Min/max argument type
250 inline const SymmetricMatrix<MT,SO,DF,NF>
251  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, const Arg& min, const Arg& max ) const
252 {
254 
255  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
256  randomize( matrix, min, max );
257  return matrix;
258 }
260 //*************************************************************************************************
261 
262 
263 //*************************************************************************************************
274 template< typename MT // Type of the adapted matrix
275  , bool SO // Storage order of the adapted matrix
276  , bool DF // Density flag
277  , bool NF > // Numeric flag
278 template< typename Arg > // Min/max argument type
279 inline const SymmetricMatrix<MT,SO,DF,NF>
280  Rand< SymmetricMatrix<MT,SO,DF,NF> >::generate( size_t n, size_t nonzeros,
281  const Arg& min, const Arg& max ) const
282 {
285 
286  if( nonzeros > n*n )
287  throw std::invalid_argument( "Invalid number of non-zero elements" );
288 
289  SymmetricMatrix<MT,SO,DF,NF> matrix( n );
290  randomize( matrix, nonzeros, min, max );
291 
292  return matrix;
293 }
295 //*************************************************************************************************
296 
297 
298 //*************************************************************************************************
305 template< typename MT // Type of the adapted matrix
306  , bool SO // Storage order of the adapted matrix
307  , bool DF // Density flag
308  , bool NF > // Numeric flag
309 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix ) const
310 {
311  randomize( matrix, typename IsDenseMatrix<MT>::Type() );
312 }
314 //*************************************************************************************************
315 
316 
317 //*************************************************************************************************
324 template< typename MT // Type of the adapted matrix
325  , bool SO // Storage order of the adapted matrix
326  , bool DF // Density flag
327  , bool NF > // Numeric flag
328 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, TrueType ) const
329 {
331 
332  typedef typename MT::ElementType ET;
333 
334  const size_t n( matrix.rows() );
335 
336  for( size_t i=0UL; i<n; ++i ) {
337  for( size_t j=0UL; j<=i; ++j ) {
338  matrix(i,j) = rand<ET>();
339  }
340  }
341 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
353 template< typename MT // Type of the adapted matrix
354  , bool SO // Storage order of the adapted matrix
355  , bool DF // Density flag
356  , bool NF > // Numeric flag
357 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, FalseType ) const
358 {
360 
361  typedef typename MT::ElementType ET;
362 
363  const size_t n( matrix.rows() );
364 
365  if( n == 0UL ) return;
366 
367  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
368 
369  matrix.reset();
370  matrix.reserve( nonzeros );
371 
372  while( matrix.nonZeros() < nonzeros ) {
373  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>();
374  }
375 }
377 //*************************************************************************************************
378 
379 
380 //*************************************************************************************************
389 template< typename MT // Type of the adapted matrix
390  , bool SO // Storage order of the adapted matrix
391  , bool DF // Density flag
392  , bool NF > // Numeric flag
393 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix, size_t nonzeros ) const
394 {
396 
397  typedef typename MT::ElementType ET;
398 
399  const size_t n( matrix.rows() );
400 
401  if( nonzeros > n*n )
402  throw std::invalid_argument( "Invalid number of non-zero elements" );
403 
404  if( n == 0UL ) return;
405 
406  matrix.reset();
407  matrix.reserve( nonzeros );
408 
409  while( matrix.nonZeros() < nonzeros ) {
410  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>();
411  }
412 }
414 //*************************************************************************************************
415 
416 
417 //*************************************************************************************************
426 template< typename MT // Type of the adapted matrix
427  , bool SO // Storage order of the adapted matrix
428  , bool DF // Density flag
429  , bool NF > // Numeric flag
430 template< typename Arg > // Min/max argument type
431 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
432  const Arg& min, const Arg& max ) const
433 {
434  randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
435 }
437 //*************************************************************************************************
438 
439 
440 //*************************************************************************************************
449 template< typename MT // Type of the adapted matrix
450  , bool SO // Storage order of the adapted matrix
451  , bool DF // Density flag
452  , bool NF > // Numeric flag
453 template< typename Arg > // Min/max argument type
454 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
455  const Arg& min, const Arg& max, TrueType ) const
456 {
458 
459  typedef typename MT::ElementType ET;
460 
461  const size_t n( matrix.rows() );
462 
463  for( size_t i=0UL; i<n; ++i ) {
464  for( size_t j=0UL; j<=i; ++j ) {
465  matrix(i,j) = rand<ET>( min, max );
466  }
467  }
468 }
470 //*************************************************************************************************
471 
472 
473 //*************************************************************************************************
482 template< typename MT // Type of the adapted matrix
483  , bool SO // Storage order of the adapted matrix
484  , bool DF // Density flag
485  , bool NF > // Numeric flag
486 template< typename Arg > // Min/max argument type
487 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
488  const Arg& min, const Arg& max, FalseType ) const
489 {
491 
492  typedef typename MT::ElementType ET;
493 
494  const size_t n( matrix.rows() );
495 
496  if( n == 0UL ) return;
497 
498  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*n*n ) ) );
499 
500  matrix.reset();
501  matrix.reserve( nonzeros );
502 
503  while( matrix.nonZeros() < nonzeros ) {
504  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>( min, max );
505  }
506 }
508 //*************************************************************************************************
509 
510 
511 //*************************************************************************************************
522 template< typename MT // Type of the adapted matrix
523  , bool SO // Storage order of the adapted matrix
524  , bool DF // Density flag
525  , bool NF > // Numeric flag
526 template< typename Arg > // Min/max argument type
527 inline void Rand< SymmetricMatrix<MT,SO,DF,NF> >::randomize( SymmetricMatrix<MT,SO,DF,NF>& matrix,
528  size_t nonzeros, const Arg& min, const Arg& max ) const
529 {
531 
532  typedef typename MT::ElementType ET;
533 
534  const size_t n( matrix.rows() );
535 
536  if( nonzeros > n*n )
537  throw std::invalid_argument( "Invalid number of non-zero elements" );
538 
539  if( n == 0UL ) return;
540 
541  matrix.reset();
542  matrix.reserve( nonzeros );
543 
544  while( matrix.nonZeros() < nonzeros ) {
545  matrix( rand<size_t>( 0UL, n-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ET>( min, max );
546  }
547 }
549 //*************************************************************************************************
550 
551 } // namespace blaze
552 
553 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:994
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.
Header file for the implementation of a symmetric matrix adaptor.
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:947
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2476
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
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 basic type definitions.
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.