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>
47 #include <blaze/math/Exception.h>
51 #include <blaze/math/views/Rows.h>
52 #include <blaze/util/Random.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // RAND SPECIALIZATION FOR DENSE ROW SELECTIONS
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename MT // Type of the matrix
72  , bool SO // Storage order
73  , bool SF // Symmetry flag
74  , typename... CRAs > // Compile time row arguments
75 class Rand< Rows<MT,SO,true,SF,CRAs...> >
76 {
77  public:
78  //**Randomize functions*************************************************************************
81  template< typename RT >
82  inline void randomize( RT&& rows ) const;
83 
84  template< typename RT, typename Arg >
85  inline void randomize( RT&& rows, const Arg& min, const Arg& max ) const;
87  //**********************************************************************************************
88 };
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
100 template< typename MT // Type of the matrix
101  , bool SO // Storage order
102  , bool SF // Symmetry flag
103  , typename... CRAs > // Compile time row arguments
104 template< typename RT > // Type of the row selection
105 inline void Rand< Rows<MT,SO,true,SF,CRAs...> >::randomize( RT&& rows ) const
106 {
107  using blaze::randomize;
108 
109  using RowsType = RemoveReference_t<RT>;
110 
113 
114  if( SO == true ) {
115  for( size_t i=0UL; i<rows.rows(); ++i ) {
116  for( size_t j=0UL; j<rows.columns(); ++j ) {
117  randomize( rows(i,j) );
118  }
119  }
120  }
121  else {
122  for( size_t j=0UL; j<rows.columns(); ++j ) {
123  for( size_t i=0UL; i<rows.rows(); ++i ) {
124  randomize( rows(i,j) );
125  }
126  }
127  }
128 }
130 //*************************************************************************************************
131 
132 
133 //*************************************************************************************************
142 template< typename MT // Type of the matrix
143  , bool SO // Storage order
144  , bool SF // Symmetry flag
145  , typename... CRAs > // Compile time row arguments
146 template< typename RT // Type of the row selection
147  , typename Arg > // Min/max argument type
148 inline void Rand< Rows<MT,SO,true,SF,CRAs...> >::randomize( RT&& rows,
149  const Arg& min, const Arg& max ) const
150 {
151  using blaze::randomize;
152 
153  using RowsType = RemoveReference_t<RT>;
154 
157 
158  if( SO == true ) {
159  for( size_t i=0UL; i<rows.rows(); ++i ) {
160  for( size_t j=0UL; j<rows.columns(); ++j ) {
161  randomize( rows(i,j), min, max );
162  }
163  }
164  }
165  else {
166  for( size_t j=0UL; j<rows.columns(); ++j ) {
167  for( size_t i=0UL; i<rows.rows(); ++i ) {
168  randomize( rows(i,j), min, max );
169  }
170  }
171  }
172 }
174 //*************************************************************************************************
175 
176 
177 
178 
179 //=================================================================================================
180 //
181 // RAND SPECIALIZATION FOR SPARSE ROW SELECTIONS
182 //
183 //=================================================================================================
184 
185 //*************************************************************************************************
192 template< typename MT // Type of the matrix
193  , bool SO // Storage order
194  , bool SF // Symmetry flag
195  , typename... CRAs > // Compile time row arguments
196 class Rand< Rows<MT,SO,false,SF,CRAs...> >
197 {
198  public:
199  //**Randomize functions*************************************************************************
202  template< typename RT >
203  inline void randomize( RT&& rows ) const;
204 
205  template< typename RT >
206  inline void randomize( RT&& rows, size_t nonzeros ) const;
207 
208  template< typename RT, typename Arg >
209  inline void randomize( RT&& rows, const Arg& min, const Arg& max ) const;
210 
211  template< typename RT, typename Arg >
212  inline void randomize( RT&& rows, size_t nonzeros, const Arg& min, const Arg& max ) const;
214  //**********************************************************************************************
215 };
217 //*************************************************************************************************
218 
219 
220 //*************************************************************************************************
227 template< typename MT // Type of the matrix
228  , bool SO // Storage order
229  , bool SF // Symmetry flag
230  , typename... CRAs > // Compile time row arguments
231 template< typename RT > // Type of the row selection
232 inline void Rand< Rows<MT,SO,false,SF,CRAs...> >::randomize( RT&& rows ) const
233 {
234  using RowsType = RemoveReference_t<RT>;
235  using ElementType = ElementType_t<RowsType>;
236 
239 
240  const size_t m( rows.rows() );
241  const size_t n( rows.columns() );
242 
243  if( m == 0UL || n == 0UL ) return;
244 
245  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
246 
247  rows.reset();
248  rows.reserve( nonzeros );
249 
250  while( rows.nonZeros() < nonzeros ) {
251  rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
252  }
253 }
255 //*************************************************************************************************
256 
257 
258 //*************************************************************************************************
267 template< typename MT // Type of the matrix
268  , bool SO // Storage order
269  , bool SF // Symmetry flag
270  , typename... CRAs > // Compile time row arguments
271 template< typename RT > // Type of the row selection
272 inline void Rand< Rows<MT,SO,false,SF,CRAs...> >::randomize( RT&& rows, size_t nonzeros ) const
273 {
274  using RowsType = RemoveReference_t<RT>;
275  using ElementType = ElementType_t<RowsType>;
276 
279 
280  const size_t m( rows.rows() );
281  const size_t n( rows.columns() );
282 
283  if( nonzeros > m*n ) {
284  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
285  }
286 
287  if( m == 0UL || n == 0UL ) return;
288 
289  rows.reset();
290  rows.reserve( nonzeros );
291 
292  while( rows.nonZeros() < nonzeros ) {
293  rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
294  }
295 }
297 //*************************************************************************************************
298 
299 
300 //*************************************************************************************************
309 template< typename MT // Type of the matrix
310  , bool SO // Storage order
311  , bool SF // Symmetry flag
312  , typename... CRAs > // Compile time row arguments
313 template< typename RT // Type of the row selection
314  , typename Arg > // Min/max argument type
315 inline void Rand< Rows<MT,SO,false,SF,CRAs...> >::randomize( RT&& rows,
316  const Arg& min, const Arg& max ) const
317 {
318  using RowsType = RemoveReference_t<RT>;
319  using ElementType = ElementType_t<RowsType>;
320 
323 
324  const size_t m( rows.rows() );
325  const size_t n( rows.columns() );
326 
327  if( m == 0UL || n == 0UL ) return;
328 
329  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
330 
331  rows.reset();
332  rows.reserve( nonzeros );
333 
334  while( rows.nonZeros() < nonzeros ) {
335  rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
336  }
337 }
339 //*************************************************************************************************
340 
341 
342 //*************************************************************************************************
353 template< typename MT // Type of the matrix
354  , bool SO // Storage order
355  , bool SF // Symmetry flag
356  , typename... CRAs > // Compile time row arguments
357 template< typename RT // Type of the row selection
358  , typename Arg > // Min/max argument type
359 inline void Rand< Rows<MT,SO,false,SF,CRAs...> >::randomize( RT&& rows, size_t nonzeros,
360  const Arg& min, const Arg& max ) const
361 {
362  using RowsType = RemoveReference_t<RT>;
363  using ElementType = ElementType_t<RowsType>;
364 
367 
368  const size_t m( rows.rows() );
369  const size_t n( rows.columns() );
370 
371  if( nonzeros > m*n ) {
372  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
373  }
374 
375  if( m == 0UL || n == 0UL ) return;
376 
377  rows.reset();
378  rows.reserve( nonzeros );
379 
380  while( rows.nonZeros() < nonzeros ) {
381  rows( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
382  }
383 }
385 //*************************************************************************************************
386 
387 } // namespace blaze
388 
389 #endif
#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.
Header file for the implementation of the Rows view.
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional matrix type...
Definition: DenseMatrix.h:61
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1239
Header file for the sparse matrix SMP implementation.
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
Implementation of a random number generator.
Constraint on the data type.
Constraint on the data type.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3080
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:1147
Header file for the dense matrix SMP implementation.
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:1179
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
Header file for the RemoveReference type trait.
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_BE_ROWS_TYPE(T)
Constraint on the data type.In case the given data type T is not a row selection (i.e. a view on selected rows of a dense or sparse matrix), a compilation error is created.
Definition: Rows.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type...
Definition: SparseMatrix.h:61
Header file for the implementation of the Columns view.