All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompressedMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_COMPRESSEDMATRIX_H_
23 #define _BLAZE_MATH_COMPRESSEDMATRIX_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
33 #include <blaze/system/Precision.h>
34 #include <blaze/util/Random.h>
35 
36 
37 namespace blaze {
38 
39 //=================================================================================================
40 //
41 // RAND SPECIALIZATION
42 //
43 //=================================================================================================
44 
45 //*************************************************************************************************
52 template< typename Type // Data type of the matrix
53  , bool SO > // Storage order
54 class Rand< CompressedMatrix<Type,SO> >
55 {
56  public:
57  //**Generate functions**************************************************************************
60  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n ) const;
61  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros ) const;
62 
63  template< typename Arg >
64  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
65 
66  template< typename Arg >
67  inline const CompressedMatrix<Type,SO> generate( size_t m, size_t n, size_t nonzeros,
68  const Arg& min, const Arg& max ) const;
70  //**********************************************************************************************
71 
72  //**Randomize functions*************************************************************************
75  inline void randomize( CompressedMatrix<Type,SO>& matrix ) const;
76  inline void randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros ) const;
77 
78  template< typename Arg >
79  inline void randomize( CompressedMatrix<Type,SO>& matrix, const Arg& min, const Arg& max ) const;
80 
81  template< typename Arg >
82  inline void randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros,
83  const Arg& min, const Arg& max ) const;
85  //**********************************************************************************************
86 };
88 //*************************************************************************************************
89 
90 
91 //*************************************************************************************************
99 template< typename Type // Data type of the matrix
100  , bool SO > // Storage order
101 inline const CompressedMatrix<Type,SO>
102  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n ) const
103 {
104  CompressedMatrix<Type,SO> matrix( m, n );
105  randomize( matrix );
106 
107  return matrix;
108 }
110 //*************************************************************************************************
111 
112 
113 //*************************************************************************************************
123 template< typename Type // Data type of the matrix
124  , bool SO > // Storage order
125 inline const CompressedMatrix<Type,SO>
126  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros ) const
127 {
128  if( nonzeros > m*n )
129  throw std::invalid_argument( "Invalid number of non-zero elements" );
130 
131  CompressedMatrix<Type,SO> matrix( m, n );
132  randomize( matrix, nonzeros );
133 
134  return matrix;
135 }
137 //*************************************************************************************************
138 
139 
140 //*************************************************************************************************
150 template< typename Type // Data type of the matrix
151  , bool SO > // Storage order
152 template< typename Arg > // Min/max argument type
153 inline const CompressedMatrix<Type,SO>
154  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
155 {
156  CompressedMatrix<Type,SO> matrix( m, n );
157  randomize( matrix, min, max );
158 
159  return matrix;
160 }
162 //*************************************************************************************************
163 
164 
165 //*************************************************************************************************
177 template< typename Type // Data type of the matrix
178  , bool SO > // Storage order
179 template< typename Arg > // Min/max argument type
180 inline const CompressedMatrix<Type,SO>
181  Rand< CompressedMatrix<Type,SO> >::generate( size_t m, size_t n, size_t nonzeros,
182  const Arg& min, const Arg& max ) const
183 {
184  if( nonzeros > m*n )
185  throw std::invalid_argument( "Invalid number of non-zero elements" );
186 
187  CompressedMatrix<Type,SO> matrix( m, n );
188  randomize( matrix, nonzeros, min, max );
189 
190  return matrix;
191 }
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
203 template< typename Type // Data type of the matrix
204  , bool SO > // Storage order
205 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix ) const
206 {
207  const size_t m( matrix.rows() );
208  const size_t n( matrix.columns() );
209 
210  if( m == 0UL || n == 0UL ) return;
211 
212  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
213 
214  matrix.reset();
215  matrix.reserve( nonzeros );
216 
217  while( matrix.nonZeros() < nonzeros ) {
218  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
219  }
220 }
222 //*************************************************************************************************
223 
224 
225 //*************************************************************************************************
234 template< typename Type // Data type of the matrix
235  , bool SO > // Storage order
236 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix, size_t nonzeros ) const
237 {
238  const size_t m( matrix.rows() );
239  const size_t n( matrix.columns() );
240 
241  if( nonzeros > m*n )
242  throw std::invalid_argument( "Invalid number of non-zero elements" );
243 
244  if( m == 0UL || n == 0UL ) return;
245 
246  matrix.reset();
247  matrix.reserve( nonzeros );
248 
249  while( matrix.nonZeros() < nonzeros ) {
250  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
251  }
252 }
254 //*************************************************************************************************
255 
256 
257 //*************************************************************************************************
266 template< typename Type // Data type of the matrix
267  , bool SO > // Storage order
268 template< typename Arg > // Min/max argument type
269 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
270  const Arg& min, const Arg& max ) const
271 {
272  const size_t m( matrix.rows() );
273  const size_t n( matrix.columns() );
274 
275  if( m == 0UL || n == 0UL ) return;
276 
277  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
278 
279  matrix.reset();
280  matrix.reserve( nonzeros );
281 
282  while( matrix.nonZeros() < nonzeros ) {
283  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
284  }
285 }
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
301 template< typename Type // Data type of the matrix
302  , bool SO > // Storage order
303 template< typename Arg > // Min/max argument type
304 inline void Rand< CompressedMatrix<Type,SO> >::randomize( CompressedMatrix<Type,SO>& matrix,
305  size_t nonzeros, const Arg& min, const Arg& max ) const
306 {
307  const size_t m( matrix.rows() );
308  const size_t n( matrix.columns() );
309 
310  if( nonzeros > m*n )
311  throw std::invalid_argument( "Invalid number of non-zero elements" );
312 
313  if( m == 0UL || n == 0UL ) return;
314 
315  matrix.reset();
316  matrix.reserve( nonzeros );
317 
318  while( matrix.nonZeros() < nonzeros ) {
319  matrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>( min, max );
320  }
321 }
323 //*************************************************************************************************
324 
325 
326 
327 
328 //=================================================================================================
329 //
330 // TYPE DEFINITIONS
331 //
332 //=================================================================================================
333 
334 //*************************************************************************************************
339 //*************************************************************************************************
340 
341 
342 //*************************************************************************************************
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
355 //*************************************************************************************************
356 
357 } // namespace blaze
358 
359 #endif