Blaze  3.6
Subvector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SUBVECTOR_H_
36 #define _BLAZE_MATH_SUBVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
52 #include <blaze/util/Random.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // RAND SPECIALIZATION FOR DENSE SUBVECTORS
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename VT // Type of the vector
72  , AlignmentFlag AF // Alignment flag
73  , bool TF // Transpose flag
74  , size_t... CSAs > // Compile time subvector arguments
75 class Rand< Subvector<VT,AF,TF,true,CSAs...> >
76 {
77  public:
78  //**Randomize functions*************************************************************************
81  template< typename SVT >
82  inline void randomize( SVT&& subvector ) const;
83 
84  template< typename SVT, typename Arg >
85  inline void randomize( SVT&& subvector, const Arg& min, const Arg& max ) const;
87  //**********************************************************************************************
88 };
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
100 template< typename VT // Type of the vector
101  , AlignmentFlag AF // Alignment flag
102  , bool TF // Transpose flag
103  , size_t... CSAs > // Compile time subvector arguments
104 template< typename SVT > // Type of the subvector
105 inline void Rand< Subvector<VT,AF,TF,true,CSAs...> >::randomize( SVT&& subvector ) const
106 {
107  using blaze::randomize;
108 
109  using SubvectorType = RemoveReference_t<SVT>;
110 
113 
114  for( size_t i=0UL; i<subvector.size(); ++i ) {
115  randomize( subvector[i] );
116  }
117 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
131 template< typename VT // Type of the vector
132  , AlignmentFlag AF // Alignment flag
133  , bool TF // Transpose flag
134  , size_t... CSAs > // Compile time subvector arguments
135 template< typename SVT // Type of the subvector
136  , typename Arg > // Min/max argument type
137 inline void Rand< Subvector<VT,AF,TF,true,CSAs...> >::randomize( SVT&& subvector, const Arg& min, const Arg& max ) const
138 {
139  using blaze::randomize;
140 
141  using SubvectorType = RemoveReference_t<SVT>;
142 
145 
146  for( size_t i=0UL; i<subvector.size(); ++i ) {
147  randomize( subvector[i], min, max );
148  }
149 }
151 //*************************************************************************************************
152 
153 
154 
155 
156 //=================================================================================================
157 //
158 // RAND SPECIALIZATION FOR SPARSE SUBVECTORS
159 //
160 //=================================================================================================
161 
162 //*************************************************************************************************
169 template< typename VT // Type of the vector
170  , AlignmentFlag AF // Alignment flag
171  , bool TF // Transpose flag
172  , size_t... CSAs > // Compile time subvector arguments
173 class Rand< Subvector<VT,AF,TF,false,CSAs...> >
174 {
175  public:
176  //**Randomize functions*************************************************************************
179  template< typename SVT >
180  inline void randomize( SVT&& subvector ) const;
181 
182  template< typename SVT >
183  inline void randomize( SVT&& subvector, size_t nonzeros ) const;
184 
185  template< typename SVT, typename Arg >
186  inline void randomize( SVT&& subvector, const Arg& min, const Arg& max ) const;
187 
188  template< typename SVT, typename Arg >
189  inline void randomize( SVT&& subvector, size_t nonzeros, const Arg& min, const Arg& max ) const;
191  //**********************************************************************************************
192 };
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
204 template< typename VT // Type of the vector
205  , AlignmentFlag AF // Alignment flag
206  , bool TF // Transpose flag
207  , size_t... CSAs > // Compile time subvector arguments
208 template< typename SVT > // Type of the subvector
209 inline void Rand< Subvector<VT,AF,TF,false,CSAs...> >::randomize( SVT&& subvector ) const
210 {
211  using SubvectorType = RemoveReference_t<SVT>;
212  using ElementType = ElementType_t<SubvectorType>;
213 
216 
217  const size_t size( subvector.size() );
218 
219  if( size == 0UL ) return;
220 
221  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
222 
223  subvector.reset();
224  subvector.reserve( nonzeros );
225 
226  while( subvector.nonZeros() < nonzeros ) {
227  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
228  }
229 }
231 //*************************************************************************************************
232 
233 
234 //*************************************************************************************************
243 template< typename VT // Type of the vector
244  , AlignmentFlag AF // Alignment flag
245  , bool TF // Transpose flag
246  , size_t... CSAs > // Compile time subvector arguments
247 template< typename SVT > // Type of the subvector
248 inline void Rand< Subvector<VT,AF,TF,false,CSAs...> >::randomize( SVT&& subvector, size_t nonzeros ) const
249 {
250  using SubvectorType = RemoveReference_t<SVT>;
251  using ElementType = ElementType_t<SubvectorType>;
252 
255 
256  const size_t size( subvector.size() );
257 
258  if( nonzeros > size ) {
259  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
260  }
261 
262  if( size == 0UL ) return;
263 
264  subvector.reset();
265  subvector.reserve( nonzeros );
266 
267  while( subvector.nonZeros() < nonzeros ) {
268  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
269  }
270 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
284 template< typename VT // Type of the vector
285  , AlignmentFlag AF // Alignment flag
286  , bool TF // Transpose flag
287  , size_t... CSAs > // Compile time subvector arguments
288 template< typename SVT // Type of the subvector
289  , typename Arg > // Min/max argument type
290 inline void Rand< Subvector<VT,AF,TF,false,CSAs...> >::randomize( SVT&& subvector,
291  const Arg& min, const Arg& max ) const
292 {
293  using SubvectorType = RemoveReference_t<SVT>;
294  using ElementType = ElementType_t<SubvectorType>;
295 
298 
299  const size_t size( subvector.size() );
300 
301  if( size == 0UL ) return;
302 
303  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
304 
305  subvector.reset();
306  subvector.reserve( nonzeros );
307 
308  while( subvector.nonZeros() < nonzeros ) {
309  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
310  }
311 }
313 //*************************************************************************************************
314 
315 
316 //*************************************************************************************************
327 template< typename VT // Type of the vector
328  , AlignmentFlag AF // Alignment flag
329  , bool TF // Transpose flag
330  , size_t... CSAs > // Compile time subvector arguments
331 template< typename SVT // Type of the subvector
332  , typename Arg > // Min/max argument type
333 inline void Rand< Subvector<VT,AF,TF,false,CSAs...> >::randomize( SVT&& subvector, size_t nonzeros,
334  const Arg& min, const Arg& max ) const
335 {
336  using SubvectorType = RemoveReference_t<SVT>;
337  using ElementType = ElementType_t<SubvectorType>;
338 
341 
342  const size_t size( subvector.size() );
343 
344  if( nonzeros > size ) {
345  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
346  }
347 
348  if( size == 0UL ) return;
349 
350  subvector.reset();
351  subvector.reserve( nonzeros );
352 
353  while( subvector.nonZeros() < nonzeros ) {
354  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
355  }
356 }
358 //*************************************************************************************************
359 
360 } // namespace blaze
361 
362 #endif
AlignmentFlag
Alignment flag for (un-)aligned vectors and matrices.Via these flags it is possible to specify subvec...
Definition: AlignmentFlag.h:62
Header file for the implementation of the Submatrix view.
#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
Header file for auxiliary alias declarations.
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
decltype(auto) subvector(Vector< VT, TF > &, RSAs...)
Creating a view on a specific subvector of the given vector.
Definition: Subvector.h:154
Implementation of a random number generator.
#define BLAZE_CONSTRAINT_MUST_BE_SUBVECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a subvector type (i....
Definition: Subvector.h:61
Constraint on the data type.
Header file for the implementation of the Subvector view.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Header file for the dense vector SMP implementation.
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
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type,...
Definition: SparseVector.h:61
Constraint on the data type.
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
Constraint on the data type.
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Header file for the RemoveReference type trait.
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type,...
Definition: DenseVector.h:61
Header file for the sparse vector SMP implementation.