Blaze 3.9
Elements.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_ELEMENTS_H_
36#define _BLAZE_MATH_ELEMENTS_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <blaze/math/Aliases.h>
51#include <blaze/util/Random.h>
53
54
55namespace blaze {
56
57//=================================================================================================
58//
59// RAND SPECIALIZATION FOR DENSE ELEMENT SELECTIONS
60//
61//=================================================================================================
62
63//*************************************************************************************************
70template< typename VT // Type of the vector
71 , bool TF // Transpose flag
72 , typename... CEAs > // Compile time element arguments
73class Rand< Elements<VT,TF,true,CEAs...> >
74{
75 public:
76 //**********************************************************************************************
82 template< typename ET > // Type of the element selection
83 inline void randomize( ET&& elements ) const
84 {
85 using blaze::randomize;
86
87 using ElementsType = RemoveReference_t<ET>;
88
91
92 for( size_t i=0UL; i<elements.size(); ++i ) {
93 randomize( elements[i] );
94 }
95 }
96 //**********************************************************************************************
97
98 //**********************************************************************************************
106 template< typename ET // Type of the element selection
107 , typename Arg > // Min/max argument type
108 inline void randomize( ET&& elements, const Arg& min, const Arg& max ) const
109 {
110 using blaze::randomize;
111
112 using ElementsType = RemoveReference_t<ET>;
113
116
117 for( size_t i=0UL; i<elements.size(); ++i ) {
118 randomize( elements[i], min, max );
119 }
120 }
121 //**********************************************************************************************
122};
124//*************************************************************************************************
125
126
127
128
129//=================================================================================================
130//
131// RAND SPECIALIZATION FOR SPARSE ELEMENT SELECTIONS
132//
133//=================================================================================================
134
135//*************************************************************************************************
142template< typename VT // Type of the vector
143 , bool TF // Transpose flag
144 , typename... CEAs > // Compile time element arguments
145class Rand< Elements<VT,TF,false,CEAs...> >
146{
147 public:
148 //**********************************************************************************************
154 template< typename ET > // Type of the element selection
155 inline void randomize( ET&& elements ) const
156 {
157 using ElementsType = RemoveReference_t<ET>;
158 using ElementType = ElementType_t<ElementsType>;
159
162
163 const size_t size( elements.size() );
164
165 if( size == 0UL ) return;
166
167 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
168
169 elements.reset();
170 elements.reserve( nonzeros );
171
172 while( elements.nonZeros() < nonzeros ) {
173 elements[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
174 }
175 }
176 //**********************************************************************************************
177
178 //**********************************************************************************************
186 template< typename ET > // Type of the element selection
187 inline void randomize( ET&& elements, size_t nonzeros ) const
188 {
189 using ElementsType = RemoveReference_t<ET>;
190 using ElementType = ElementType_t<ElementsType>;
191
194
195 const size_t size( elements.size() );
196
197 if( nonzeros > size ) {
198 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
199 }
200
201 if( size == 0UL ) return;
202
203 elements.reset();
204 elements.reserve( nonzeros );
205
206 while( elements.nonZeros() < nonzeros ) {
207 elements[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
208 }
209 }
210 //**********************************************************************************************
211
212 //**********************************************************************************************
220 template< typename ET // Type of the element selection
221 , typename Arg > // Min/max argument type
222 inline void randomize( ET&& elements, const Arg& min, const Arg& max ) const
223 {
224 using ElementsType = RemoveReference_t<ET>;
225 using ElementType = ElementType_t<ElementsType>;
226
229
230 const size_t size( elements.size() );
231
232 if( size == 0UL ) return;
233
234 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
235
236 elements.reset();
237 elements.reserve( nonzeros );
238
239 while( elements.nonZeros() < nonzeros ) {
240 elements[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
241 }
242 }
243 //**********************************************************************************************
244
245 //**********************************************************************************************
255 template< typename ET // Type of the element selection
256 , typename Arg > // Min/max argument type
257 inline void randomize( ET&& elements, size_t nonzeros, const Arg& min, const Arg& max ) const
258 {
259 using ElementsType = RemoveReference_t<ET>;
260 using ElementType = ElementType_t<ElementsType>;
261
264
265 const size_t size( elements.size() );
266
267 if( nonzeros > size ) {
268 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
269 }
270
271 if( size == 0UL ) return;
272
273 elements.reset();
274 elements.reserve( nonzeros );
275
276 while( elements.nonZeros() < nonzeros ) {
277 elements[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
278 }
279 }
280 //**********************************************************************************************
281};
283//*************************************************************************************************
284
285} // namespace blaze
286
287#endif
Header file for auxiliary alias declarations.
Header file for the RemoveReference type trait.
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
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) elements(VT &&vector, const SmallArray< T, N > &indices, REAs... args)
Creating a view on a selection of elements of the given vector.
Definition: Elements.h:741
#define BLAZE_CONSTRAINT_MUST_BE_ELEMENTS_TYPE(T)
Constraint on the data type.
Definition: Elements.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: SparseVector.h:61
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: DenseVector.h:61
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.
Header file for the dense vector SMP implementation.
Header file for the sparse vector SMP implementation.
Implementation of a random number generator.
Header file for the implementation of the Elements view.