Blaze 3.9
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
49#include <blaze/util/Indices.h>
50#include <blaze/util/Random.h>
51
52
53namespace blaze {
54
55//=================================================================================================
56//
57// RAND SPECIALIZATION
58//
59//=================================================================================================
60
61//*************************************************************************************************
68template< typename Type // Data type of the vector
69 , bool TF // Transpose flag
70 , typename Tag > // Type tag
71class Rand< CompressedVector<Type,TF,Tag> >
72{
73 public:
74 //**********************************************************************************************
80 inline const CompressedVector<Type,TF,Tag> generate( size_t size ) const
81 {
82 CompressedVector<Type,TF,Tag> vector( size );
83 randomize( vector );
84
85 return vector;
86 }
87 //**********************************************************************************************
88
89 //**********************************************************************************************
97 inline const CompressedVector<Type,TF,Tag> generate( size_t size, size_t nonzeros ) const
98 {
99 if( nonzeros > size ) {
100 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
101 }
102
103 CompressedVector<Type,TF,Tag> vector( size, nonzeros );
104 randomize( vector, nonzeros );
105
106 return vector;
107 }
108 //**********************************************************************************************
109
110 //**********************************************************************************************
118 template< typename Arg > // Min/max argument type
119 inline const CompressedVector<Type,TF,Tag>
120 generate( size_t size, const Arg& min, const Arg& max ) const
121 {
122 CompressedVector<Type,TF,Tag> vector( size );
123 randomize( vector, min, max );
124
125 return vector;
126 }
127 //**********************************************************************************************
128
129 //**********************************************************************************************
139 template< typename Arg > // Min/max argument type
140 inline const CompressedVector<Type,TF,Tag>
141 generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const
142 {
143 if( nonzeros > size ) {
144 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
145 }
146
147 CompressedVector<Type,TF,Tag> vector( size, nonzeros );
148 randomize( vector, nonzeros, min, max );
149
150 return vector;
151 }
152 //**********************************************************************************************
153
154 //**********************************************************************************************
160 inline void randomize( CompressedVector<Type,TF,Tag>& vector ) const
161 {
162 const size_t size( vector.size() );
163
164 if( size == 0UL ) return;
165
166 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
167
168 randomize( vector, nonzeros );
169 }
170 //**********************************************************************************************
171
172 //**********************************************************************************************
180 inline void randomize( CompressedVector<Type,TF,Tag>& vector, size_t nonzeros ) const
181 {
182 const size_t size( vector.size() );
183
184 if( nonzeros > size ) {
185 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
186 }
187
188 if( size == 0UL ) return;
189
190 vector.reset();
191 vector.reserve( nonzeros );
192
193 const Indices<size_t> indices( 0UL, vector.size()-1UL, nonzeros );
194
195 for( size_t index : indices ) {
196 vector.append( index, rand<Type>() );
197 }
198 }
199 //**********************************************************************************************
200
201 //**********************************************************************************************
209 template< typename Arg > // Min/max argument type
210 inline void randomize( CompressedVector<Type,TF,Tag>& vector,
211 const Arg& min, const Arg& max ) const
212 {
213 const size_t size( vector.size() );
214
215 if( size == 0UL ) return;
216
217 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
218
219 randomize( vector, nonzeros, min, max );
220 }
221 //**********************************************************************************************
222
223 //**********************************************************************************************
233 template< typename Arg > // Min/max argument type
234 inline void randomize( CompressedVector<Type,TF,Tag>& vector,
235 size_t nonzeros, const Arg& min, const Arg& max ) const
236 {
237 const size_t size( vector.size() );
238
239 if( nonzeros > size ) {
240 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
241 }
242
243 if( size == 0UL ) return;
244
245 vector.reset();
246 vector.reserve( nonzeros );
247
248 const Indices<size_t> indices( 0UL, vector.size()-1UL, nonzeros );
249
250 for( size_t index : indices ) {
251 vector.append( index, rand<Type>( min, max ) );
252 }
253 }
254 //**********************************************************************************************
255};
257//*************************************************************************************************
258
259} // namespace blaze
260
261#endif
Header file for the complete CompressedMatrix implementation.
Header file for the Indices class.
Header file for all basic SparseVector functionality.
Header file for the complete ZeroVector implementation.
Header file for the implementation of a fixed-size vector.
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:1339
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:1375
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1380
decltype(auto) generate(size_t m, size_t n, OP op)
Generates a new dense matrix filled via the given custom binary operation.
Definition: DMatGenExpr.h:675
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:676
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:626
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
Header file for the exception macros of the math module.
Implementation of an arbitrarily sized compressed vector.
Implementation of a random number generator.