All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompressedVector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_COMPRESSEDVECTOR_H_
36 #define _BLAZE_MATH_COMPRESSEDVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
47 #include <blaze/system/Precision.h>
48 #include <blaze/util/Random.h>
49 
50 
51 namespace blaze {
52 
53 //=================================================================================================
54 //
55 // RAND SPECIALIZATION
56 //
57 //=================================================================================================
58 
59 //*************************************************************************************************
66 template< typename Type // Data type of the vector
67  , bool TF > // Transpose flag
68 class Rand< CompressedVector<Type,TF> >
69 {
70  public:
71  //**Generate functions**************************************************************************
74  inline const CompressedVector<Type,TF> generate( size_t size ) const;
75  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros ) const;
76 
77  template< typename Arg >
78  inline const CompressedVector<Type,TF> generate( size_t size, const Arg& min, const Arg& max ) const;
79 
80  template< typename Arg >
81  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const;
83  //**********************************************************************************************
84 
85  //**Randomize functions*************************************************************************
88  inline void randomize( CompressedVector<Type,TF>& vector ) const;
89  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const;
90 
91  template< typename Arg >
92  inline void randomize( CompressedVector<Type,TF>& vector, const Arg& min, const Arg& max ) const;
93 
94  template< typename Arg >
95  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros, const Arg& min, const Arg& max ) const;
97  //**********************************************************************************************
98 };
100 //*************************************************************************************************
101 
102 
103 //*************************************************************************************************
110 template< typename Type // Data type of the vector
111  , bool TF > // Transpose flag
112 inline const CompressedVector<Type,TF>
113  Rand< CompressedVector<Type,TF> >::generate( size_t size ) const
114 {
115  CompressedVector<Type,TF> vector( size );
116  randomize( vector );
117 
118  return vector;
119 }
121 //*************************************************************************************************
122 
123 
124 //*************************************************************************************************
133 template< typename Type // Data type of the vector
134  , bool TF > // Transpose flag
135 inline const CompressedVector<Type,TF>
136  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros ) const
137 {
138  if( nonzeros > size )
139  throw std::invalid_argument( "Invalid number of non-zero elements" );
140 
141  CompressedVector<Type,TF> vector( size, nonzeros );
142  randomize( vector, nonzeros );
143 
144  return vector;
145 }
147 //*************************************************************************************************
148 
149 
150 //*************************************************************************************************
159 template< typename Type // Data type of the vector
160  , bool TF > // Transpose flag
161 template< typename Arg > // Min/max argument type
162 inline const CompressedVector<Type,TF>
163  Rand< CompressedVector<Type,TF> >::generate( size_t size, const Arg& min, const Arg& max ) const
164 {
165  CompressedVector<Type,TF> vector( size );
166  randomize( vector, min, max );
167 
168  return vector;
169 }
171 //*************************************************************************************************
172 
173 
174 //*************************************************************************************************
185 template< typename Type // Data type of the vector
186  , bool TF > // Transpose flag
187 template< typename Arg > // Min/max argument type
188 inline const CompressedVector<Type,TF>
189  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const
190 {
191  if( nonzeros > size )
192  throw std::invalid_argument( "Invalid number of non-zero elements" );
193 
194  CompressedVector<Type,TF> vector( size, nonzeros );
195  randomize( vector, nonzeros, min, max );
196 
197  return vector;
198 }
200 //*************************************************************************************************
201 
202 
203 //*************************************************************************************************
210 template< typename Type // Data type of the vector
211  , bool TF > // Transpose flag
212 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector ) const
213 {
214  const size_t size( vector.size() );
215 
216  if( size == 0UL ) return;
217 
218  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
219 
220  vector.reset();
221  vector.reserve( nonzeros );
222 
223  while( vector.nonZeros() < nonzeros ) {
224  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
225  }
226 }
228 //*************************************************************************************************
229 
230 
231 //*************************************************************************************************
240 template< typename Type // Data type of the vector
241  , bool TF > // Transpose flag
242 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const
243 {
244  const size_t size( vector.size() );
245 
246  if( nonzeros > size )
247  throw std::invalid_argument( "Invalid number of non-zero elements" );
248 
249  if( size == 0UL ) return;
250 
251  vector.reset();
252  vector.reserve( nonzeros );
253 
254  while( vector.nonZeros() < nonzeros ) {
255  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
256  }
257 }
259 //*************************************************************************************************
260 
261 
262 //*************************************************************************************************
271 template< typename Type // Data type of the vector
272  , bool TF > // Transpose flag
273 template< typename Arg > // Min/max argument type
274 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
275  const Arg& min, const Arg& max ) const
276 {
277  const size_t size( vector.size() );
278 
279  if( size == 0UL ) return;
280 
281  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
282 
283  vector.reset();
284  vector.reserve( nonzeros );
285 
286  while( vector.nonZeros() < nonzeros ) {
287  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>( min, max );
288  }
289 }
291 //*************************************************************************************************
292 
293 
294 //*************************************************************************************************
305 template< typename Type // Data type of the vector
306  , bool TF > // Transpose flag
307 template< typename Arg > // Min/max argument type
308 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
309  size_t nonzeros, const Arg& min, const Arg& max ) const
310 {
311  const size_t size( vector.size() );
312 
313  if( nonzeros > size )
314  throw std::invalid_argument( "Invalid number of non-zero elements" );
315 
316  if( size == 0UL ) return;
317 
318  vector.reset();
319  vector.reserve( nonzeros );
320 
321  while( vector.nonZeros() < nonzeros ) {
322  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>( min, max );
323  }
324 }
326 //*************************************************************************************************
327 
328 
329 
330 
331 //=================================================================================================
332 //
333 // TYPE DEFINITIONS
334 //
335 //=================================================================================================
336 
337 //*************************************************************************************************
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
350 //*************************************************************************************************
351 
352 
353 //*************************************************************************************************
358 //*************************************************************************************************
359 
360 } // namespace blaze
361 
362 #endif
Header file for the implementation of a fixed-size vector.
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1043
CompressedVector< float, false > CVecNf
Compressed single precision vector.
Definition: CompressedVector.h:341
Implementation of a random number generator.
Implementation of an arbitrarily sized compressed vector.
Header file for the floating point precision of the Blaze library.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:262
CompressedVector< real, false > CVecN
Compressed vector with system-specific precision.
Definition: CompressedVector.h:357
CompressedVector< double, false > CVecNd
Compressed double precision vector.
Definition: CompressedVector.h:349
T generate() const
Generation of a random value in the range .
Definition: Random.h:222
Header file for the complete CompressedMatrix implementation.
Header file for all basic SparseVector functionality.
Efficient implementation of an arbitrary sized sparse vector.The CompressedVector class is the repres...
Definition: CompressedVector.h:180