Submatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SUBMATRIX_H_
36 #define _BLAZE_MATH_SUBMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
52 #include <blaze/util/Random.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // RAND SPECIALIZATION FOR DENSE SUBMATRICES
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename MT // Type of the matrix
72  , AlignmentFlag AF // Alignment flag
73  , bool SO // Storage order
74  , size_t... CSAs > // Compile time submatrix arguments
75 class Rand< Submatrix<MT,AF,SO,true,CSAs...> >
76 {
77  public:
78  //**Randomize functions*************************************************************************
81  template< typename SMT >
82  inline void randomize( SMT&& submatrix ) const;
83 
84  template< typename SMT, typename Arg >
85  inline void randomize( SMT&& submatrix, const Arg& min, const Arg& max ) const;
87  //**********************************************************************************************
88 };
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
100 template< typename MT // Type of the dense matrix
101  , AlignmentFlag AF // Alignment flag
102  , bool SO // Storage order
103  , size_t... CSAs > // Compile time submatrix arguments
104 template< typename SMT > // Type of the submatrix
105 inline void Rand< Submatrix<MT,AF,SO,true,CSAs...> >::randomize( SMT&& submatrix ) const
106 {
107  using blaze::randomize;
108 
109  using SubmatrixType = RemoveReference_<SMT>;
110 
113 
114  if( SO == rowMajor ) {
115  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
116  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
117  randomize( submatrix(i,j) );
118  }
119  }
120  }
121  else {
122  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
123  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
124  randomize( submatrix(i,j) );
125  }
126  }
127  }
128 }
130 //*************************************************************************************************
131 
132 
133 //*************************************************************************************************
142 template< typename MT // Type of the dense matrix
143  , AlignmentFlag AF // Alignment flag
144  , bool SO // Storage order
145  , size_t... CSAs > // Compile time submatrix arguments
146 template< typename SMT // Type of the submatrix
147  , typename Arg > // Min/max argument type
148 inline void Rand< Submatrix<MT,AF,SO,true,CSAs...> >::randomize( SMT&& submatrix,
149  const Arg& min, const Arg& max ) const
150 {
151  using blaze::randomize;
152 
153  using SubmatrixType = RemoveReference_<SMT>;
154 
157 
158  if( SO == rowMajor ) {
159  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
160  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
161  randomize( submatrix(i,j), min, max );
162  }
163  }
164  }
165  else {
166  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
167  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
168  randomize( submatrix(i,j), min, max );
169  }
170  }
171  }
172 }
174 //*************************************************************************************************
175 
176 
177 
178 
179 //=================================================================================================
180 //
181 // RAND SPECIALIZATION FOR SPARSE SUBMATRICES
182 //
183 //=================================================================================================
184 
185 //*************************************************************************************************
192 template< typename MT // Type of the dense matrix
193  , AlignmentFlag AF // Alignment flag
194  , bool SO // Storage order
195  , size_t... CSAs > // Compile time submatrix arguments
196 class Rand< Submatrix<MT,AF,SO,false,CSAs...> >
197 {
198  public:
199  //**Randomize functions*************************************************************************
202  template< typename SMT >
203  inline void randomize( SMT&& submatrix ) const;
204 
205  template< typename SMT >
206  inline void randomize( SMT&& submatrix, size_t nonzeros ) const;
207 
208  template< typename SMT, typename Arg >
209  inline void randomize( SMT&& submatrix, const Arg& min, const Arg& max ) const;
210 
211  template< typename SMT, typename Arg >
212  inline void randomize( SMT&& submatrix, size_t nonzeros, const Arg& min, const Arg& max ) const;
214  //**********************************************************************************************
215 };
217 //*************************************************************************************************
218 
219 
220 //*************************************************************************************************
227 template< typename MT // Type of the dense matrix
228  , AlignmentFlag AF // Alignment flag
229  , bool SO // Storage order
230  , size_t... CSAs > // Compile time submatrix arguments
231 template< typename SMT > // Type of the submatrix
232 inline void Rand< Submatrix<MT,AF,SO,false,CSAs...> >::randomize( SMT&& submatrix ) const
233 {
234  using SubmatrixType = RemoveReference_<SMT>;
235  using ElementType = ElementType_<SubmatrixType>;
236 
239 
240  const size_t m( submatrix.rows() );
241  const size_t n( submatrix.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  submatrix.reset();
248  submatrix.reserve( nonzeros );
249 
250  while( submatrix.nonZeros() < nonzeros ) {
251  submatrix( 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 dense matrix
268  , AlignmentFlag AF // Alignment flag
269  , bool SO // Storage order
270  , size_t... CSAs > // Compile time submatrix arguments
271 template< typename SMT > // Type of the submatrix
272 inline void Rand< Submatrix<MT,AF,SO,false,CSAs...> >::randomize( SMT&& submatrix, size_t nonzeros ) const
273 {
274  using SubmatrixType = RemoveReference_<SMT>;
275  using ElementType = ElementType_<SubmatrixType>;
276 
279 
280  const size_t m( submatrix.rows() );
281  const size_t n( submatrix.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  submatrix.reset();
290  submatrix.reserve( nonzeros );
291 
292  while( submatrix.nonZeros() < nonzeros ) {
293  submatrix( 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 dense matrix
310  , AlignmentFlag AF // Alignment flag
311  , bool SO // Storage order
312  , size_t... CSAs > // Compile time submatrix arguments
313 template< typename SMT // Type of the submatrix
314  , typename Arg > // Min/max argument type
315 inline void Rand< Submatrix<MT,AF,SO,false,CSAs...> >::randomize( SMT&& submatrix,
316  const Arg& min, const Arg& max ) const
317 {
318  using SubmatrixType = RemoveReference_<SMT>;
319  using ElementType = ElementType_<SubmatrixType>;
320 
323 
324  const size_t m( submatrix.rows() );
325  const size_t n( submatrix.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  submatrix.reset();
332  submatrix.reserve( nonzeros );
333 
334  while( submatrix.nonZeros() < nonzeros ) {
335  submatrix( 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 dense matrix
354  , AlignmentFlag AF // Alignment flag
355  , bool SO // Storage order
356  , size_t... CSAs > // Compile time submatrix arguments
357 template< typename SMT // Type of the submatrix
358  , typename Arg > // Min/max argument type
359 inline void Rand< Submatrix<MT,AF,SO,false,CSAs...> >::randomize( SMT&& submatrix, size_t nonzeros,
360  const Arg& min, const Arg& max ) const
361 {
362  using SubmatrixType = RemoveReference_<SMT>;
363  using ElementType = ElementType_<SubmatrixType>;
364 
367 
368  const size_t m( submatrix.rows() );
369  const size_t n( submatrix.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  submatrix.reset();
378  submatrix.reserve( nonzeros );
379 
380  while( submatrix.nonZeros() < nonzeros ) {
381  submatrix( 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
AlignmentFlag
Alignment flag for (un-)aligned vectors and matrices.Via these flags it is possible to specify subvec...
Definition: AlignmentFlag.h:62
Header file for the implementation of the Submatrix view.
#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.
#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:1234
Header file for the sparse matrix SMP implementation.
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1903
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:1950
Constraint on the data type.
Constraint on the data type.
Header file for the implementation of the Subvector view.
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
#define BLAZE_CONSTRAINT_MUST_BE_SUBMATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a submatrix type (i...
Definition: Submatrix.h:61
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3079
Header file for the dense matrix SMP implementation.
Header file for the exception macros of the math module.
decltype(auto) submatrix(Matrix< MT, SO > &, RSAs...)
Creating a view on a specific submatrix of the given matrix.
Definition: Submatrix.h:360
Header file for the RemoveReference type trait.
const bool rowMajor
Storage order flag for row-major matrices.
Definition: StorageOrder.h:71
#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
Constraint on the data type.