Blaze  3.6
Column.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_COLUMN_H_
36 #define _BLAZE_MATH_COLUMN_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/Row.h>
52 #include <blaze/util/Random.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // RAND SPECIALIZATION FOR DENSE COLUMNS
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename MT // Type of the matrix
72  , bool SO // Storage order
73  , bool SF // Symmetry flag
74  , size_t... CCAs > // Compile time column arguments
75 class Rand< Column<MT,SO,true,SF,CCAs...> >
76 {
77  public:
78  //**Randomize functions*************************************************************************
81  template< typename CT >
82  inline void randomize( CT&& column ) const;
83 
84  template< typename CT, typename Arg >
85  inline void randomize( CT&& column, 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  , size_t... CCAs > // Compile time column arguments
104 template< typename CT > // Type of the column
105 inline void Rand< Column<MT,SO,true,SF,CCAs...> >::randomize( CT&& column ) const
106 {
107  using blaze::randomize;
108 
109  using ColumnType = RemoveReference_t<CT>;
110 
113 
114  for( size_t i=0UL; i<column.size(); ++i ) {
115  randomize( column[i] );
116  }
117 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
131 template< typename MT // Type of the matrix
132  , bool SO // Storage order
133  , bool SF // Symmetry flag
134  , size_t... CCAs > // Compile time column arguments
135 template< typename CT // Type of the column
136  , typename Arg > // Min/max argument type
137 inline void Rand< Column<MT,SO,true,SF,CCAs...> >::randomize( CT&& column, const Arg& min, const Arg& max ) const
138 {
139  using blaze::randomize;
140 
141  using ColumnType = RemoveReference_t<CT>;
142 
145 
146  for( size_t i=0UL; i<column.size(); ++i ) {
147  randomize( column[i], min, max );
148  }
149 }
151 //*************************************************************************************************
152 
153 
154 
155 
156 //=================================================================================================
157 //
158 // RAND SPECIALIZATION FOR SPARSE COLUMNS
159 //
160 //=================================================================================================
161 
162 //*************************************************************************************************
169 template< typename MT // Type of the matrix
170  , bool SO // Storage order
171  , bool SF // Symmetry flag
172  , size_t... CCAs > // Compile time column arguments
173 class Rand< Column<MT,SO,false,SF,CCAs...> >
174 {
175  public:
176  //**Randomize functions*************************************************************************
179  template< typename CT >
180  inline void randomize( CT&& column ) const;
181 
182  template< typename CT >
183  inline void randomize( CT&& column, size_t nonzeros ) const;
184 
185  template< typename CT, typename Arg >
186  inline void randomize( CT&& column, const Arg& min, const Arg& max ) const;
187 
188  template< typename CT, typename Arg >
189  inline void randomize( CT&& column, size_t nonzeros, const Arg& min, const Arg& max ) const;
191  //**********************************************************************************************
192 };
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
204 template< typename MT // Type of the matrix
205  , bool SO // Storage order
206  , bool SF // Symmetry flag
207  , size_t... CCAs > // Compile time column arguments
208 template< typename CT > // Type of the column
209 inline void Rand< Column<MT,SO,false,SF,CCAs...> >::randomize( CT&& column ) const
210 {
211  using ColumnType = RemoveReference_t<CT>;
212  using ElementType = ElementType_t<ColumnType>;
213 
216 
217  const size_t size( column.size() );
218 
219  if( size == 0UL ) return;
220 
221  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
222 
223  column.reset();
224  column.reserve( nonzeros );
225 
226  while( column.nonZeros() < nonzeros ) {
227  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
228  }
229 }
231 //*************************************************************************************************
232 
233 
234 //*************************************************************************************************
243 template< typename MT // Type of the matrix
244  , bool SO // Storage order
245  , bool SF // Symmetry flag
246  , size_t... CCAs > // Compile time column arguments
247 template< typename CT > // Type of the column
248 inline void Rand< Column<MT,SO,false,SF,CCAs...> >::randomize( CT&& column, size_t nonzeros ) const
249 {
250  using ColumnType = RemoveReference_t<CT>;
251  using ElementType = ElementType_t<ColumnType>;
252 
255 
256  const size_t size( column.size() );
257 
258  if( nonzeros > size ) {
259  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
260  }
261 
262  if( size == 0UL ) return;
263 
264  column.reset();
265  column.reserve( nonzeros );
266 
267  while( column.nonZeros() < nonzeros ) {
268  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
269  }
270 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
284 template< typename MT // Type of the matrix
285  , bool SO // Storage order
286  , bool SF // Symmetry flag
287  , size_t... CCAs > // Compile time column arguments
288 template< typename CT // Type of the column
289  , typename Arg > // Min/max argument type
290 inline void Rand< Column<MT,SO,false,SF,CCAs...> >::randomize( CT&& column, const Arg& min, const Arg& max ) const
291 {
292  using ColumnType = RemoveReference_t<CT>;
293  using ElementType = ElementType_t<ColumnType>;
294 
297 
298  const size_t size( column.size() );
299 
300  if( size == 0UL ) return;
301 
302  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
303 
304  column.reset();
305  column.reserve( nonzeros );
306 
307  while( column.nonZeros() < nonzeros ) {
308  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
309  }
310 }
312 //*************************************************************************************************
313 
314 
315 //*************************************************************************************************
326 template< typename MT // Type of the matrix
327  , bool SO // Storage order
328  , bool SF // Symmetry flag
329  , size_t... CCAs > // Compile time column arguments
330 template< typename CT // Type of the column
331  , typename Arg > // Min/max argument type
332 inline void Rand< Column<MT,SO,false,SF,CCAs...> >::randomize( CT&& column, size_t nonzeros,
333  const Arg& min, const Arg& max ) const
334 {
335  using ColumnType = RemoveReference_t<CT>;
336  using ElementType = ElementType_t<ColumnType>;
337 
340 
341  const size_t size( column.size() );
342 
343  if( nonzeros > size ) {
344  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
345  }
346 
347  if( size == 0UL ) return;
348 
349  column.reset();
350  column.reserve( nonzeros );
351 
352  while( column.nonZeros() < nonzeros ) {
353  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
354  }
355 }
357 //*************************************************************************************************
358 
359 } // namespace blaze
360 
361 #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.
decltype(auto) column(Matrix< MT, SO > &matrix, RCAs... args)
Creating a view on a specific column of the given matrix.
Definition: Column.h:133
decltype(auto) ceil(const DenseMatrix< MT, SO > &dm)
Applies the ceil() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1240
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
Implementation of a random number generator.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Header file for the dense vector SMP implementation.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
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:1162
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type,...
Definition: SparseVector.h:61
Constraint on the data type.
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:1198
Constraint on the data type.
Header file for the implementation of the Column view.
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_TYPE(T)
Constraint on the data type.In case the given data type T is not a column type (i....
Definition: Column.h:61
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Header file for the RemoveReference type trait.
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type,...
Definition: DenseVector.h:61
Header file for the sparse vector SMP implementation.
Header file for the implementation of the Row view.