Blaze 3.9
Rows.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_ROWS_H_
36#define _BLAZE_MATH_ROWS_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 ROW SELECTIONS
61//
62//=================================================================================================
63
64//*************************************************************************************************
71template< typename MT // Type of the matrix
72 , bool SO // Storage order
73 , bool SF // Symmetry flag
74 , typename... CRAs > // Compile time row arguments
75class Rand< Rows<MT,SO,true,SF,CRAs...> >
76{
77 public:
78 //**********************************************************************************************
84 template< typename RT > // Type of the row selection
85 inline void randomize( RT&& rows ) const
86 {
87 using blaze::randomize;
88
89 using RowsType = RemoveReference_t<RT>;
90
93
94 if( SO == true ) {
95 for( size_t i=0UL; i<rows.rows(); ++i ) {
96 for( size_t j=0UL; j<rows.columns(); ++j ) {
97 randomize( rows(i,j) );
98 }
99 }
100 }
101 else {
102 for( size_t j=0UL; j<rows.columns(); ++j ) {
103 for( size_t i=0UL; i<rows.rows(); ++i ) {
104 randomize( rows(i,j) );
105 }
106 }
107 }
108 }
109 //**********************************************************************************************
110
111 //**********************************************************************************************
119 template< typename RT // Type of the row selection
120 , typename Arg > // Min/max argument type
121 inline void randomize( RT&& rows, const Arg& min, const Arg& max ) const
122 {
123 using blaze::randomize;
124
125 using RowsType = RemoveReference_t<RT>;
126
129
130 if( SO == true ) {
131 for( size_t i=0UL; i<rows.rows(); ++i ) {
132 for( size_t j=0UL; j<rows.columns(); ++j ) {
133 randomize( rows(i,j), min, max );
134 }
135 }
136 }
137 else {
138 for( size_t j=0UL; j<rows.columns(); ++j ) {
139 for( size_t i=0UL; i<rows.rows(); ++i ) {
140 randomize( rows(i,j), min, max );
141 }
142 }
143 }
144 }
145 //**********************************************************************************************
146};
148//*************************************************************************************************
149
150
151
152
153//=================================================================================================
154//
155// RAND SPECIALIZATION FOR SPARSE ROW SELECTIONS
156//
157//=================================================================================================
158
159//*************************************************************************************************
166template< typename MT // Type of the matrix
167 , bool SO // Storage order
168 , bool SF // Symmetry flag
169 , typename... CRAs > // Compile time row arguments
170class Rand< Rows<MT,SO,false,SF,CRAs...> >
171{
172 public:
173 //**********************************************************************************************
179 template< typename RT > // Type of the row selection
180 inline void randomize( RT&& rows ) const
181 {
182 using RowsType = RemoveReference_t<RT>;
183 using ElementType = ElementType_t<RowsType>;
184
187
188 const size_t m( rows.rows() );
189 const size_t n( rows.columns() );
190
191 if( m == 0UL || n == 0UL ) return;
192
193 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
194
195 rows.reset();
196 rows.reserve( nonzeros );
197
198 while( rows.nonZeros() < nonzeros ) {
199 rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
200 }
201 }
202 //**********************************************************************************************
203
204 //**********************************************************************************************
212 template< typename RT > // Type of the row selection
213 inline void randomize( RT&& rows, size_t nonzeros ) const
214 {
215 using RowsType = RemoveReference_t<RT>;
216 using ElementType = ElementType_t<RowsType>;
217
220
221 const size_t m( rows.rows() );
222 const size_t n( rows.columns() );
223
224 if( nonzeros > m*n ) {
225 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
226 }
227
228 if( m == 0UL || n == 0UL ) return;
229
230 rows.reset();
231 rows.reserve( nonzeros );
232
233 while( rows.nonZeros() < nonzeros ) {
234 rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
235 }
236 }
237 //**********************************************************************************************
238
239 //**********************************************************************************************
247 template< typename RT // Type of the row selection
248 , typename Arg > // Min/max argument type
249 inline void randomize( RT&& rows, const Arg& min, const Arg& max ) const
250 {
251 using RowsType = RemoveReference_t<RT>;
252 using ElementType = ElementType_t<RowsType>;
253
256
257 const size_t m( rows.rows() );
258 const size_t n( rows.columns() );
259
260 if( m == 0UL || n == 0UL ) return;
261
262 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
263
264 rows.reset();
265 rows.reserve( nonzeros );
266
267 while( rows.nonZeros() < nonzeros ) {
268 rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
269 }
270 }
271 //**********************************************************************************************
272
273 //**********************************************************************************************
283 template< typename RT // Type of the row selection
284 , typename Arg > // Min/max argument type
285 inline void randomize( RT&& rows, size_t nonzeros, const Arg& min, const Arg& max ) const
286 {
287 using RowsType = RemoveReference_t<RT>;
288 using ElementType = ElementType_t<RowsType>;
289
292
293 const size_t m( rows.rows() );
294 const size_t n( rows.columns() );
295
296 if( nonzeros > m*n ) {
297 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
298 }
299
300 if( m == 0UL || n == 0UL ) return;
301
302 rows.reset();
303 rows.reserve( nonzeros );
304
305 while( rows.nonZeros() < nonzeros ) {
306 rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
307 }
308 }
309 //**********************************************************************************************
310};
312//*************************************************************************************************
313
314} // namespace blaze
315
316#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
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: DenseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: SparseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_ROWS_TYPE(T)
Constraint on the data type.
Definition: Rows.h:61
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:626
decltype(auto) rows(MT &&matrix, const SmallArray< T, N > &indices, RRAs... args)
Creating a view on a selection of rows of the given matrix.
Definition: Rows.h:753
#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 matrix SMP implementation.
Header file for the sparse matrix SMP implementation.
Implementation of a random number generator.
Header file for the implementation of the Columns view.
Header file for the implementation of the Rows view.