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>
47 #include <blaze/math/Exception.h>
51 #include <blaze/math/views/Rows.h>
52 #include <blaze/util/Random.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // RAND SPECIALIZATION FOR DENSE COLUMN SELECTIONS
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename MT // Type of the matrix
72  , bool SO // Storage order
73  , bool SF // Symmetry flag
74  , typename... CCAs > // Compile time column arguments
75 class Rand< Columns<MT,SO,true,SF,CCAs...> >
76 {
77  public:
78  //**Randomize functions*************************************************************************
81  template< typename CT >
82  inline void randomize( CT&& columns ) const;
83 
84  template< typename CT, typename Arg >
85  inline void randomize( CT&& columns, const Arg& min, const Arg& max ) const;
87  //**********************************************************************************************
88 };
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
100 template< typename MT // Type of the matrix
101  , bool SO // Storage order
102  , bool SF // Symmetry flag
103  , typename... CCAs > // Compile time column arguments
104 template< typename CT > // Type of the column selection
105 inline void Rand< Columns<MT,SO,true,SF,CCAs...> >::randomize( CT&& columns ) const
106 {
107  using blaze::randomize;
108 
109  using ColumnsType = RemoveReference_t<CT>;
110 
113 
114  if( SO == false ) {
115  for( size_t i=0UL; i<columns.rows(); ++i ) {
116  for( size_t j=0UL; j<columns.columns(); ++j ) {
117  randomize( columns(i,j) );
118  }
119  }
120  }
121  else {
122  for( size_t j=0UL; j<columns.columns(); ++j ) {
123  for( size_t i=0UL; i<columns.rows(); ++i ) {
124  randomize( columns(i,j) );
125  }
126  }
127  }
128 }
130 //*************************************************************************************************
131 
132 
133 //*************************************************************************************************
142 template< typename MT // Type of the matrix
143  , bool SO // Storage order
144  , bool SF // Symmetry flag
145  , typename... CCAs > // Compile time column arguments
146 template< typename CT // Type of the column selection
147  , typename Arg > // Min/max argument type
148 inline void Rand< Columns<MT,SO,true,SF,CCAs...> >::randomize( CT&& columns,
149  const Arg& min, const Arg& max ) const
150 {
151  using blaze::randomize;
152 
153  using ColumnsType = RemoveReference_t<CT>;
154 
157 
158  if( SO == false ) {
159  for( size_t i=0UL; i<columns.rows(); ++i ) {
160  for( size_t j=0UL; j<columns.columns(); ++j ) {
161  randomize( columns(i,j), min, max );
162  }
163  }
164  }
165  else {
166  for( size_t j=0UL; j<columns.columns(); ++j ) {
167  for( size_t i=0UL; i<columns.rows(); ++i ) {
168  randomize( columns(i,j), min, max );
169  }
170  }
171  }
172 }
174 //*************************************************************************************************
175 
176 
177 
178 
179 //=================================================================================================
180 //
181 // RAND SPECIALIZATION FOR SPARSE COLUMN SELECTIONS
182 //
183 //=================================================================================================
184 
185 //*************************************************************************************************
192 template< typename MT // Type of the matrix
193  , bool SO // Storage order
194  , bool SF // Symmetry flag
195  , typename... CCAs > // Compile time column arguments
196 class Rand< Columns<MT,SO,false,SF,CCAs...> >
197 {
198  public:
199  //**Randomize functions*************************************************************************
202  template< typename CT >
203  inline void randomize( CT&& columns ) const;
204 
205  template< typename CT >
206  inline void randomize( CT&& columns, size_t nonzeros ) const;
207 
208  template< typename CT, typename Arg >
209  inline void randomize( CT&& columns, const Arg& min, const Arg& max ) const;
210 
211  template< typename CT, typename Arg >
212  inline void randomize( CT&& columns, size_t nonzeros, const Arg& min, const Arg& max ) const;
214  //**********************************************************************************************
215 };
217 //*************************************************************************************************
218 
219 
220 //*************************************************************************************************
227 template< typename MT // Type of the matrix
228  , bool SO // Storage order
229  , bool SF // Symmetry flag
230  , typename... CCAs > // Compile time column arguments
231 template< typename CT > // Type of the column selection
232 inline void Rand< Columns<MT,SO,false,SF,CCAs...> >::randomize( CT&& columns ) const
233 {
234  using ColumnsType = RemoveReference_t<CT>;
235  using ElementType = ElementType_t<ColumnsType>;
236 
239 
240  const size_t m( columns.rows() );
241  const size_t n( columns.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  columns.reset();
248  columns.reserve( nonzeros );
249 
250  while( columns.nonZeros() < nonzeros ) {
251  columns( 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 matrix
268  , bool SO // Storage order
269  , bool SF // Symmetry flag
270  , typename... CCAs > // Compile time column arguments
271 template< typename CT > // Type of the column selection
272 inline void Rand< Columns<MT,SO,false,SF,CCAs...> >::randomize( CT&& columns, size_t nonzeros ) const
273 {
274  using ColumnsType = RemoveReference_t<CT>;
275  using ElementType = ElementType_t<ColumnsType>;
276 
279 
280  const size_t m( columns.rows() );
281  const size_t n( columns.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  columns.reset();
290  columns.reserve( nonzeros );
291 
292  while( columns.nonZeros() < nonzeros ) {
293  columns( 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 matrix
310  , bool SO // Storage order
311  , bool SF // Symmetry flag
312  , typename... CCAs > // Compile time column arguments
313 template< typename CT // Type of the column selection
314  , typename Arg > // Min/max argument type
315 inline void Rand< Columns<MT,SO,false,SF,CCAs...> >::randomize( CT&& columns,
316  const Arg& min, const Arg& max ) const
317 {
318  using ColumnsType = RemoveReference_t<CT>;
319  using ElementType = ElementType_t<ColumnsType>;
320 
323 
324  const size_t m( columns.rows() );
325  const size_t n( columns.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  columns.reset();
332  columns.reserve( nonzeros );
333 
334  while( columns.nonZeros() < nonzeros ) {
335  columns( 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 matrix
354  , bool SO // Storage order
355  , bool SF // Symmetry flag
356  , typename... CCAs > // Compile time column arguments
357 template< typename CT // Type of the column selection
358  , typename Arg > // Min/max argument type
359 inline void Rand< Columns<MT,SO,false,SF,CCAs...> >::randomize( CT&& columns, size_t nonzeros,
360  const Arg& min, const Arg& max ) const
361 {
362  using ColumnsType = RemoveReference_t<CT>;
363  using ElementType = ElementType_t<ColumnsType>;
364 
367 
368  const size_t m( columns.rows() );
369  const size_t n( columns.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  columns.reset();
378  columns.reserve( nonzeros );
379 
380  while( columns.nonZeros() < nonzeros ) {
381  columns( 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
#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.
Header file for the implementation of the Rows view.
#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:1239
Header file for the sparse matrix SMP implementation.
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
Implementation of a random number generator.
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
Constraint on the data type.
Constraint on the data type.
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
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3080
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:1147
Header file for the dense matrix SMP implementation.
Header file for the exception macros of the math module.
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:1179
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_BE_COLUMNS_TYPE(T)
Constraint on the data type.In case the given data type T is not a column selection (i...
Definition: Columns.h:61
Header file for the RemoveReference type trait.
#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
Header file for the implementation of the Columns view.