Blaze 3.9
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>
52#include <blaze/util/Random.h>
54
55
56namespace blaze {
57
58//=================================================================================================
59//
60// RAND SPECIALIZATION FOR DENSE SUBMATRICES
61//
62//=================================================================================================
63
64//*************************************************************************************************
71template< typename MT // Type of the matrix
72 , AlignmentFlag AF // Alignment flag
73 , bool SO // Storage order
74 , size_t... CSAs > // Compile time submatrix arguments
75class Rand< Submatrix<MT,AF,SO,true,CSAs...> >
76{
77 public:
78 //**********************************************************************************************
84 template< typename SMT > // Type of the submatrix
85 inline void randomize( SMT&& submatrix ) const
86 {
87 using blaze::randomize;
88
89 using SubmatrixType = RemoveReference_t<SMT>;
90
93
94 if( SO == rowMajor ) {
95 for( size_t i=0UL; i<submatrix.rows(); ++i ) {
96 for( size_t j=0UL; j<submatrix.columns(); ++j ) {
97 randomize( submatrix(i,j) );
98 }
99 }
100 }
101 else {
102 for( size_t j=0UL; j<submatrix.columns(); ++j ) {
103 for( size_t i=0UL; i<submatrix.rows(); ++i ) {
104 randomize( submatrix(i,j) );
105 }
106 }
107 }
108 }
109 //**********************************************************************************************
110
111 //**********************************************************************************************
119 template< typename SMT // Type of the submatrix
120 , typename Arg > // Min/max argument type
121 inline void randomize( SMT&& submatrix, const Arg& min, const Arg& max ) const
122 {
123 using blaze::randomize;
124
125 using SubmatrixType = RemoveReference_t<SMT>;
126
129
130 if( SO == rowMajor ) {
131 for( size_t i=0UL; i<submatrix.rows(); ++i ) {
132 for( size_t j=0UL; j<submatrix.columns(); ++j ) {
133 randomize( submatrix(i,j), min, max );
134 }
135 }
136 }
137 else {
138 for( size_t j=0UL; j<submatrix.columns(); ++j ) {
139 for( size_t i=0UL; i<submatrix.rows(); ++i ) {
140 randomize( submatrix(i,j), min, max );
141 }
142 }
143 }
144 }
145 //**********************************************************************************************
146};
148//*************************************************************************************************
149
150
151
152
153//=================================================================================================
154//
155// RAND SPECIALIZATION FOR SPARSE SUBMATRICES
156//
157//=================================================================================================
158
159//*************************************************************************************************
166template< typename MT // Type of the dense matrix
167 , AlignmentFlag AF // Alignment flag
168 , bool SO // Storage order
169 , size_t... CSAs > // Compile time submatrix arguments
170class Rand< Submatrix<MT,AF,SO,false,CSAs...> >
171{
172 public:
173 //**********************************************************************************************
179 template< typename SMT > // Type of the submatrix
180 inline void randomize( SMT&& submatrix ) const
181 {
182 using SubmatrixType = RemoveReference_t<SMT>;
183 using ElementType = ElementType_t<SubmatrixType>;
184
187
188 const size_t m( submatrix.rows() );
189 const size_t n( submatrix.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 submatrix.reset();
196 submatrix.reserve( nonzeros );
197
198 while( submatrix.nonZeros() < nonzeros ) {
199 submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
200 }
201 }
202 //**********************************************************************************************
203
204 //**********************************************************************************************
212 template< typename SMT > // Type of the submatrix
213 inline void randomize( SMT&& submatrix, size_t nonzeros ) const
214 {
215 using SubmatrixType = RemoveReference_t<SMT>;
216 using ElementType = ElementType_t<SubmatrixType>;
217
220
221 const size_t m( submatrix.rows() );
222 const size_t n( submatrix.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 submatrix.reset();
231 submatrix.reserve( nonzeros );
232
233 while( submatrix.nonZeros() < nonzeros ) {
234 submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
235 }
236 }
237 //**********************************************************************************************
238
239 //**********************************************************************************************
247 template< typename SMT // Type of the submatrix
248 , typename Arg > // Min/max argument type
249 inline void randomize( SMT&& submatrix, const Arg& min, const Arg& max ) const
250 {
251 using SubmatrixType = RemoveReference_t<SMT>;
252 using ElementType = ElementType_t<SubmatrixType>;
253
256
257 const size_t m( submatrix.rows() );
258 const size_t n( submatrix.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 submatrix.reset();
265 submatrix.reserve( nonzeros );
266
267 while( submatrix.nonZeros() < nonzeros ) {
268 submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
269 }
270 }
271 //**********************************************************************************************
272
273 //**********************************************************************************************
283 template< typename SMT // Type of the submatrix
284 , typename Arg > // Min/max argument type
285 inline void randomize( SMT&& submatrix, size_t nonzeros, const Arg& min, const Arg& max ) const
286 {
287 using SubmatrixType = RemoveReference_t<SMT>;
288 using ElementType = ElementType_t<SubmatrixType>;
289
292
293 const size_t m( submatrix.rows() );
294 const size_t n( submatrix.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 submatrix.reset();
303 submatrix.reserve( nonzeros );
304
305 while( submatrix.nonZeros() < nonzeros ) {
306 submatrix( 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_SUBMATRIX_TYPE(T)
Constraint on the data type.
Definition: Submatrix.h:61
#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
AlignmentFlag
Alignment flag for (un-)aligned vectors and matrices.
Definition: AlignmentFlag.h:63
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:626
decltype(auto) submatrix(Matrix< MT, SO > &, RSAs...)
Creating a view on a specific submatrix of the given matrix.
Definition: Submatrix.h:181
#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 Submatrix view.
Header file for the implementation of the Subvector view.