All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <stdexcept>
48 #include <blaze/system/Precision.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  throw std::invalid_argument( "Invalid number of non-zero elements" );
145 
146  CompressedMatrix<Type,SO> matrix( m, n );
147  randomize( matrix, nonzeros );
148 
149  return matrix;
150 }
152 //*************************************************************************************************
153 
154 
155 //*************************************************************************************************
165 template< typename Type // Data type of the matrix
166  , bool SO > // Storage order
167 template< typename Arg > // Min/max argument type
168 inline const CompressedMatrix<Type,SO>
169  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
170 {
171  CompressedMatrix<Type,SO> matrix( m, n );
172  randomize( matrix, min, max );
173 
174  return matrix;
175 }
177 //*************************************************************************************************
178 
179 
180 //*************************************************************************************************
192 template< typename Type // Data type of the matrix
193  , bool SO > // Storage order
194 template< typename Arg > // Min/max argument type
195 inline const CompressedMatrix<Type,SO>
196  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros,
197  const Arg& min, const Arg& max ) const
198 {
199  if( nonzeros > m*n )
200  throw std::invalid_argument( "Invalid number of non-zero elements" );
201 
202  CompressedMatrix<Type,SO> matrix( m, n );
203  randomize( matrix, nonzeros, min, max );
204 
205  return matrix;
206 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
218 template< typename Type // Data type of the matrix
219  , bool SO > // Storage order
220 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix ) const
221 {
222  const size_t m( matrix.rows() );
223  const size_t n( matrix.columns() );
224 
225  if( m == 0UL || n == 0UL ) return;
226 
227  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
228 
229  matrix.reset();
230  matrix.reserve( nonzeros );
231 
232  while( matrix.nonZeros() < nonzeros ) {
233  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
234  }
235 }
237 //*************************************************************************************************
238 
239 
240 //*************************************************************************************************
249 template< typename Type // Data type of the matrix
250  , bool SO > // Storage order
251 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros ) const
252 {
253  const size_t m( matrix.rows() );
254  const size_t n( matrix.columns() );
255 
256  if( nonzeros > m*n )
257  throw std::invalid_argument( "Invalid number of non-zero elements" );
258 
259  if( m == 0UL || n == 0UL ) return;
260 
261  matrix.reset();
262  matrix.reserve( nonzeros );
263 
264  while( matrix.nonZeros() < nonzeros ) {
265  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
266  }
267 }
269 //*************************************************************************************************
270 
271 
272 //*************************************************************************************************
281 template< typename Type // Data type of the matrix
282  , bool SO > // Storage order
283 template< typename Arg > // Min/max argument type
284 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
285  const Arg& min, const Arg& max ) const
286 {
287  const size_t m( matrix.rows() );
288  const size_t n( matrix.columns() );
289 
290  if( m == 0UL || n == 0UL ) return;
291 
292  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
293 
294  matrix.reset();
295  matrix.reserve( nonzeros );
296 
297  while( matrix.nonZeros() < nonzeros ) {
298  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
299  }
300 }
302 //*************************************************************************************************
303 
304 
305 //*************************************************************************************************
316 template< typename Type // Data type of the matrix
317  , bool SO > // Storage order
318 template< typename Arg > // Min/max argument type
319 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
320  size_t nonzeros, const Arg& min, const Arg& max ) const
321 {
322  const size_t m( matrix.rows() );
323  const size_t n( matrix.columns() );
324 
325  if( nonzeros > m*n )
326  throw std::invalid_argument( "Invalid number of non-zero elements" );
327 
328  if( m == 0UL || n == 0UL ) return;
329 
330  matrix.reset();
331  matrix.reserve( nonzeros );
332 
333  while( matrix.nonZeros() < nonzeros ) {
334  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
335  }
336 }
338 //*************************************************************************************************
339 
340 
341 
342 
343 //=================================================================================================
344 //
345 // TYPE DEFINITIONS
346 //
347 //=================================================================================================
348 
349 //*************************************************************************************************
354 //*************************************************************************************************
355 
356 
357 //*************************************************************************************************
362 //*************************************************************************************************
363 
364 
365 //*************************************************************************************************
370 //*************************************************************************************************
371 
372 } // namespace blaze
373 
374 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:994
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:205
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1043
CompressedMatrix< real, false > CMatMxN
MxN matrix with system-specific precision.
Definition: CompressedMatrix.h:369
Implementation of a compressed MxN matrix.
Implementation of a random number generator.
Header file for the floating point precision of the Blaze library.
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
CompressedMatrix< float, false > CMatMxNf
MxN single precision matrix.
Definition: CompressedMatrix.h:353
Header file for the complete CompressedVector implementation.
T generate() const
Generation of a random value in the range .
Definition: Random.h:222
CompressedMatrix< double, false > CMatMxNd
MxN double precision matrix.
Definition: CompressedMatrix.h:361