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 
45 #include <blaze/math/Exception.h>
48 #include <blaze/util/Indices.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  randomize( vector, nonzeros );
224 }
226 //*************************************************************************************************
227 
228 
229 //*************************************************************************************************
238 template< typename Type // Data type of the vector
239  , bool TF > // Transpose flag
240 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const
241 {
242  const size_t size( vector.size() );
243 
244  if( nonzeros > size ) {
245  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
246  }
247 
248  if( size == 0UL ) return;
249 
250  vector.reset();
251  vector.reserve( nonzeros );
252 
253  const Indices indices( 0UL, vector.size()-1UL, nonzeros );
254 
255  for( size_t index : indices ) {
256  vector.append( index, rand<Type>() );
257  }
258 }
260 //*************************************************************************************************
261 
262 
263 //*************************************************************************************************
272 template< typename Type // Data type of the vector
273  , bool TF > // Transpose flag
274 template< typename Arg > // Min/max argument type
275 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
276  const Arg& min, const Arg& max ) const
277 {
278  const size_t size( vector.size() );
279 
280  if( size == 0UL ) return;
281 
282  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
283 
284  randomize( vector, nonzeros, min, max );
285 }
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
301 template< typename Type // Data type of the vector
302  , bool TF > // Transpose flag
303 template< typename Arg > // Min/max argument type
304 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
305  size_t nonzeros, const Arg& min, const Arg& max ) const
306 {
307  const size_t size( vector.size() );
308 
309  if( nonzeros > size ) {
310  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
311  }
312 
313  if( size == 0UL ) return;
314 
315  vector.reset();
316  vector.reserve( nonzeros );
317 
318  const Indices indices( 0UL, vector.size()-1UL, nonzeros );
319 
320  for( size_t index : indices ) {
321  vector.append( index, rand<Type>( min, max ) );
322  }
323 }
325 //*************************************************************************************************
326 
327 } // namespace blaze
328 
329 #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 exception.This macro encapsulates the default way o...
Definition: Exception.h:235
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1234
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
Implementation of a random number generator.
Implementation of an arbitrarily sized compressed vector.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:290
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the exception macros of the math module.
Header file for the Indices class.
T generate() const
Generation of a random value in the range .
Definition: Random.h:250
Header file for the complete CompressedMatrix implementation.
Header file for all basic SparseVector functionality.