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>
44 #include <blaze/math/Exception.h>
48 #include <blaze/math/views/Row.h>
49 #include <blaze/util/Random.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // RAND SPECIALIZATION FOR DENSE ROWS
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
67 template< typename MT // Type of the dense matrix
68  , bool SO // Storage order
69  , bool SF > // Symmetry flag
70 class Rand< Row<MT,SO,true,SF> >
71 {
72  public:
73  //**Randomize functions*************************************************************************
76  inline void randomize( Row<MT,SO,true,SF>& row ) const;
77 
78  template< typename Arg >
79  inline void randomize( Row<MT,SO,true,SF>& row, const Arg& min, const Arg& max ) const;
81  //**********************************************************************************************
82 };
84 //*************************************************************************************************
85 
86 
87 //*************************************************************************************************
94 template< typename MT // Type of the dense matrix
95  , bool SO // Storage order
96  , bool SF > // Symmetry flag
97 inline void Rand< Row<MT,SO,true,SF> >::randomize( Row<MT,SO,true,SF>& row ) const
98 {
99  using blaze::randomize;
100 
101  for( size_t i=0UL; i<row.size(); ++i ) {
102  randomize( row[i] );
103  }
104 }
106 //*************************************************************************************************
107 
108 
109 //*************************************************************************************************
118 template< typename MT // Type of the dense matrix
119  , bool SO // Storage order
120  , bool SF > // Symmetry flag
121 template< typename Arg > // Min/max argument type
122 inline void Rand< Row<MT,SO,true,SF> >::randomize( Row<MT,SO,true,SF>& row,
123  const Arg& min, const Arg& max ) const
124 {
125  using blaze::randomize;
126 
127  for( size_t i=0UL; i<row.size(); ++i ) {
128  randomize( row[i], min, max );
129  }
130 }
132 //*************************************************************************************************
133 
134 
135 
136 
137 //=================================================================================================
138 //
139 // RAND SPECIALIZATION
140 //
141 //=================================================================================================
142 
143 //*************************************************************************************************
150 template< typename MT // Type of the sparse matrix
151  , bool SO // Storage order
152  , bool SF > // Symmetry flag
153 class Rand< Row<MT,SO,false,SF> >
154 {
155  public:
156  //**Randomize functions*************************************************************************
159  inline void randomize( Row<MT,SO,false,SF>& row ) const;
160  inline void randomize( Row<MT,SO,false,SF>& row, size_t nonzeros ) const;
161 
162  template< typename Arg >
163  inline void randomize( Row<MT,SO,false,SF>& row, const Arg& min, const Arg& max ) const;
164 
165  template< typename Arg >
166  inline void randomize( Row<MT,SO,false,SF>& row, size_t nonzeros, const Arg& min, const Arg& max ) const;
168  //**********************************************************************************************
169 };
171 //*************************************************************************************************
172 
173 
174 //*************************************************************************************************
181 template< typename MT // Type of the sparse matrix
182  , bool SO // Storage order
183  , bool SF > // Symmetry flag
184 inline void Rand< Row<MT,SO,false,SF> >::randomize( Row<MT,SO,false,SF>& row ) const
185 {
186  typedef ElementType_< Row<MT,SO,false,SF> > ElementType;
187 
188  const size_t size( row.size() );
189 
190  if( size == 0UL ) return;
191 
192  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
193 
194  row.reset();
195  row.reserve( nonzeros );
196 
197  while( row.nonZeros() < nonzeros ) {
198  row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
199  }
200 }
202 //*************************************************************************************************
203 
204 
205 //*************************************************************************************************
214 template< typename MT // Type of the sparse matrix
215  , bool SO // Storage order
216  , bool SF > // Symmetry flag
217 inline void Rand< Row<MT,SO,false,SF> >::randomize( Row<MT,SO,false,SF>& row, size_t nonzeros ) const
218 {
219  typedef ElementType_< Row<MT,SO,false,SF> > ElementType;
220 
221  const size_t size( row.size() );
222 
223  if( nonzeros > size ) {
224  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
225  }
226 
227  if( size == 0UL ) return;
228 
229  row.reset();
230  row.reserve( nonzeros );
231 
232  while( row.nonZeros() < nonzeros ) {
233  row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
234  }
235 }
237 //*************************************************************************************************
238 
239 
240 //*************************************************************************************************
249 template< typename MT // Type of the sparse matrix
250  , bool SO // Storage order
251  , bool SF > // Symmetry flag
252 template< typename Arg > // Min/max argument type
253 inline void Rand< Row<MT,SO,false,SF> >::randomize( Row<MT,SO,false,SF>& row,
254  const Arg& min, const Arg& max ) const
255 {
256  typedef ElementType_< Row<MT,SO,false,SF> > ElementType;
257 
258  const size_t size( row.size() );
259 
260  if( size == 0UL ) return;
261 
262  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
263 
264  row.reset();
265  row.reserve( nonzeros );
266 
267  while( row.nonZeros() < nonzeros ) {
268  row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
269  }
270 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
286 template< typename MT // Type of the sparse matrix
287  , bool SO // Storage order
288  , bool SF > // Symmetry flag
289 template< typename Arg > // Min/max argument type
290 inline void Rand< Row<MT,SO,false,SF> >::randomize( Row<MT,SO,false,SF>& row, size_t nonzeros,
291  const Arg& min, const Arg& max ) const
292 {
293  typedef ElementType_< Row<MT,SO,false,SF> > ElementType;
294 
295  const size_t size( row.size() );
296 
297  if( nonzeros > size ) {
298  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
299  }
300 
301  if( size == 0UL ) return;
302 
303  row.reset();
304  row.reserve( nonzeros );
305 
306  while( row.nonZeros() < nonzeros ) {
307  row[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
308  }
309 }
311 //*************************************************************************************************
312 
313 } // namespace blaze
314 
315 #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.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:258
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:926
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
Implementation of a random number generator.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
Header file for the dense vector SMP implementation.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:289
Header file for the exception macros of the math module.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2641
Header file for all restructuring column functions.
DisableIf_< Or< IsComputation< MT >, IsTransExpr< MT > >, RowExprTrait_< MT > > row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:126
Header file for the sparse vector SMP implementation.
Header file for the implementation of the Row view.
const DMatForEachExpr< MT, Ceil, SO > ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatForEachExpr.h:1130