Blaze 3.9
Loading...
Searching...
No Matches
Row.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_ROW_H_
36#define _BLAZE_MATH_ROW_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <blaze/math/Aliases.h>
52#include <blaze/util/Random.h>
54
55
56namespace blaze {
57
58//=================================================================================================
59//
60// RAND SPECIALIZATION FOR DENSE ROWS
61//
62//=================================================================================================
63
64//*************************************************************************************************
71template< typename MT // Type of the matrix
72 , bool SO // Storage order
73 , bool SF // Symmetry flag
74 , size_t... CRAs > // Compile time row arguments
75class Rand< Row<MT,SO,true,SF,CRAs...> >
76{
77 public:
78 //**********************************************************************************************
84 template< typename RT > // Type of the row
85 inline void randomize( RT&& row ) const
86 {
87 using blaze::randomize;
88
89 using RowType = RemoveReference_t<RT>;
90
93
94 for( size_t i=0UL; i<row.size(); ++i ) {
95 randomize( row[i] );
96 }
97 }
98 //**********************************************************************************************
99
100 //**********************************************************************************************
108 template< typename RT // Type of the row
109 , typename Arg > // Min/max argument type
110 inline void randomize( RT&& row, const Arg& min, const Arg& max ) const
111 {
112 using blaze::randomize;
113
114 using RowType = RemoveReference_t<RT>;
115
118
119 for( size_t i=0UL; i<row.size(); ++i ) {
120 randomize( row[i], min, max );
121 }
122 }
123 //**********************************************************************************************
124};
126//*************************************************************************************************
127
128
129
130
131//=================================================================================================
132//
133// RAND SPECIALIZATION FOR SPARSE ROWS
134//
135//=================================================================================================
136
137//*************************************************************************************************
144template< typename MT // Type of the matrix
145 , bool SO // Storage order
146 , bool SF // Symmetry flag
147 , size_t... CRAs > // Compile time row arguments
148class Rand< Row<MT,SO,false,SF,CRAs...> >
149{
150 public:
151 //**********************************************************************************************
157 template< typename RT > // Type of the row
158 inline void randomize( RT&& row ) const
159 {
160 using RowType = RemoveReference_t<RT>;
161 using ElementType = ElementType_t<RowType>;
162
165
166 const size_t size( row.size() );
167
168 if( size == 0UL ) return;
169
170 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
171
172 row.reset();
173 row.reserve( nonzeros );
174
175 while( row.nonZeros() < nonzeros ) {
176 row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
177 }
178 }
179 //**********************************************************************************************
180
181 //**********************************************************************************************
189 template< typename RT > // Type of the row
190 inline void randomize( RT&& row, size_t nonzeros ) const
191 {
192 using RowType = RemoveReference_t<RT>;
193 using ElementType = ElementType_t<RowType>;
194
197
198 const size_t size( row.size() );
199
200 if( nonzeros > size ) {
201 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
202 }
203
204 if( size == 0UL ) return;
205
206 row.reset();
207 row.reserve( nonzeros );
208
209 while( row.nonZeros() < nonzeros ) {
210 row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
211 }
212 }
213 //**********************************************************************************************
214
215 //**********************************************************************************************
223 template< typename RT // Type of the row
224 , typename Arg > // Min/max argument type
225 inline void randomize( RT&& row, const Arg& min, const Arg& max ) const
226 {
227 using RowType = RemoveReference_t<RT>;
228 using ElementType = ElementType_t<RowType>;
229
232
233 const size_t size( row.size() );
234
235 if( size == 0UL ) return;
236
237 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
238
239 row.reset();
240 row.reserve( nonzeros );
241
242 while( row.nonZeros() < nonzeros ) {
243 row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
244 }
245 }
246 //**********************************************************************************************
247
248 //**********************************************************************************************
258 template< typename RT // Type of the row
259 , typename Arg > // Min/max argument type
260 inline void randomize( RT&& row, size_t nonzeros, const Arg& min, const Arg& max ) const
261 {
262 using RowType = RemoveReference_t<RT>;
263 using ElementType = ElementType_t<RowType>;
264
267
268 const size_t size( row.size() );
269
270 if( nonzeros > size ) {
271 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
272 }
273
274 if( size == 0UL ) return;
275
276 row.reset();
277 row.reserve( nonzeros );
278
279 while( row.nonZeros() < nonzeros ) {
280 row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
281 }
282 }
283 //**********************************************************************************************
284};
286//*************************************************************************************************
287
288} // namespace blaze
289
290#endif
Header file for auxiliary alias declarations.
Header file for the RemoveReference type trait.
Default implementation of the Rand class.
Definition Random.h:230
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
#define BLAZE_CONSTRAINT_MUST_BE_ROW_TYPE(T)
Constraint on the data type.
Definition Row.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
T rand()
Random number function.
Definition Random.h:583
void randomize(T &&value)
Randomization of a given variable.
Definition Random.h:626
decltype(auto) row(Matrix< MT, SO > &, RRAs...)
Creating a view on a specific row of the given matrix.
Definition Row.h:137
typename RemoveReference< T >::Type RemoveReference_t
Auxiliary alias declaration for the RemoveReference type trait.
Definition RemoveReference.h:95
#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 Column view.
Header file for the implementation of the Row view.