Blaze 3.9
DiagonalMatrix.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_DIAGONALMATRIX_H_
36#define _BLAZE_MATH_DIAGONALMATRIX_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <blaze/math/Aliases.h>
54#include <blaze/util/Indices.h>
56#include <blaze/util/Random.h>
57#include <blaze/util/Types.h>
58
59
60namespace blaze {
61
62//=================================================================================================
63//
64// RAND SPECIALIZATION
65//
66//=================================================================================================
67
68//*************************************************************************************************
75template< typename MT // Type of the adapted matrix
76 , bool SO // Storage order of the adapted matrix
77 , bool DF > // Density flag
78class Rand< DiagonalMatrix<MT,SO,DF> >
79{
80 public:
81 //**********************************************************************************************
86 inline const DiagonalMatrix<MT,SO,DF> generate() const
87 {
89
90 DiagonalMatrix<MT,SO,DF> matrix;
91 randomize( matrix );
92 return matrix;
93 }
94 //**********************************************************************************************
95
96 //**********************************************************************************************
102 inline const DiagonalMatrix<MT,SO,DF> generate( size_t n ) const
103 {
105
106 DiagonalMatrix<MT,SO,DF> matrix( n );
107 randomize( matrix );
108 return matrix;
109 }
110 //**********************************************************************************************
111
112 //**********************************************************************************************
120 inline const DiagonalMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const
121 {
124
125 if( nonzeros > n ) {
126 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
127 }
128
129 DiagonalMatrix<MT,SO,DF> matrix( n );
130 randomize( matrix, nonzeros );
131
132 return matrix;
133 }
134 //**********************************************************************************************
135
136 //**********************************************************************************************
143 template< typename Arg > // Min/max argument type
144 inline const DiagonalMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const
145 {
147
148 DiagonalMatrix<MT,SO,DF> matrix;
149 randomize( matrix, min, max );
150 return matrix;
151 }
152 //**********************************************************************************************
153
154 //**********************************************************************************************
162 template< typename Arg > // Min/max argument type
163 inline const DiagonalMatrix<MT,SO,DF>
164 generate( size_t n, const Arg& min, const Arg& max ) const
165 {
167
168 DiagonalMatrix<MT,SO,DF> matrix( n );
169 randomize( matrix, min, max );
170 return matrix;
171 }
172 //**********************************************************************************************
173
174 //**********************************************************************************************
184 template< typename Arg > // Min/max argument type
185 inline const DiagonalMatrix<MT,SO,DF>
186 generate( size_t n, size_t nonzeros, const Arg& min, const Arg& max ) const
187 {
190
191 if( nonzeros > DiagonalMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
192 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
193 }
194
195 DiagonalMatrix<MT,SO,DF> matrix( n );
196 randomize( matrix, nonzeros, min, max );
197
198 return matrix;
199 }
200 //**********************************************************************************************
201
202 //**********************************************************************************************
208 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix ) const
209 {
210 randomize( matrix, typename IsDenseMatrix<MT>::Type() );
211 }
212 //**********************************************************************************************
213
214 //**********************************************************************************************
222 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, size_t nonzeros ) const
223 {
225
226 using ET = ElementType_t<MT>;
227
228 const size_t n( matrix.rows() );
229
230 if( nonzeros > n ) {
231 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
232 }
233
234 if( n == 0UL ) return;
235
236 matrix.reset();
237 matrix.reserve( nonzeros );
238
239 Indices<size_t> indices( 0UL, n-1UL, nonzeros );
240 size_t i( 0UL );
241
242 for( size_t index : indices ) {
243 for( ; i<index; ++i )
244 matrix.finalize( i );
245 matrix.append( i, i, rand<ET>() );
246 }
247
248 for( ; i<n; ++i ) {
249 matrix.finalize( i );
250 }
251 }
252 //**********************************************************************************************
253
254 //**********************************************************************************************
262 template< typename Arg > // Min/max argument type
263 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix,
264 const Arg& min, const Arg& max ) const
265 {
266 randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
267 }
268 //**********************************************************************************************
269
270 //**********************************************************************************************
280 template< typename Arg > // Min/max argument type
281 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix,
282 size_t nonzeros, const Arg& min, const Arg& max ) const
283 {
285
286 using ET = ElementType_t<MT>;
287
288 const size_t n( matrix.rows() );
289
290 if( nonzeros > n ) {
291 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
292 }
293
294 if( n == 0UL ) return;
295
296 matrix.reset();
297 matrix.reserve( nonzeros );
298
299 Indices<size_t> indices( 0UL, n-1UL, nonzeros );
300 size_t i( 0UL );
301
302 for( size_t index : indices ) {
303 for( ; i<index; ++i )
304 matrix.finalize( i );
305 matrix.append( i, i, rand<ET>( min, max ) );
306 }
307
308 for( ; i<n; ++i ) {
309 matrix.finalize( i );
310 }
311 }
312 //**********************************************************************************************
313
314 private:
315 //**********************************************************************************************
321 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, TrueType ) const
322 {
324
325 using ET = ElementType_t<MT>;
326
327 const size_t n( matrix.rows() );
328
329 for( size_t i=0UL; i<n; ++i ) {
330 matrix(i,i) = rand<ET>();
331 }
332 }
333 //**********************************************************************************************
334
335 //**********************************************************************************************
341 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix, FalseType ) const
342 {
344
345 const size_t n( matrix.rows() );
346
347 if( n == 0UL ) return;
348
349 const size_t nonzeros( rand<size_t>( 1UL, n ) );
350
351 randomize( matrix, nonzeros );
352 }
353 //**********************************************************************************************
354
355 //**********************************************************************************************
363 template< typename Arg > // Min/max argument type
364 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix,
365 const Arg& min, const Arg& max, TrueType ) const
366 {
368
369 using ET = ElementType_t<MT>;
370
371 const size_t n( matrix.rows() );
372
373 for( size_t i=0UL; i<n; ++i ) {
374 matrix(i,i) = rand<ET>( min, max );
375 }
376 }
377 //**********************************************************************************************
378
379 //**********************************************************************************************
387 template< typename Arg > // Min/max argument type
388 inline void randomize( DiagonalMatrix<MT,SO,DF>& matrix,
389 const Arg& min, const Arg& max, FalseType ) const
390 {
392
393 const size_t n( matrix.rows() );
394
395 if( n == 0UL ) return;
396
397 const size_t nonzeros( rand<size_t>( 1UL, n ) );
398
399 randomize( matrix, nonzeros, min, max );
400 }
401 //**********************************************************************************************
402};
404//*************************************************************************************************
405
406
407
408
409//=================================================================================================
410//
411// MAKE FUNCTIONS
412//
413//=================================================================================================
414
415//*************************************************************************************************
422template< typename MT // Type of the adapted matrix
423 , bool SO // Storage order of the adapted matrix
424 , bool DF > // Density flag
425void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix )
426{
427 const size_t n( matrix.rows() );
428
429 reset( matrix );
430
431 for( size_t i=0UL; i<n; ++i ) {
432 matrix(i,i) = rand< ElementType_t<MT> >();
433 }
434
435 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
436}
438//*************************************************************************************************
439
440
441//*************************************************************************************************
450template< typename MT // Type of the adapted matrix
451 , bool SO // Storage order of the adapted matrix
452 , bool DF // Density flag
453 , typename Arg > // Min/max argument type
454void makeSymmetric( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
455{
456 using Type = ElementType_t<MT>;
457
458 const size_t n( matrix.rows() );
459
460 reset( matrix );
461
462 for( size_t i=0UL; i<n; ++i ) {
463 matrix(i,i) = rand<Type>( min, max );
464 }
465
466 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
467}
469//*************************************************************************************************
470
471
472//*************************************************************************************************
479template< typename MT // Type of the adapted matrix
480 , bool SO // Storage order of the adapted matrix
481 , bool DF > // Density flag
482void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix )
483{
484 using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
485
487
488 const size_t n( matrix.rows() );
489
490 reset( matrix );
491
492 for( size_t i=0UL; i<n; ++i ) {
493 matrix(i,i) = rand<Type>();
494 }
495
496 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
497}
499//*************************************************************************************************
500
501
502//*************************************************************************************************
511template< typename MT // Type of the adapted matrix
512 , bool SO // Storage order of the adapted matrix
513 , bool DF // Density flag
514 , typename Arg > // Min/max argument type
515void makeHermitian( DiagonalMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
516{
517 using Type = UnderlyingBuiltin_t< ElementType_t<MT> >;
518
520
521 const size_t n( matrix.rows() );
522
523 reset( matrix );
524
525 for( size_t i=0UL; i<n; ++i ) {
526 matrix(i,i) = rand<Type>( min, max );
527 }
528
529 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
530}
532//*************************************************************************************************
533
534
535//*************************************************************************************************
542template< typename MT // Type of the adapted matrix
543 , bool SO // Storage order of the adapted matrix
544 , bool DF > // Density flag
545void makePositiveDefinite( DiagonalMatrix<MT,SO,DF>& matrix )
546{
547 makeHermitian( matrix );
548}
550//*************************************************************************************************
551
552} // namespace blaze
553
554#endif
Header file for auxiliary alias declarations.
Header file for all basic DenseMatrix functionality.
Header file for the Indices class.
Header file for the IntegralConstant class template.
Header file for the IsDenseMatrix type trait.
Constraint on the data type.
Constraint on the data type.
Header file for all basic SparseMatrix functionality.
Header file for the UnderlyingBuiltin type trait.
Header file for the implementation of a diagonal matrix adaptor.
Constraint on the data type.
Constraint on the data type.
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
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:1534
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:1456
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
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: DenseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_NOT_BE_RESIZABLE_TYPE(T)
Constraint on the data type.
Definition: Resizable.h:81
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: SparseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_RESIZABLE_TYPE(T)
Constraint on the data type.
Definition: Resizable.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SCALAR_TYPE(T)
Constraint on the data type.
Definition: Scalar.h:61
constexpr void reset(Matrix< MT, SO > &matrix)
Resetting the given matrix.
Definition: Matrix.h:806
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:626
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
BoolConstant< true > TrueType
Type traits base class.
Definition: IntegralConstant.h:132
BoolConstant< false > FalseType
Type/value traits base class.
Definition: IntegralConstant.h:121
#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 random number generator.
Header file for basic type definitions.