Blaze  3.6
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/math/ZeroVector.h>
49 #include <blaze/util/Indices.h>
50 #include <blaze/util/Random.h>
51 
52 
53 namespace blaze {
54 
55 //=================================================================================================
56 //
57 // RAND SPECIALIZATION
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
68 template< typename Type // Data type of the vector
69  , bool TF > // Transpose flag
70 class Rand< CompressedVector<Type,TF> >
71 {
72  public:
73  //**Generate functions**************************************************************************
76  inline const CompressedVector<Type,TF> generate( size_t size ) const;
77  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros ) const;
78 
79  template< typename Arg >
80  inline const CompressedVector<Type,TF> generate( size_t size, const Arg& min, const Arg& max ) const;
81 
82  template< typename Arg >
83  inline const CompressedVector<Type,TF> generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const;
85  //**********************************************************************************************
86 
87  //**Randomize functions*************************************************************************
90  inline void randomize( CompressedVector<Type,TF>& vector ) const;
91  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const;
92 
93  template< typename Arg >
94  inline void randomize( CompressedVector<Type,TF>& vector, const Arg& min, const Arg& max ) const;
95 
96  template< typename Arg >
97  inline void randomize( CompressedVector<Type,TF>& vector, size_t nonzeros, const Arg& min, const Arg& max ) const;
99  //**********************************************************************************************
100 };
102 //*************************************************************************************************
103 
104 
105 //*************************************************************************************************
112 template< typename Type // Data type of the vector
113  , bool TF > // Transpose flag
114 inline const CompressedVector<Type,TF>
115  Rand< CompressedVector<Type,TF> >::generate( size_t size ) const
116 {
117  CompressedVector<Type,TF> vector( size );
118  randomize( vector );
119 
120  return vector;
121 }
123 //*************************************************************************************************
124 
125 
126 //*************************************************************************************************
135 template< typename Type // Data type of the vector
136  , bool TF > // Transpose flag
137 inline const CompressedVector<Type,TF>
138  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros ) const
139 {
140  if( nonzeros > size ) {
141  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
142  }
143 
144  CompressedVector<Type,TF> vector( size, nonzeros );
145  randomize( vector, nonzeros );
146 
147  return vector;
148 }
150 //*************************************************************************************************
151 
152 
153 //*************************************************************************************************
162 template< typename Type // Data type of the vector
163  , bool TF > // Transpose flag
164 template< typename Arg > // Min/max argument type
165 inline const CompressedVector<Type,TF>
166  Rand< CompressedVector<Type,TF> >::generate( size_t size, const Arg& min, const Arg& max ) const
167 {
168  CompressedVector<Type,TF> vector( size );
169  randomize( vector, min, max );
170 
171  return vector;
172 }
174 //*************************************************************************************************
175 
176 
177 //*************************************************************************************************
188 template< typename Type // Data type of the vector
189  , bool TF > // Transpose flag
190 template< typename Arg > // Min/max argument type
191 inline const CompressedVector<Type,TF>
192  Rand< CompressedVector<Type,TF> >::generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const
193 {
194  if( nonzeros > size ) {
195  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
196  }
197 
198  CompressedVector<Type,TF> vector( size, nonzeros );
199  randomize( vector, nonzeros, min, max );
200 
201  return vector;
202 }
204 //*************************************************************************************************
205 
206 
207 //*************************************************************************************************
214 template< typename Type // Data type of the vector
215  , bool TF > // Transpose flag
216 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector ) const
217 {
218  const size_t size( vector.size() );
219 
220  if( size == 0UL ) return;
221 
222  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
223 
224  randomize( vector, nonzeros );
225 }
227 //*************************************************************************************************
228 
229 
230 //*************************************************************************************************
239 template< typename Type // Data type of the vector
240  , bool TF > // Transpose flag
241 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector, size_t nonzeros ) const
242 {
243  const size_t size( vector.size() );
244 
245  if( nonzeros > size ) {
246  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
247  }
248 
249  if( size == 0UL ) return;
250 
251  vector.reset();
252  vector.reserve( nonzeros );
253 
254  const Indices indices( 0UL, vector.size()-1UL, nonzeros );
255 
256  for( size_t index : indices ) {
257  vector.append( index, rand<Type>() );
258  }
259 }
261 //*************************************************************************************************
262 
263 
264 //*************************************************************************************************
273 template< typename Type // Data type of the vector
274  , bool TF > // Transpose flag
275 template< typename Arg > // Min/max argument type
276 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
277  const Arg& min, const Arg& max ) const
278 {
279  const size_t size( vector.size() );
280 
281  if( size == 0UL ) return;
282 
283  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
284 
285  randomize( vector, nonzeros, min, max );
286 }
288 //*************************************************************************************************
289 
290 
291 //*************************************************************************************************
302 template< typename Type // Data type of the vector
303  , bool TF > // Transpose flag
304 template< typename Arg > // Min/max argument type
305 inline void Rand< CompressedVector<Type,TF> >::randomize( CompressedVector<Type,TF>& vector,
306  size_t nonzeros, const Arg& min, const Arg& max ) const
307 {
308  const size_t size( vector.size() );
309 
310  if( nonzeros > size ) {
311  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
312  }
313 
314  if( size == 0UL ) return;
315 
316  vector.reset();
317  vector.reserve( nonzeros );
318 
319  const Indices indices( 0UL, vector.size()-1UL, nonzeros );
320 
321  for( size_t index : indices ) {
322  vector.append( index, rand<Type>( min, max ) );
323  }
324 }
326 //*************************************************************************************************
327 
328 } // namespace blaze
329 
330 #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
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1240
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
Implementation of a random number generator.
Implementation of an arbitrarily sized compressed vector.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1162
Header file for the complete ZeroVector implementation.
Header file for the exception macros of the math module.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1198
Header file for the Indices class.
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
T generate() const
Generation of a random value in the range .
Definition: Random.h:252
Header file for the complete CompressedMatrix implementation.
Header file for all basic SparseVector functionality.