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/Exception.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 vector
68  , bool TF > // Transpose flag
69 class Rand< CompressedVector<Type,TF> >
70 {
71  public:
72  //**Generate functions**************************************************************************
75  inline const CompressedVector<Type,TF> generate( size_t size ) const;
76  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros ) const;
77 
78  template< typename Arg >
79  inline const CompressedVector<Type,TF> generate( size_t size, const Arg& min, const Arg& max ) const;
80 
81  template< typename Arg >
82  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const;
84  //**********************************************************************************************
85 
86  //**Randomize functions*************************************************************************
89  inline void randomize( CompressedVector<Type,TF>& vector ) const;
90  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const;
91 
92  template< typename Arg >
93  inline void randomize( CompressedVector<Type,TF>& vector, const Arg& min, const Arg& max ) const;
94 
95  template< typename Arg >
96  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros, const Arg& min, const Arg& max ) const;
98  //**********************************************************************************************
99 };
101 //*************************************************************************************************
102 
103 
104 //*************************************************************************************************
111 template< typename Type // Data type of the vector
112  , bool TF > // Transpose flag
113 inline const CompressedVector<Type,TF>
114  Rand< CompressedVector<Type,TF> >::generate( size_t size ) const
115 {
116  CompressedVector<Type,TF> vector( size );
117  randomize( vector );
118 
119  return vector;
120 }
122 //*************************************************************************************************
123 
124 
125 //*************************************************************************************************
134 template< typename Type // Data type of the vector
135  , bool TF > // Transpose flag
136 inline const CompressedVector<Type,TF>
137  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros ) const
138 {
139  if( nonzeros > size ) {
140  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
141  }
142 
143  CompressedVector<Type,TF> vector( size, nonzeros );
144  randomize( vector, nonzeros );
145 
146  return vector;
147 }
149 //*************************************************************************************************
150 
151 
152 //*************************************************************************************************
161 template< typename Type // Data type of the vector
162  , bool TF > // Transpose flag
163 template< typename Arg > // Min/max argument type
164 inline const CompressedVector<Type,TF>
165  Rand< CompressedVector<Type,TF> >::generate( size_t size, const Arg& min, const Arg& max ) const
166 {
167  CompressedVector<Type,TF> vector( size );
168  randomize( vector, min, max );
169 
170  return vector;
171 }
173 //*************************************************************************************************
174 
175 
176 //*************************************************************************************************
187 template< typename Type // Data type of the vector
188  , bool TF > // Transpose flag
189 template< typename Arg > // Min/max argument type
190 inline const CompressedVector<Type,TF>
191  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const
192 {
193  if( nonzeros > size ) {
194  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
195  }
196 
197  CompressedVector<Type,TF> vector( size, nonzeros );
198  randomize( vector, nonzeros, min, max );
199 
200  return vector;
201 }
203 //*************************************************************************************************
204 
205 
206 //*************************************************************************************************
213 template< typename Type // Data type of the vector
214  , bool TF > // Transpose flag
215 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector ) const
216 {
217  const size_t size( vector.size() );
218 
219  if( size == 0UL ) return;
220 
221  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
222 
223  vector.reset();
224  vector.reserve( nonzeros );
225 
226  while( vector.nonZeros() < nonzeros ) {
227  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
228  }
229 }
231 //*************************************************************************************************
232 
233 
234 //*************************************************************************************************
243 template< typename Type // Data type of the vector
244  , bool TF > // Transpose flag
245 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const
246 {
247  const size_t size( vector.size() );
248 
249  if( nonzeros > size ) {
250  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
251  }
252 
253  if( size == 0UL ) return;
254 
255  vector.reset();
256  vector.reserve( nonzeros );
257 
258  while( vector.nonZeros() < nonzeros ) {
259  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
260  }
261 }
263 //*************************************************************************************************
264 
265 
266 //*************************************************************************************************
275 template< typename Type // Data type of the vector
276  , bool TF > // Transpose flag
277 template< typename Arg > // Min/max argument type
278 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
279  const Arg& min, const Arg& max ) const
280 {
281  const size_t size( vector.size() );
282 
283  if( size == 0UL ) return;
284 
285  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
286 
287  vector.reset();
288  vector.reserve( nonzeros );
289 
290  while( vector.nonZeros() < nonzeros ) {
291  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>( min, max );
292  }
293 }
295 //*************************************************************************************************
296 
297 
298 //*************************************************************************************************
309 template< typename Type // Data type of the vector
310  , bool TF > // Transpose flag
311 template< typename Arg > // Min/max argument type
312 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
313  size_t nonzeros, const Arg& min, const Arg& max ) const
314 {
315  const size_t size( vector.size() );
316 
317  if( nonzeros > size ) {
318  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
319  }
320 
321  if( size == 0UL ) return;
322 
323  vector.reset();
324  vector.reserve( nonzeros );
325 
326  while( vector.nonZeros() < nonzeros ) {
327  vector[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>( min, max );
328  }
329 }
331 //*************************************************************************************************
332 
333 
334 
335 
336 //=================================================================================================
337 //
338 // TYPE DEFINITIONS
339 //
340 //=================================================================================================
341 
342 //*************************************************************************************************
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
355 //*************************************************************************************************
356 
357 
358 //*************************************************************************************************
363 //*************************************************************************************************
364 
365 } // namespace blaze
366 
367 #endif
Header file for the implementation of a fixed-size vector.
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exceptionThis macro encapsulates the default way of...
Definition: Exception.h:187
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:252
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1041
CompressedVector< float, false > CVecNf
Compressed single precision vector.
Definition: CompressedVector.h:346
Implementation of a random number generator.
Implementation of an arbitrarily sized compressed vector.
CompressedVector< real_t, false > CVecN
Compressed vector with system-specific precision.
Definition: CompressedVector.h:362
Header file for the floating point precision of the Blaze library.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:260
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
CompressedVector< double, false > CVecNd
Compressed double precision vector.
Definition: CompressedVector.h:354
T generate() const
Generation of a random value in the range .
Definition: Random.h:220
Header file for the complete CompressedMatrix implementation.
Header file for all basic SparseVector functionality.
Header file for exception macros.
Efficient implementation of an arbitrary sized sparse vector.The CompressedVector class is the repres...
Definition: CompressedVector.h:187