CompressedMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_COMPRESSEDMATRIX_H_
36 #define _BLAZE_MATH_COMPRESSEDMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <vector>
47 #include <blaze/math/Exception.h>
49 #include <blaze/system/Precision.h>
50 #include <blaze/util/Assert.h>
51 #include <blaze/util/Indices.h>
52 #include <blaze/util/Random.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // RAND SPECIALIZATION
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
70 template< typename Type // Data type of the matrix
71  , bool SO > // Storage order
72 class Rand< CompressedMatrix<Type,SO> >
73 {
74  public:
75  //**Generate functions**************************************************************************
78  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n ) const;
79  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros ) const;
80 
81  template< typename Arg >
82  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
83 
84  template< typename Arg >
85  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros,
86  const Arg& min, const Arg& max ) const;
88  //**********************************************************************************************
89 
90  //**Randomize functions*************************************************************************
93  inline void randomize( CompressedMatrix<Type,SO>& matrix ) const;
94  inline void randomize( CompressedMatrix<Type,false>& matrix, size_t nonzeros ) const;
95  inline void randomize( CompressedMatrix<Type,true>& matrix, size_t nonzeros ) const;
96 
97  template< typename Arg >
98  inline void randomize( CompressedMatrix<Type,SO>& matrix, const Arg& min, const Arg& max ) const;
99 
100  template< typename Arg >
101  inline void randomize( CompressedMatrix<Type,false>& matrix, size_t nonzeros,
102  const Arg& min, const Arg& max ) const;
103  template< typename Arg >
104  inline void randomize( CompressedMatrix<Type,true>& matrix, size_t nonzeros,
105  const Arg& min, const Arg& max ) const;
107  //**********************************************************************************************
108 };
110 //*************************************************************************************************
111 
112 
113 //*************************************************************************************************
121 template< typename Type // Data type of the matrix
122  , bool SO > // Storage order
123 inline const CompressedMatrix<Type,SO>
124  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n ) const
125 {
126  CompressedMatrix<Type,SO> matrix( m, n );
127  randomize( matrix );
128 
129  return matrix;
130 }
132 //*************************************************************************************************
133 
134 
135 //*************************************************************************************************
145 template< typename Type // Data type of the matrix
146  , bool SO > // Storage order
147 inline const CompressedMatrix<Type,SO>
148  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros ) const
149 {
150  if( nonzeros > m*n ) {
151  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
152  }
153 
154  CompressedMatrix<Type,SO> matrix( m, n );
155  randomize( matrix, nonzeros );
156 
157  return matrix;
158 }
160 //*************************************************************************************************
161 
162 
163 //*************************************************************************************************
173 template< typename Type // Data type of the matrix
174  , bool SO > // Storage order
175 template< typename Arg > // Min/max argument type
176 inline const CompressedMatrix<Type,SO>
177  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
178 {
179  CompressedMatrix<Type,SO> matrix( m, n );
180  randomize( matrix, min, max );
181 
182  return matrix;
183 }
185 //*************************************************************************************************
186 
187 
188 //*************************************************************************************************
200 template< typename Type // Data type of the matrix
201  , bool SO > // Storage order
202 template< typename Arg > // Min/max argument type
203 inline const CompressedMatrix<Type,SO>
204  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros,
205  const Arg& min, const Arg& max ) const
206 {
207  if( nonzeros > m*n ) {
208  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
209  }
210 
211  CompressedMatrix<Type,SO> matrix( m, n );
212  randomize( matrix, nonzeros, min, max );
213 
214  return matrix;
215 }
217 //*************************************************************************************************
218 
219 
220 //*************************************************************************************************
227 template< typename Type // Data type of the matrix
228  , bool SO > // Storage order
229 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix ) const
230 {
231  const size_t m( matrix.rows() );
232  const size_t n( matrix.columns() );
233 
234  if( m == 0UL || n == 0UL ) return;
235 
236  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
237 
238  randomize( matrix, nonzeros );
239 }
241 //*************************************************************************************************
242 
243 
244 //*************************************************************************************************
253 template< typename Type // Data type of the matrix
254  , bool SO > // Storage order
255 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,false>& matrix, size_t nonzeros ) const
256 {
257  const size_t m( matrix.rows() );
258  const size_t n( matrix.columns() );
259 
260  if( nonzeros > m*n ) {
261  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
262  }
263 
264  if( m == 0UL || n == 0UL ) return;
265 
266  matrix.reset();
267  matrix.reserve( nonzeros );
268 
269  std::vector<size_t> dist( m );
270 
271  for( size_t nz=0UL; nz<nonzeros; ) {
272  const size_t index = rand<size_t>( 0UL, m-1UL );
273  if( dist[index] == n ) continue;
274  ++dist[index];
275  ++nz;
276  }
277 
278  for( size_t i=0UL; i<m; ++i ) {
279  const Indices indices( 0UL, n-1UL, dist[i] );
280  for( size_t j : indices ) {
281  matrix.append( i, j, rand<Type>() );
282  }
283  matrix.finalize( i );
284  }
285 }
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
299 template< typename Type // Data type of the matrix
300  , bool SO > // Storage order
301 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,true>& matrix, size_t nonzeros ) const
302 {
303  const size_t m( matrix.rows() );
304  const size_t n( matrix.columns() );
305 
306  if( nonzeros > m*n ) {
307  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
308  }
309 
310  if( m == 0UL || n == 0UL ) return;
311 
312  matrix.reset();
313  matrix.reserve( nonzeros );
314 
315  std::vector<size_t> dist( n );
316 
317  for( size_t nz=0UL; nz<nonzeros; ) {
318  const size_t index = rand<size_t>( 0UL, n-1UL );
319  if( dist[index] == m ) continue;
320  ++dist[index];
321  ++nz;
322  }
323 
324  for( size_t j=0UL; j<n; ++j ) {
325  const Indices indices( 0UL, m-1UL, dist[j] );
326  for( size_t i : indices ) {
327  matrix.append( i, j, rand<Type>() );
328  }
329  matrix.finalize( j );
330  }
331 }
333 //*************************************************************************************************
334 
335 
336 //*************************************************************************************************
345 template< typename Type // Data type of the matrix
346  , bool SO > // Storage order
347 template< typename Arg > // Min/max argument type
348 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
349  const Arg& min, const Arg& max ) const
350 {
351  const size_t m( matrix.rows() );
352  const size_t n( matrix.columns() );
353 
354  if( m == 0UL || n == 0UL ) return;
355 
356  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
357 
358  randomize( matrix, nonzeros, min, max );
359 }
361 //*************************************************************************************************
362 
363 
364 //*************************************************************************************************
375 template< typename Type // Data type of the matrix
376  , bool SO > // Storage order
377 template< typename Arg > // Min/max argument type
378 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,false>& matrix,
379  size_t nonzeros, const Arg& min, const Arg& max ) const
380 {
381  const size_t m( matrix.rows() );
382  const size_t n( matrix.columns() );
383 
384  if( nonzeros > m*n ) {
385  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
386  }
387 
388  if( m == 0UL || n == 0UL ) return;
389 
390  matrix.reset();
391  matrix.reserve( nonzeros );
392 
393  std::vector<size_t> dist( m );
394 
395  for( size_t nz=0UL; nz<nonzeros; ) {
396  const size_t index = rand<size_t>( 0UL, m-1UL );
397  if( dist[index] == n ) continue;
398  ++dist[index];
399  ++nz;
400  }
401 
402  for( size_t i=0UL; i<m; ++i ) {
403  const Indices indices( 0UL, n-1UL, dist[i] );
404  for( size_t j : indices ) {
405  matrix.append( i, j, rand<Type>( min, max ) );
406  }
407  matrix.finalize( i );
408  }
409 }
411 //*************************************************************************************************
412 
413 
414 //*************************************************************************************************
425 template< typename Type // Data type of the matrix
426  , bool SO > // Storage order
427 template< typename Arg > // Min/max argument type
428 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,true>& matrix,
429  size_t nonzeros, const Arg& min, const Arg& max ) const
430 {
431  const size_t m( matrix.rows() );
432  const size_t n( matrix.columns() );
433 
434  if( nonzeros > m*n ) {
435  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
436  }
437 
438  if( m == 0UL || n == 0UL ) return;
439 
440  matrix.reset();
441  matrix.reserve( nonzeros );
442 
443  std::vector<size_t> dist( n );
444 
445  for( size_t nz=0UL; nz<nonzeros; ) {
446  const size_t index = rand<size_t>( 0UL, n-1UL );
447  if( dist[index] == m ) continue;
448  ++dist[index];
449  ++nz;
450  }
451 
452  for( size_t j=0UL; j<n; ++j ) {
453  const Indices indices( 0UL, m-1UL, dist[j] );
454  for( size_t i : indices ) {
455  matrix.append( i, j, rand<Type>( min, max ) );
456  }
457  matrix.finalize( j );
458  }
459 }
461 //*************************************************************************************************
462 
463 } // namespace blaze
464 
465 #endif
#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
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:926
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
Implementation of a compressed MxN matrix.
Implementation of a random number generator.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
Header file for the floating point precision of the Blaze library.
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 exception macros of the math module.
Header file for the complete CompressedVector implementation.
Header file for run time assertion macros.
Header file for the Indices class.
T generate() const
Generation of a random value in the range .
Definition: Random.h:249
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