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>
50 #include <blaze/math/ZeroMatrix.h>
51 #include <blaze/util/Assert.h>
52 #include <blaze/util/Indices.h>
53 #include <blaze/util/Random.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // RAND SPECIALIZATION
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename Type // Data type of the matrix
72  , bool SO > // Storage order
73 class Rand< CompressedMatrix<Type,SO> >
74 {
75  public:
76  //**Generate functions**************************************************************************
79  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n ) const;
80  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros ) const;
81 
82  template< typename Arg >
83  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
84 
85  template< typename Arg >
86  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros,
87  const Arg& min, const Arg& max ) const;
89  //**********************************************************************************************
90 
91  //**Randomize functions*************************************************************************
94  inline void randomize( CompressedMatrix<Type,SO>& matrix ) const;
95  inline void randomize( CompressedMatrix<Type,false>& matrix, size_t nonzeros ) const;
96  inline void randomize( CompressedMatrix<Type,true>& matrix, size_t nonzeros ) const;
97 
98  template< typename Arg >
99  inline void randomize( CompressedMatrix<Type,SO>& matrix, const Arg& min, const Arg& max ) const;
100 
101  template< typename Arg >
102  inline void randomize( CompressedMatrix<Type,false>& matrix, size_t nonzeros,
103  const Arg& min, const Arg& max ) const;
104  template< typename Arg >
105  inline void randomize( CompressedMatrix<Type,true>& matrix, size_t nonzeros,
106  const Arg& min, const Arg& max ) const;
108  //**********************************************************************************************
109 };
111 //*************************************************************************************************
112 
113 
114 //*************************************************************************************************
122 template< typename Type // Data type of the matrix
123  , bool SO > // Storage order
124 inline const CompressedMatrix<Type,SO>
125  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n ) const
126 {
127  CompressedMatrix<Type,SO> matrix( m, n );
128  randomize( matrix );
129 
130  return matrix;
131 }
133 //*************************************************************************************************
134 
135 
136 //*************************************************************************************************
146 template< typename Type // Data type of the matrix
147  , bool SO > // Storage order
148 inline const CompressedMatrix<Type,SO>
149  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros ) const
150 {
151  if( nonzeros > m*n ) {
152  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
153  }
154 
155  CompressedMatrix<Type,SO> matrix( m, n );
156  randomize( matrix, nonzeros );
157 
158  return matrix;
159 }
161 //*************************************************************************************************
162 
163 
164 //*************************************************************************************************
174 template< typename Type // Data type of the matrix
175  , bool SO > // Storage order
176 template< typename Arg > // Min/max argument type
177 inline const CompressedMatrix<Type,SO>
178  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
179 {
180  CompressedMatrix<Type,SO> matrix( m, n );
181  randomize( matrix, min, max );
182 
183  return matrix;
184 }
186 //*************************************************************************************************
187 
188 
189 //*************************************************************************************************
201 template< typename Type // Data type of the matrix
202  , bool SO > // Storage order
203 template< typename Arg > // Min/max argument type
204 inline const CompressedMatrix<Type,SO>
205  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros,
206  const Arg& min, const Arg& max ) const
207 {
208  if( nonzeros > m*n ) {
209  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
210  }
211 
212  CompressedMatrix<Type,SO> matrix( m, n );
213  randomize( matrix, nonzeros, min, max );
214 
215  return matrix;
216 }
218 //*************************************************************************************************
219 
220 
221 //*************************************************************************************************
228 template< typename Type // Data type of the matrix
229  , bool SO > // Storage order
230 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix ) const
231 {
232  const size_t m( matrix.rows() );
233  const size_t n( matrix.columns() );
234 
235  if( m == 0UL || n == 0UL ) return;
236 
237  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
238 
239  randomize( matrix, nonzeros );
240 }
242 //*************************************************************************************************
243 
244 
245 //*************************************************************************************************
254 template< typename Type // Data type of the matrix
255  , bool SO > // Storage order
256 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,false>& matrix, size_t nonzeros ) const
257 {
258  const size_t m( matrix.rows() );
259  const size_t n( matrix.columns() );
260 
261  if( nonzeros > m*n ) {
262  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
263  }
264 
265  if( m == 0UL || n == 0UL ) return;
266 
267  matrix.reset();
268  matrix.reserve( nonzeros );
269 
270  std::vector<size_t> dist( m );
271 
272  for( size_t nz=0UL; nz<nonzeros; ) {
273  const size_t index = rand<size_t>( 0UL, m-1UL );
274  if( dist[index] == n ) continue;
275  ++dist[index];
276  ++nz;
277  }
278 
279  for( size_t i=0UL; i<m; ++i ) {
280  const Indices indices( 0UL, n-1UL, dist[i] );
281  for( size_t j : indices ) {
282  matrix.append( i, j, rand<Type>() );
283  }
284  matrix.finalize( i );
285  }
286 }
288 //*************************************************************************************************
289 
290 
291 //*************************************************************************************************
300 template< typename Type // Data type of the matrix
301  , bool SO > // Storage order
302 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,true>& matrix, size_t nonzeros ) const
303 {
304  const size_t m( matrix.rows() );
305  const size_t n( matrix.columns() );
306 
307  if( nonzeros > m*n ) {
308  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
309  }
310 
311  if( m == 0UL || n == 0UL ) return;
312 
313  matrix.reset();
314  matrix.reserve( nonzeros );
315 
316  std::vector<size_t> dist( n );
317 
318  for( size_t nz=0UL; nz<nonzeros; ) {
319  const size_t index = rand<size_t>( 0UL, n-1UL );
320  if( dist[index] == m ) continue;
321  ++dist[index];
322  ++nz;
323  }
324 
325  for( size_t j=0UL; j<n; ++j ) {
326  const Indices indices( 0UL, m-1UL, dist[j] );
327  for( size_t i : indices ) {
328  matrix.append( i, j, rand<Type>() );
329  }
330  matrix.finalize( j );
331  }
332 }
334 //*************************************************************************************************
335 
336 
337 //*************************************************************************************************
346 template< typename Type // Data type of the matrix
347  , bool SO > // Storage order
348 template< typename Arg > // Min/max argument type
349 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
350  const Arg& min, const Arg& max ) const
351 {
352  const size_t m( matrix.rows() );
353  const size_t n( matrix.columns() );
354 
355  if( m == 0UL || n == 0UL ) return;
356 
357  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
358 
359  randomize( matrix, nonzeros, min, max );
360 }
362 //*************************************************************************************************
363 
364 
365 //*************************************************************************************************
376 template< typename Type // Data type of the matrix
377  , bool SO > // Storage order
378 template< typename Arg > // Min/max argument type
379 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,false>& matrix,
380  size_t nonzeros, const Arg& min, const Arg& max ) const
381 {
382  const size_t m( matrix.rows() );
383  const size_t n( matrix.columns() );
384 
385  if( nonzeros > m*n ) {
386  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
387  }
388 
389  if( m == 0UL || n == 0UL ) return;
390 
391  matrix.reset();
392  matrix.reserve( nonzeros );
393 
394  std::vector<size_t> dist( m );
395 
396  for( size_t nz=0UL; nz<nonzeros; ) {
397  const size_t index = rand<size_t>( 0UL, m-1UL );
398  if( dist[index] == n ) continue;
399  ++dist[index];
400  ++nz;
401  }
402 
403  for( size_t i=0UL; i<m; ++i ) {
404  const Indices indices( 0UL, n-1UL, dist[i] );
405  for( size_t j : indices ) {
406  matrix.append( i, j, rand<Type>( min, max ) );
407  }
408  matrix.finalize( i );
409  }
410 }
412 //*************************************************************************************************
413 
414 
415 //*************************************************************************************************
426 template< typename Type // Data type of the matrix
427  , bool SO > // Storage order
428 template< typename Arg > // Min/max argument type
429 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,true>& matrix,
430  size_t nonzeros, const Arg& min, const Arg& max ) const
431 {
432  const size_t m( matrix.rows() );
433  const size_t n( matrix.columns() );
434 
435  if( nonzeros > m*n ) {
436  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
437  }
438 
439  if( m == 0UL || n == 0UL ) return;
440 
441  matrix.reset();
442  matrix.reserve( nonzeros );
443 
444  std::vector<size_t> dist( n );
445 
446  for( size_t nz=0UL; nz<nonzeros; ) {
447  const size_t index = rand<size_t>( 0UL, n-1UL );
448  if( dist[index] == m ) continue;
449  ++dist[index];
450  ++nz;
451  }
452 
453  for( size_t j=0UL; j<n; ++j ) {
454  const Indices indices( 0UL, m-1UL, dist[j] );
455  for( size_t i : indices ) {
456  matrix.append( i, j, rand<Type>( min, max ) );
457  }
458  matrix.finalize( j );
459  }
460 }
462 //*************************************************************************************************
463 
464 } // namespace blaze
465 
466 #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
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1239
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
Implementation of a compressed MxN matrix.
Implementation of a random number generator.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for all basic SparseMatrix functionality.
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1147
Header file for the exception macros of the math module.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1179
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:252
Header file for the complete IdentityMatrix implementation.
Header file for the complete ZeroMatrix implementation.