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>
47 #include <blaze/system/Precision.h>
48 #include <blaze/util/Exception.h>
49 #include <blaze/util/Random.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // RAND SPECIALIZATION
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
67 template< typename Type // Data type of the matrix
68  , bool SO > // Storage order
69 class Rand< CompressedMatrix<Type,SO> >
70 {
71  public:
72  //**Generate functions**************************************************************************
75  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n ) const;
76  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros ) const;
77 
78  template< typename Arg >
79  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
80 
81  template< typename Arg >
82  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros,
83  const Arg& min, const Arg& max ) const;
85  //**********************************************************************************************
86 
87  //**Randomize functions*************************************************************************
90  inline void randomize( CompressedMatrix<Type,SO>& matrix ) const;
91  inline void randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros ) const;
92 
93  template< typename Arg >
94  inline void randomize( CompressedMatrix<Type,SO>& matrix, const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline void randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros,
98  const Arg& min, const Arg& max ) const;
100  //**********************************************************************************************
101 };
103 //*************************************************************************************************
104 
105 
106 //*************************************************************************************************
114 template< typename Type // Data type of the matrix
115  , bool SO > // Storage order
116 inline const CompressedMatrix<Type,SO>
117  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n ) const
118 {
119  CompressedMatrix<Type,SO> matrix( m, n );
120  randomize( matrix );
121 
122  return matrix;
123 }
125 //*************************************************************************************************
126 
127 
128 //*************************************************************************************************
138 template< typename Type // Data type of the matrix
139  , bool SO > // Storage order
140 inline const CompressedMatrix<Type,SO>
141  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros ) const
142 {
143  if( nonzeros > m*n ) {
144  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
145  }
146 
147  CompressedMatrix<Type,SO> matrix( m, n );
148  randomize( matrix, nonzeros );
149 
150  return matrix;
151 }
153 //*************************************************************************************************
154 
155 
156 //*************************************************************************************************
166 template< typename Type // Data type of the matrix
167  , bool SO > // Storage order
168 template< typename Arg > // Min/max argument type
169 inline const CompressedMatrix<Type,SO>
170  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
171 {
172  CompressedMatrix<Type,SO> matrix( m, n );
173  randomize( matrix, min, max );
174 
175  return matrix;
176 }
178 //*************************************************************************************************
179 
180 
181 //*************************************************************************************************
193 template< typename Type // Data type of the matrix
194  , bool SO > // Storage order
195 template< typename Arg > // Min/max argument type
196 inline const CompressedMatrix<Type,SO>
197  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros,
198  const Arg& min, const Arg& max ) const
199 {
200  if( nonzeros > m*n ) {
201  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
202  }
203 
204  CompressedMatrix<Type,SO> matrix( m, n );
205  randomize( matrix, nonzeros, min, max );
206 
207  return matrix;
208 }
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
220 template< typename Type // Data type of the matrix
221  , bool SO > // Storage order
222 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix ) const
223 {
224  const size_t m( matrix.rows() );
225  const size_t n( matrix.columns() );
226 
227  if( m == 0UL || n == 0UL ) return;
228 
229  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
230 
231  matrix.reset();
232  matrix.reserve( nonzeros );
233 
234  while( matrix.nonZeros() < nonzeros ) {
235  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
236  }
237 }
239 //*************************************************************************************************
240 
241 
242 //*************************************************************************************************
251 template< typename Type // Data type of the matrix
252  , bool SO > // Storage order
253 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros ) const
254 {
255  const size_t m( matrix.rows() );
256  const size_t n( matrix.columns() );
257 
258  if( nonzeros > m*n ) {
259  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
260  }
261 
262  if( m == 0UL || n == 0UL ) return;
263 
264  matrix.reset();
265  matrix.reserve( nonzeros );
266 
267  while( matrix.nonZeros() < nonzeros ) {
268  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
269  }
270 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
284 template< typename Type // Data type of the matrix
285  , bool SO > // Storage order
286 template< typename Arg > // Min/max argument type
287 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
288  const Arg& min, const Arg& max ) const
289 {
290  const size_t m( matrix.rows() );
291  const size_t n( matrix.columns() );
292 
293  if( m == 0UL || n == 0UL ) return;
294 
295  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
296 
297  matrix.reset();
298  matrix.reserve( nonzeros );
299 
300  while( matrix.nonZeros() < nonzeros ) {
301  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
302  }
303 }
305 //*************************************************************************************************
306 
307 
308 //*************************************************************************************************
319 template< typename Type // Data type of the matrix
320  , bool SO > // Storage order
321 template< typename Arg > // Min/max argument type
322 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
323  size_t nonzeros, const Arg& min, const Arg& max ) const
324 {
325  const size_t m( matrix.rows() );
326  const size_t n( matrix.columns() );
327 
328  if( nonzeros > m*n ) {
329  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
330  }
331 
332  if( m == 0UL || n == 0UL ) return;
333 
334  matrix.reset();
335  matrix.reserve( nonzeros );
336 
337  while( matrix.nonZeros() < nonzeros ) {
338  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
339  }
340 }
342 //*************************************************************************************************
343 
344 
345 
346 
347 //=================================================================================================
348 //
349 // TYPE DEFINITIONS
350 //
351 //=================================================================================================
352 
353 //*************************************************************************************************
358 //*************************************************************************************************
359 
360 
361 //*************************************************************************************************
366 //*************************************************************************************************
367 
368 
369 //*************************************************************************************************
374 //*************************************************************************************************
375 
376 } // namespace blaze
377 
378 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exceptionThis macro encapsulates the default way of...
Definition: Exception.h:187
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:207
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1041
Implementation of a compressed MxN matrix.
Implementation of a random number generator.
CompressedMatrix< real_t, false > CMatMxN
MxN matrix with system-specific precision.
Definition: CompressedMatrix.h:373
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:260
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
CompressedMatrix< float, false > CMatMxNf
MxN single precision matrix.
Definition: CompressedMatrix.h:357
Header file for the complete CompressedVector implementation.
T generate() const
Generation of a random value in the range .
Definition: Random.h:220
Header file for exception macros.
CompressedMatrix< double, false > CMatMxNd
MxN double precision matrix.
Definition: CompressedMatrix.h:365