Blaze 3.9
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>
51#include <blaze/util/Random.h>
53
54
55namespace blaze {
56
57//=================================================================================================
58//
59// RAND SPECIALIZATION FOR DENSE BANDS
60//
61//=================================================================================================
62
63//*************************************************************************************************
70template< typename MT // Type of the matrix
71 , bool TF // Transpose flag
72 , bool MF // Multiplication flag
73 , ptrdiff_t... CBAs > // Compile time band arguments
74class Rand< Band<MT,TF,true,MF,CBAs...> >
75{
76 public:
77 //**********************************************************************************************
83 template< typename BT > // Type of the band
84 inline void randomize( BT&& band ) const
85 {
86 using blaze::randomize;
87
88 using BandType = RemoveReference_t<BT>;
89
92
93 for( size_t i=0UL; i<band.size(); ++i ) {
94 randomize( band[i] );
95 }
96 }
97 //**********************************************************************************************
98
99 //**********************************************************************************************
107 template< typename BT // Type of the band
108 , typename Arg > // Min/max argument type
109 inline void randomize( BT&& band, const Arg& min, const Arg& max ) const
110 {
111 using blaze::randomize;
112
113 using BandType = RemoveReference_t<BT>;
114
117
118 for( size_t i=0UL; i<band.size(); ++i ) {
119 randomize( band[i], min, max );
120 }
121 }
122 //**********************************************************************************************
123};
125//*************************************************************************************************
126
127
128
129
130//=================================================================================================
131//
132// RAND SPECIALIZATION FOR SPARSE BANDS
133//
134//=================================================================================================
135
136//*************************************************************************************************
143template< typename MT // Type of the matrix
144 , bool TF // Transpose flag
145 , bool MF // Multiplication flag
146 , ptrdiff_t... CBAs > // Compile time band arguments
147class Rand< Band<MT,TF,false,MF,CBAs...> >
148{
149 public:
150 //**********************************************************************************************
156 template< typename BT > // Type of the band
157 inline void randomize( BT&& band ) const
158 {
159 using BandType = RemoveReference_t<BT>;
160 using ElementType = ElementType_t<BandType>;
161
164
165 const size_t size( band.size() );
166
167 if( size == 0UL ) return;
168
169 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
170
171 band.reset();
172 band.reserve( nonzeros );
173
174 while( band.nonZeros() < nonzeros ) {
175 band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
176 }
177 }
178 //**********************************************************************************************
179
180 //**********************************************************************************************
188 template< typename BT > // Type of the band
189 inline void randomize( BT&& band, size_t nonzeros ) const
190 {
191 using BandType = RemoveReference_t<BT>;
192 using ElementType = ElementType_t<BandType>;
193
196
197 const size_t size( band.size() );
198
199 if( nonzeros > size ) {
200 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
201 }
202
203 if( size == 0UL ) return;
204
205 band.reset();
206 band.reserve( nonzeros );
207
208 while( band.nonZeros() < nonzeros ) {
209 band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
210 }
211 }
212 //**********************************************************************************************
213
214 //**********************************************************************************************
222 template< typename BT // Type of the band
223 , typename Arg > // Min/max argument type
224 inline void randomize( BT&& band, const Arg& min, const Arg& max ) const
225 {
226 using BandType = RemoveReference_t<BT>;
227 using ElementType = ElementType_t<BandType>;
228
231
232 const size_t size( band.size() );
233
234 if( size == 0UL ) return;
235
236 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
237
238 band.reset();
239 band.reserve( nonzeros );
240
241 while( band.nonZeros() < nonzeros ) {
242 band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
243 }
244 }
245 //**********************************************************************************************
246
247 //**********************************************************************************************
257 template< typename BT // Type of the band
258 , typename Arg > // Min/max argument type
259 inline void randomize( BT&& band, size_t nonzeros, const Arg& min, const Arg& max ) const
260 {
261 using BandType = RemoveReference_t<BT>;
262 using ElementType = ElementType_t<BandType>;
263
266
267 const size_t size( band.size() );
268
269 if( nonzeros > size ) {
270 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
271 }
272
273 if( size == 0UL ) return;
274
275 band.reset();
276 band.reserve( nonzeros );
277
278 while( band.nonZeros() < nonzeros ) {
279 band[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
280 }
281 }
282 //**********************************************************************************************
283};
285//*************************************************************************************************
286
287} // namespace blaze
288
289#endif
Header file for auxiliary alias declarations.
Header file for the RemoveReference type trait.
Pointer difference type of the Blaze library.
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
decltype(auto) band(Matrix< MT, SO > &&matrix, ptrdiff_t index, RBAs... args)
Creating a view on a specific band of the given temporary matrix.
Definition: Band.h:343
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_BAND_TYPE(T)
Constraint on the data type.
Definition: Band.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: SparseVector.h:61
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: DenseVector.h:61
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:676
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 vector SMP implementation.
Header file for the sparse vector SMP implementation.
Implementation of a random number generator.
Header file for the implementation of the Band view.