Blaze 3.9
Columns.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_COLUMNS_H_
36#define _BLAZE_MATH_COLUMNS_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 COLUMN SELECTIONS
61//
62//=================================================================================================
63
64//*************************************************************************************************
71template< typename MT // Type of the matrix
72 , bool SO // Storage order
73 , bool SF // Symmetry flag
74 , typename... CCAs > // Compile time column arguments
75class Rand< Columns<MT,SO,true,SF,CCAs...> >
76{
77 public:
78 //**********************************************************************************************
84 template< typename CT > // Type of the column selection
85 inline void randomize( CT&& columns ) const
86 {
87 using blaze::randomize;
88
89 using ColumnsType = RemoveReference_t<CT>;
90
93
94 if( SO == false ) {
95 for( size_t i=0UL; i<columns.rows(); ++i ) {
96 for( size_t j=0UL; j<columns.columns(); ++j ) {
97 randomize( columns(i,j) );
98 }
99 }
100 }
101 else {
102 for( size_t j=0UL; j<columns.columns(); ++j ) {
103 for( size_t i=0UL; i<columns.rows(); ++i ) {
104 randomize( columns(i,j) );
105 }
106 }
107 }
108 }
109 //**********************************************************************************************
110
111 //**********************************************************************************************
119 template< typename CT // Type of the column selection
120 , typename Arg > // Min/max argument type
121 inline void randomize( CT&& columns, const Arg& min, const Arg& max ) const
122 {
123 using blaze::randomize;
124
125 using ColumnsType = RemoveReference_t<CT>;
126
129
130 if( SO == false ) {
131 for( size_t i=0UL; i<columns.rows(); ++i ) {
132 for( size_t j=0UL; j<columns.columns(); ++j ) {
133 randomize( columns(i,j), min, max );
134 }
135 }
136 }
137 else {
138 for( size_t j=0UL; j<columns.columns(); ++j ) {
139 for( size_t i=0UL; i<columns.rows(); ++i ) {
140 randomize( columns(i,j), min, max );
141 }
142 }
143 }
144 }
145 //**********************************************************************************************
146};
148//*************************************************************************************************
149
150
151
152
153//=================================================================================================
154//
155// RAND SPECIALIZATION FOR SPARSE COLUMN SELECTIONS
156//
157//=================================================================================================
158
159//*************************************************************************************************
166template< typename MT // Type of the matrix
167 , bool SO // Storage order
168 , bool SF // Symmetry flag
169 , typename... CCAs > // Compile time column arguments
170class Rand< Columns<MT,SO,false,SF,CCAs...> >
171{
172 public:
173 //**********************************************************************************************
179 template< typename CT > // Type of the column selection
180 inline void randomize( CT&& columns ) const
181 {
182 using ColumnsType = RemoveReference_t<CT>;
183 using ElementType = ElementType_t<ColumnsType>;
184
187
188 const size_t m( columns.rows() );
189 const size_t n( columns.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 columns.reset();
196 columns.reserve( nonzeros );
197
198 while( columns.nonZeros() < nonzeros ) {
199 columns( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
200 }
201 }
202 //**********************************************************************************************
203
204 //**********************************************************************************************
212 template< typename CT > // Type of the column selection
213 inline void randomize( CT&& columns, size_t nonzeros ) const
214 {
215 using ColumnsType = RemoveReference_t<CT>;
216 using ElementType = ElementType_t<ColumnsType>;
217
220
221 const size_t m( columns.rows() );
222 const size_t n( columns.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 columns.reset();
231 columns.reserve( nonzeros );
232
233 while( columns.nonZeros() < nonzeros ) {
234 columns( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
235 }
236 }
237 //**********************************************************************************************
238
239 //**********************************************************************************************
247 template< typename CT // Type of the column selection
248 , typename Arg > // Min/max argument type
249 inline void randomize( CT&& columns, const Arg& min, const Arg& max ) const
250 {
251 using ColumnsType = RemoveReference_t<CT>;
252 using ElementType = ElementType_t<ColumnsType>;
253
256
257 const size_t m( columns.rows() );
258 const size_t n( columns.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 columns.reset();
265 columns.reserve( nonzeros );
266
267 while( columns.nonZeros() < nonzeros ) {
268 columns( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
269 }
270 }
271 //**********************************************************************************************
272
273 //**********************************************************************************************
283 template< typename CT // Type of the column selection
284 , typename Arg > // Min/max argument type
285 inline void randomize( CT&& columns, size_t nonzeros, const Arg& min, const Arg& max ) const
286 {
287 using ColumnsType = RemoveReference_t<CT>;
288 using ElementType = ElementType_t<ColumnsType>;
289
292
293 const size_t m( columns.rows() );
294 const size_t n( columns.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 columns.reset();
303 columns.reserve( nonzeros );
304
305 while( columns.nonZeros() < nonzeros ) {
306 columns( 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) columns(MT &&matrix, const std::vector< T > &indices, RCAs... args)
Creating a view on a selection of columns of the given matrix.
Definition: Columns.h:703
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_DENSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: DenseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_COLUMNS_TYPE(T)
Constraint on the data type.
Definition: Columns.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: SparseMatrix.h:61
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:626
#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 Columns view.
Header file for the implementation of the Rows view.