Blaze 3.9
CompressedMatrix.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_COMPRESSEDMATRIX_H_
36#define _BLAZE_MATH_COMPRESSEDMATRIX_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <cmath>
44#include <vector>
51#include <blaze/util/Assert.h>
52#include <blaze/util/Indices.h>
53#include <blaze/util/Random.h>
54
55
56namespace blaze {
57
58//=================================================================================================
59//
60// RAND SPECIALIZATION
61//
62//=================================================================================================
63
64//*************************************************************************************************
71template< typename Type // Data type of the matrix
72 , bool SO // Storage order
73 , typename Tag > // Tag type
74class Rand< CompressedMatrix<Type,SO,Tag> >
75{
76 public:
77 //**********************************************************************************************
84 inline const CompressedMatrix<Type,SO,Tag> generate( size_t m, size_t n ) const
85 {
86 CompressedMatrix<Type,SO,Tag> matrix( m, n );
87 randomize( matrix );
88
89 return matrix;
90 }
91 //**********************************************************************************************
92
93 //**********************************************************************************************
102 inline const CompressedMatrix<Type,SO,Tag>
103 generate( size_t m, size_t n, size_t nonzeros ) const
104 {
105 if( nonzeros > m*n ) {
106 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
107 }
108
109 CompressedMatrix<Type,SO,Tag> matrix( m, n );
110 randomize( matrix, nonzeros );
111
112 return matrix;
113 }
114 //**********************************************************************************************
115
116 //**********************************************************************************************
125 template< typename Arg > // Min/max argument type
126 inline const CompressedMatrix<Type,SO,Tag>
127 generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
128 {
129 CompressedMatrix<Type,SO,Tag> matrix( m, n );
130 randomize( matrix, min, max );
131
132 return matrix;
133 }
134 //**********************************************************************************************
135
136 //**********************************************************************************************
147 template< typename Arg > // Min/max argument type
148 inline const CompressedMatrix<Type,SO,Tag>
149 generate( size_t m, size_t n, size_t nonzeros,
150 const Arg& min, const Arg& max ) const
151 {
152 if( nonzeros > m*n ) {
153 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
154 }
155
156 CompressedMatrix<Type,SO,Tag> matrix( m, n );
157 randomize( matrix, nonzeros, min, max );
158
159 return matrix;
160 }
161 //**********************************************************************************************
162
163 //**********************************************************************************************
169 inline void randomize( CompressedMatrix<Type,SO,Tag>& matrix ) const
170 {
171 const size_t m( matrix.rows() );
172 const size_t n( matrix.columns() );
173
174 if( m == 0UL || n == 0UL ) return;
175
176 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
177
178 randomize( matrix, nonzeros );
179 }
180 //**********************************************************************************************
181
182 //**********************************************************************************************
190 inline void randomize( CompressedMatrix<Type,false,Tag>& matrix, size_t nonzeros ) const
191 {
192 const size_t m( matrix.rows() );
193 const size_t n( matrix.columns() );
194
195 if( nonzeros > m*n ) {
196 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
197 }
198
199 if( m == 0UL || n == 0UL ) return;
200
201 matrix.reset();
202 matrix.reserve( nonzeros );
203
204 std::vector<size_t> dist( m );
205
206 for( size_t nz=0UL; nz<nonzeros; ) {
207 const size_t index = rand<size_t>( 0UL, m-1UL );
208 if( dist[index] == n ) continue;
209 ++dist[index];
210 ++nz;
211 }
212
213 for( size_t i=0UL; i<m; ++i ) {
214 const Indices<size_t> indices( 0UL, n-1UL, dist[i] );
215 for( size_t j : indices ) {
216 matrix.append( i, j, rand<Type>() );
217 }
218 matrix.finalize( i );
219 }
220 }
221 //**********************************************************************************************
222
223 //**********************************************************************************************
231 inline void randomize( CompressedMatrix<Type,true,Tag>& matrix, size_t nonzeros ) const
232 {
233 const size_t m( matrix.rows() );
234 const size_t n( matrix.columns() );
235
236 if( nonzeros > m*n ) {
237 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
238 }
239
240 if( m == 0UL || n == 0UL ) return;
241
242 matrix.reset();
243 matrix.reserve( nonzeros );
244
245 std::vector<size_t> dist( n );
246
247 for( size_t nz=0UL; nz<nonzeros; ) {
248 const size_t index = rand<size_t>( 0UL, n-1UL );
249 if( dist[index] == m ) continue;
250 ++dist[index];
251 ++nz;
252 }
253
254 for( size_t j=0UL; j<n; ++j ) {
255 const Indices<size_t> indices( 0UL, m-1UL, dist[j] );
256 for( size_t i : indices ) {
257 matrix.append( i, j, rand<Type>() );
258 }
259 matrix.finalize( j );
260 }
261 }
262 //**********************************************************************************************
263
264 //**********************************************************************************************
272 template< typename Arg > // Min/max argument type
273 inline void randomize( CompressedMatrix<Type,SO,Tag>& matrix,
274 const Arg& min, const Arg& max ) const
275 {
276 const size_t m( matrix.rows() );
277 const size_t n( matrix.columns() );
278
279 if( m == 0UL || n == 0UL ) return;
280
281 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
282
283 randomize( matrix, nonzeros, min, max );
284 }
285 //**********************************************************************************************
286
287 //**********************************************************************************************
297 template< typename Arg > // Min/max argument type
298 inline void randomize( CompressedMatrix<Type,false,Tag>& matrix,
299 size_t nonzeros, const Arg& min, const Arg& max ) const
300 {
301 const size_t m( matrix.rows() );
302 const size_t n( matrix.columns() );
303
304 if( nonzeros > m*n ) {
305 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
306 }
307
308 if( m == 0UL || n == 0UL ) return;
309
310 matrix.reset();
311 matrix.reserve( nonzeros );
312
313 std::vector<size_t> dist( m );
314
315 for( size_t nz=0UL; nz<nonzeros; ) {
316 const size_t index = rand<size_t>( 0UL, m-1UL );
317 if( dist[index] == n ) continue;
318 ++dist[index];
319 ++nz;
320 }
321
322 for( size_t i=0UL; i<m; ++i ) {
323 const Indices<size_t> indices( 0UL, n-1UL, dist[i] );
324 for( size_t j : indices ) {
325 matrix.append( i, j, rand<Type>( min, max ) );
326 }
327 matrix.finalize( i );
328 }
329 }
330 //**********************************************************************************************
331
332 //**********************************************************************************************
342 template< typename Arg > // Min/max argument type
343 inline void randomize( CompressedMatrix<Type,true,Tag>& matrix,
344 size_t nonzeros, const Arg& min, const Arg& max ) const
345 {
346 const size_t m( matrix.rows() );
347 const size_t n( matrix.columns() );
348
349 if( nonzeros > m*n ) {
350 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
351 }
352
353 if( m == 0UL || n == 0UL ) return;
354
355 matrix.reset();
356 matrix.reserve( nonzeros );
357
358 std::vector<size_t> dist( n );
359
360 for( size_t nz=0UL; nz<nonzeros; ) {
361 const size_t index = rand<size_t>( 0UL, n-1UL );
362 if( dist[index] == m ) continue;
363 ++dist[index];
364 ++nz;
365 }
366
367 for( size_t j=0UL; j<n; ++j ) {
368 const Indices<size_t> indices( 0UL, m-1UL, dist[j] );
369 for( size_t i : indices ) {
370 matrix.append( i, j, rand<Type>( min, max ) );
371 }
372 matrix.finalize( j );
373 }
374 }
375 //**********************************************************************************************
376};
378//*************************************************************************************************
379
380} // namespace blaze
381
382#endif
Header file for run time assertion macros.
Header file for the complete CompressedVector implementation.
Header file for the complete IdentityMatrix implementation.
Header file for the Indices class.
Header file for all basic SparseMatrix functionality.
Header file for the complete ZeroMatrix implementation.
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
decltype(auto) generate(size_t m, size_t n, OP op)
Generates a new dense matrix filled via the given custom binary operation.
Definition: DMatGenExpr.h:675
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.
Implementation of a compressed MxN matrix.
Implementation of a random number generator.