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>
44 #include <blaze/math/Exception.h>
49 #include <blaze/util/Random.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // RAND SPECIALIZATION FOR DENSE SUBMATRICES
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
67 template< typename MT // Type of the dense matrix
68  , bool AF // Alignment flag
69  , bool SO > // Storage order
70 class Rand< Submatrix<MT,AF,SO,true> >
71 {
72  public:
73  //**Randomize functions*************************************************************************
76  inline void randomize( Submatrix<MT,AF,SO,true>& submatrix ) const;
77 
78  template< typename Arg >
79  inline void randomize( Submatrix<MT,AF,SO,true>& submatrix, const Arg& min, const Arg& max ) const;
81  //**********************************************************************************************
82 };
84 //*************************************************************************************************
85 
86 
87 //*************************************************************************************************
94 template< typename MT // Type of the dense matrix
95  , bool AF // Alignment flag
96  , bool SO > // Storage order
97 inline void Rand< Submatrix<MT,AF,SO,true> >::randomize( Submatrix<MT,AF,SO,true>& submatrix ) const
98 {
99  using blaze::randomize;
100 
101  if( SO == rowMajor ) {
102  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
103  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
104  randomize( submatrix(i,j) );
105  }
106  }
107  }
108  else {
109  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
110  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
111  randomize( submatrix(i,j) );
112  }
113  }
114  }
115 }
117 //*************************************************************************************************
118 
119 
120 //*************************************************************************************************
129 template< typename MT // Type of the dense matrix
130  , bool AF // Alignment flag
131  , bool SO > // Storage order
132 template< typename Arg > // Min/max argument type
133 inline void Rand< Submatrix<MT,AF,SO,true> >::randomize( Submatrix<MT,AF,SO,true>& submatrix,
134  const Arg& min, const Arg& max ) const
135 {
136  using blaze::randomize;
137 
138  if( SO == rowMajor ) {
139  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
140  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
141  randomize( submatrix(i,j), min, max );
142  }
143  }
144  }
145  else {
146  for( size_t j=0UL; j<submatrix.columns(); ++j ) {
147  for( size_t i=0UL; i<submatrix.rows(); ++i ) {
148  randomize( submatrix(i,j), min, max );
149  }
150  }
151  }
152 }
154 //*************************************************************************************************
155 
156 
157 
158 
159 //=================================================================================================
160 //
161 // RAND SPECIALIZATION FOR SPARSE SUBMATRICES
162 //
163 //=================================================================================================
164 
165 //*************************************************************************************************
172 template< typename MT // Type of the sparse matrix
173  , bool AF // Alignment flag
174  , bool SO > // Storage order
175 class Rand< Submatrix<MT,AF,SO,false> >
176 {
177  public:
178  //**Randomize functions*************************************************************************
181  inline void randomize( Submatrix<MT,AF,SO,false>& submatrix ) const;
182  inline void randomize( Submatrix<MT,AF,SO,false>& submatrix, size_t nonzeros ) const;
183 
184  template< typename Arg >
185  inline void randomize( Submatrix<MT,AF,SO,false>& submatrix, const Arg& min, const Arg& max ) const;
186 
187  template< typename Arg >
188  inline void randomize( Submatrix<MT,AF,SO,false>& submatrix, size_t nonzeros,
189  const Arg& min, const Arg& max ) const;
191  //**********************************************************************************************
192 };
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
204 template< typename MT // Type of the sparse matrix
205  , bool AF // Alignment flag
206  , bool SO > // Storage order
207 inline void Rand< Submatrix<MT,AF,SO,false> >::randomize( Submatrix<MT,AF,SO,false>& submatrix ) const
208 {
209  using ElementType = ElementType_< Submatrix<MT,AF,SO,false> >;
210 
211  const size_t m( submatrix.rows() );
212  const size_t n( submatrix.columns() );
213 
214  if( m == 0UL || n == 0UL ) return;
215 
216  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
217 
218  submatrix.reset();
219  submatrix.reserve( nonzeros );
220 
221  while( submatrix.nonZeros() < nonzeros ) {
222  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
223  }
224 }
226 //*************************************************************************************************
227 
228 
229 //*************************************************************************************************
238 template< typename MT // Type of the sparse matrix
239  , bool AF // Alignment flag
240  , bool SO > // Storage order
241 inline void Rand< Submatrix<MT,AF,SO,false> >::randomize( Submatrix<MT,AF,SO,false>& submatrix, size_t nonzeros ) const
242 {
243  using ElementType = ElementType_< Submatrix<MT,AF,SO,false> >;
244 
245  const size_t m( submatrix.rows() );
246  const size_t n( submatrix.columns() );
247 
248  if( nonzeros > m*n ) {
249  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
250  }
251 
252  if( m == 0UL || n == 0UL ) return;
253 
254  submatrix.reset();
255  submatrix.reserve( nonzeros );
256 
257  while( submatrix.nonZeros() < nonzeros ) {
258  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
259  }
260 }
262 //*************************************************************************************************
263 
264 
265 //*************************************************************************************************
274 template< typename MT // Type of the sparse matrix
275  , bool AF // Alignment flag
276  , bool SO > // Storage order
277 template< typename Arg > // Min/max argument type
278 inline void Rand< Submatrix<MT,AF,SO,false> >::randomize( Submatrix<MT,AF,SO,false>& submatrix,
279  const Arg& min, const Arg& max ) const
280 {
281  using ElementType = ElementType_< Submatrix<MT,AF,SO,false> >;
282 
283  const size_t m( submatrix.rows() );
284  const size_t n( submatrix.columns() );
285 
286  if( m == 0UL || n == 0UL ) return;
287 
288  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
289 
290  submatrix.reset();
291  submatrix.reserve( nonzeros );
292 
293  while( submatrix.nonZeros() < nonzeros ) {
294  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
295  }
296 }
298 //*************************************************************************************************
299 
300 
301 //*************************************************************************************************
312 template< typename MT // Type of the sparse matrix
313  , bool AF // Alignment flag
314  , bool SO > // Storage order
315 template< typename Arg > // Min/max argument type
316 inline void Rand< Submatrix<MT,AF,SO,false> >::randomize( Submatrix<MT,AF,SO,false>& submatrix,
317  size_t nonzeros, const Arg& min, const Arg& max ) const
318 {
319  using ElementType = ElementType_< Submatrix<MT,AF,SO,false> >;
320 
321  const size_t m( submatrix.rows() );
322  const size_t n( submatrix.columns() );
323 
324  if( nonzeros > m*n ) {
325  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
326  }
327 
328  if( m == 0UL || n == 0UL ) return;
329 
330  submatrix.reset();
331  submatrix.reserve( nonzeros );
332 
333  while( submatrix.nonZeros() < nonzeros ) {
334  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
335  }
336 }
338 //*************************************************************************************************
339 
340 } // namespace blaze
341 
342 #endif
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.
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:927
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.
Submatrix< MT, AF > submatrix(Matrix< MT, SO > &matrix, size_t row, size_t column, size_t m, size_t n)
Creating a view on a specific submatrix of the given matrix.
Definition: Submatrix.h:352
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
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:1809
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:290
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3081
Header file for the dense matrix SMP implementation.
Header file for the exception macros of the math module.
const bool rowMajor
Storage order flag for row-major matrices.
Definition: StorageOrder.h:71