Band.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_BAND_H_
36 #define _BLAZE_MATH_BAND_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
50 #include <blaze/math/views/Band.h>
51 #include <blaze/util/Random.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // RAND SPECIALIZATION FOR DENSE BANDS
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
70 template< typename MT // Type of the matrix
71  , bool TF // Transpose flag
72  , bool MF // Multiplication flag
73  , ptrdiff_t... CBAs > // Compile time band arguments
74 class Rand< Band<MT,TF,true,MF,CBAs...> >
75 {
76  public:
77  //**Randomize functions*************************************************************************
80  template< typename BT >
81  inline void randomize( BT&& band ) const;
82 
83  template< typename BT, typename Arg >
84  inline void randomize( BT&& band, const Arg& min, const Arg& max ) const;
86  //**********************************************************************************************
87 };
89 //*************************************************************************************************
90 
91 
92 //*************************************************************************************************
99 template< typename MT // Type of the matrix
100  , bool TF // Transpose flag
101  , bool MF // Multiplication flag
102  , ptrdiff_t... CBAs > // Compile time band arguments
103 template< typename BT > // Type of the band
104 inline void Rand< Band<MT,TF,true,MF,CBAs...> >::randomize( BT&& band ) const
105 {
106  using blaze::randomize;
107 
108  using BandType = RemoveReference_<BT>;
109 
112 
113  for( size_t i=0UL; i<band.size(); ++i ) {
114  randomize( band[i] );
115  }
116 }
118 //*************************************************************************************************
119 
120 
121 //*************************************************************************************************
130 template< typename MT // Type of the matrix
131  , bool TF // Transpose flag
132  , bool MF // Multiplication flag
133  , ptrdiff_t... CBAs > // Compile time band arguments
134 template< typename BT // Type of the band
135  , typename Arg > // Min/max argument type
136 inline void Rand< Band<MT,TF,true,MF,CBAs...> >::randomize( BT&& band, const Arg& min, const Arg& max ) const
137 {
138  using blaze::randomize;
139 
140  using BandType = RemoveReference_<BT>;
141 
144 
145  for( size_t i=0UL; i<band.size(); ++i ) {
146  randomize( band[i], min, max );
147  }
148 }
150 //*************************************************************************************************
151 
152 
153 
154 
155 //=================================================================================================
156 //
157 // RAND SPECIALIZATION FOR SPARSE BANDS
158 //
159 //=================================================================================================
160 
161 //*************************************************************************************************
168 template< typename MT // Type of the matrix
169  , bool TF // Transpose flag
170  , bool MF // Multiplication flag
171  , ptrdiff_t... CBAs > // Compile time band arguments
172 class Rand< Band<MT,TF,false,MF,CBAs...> >
173 {
174  public:
175  //**Randomize functions*************************************************************************
178  template< typename BT >
179  inline void randomize( BT&& band ) const;
180 
181  template< typename BT >
182  inline void randomize( BT&& band, size_t nonzeros ) const;
183 
184  template< typename BT, typename Arg >
185  inline void randomize( BT&& band, const Arg& min, const Arg& max ) const;
186 
187  template< typename BT, typename Arg >
188  inline void randomize( BT&& band, size_t nonzeros, const Arg& min, const Arg& max ) const;
190  //**********************************************************************************************
191 };
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
203 template< typename MT // Type of the matrix
204  , bool TF // Transpose flag
205  , bool MF // Multiplication flag
206  , ptrdiff_t... CBAs > // Compile time band arguments
207 template< typename BT > // Type of the band
208 inline void Rand< Band<MT,TF,false,MF,CBAs...> >::randomize( BT&& band ) const
209 {
210  using BandType = RemoveReference_<BT>;
211  using ElementType = ElementType_<BandType>;
212 
215 
216  const size_t size( band.size() );
217 
218  if( size == 0UL ) return;
219 
220  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
221 
222  band.reset();
223  band.reserve( nonzeros );
224 
225  while( band.nonZeros() < nonzeros ) {
226  band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
227  }
228 }
230 //*************************************************************************************************
231 
232 
233 //*************************************************************************************************
242 template< typename MT // Type of the matrix
243  , bool TF // Transpose flag
244  , bool MF // Multiplication flag
245  , ptrdiff_t... CBAs > // Compile time band arguments
246 template< typename BT > // Type of the band
247 inline void Rand< Band<MT,TF,false,MF,CBAs...> >::randomize( BT&& band, size_t nonzeros ) const
248 {
249  using BandType = RemoveReference_<BT>;
250  using ElementType = ElementType_<BandType>;
251 
254 
255  const size_t size( band.size() );
256 
257  if( nonzeros > size ) {
258  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
259  }
260 
261  if( size == 0UL ) return;
262 
263  band.reset();
264  band.reserve( nonzeros );
265 
266  while( band.nonZeros() < nonzeros ) {
267  band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
268  }
269 }
271 //*************************************************************************************************
272 
273 
274 //*************************************************************************************************
283 template< typename MT // Type of the matrix
284  , bool TF // Transpose flag
285  , bool MF // Multiplication flag
286  , ptrdiff_t... CBAs > // Compile time band arguments
287 template< typename BT // Type of the band
288  , typename Arg > // Min/max argument type
289 inline void Rand< Band<MT,TF,false,MF,CBAs...> >::randomize( BT&& band, const Arg& min, const Arg& max ) const
290 {
291  using BandType = RemoveReference_<BT>;
292  using ElementType = ElementType_<BandType>;
293 
296 
297  const size_t size( band.size() );
298 
299  if( size == 0UL ) return;
300 
301  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
302 
303  band.reset();
304  band.reserve( nonzeros );
305 
306  while( band.nonZeros() < nonzeros ) {
307  band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
308  }
309 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
325 template< typename MT // Type of the matrix
326  , bool TF // Transpose flag
327  , bool MF // Multiplication flag
328  , ptrdiff_t... CBAs > // Compile time band arguments
329 template< typename BT // Type of the band
330  , typename Arg > // Min/max argument type
331 inline void Rand< Band<MT,TF,false,MF,CBAs...> >::randomize( BT&& band, size_t nonzeros,
332  const Arg& min, const Arg& max ) const
333 {
334  using BandType = RemoveReference_<BT>;
335  using ElementType = ElementType_<BandType>;
336 
339 
340  const size_t size( band.size() );
341 
342  if( nonzeros > size ) {
343  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
344  }
345 
346  if( size == 0UL ) return;
347 
348  band.reset();
349  band.reserve( nonzeros );
350 
351  while( band.nonZeros() < nonzeros ) {
352  band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
353  }
354 }
356 //*************************************************************************************************
357 
358 } // namespace blaze
359 
360 #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.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
Header file for the implementation of the Band view.
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
#define BLAZE_CONSTRAINT_MUST_BE_BAND_TYPE(T)
Constraint on the data type.In case the given data type T is not a band type (i.e. a dense or sparse band), a compilation error is created.
Definition: Band.h:61
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1903
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:1950
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
Type ElementType
Type of the compressed matrix elements.
Definition: CompressedMatrix.h:3079
#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.
Constraint on the data type.
decltype(auto) band(Matrix< MT, SO > &matrix, RBAs... args)
Creating a view on a specific band of the given matrix.
Definition: Band.h:134
Constraint on the data type.
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.