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 
46 #include <blaze/system/Precision.h>
47 #include <blaze/util/Random.h>
48 
49 
50 namespace blaze {
51 
52 //=================================================================================================
53 //
54 // RAND SPECIALIZATION
55 //
56 //=================================================================================================
57 
58 //*************************************************************************************************
65 template< typename Type // Data type of the matrix
66  , bool SO > // Storage order
67 class Rand< CompressedMatrix<Type,SO> >
68 {
69  public:
70  //**Generate functions**************************************************************************
73  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n ) const;
74  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros ) const;
75 
76  template< typename Arg >
77  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
78 
79  template< typename Arg >
80  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros,
81  const Arg& min, const Arg& max ) const;
83  //**********************************************************************************************
84 
85  //**Randomize functions*************************************************************************
88  inline void randomize( CompressedMatrix<Type,SO>& matrix ) const;
89  inline void randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros ) const;
90 
91  template< typename Arg >
92  inline void randomize( CompressedMatrix<Type,SO>& matrix, const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline void randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros,
96  const Arg& min, const Arg& max ) const;
98  //**********************************************************************************************
99 };
101 //*************************************************************************************************
102 
103 
104 //*************************************************************************************************
112 template< typename Type // Data type of the matrix
113  , bool SO > // Storage order
114 inline const CompressedMatrix<Type,SO>
115  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n ) const
116 {
117  CompressedMatrix<Type,SO> matrix( m, n );
118  randomize( matrix );
119 
120  return matrix;
121 }
123 //*************************************************************************************************
124 
125 
126 //*************************************************************************************************
136 template< typename Type // Data type of the matrix
137  , bool SO > // Storage order
138 inline const CompressedMatrix<Type,SO>
139  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros ) const
140 {
141  if( nonzeros > m*n )
142  throw std::invalid_argument( "Invalid number of non-zero elements" );
143 
144  CompressedMatrix<Type,SO> matrix( m, n );
145  randomize( matrix, nonzeros );
146 
147  return matrix;
148 }
150 //*************************************************************************************************
151 
152 
153 //*************************************************************************************************
163 template< typename Type // Data type of the matrix
164  , bool SO > // Storage order
165 template< typename Arg > // Min/max argument type
166 inline const CompressedMatrix<Type,SO>
167  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
168 {
169  CompressedMatrix<Type,SO> matrix( m, n );
170  randomize( matrix, min, max );
171 
172  return matrix;
173 }
175 //*************************************************************************************************
176 
177 
178 //*************************************************************************************************
190 template< typename Type // Data type of the matrix
191  , bool SO > // Storage order
192 template< typename Arg > // Min/max argument type
193 inline const CompressedMatrix<Type,SO>
194  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros,
195  const Arg& min, const Arg& max ) const
196 {
197  if( nonzeros > m*n )
198  throw std::invalid_argument( "Invalid number of non-zero elements" );
199 
200  CompressedMatrix<Type,SO> matrix( m, n );
201  randomize( matrix, nonzeros, min, max );
202 
203  return matrix;
204 }
206 //*************************************************************************************************
207 
208 
209 //*************************************************************************************************
216 template< typename Type // Data type of the matrix
217  , bool SO > // Storage order
218 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix ) const
219 {
220  const size_t m( matrix.rows() );
221  const size_t n( matrix.columns() );
222 
223  if( m == 0UL || n == 0UL ) return;
224 
225  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
226 
227  matrix.reset();
228  matrix.reserve( nonzeros );
229 
230  while( matrix.nonZeros() < nonzeros ) {
231  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
232  }
233 }
235 //*************************************************************************************************
236 
237 
238 //*************************************************************************************************
247 template< typename Type // Data type of the matrix
248  , bool SO > // Storage order
249 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros ) const
250 {
251  const size_t m( matrix.rows() );
252  const size_t n( matrix.columns() );
253 
254  if( nonzeros > m*n )
255  throw std::invalid_argument( "Invalid number of non-zero elements" );
256 
257  if( m == 0UL || n == 0UL ) return;
258 
259  matrix.reset();
260  matrix.reserve( nonzeros );
261 
262  while( matrix.nonZeros() < nonzeros ) {
263  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
264  }
265 }
267 //*************************************************************************************************
268 
269 
270 //*************************************************************************************************
279 template< typename Type // Data type of the matrix
280  , bool SO > // Storage order
281 template< typename Arg > // Min/max argument type
282 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
283  const Arg& min, const Arg& max ) const
284 {
285  const size_t m( matrix.rows() );
286  const size_t n( matrix.columns() );
287 
288  if( m == 0UL || n == 0UL ) return;
289 
290  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
291 
292  matrix.reset();
293  matrix.reserve( nonzeros );
294 
295  while( matrix.nonZeros() < nonzeros ) {
296  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
297  }
298 }
300 //*************************************************************************************************
301 
302 
303 //*************************************************************************************************
314 template< typename Type // Data type of the matrix
315  , bool SO > // Storage order
316 template< typename Arg > // Min/max argument type
317 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
318  size_t nonzeros, const Arg& min, const Arg& max ) const
319 {
320  const size_t m( matrix.rows() );
321  const size_t n( matrix.columns() );
322 
323  if( nonzeros > m*n )
324  throw std::invalid_argument( "Invalid number of non-zero elements" );
325 
326  if( m == 0UL || n == 0UL ) return;
327 
328  matrix.reset();
329  matrix.reserve( nonzeros );
330 
331  while( matrix.nonZeros() < nonzeros ) {
332  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
333  }
334 }
336 //*************************************************************************************************
337 
338 
339 
340 
341 //=================================================================================================
342 //
343 // TYPE DEFINITIONS
344 //
345 //=================================================================================================
346 
347 //*************************************************************************************************
352 //*************************************************************************************************
353 
354 
355 //*************************************************************************************************
360 //*************************************************************************************************
361 
362 
363 //*************************************************************************************************
368 //*************************************************************************************************
369 
370 } // namespace blaze
371 
372 #endif
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:199
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:367
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 the SparseMatrix CRTP base class.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:262
CompressedMatrix< float, false > CMatMxNf
MxN single precision matrix.
Definition: CompressedMatrix.h:351
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:359