All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompressedVector.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_COMPRESSEDVECTOR_H_
23 #define _BLAZE_MATH_COMPRESSEDVECTOR_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 vector
53  , bool TF > // Transpose flag
54 class Rand< CompressedVector<Type,TF> >
55 {
56  public:
57  //**Generate functions**************************************************************************
60  inline const CompressedVector<Type,TF> generate( size_t size ) const;
61  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros ) const;
62 
63  template< typename Arg >
64  inline const CompressedVector<Type,TF> generate( size_t size, const Arg& min, const Arg& max ) const;
65 
66  template< typename Arg >
67  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const;
69  //**********************************************************************************************
70 
71  //**Randomize functions*************************************************************************
74  inline void randomize( CompressedVector<Type,TF>& vector ) const;
75  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const;
76 
77  template< typename Arg >
78  inline void randomize( CompressedVector<Type,TF>& vector, const Arg& min, const Arg& max ) const;
79 
80  template< typename Arg >
81  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros, const Arg& min, const Arg& max ) const;
83  //**********************************************************************************************
84 };
86 //*************************************************************************************************
87 
88 
89 //*************************************************************************************************
96 template< typename Type // Data type of the vector
97  , bool TF > // Transpose flag
98 inline const CompressedVector<Type,TF>
99  Rand< CompressedVector<Type,TF> >::generate( size_t size ) const
100 {
101  CompressedVector<Type,TF> vector( size );
102  randomize( vector );
103 
104  return vector;
105 }
107 //*************************************************************************************************
108 
109 
110 //*************************************************************************************************
119 template< typename Type // Data type of the vector
120  , bool TF > // Transpose flag
121 inline const CompressedVector<Type,TF>
122  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros ) const
123 {
124  if( nonzeros > size )
125  throw std::invalid_argument( "Invalid number of non-zero elements" );
126 
127  CompressedVector<Type,TF> vector( size, nonzeros );
128  randomize( vector, nonzeros );
129 
130  return vector;
131 }
133 //*************************************************************************************************
134 
135 
136 //*************************************************************************************************
145 template< typename Type // Data type of the vector
146  , bool TF > // Transpose flag
147 template< typename Arg > // Min/max argument type
148 inline const CompressedVector<Type,TF>
149  Rand< CompressedVector<Type,TF> >::generate( size_t size, const Arg& min, const Arg& max ) const
150 {
151  CompressedVector<Type,TF> vector( size );
152  randomize( vector, min, max );
153 
154  return vector;
155 }
157 //*************************************************************************************************
158 
159 
160 //*************************************************************************************************
171 template< typename Type // Data type of the vector
172  , bool TF > // Transpose flag
173 template< typename Arg > // Min/max argument type
174 inline const CompressedVector<Type,TF>
175  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const
176 {
177  if( nonzeros > size )
178  throw std::invalid_argument( "Invalid number of non-zero elements" );
179 
180  CompressedVector<Type,TF> vector( size, nonzeros );
181  randomize( vector, nonzeros, min, max );
182 
183  return vector;
184 }
186 //*************************************************************************************************
187 
188 
189 //*************************************************************************************************
196 template< typename Type // Data type of the vector
197  , bool TF > // Transpose flag
198 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector ) const
199 {
200  const size_t size( vector.size() );
201 
202  if( size == 0UL ) return;
203 
204  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
205 
206  vector.reset();
207  vector.reserve( nonzeros );
208 
209  while( vector.nonZeros() < nonzeros ) {
210  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
211  }
212 }
214 //*************************************************************************************************
215 
216 
217 //*************************************************************************************************
226 template< typename Type // Data type of the vector
227  , bool TF > // Transpose flag
228 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const
229 {
230  const size_t size( vector.size() );
231 
232  if( nonzeros > size )
233  throw std::invalid_argument( "Invalid number of non-zero elements" );
234 
235  if( size == 0UL ) return;
236 
237  vector.reset();
238  vector.reserve( nonzeros );
239 
240  while( vector.nonZeros() < nonzeros ) {
241  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
242  }
243 }
245 //*************************************************************************************************
246 
247 
248 //*************************************************************************************************
257 template< typename Type // Data type of the vector
258  , bool TF > // Transpose flag
259 template< typename Arg > // Min/max argument type
260 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
261  const Arg& min, const Arg& max ) const
262 {
263  const size_t size( vector.size() );
264 
265  if( size == 0UL ) return;
266 
267  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
268 
269  vector.reset();
270  vector.reserve( nonzeros );
271 
272  while( vector.nonZeros() < nonzeros ) {
273  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>( min, max );
274  }
275 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
291 template< typename Type // Data type of the vector
292  , bool TF > // Transpose flag
293 template< typename Arg > // Min/max argument type
294 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
295  size_t nonzeros, const Arg& min, const Arg& max ) const
296 {
297  const size_t size( vector.size() );
298 
299  if( nonzeros > size )
300  throw std::invalid_argument( "Invalid number of non-zero elements" );
301 
302  if( size == 0UL ) return;
303 
304  vector.reset();
305  vector.reserve( nonzeros );
306 
307  while( vector.nonZeros() < nonzeros ) {
308  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>( min, max );
309  }
310 }
312 //*************************************************************************************************
313 
314 
315 
316 
317 //=================================================================================================
318 //
319 // TYPE DEFINITIONS
320 //
321 //=================================================================================================
322 
323 //*************************************************************************************************
328 //*************************************************************************************************
329 
330 
331 //*************************************************************************************************
336 //*************************************************************************************************
337 
338 
339 //*************************************************************************************************
344 //*************************************************************************************************
345 
346 } // namespace blaze
347 
348 #endif