Blaze 3.9
UniLowerMatrix.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_UNILOWERMATRIX_H_
36#define _BLAZE_MATH_UNILOWERMATRIX_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <cmath>
44#include <vector>
45#include <blaze/math/Aliases.h>
57#include <blaze/util/Indices.h>
60#include <blaze/util/Random.h>
61#include <blaze/util/Types.h>
62
63
64namespace blaze {
65
66//=================================================================================================
67//
68// RAND SPECIALIZATION
69//
70//=================================================================================================
71
72//*************************************************************************************************
79template< typename MT // Type of the adapted matrix
80 , bool SO // Storage order of the adapted matrix
81 , bool DF > // Density flag
82class Rand< UniLowerMatrix<MT,SO,DF> >
83{
84 public:
85 //**********************************************************************************************
90 inline const UniLowerMatrix<MT,SO,DF> generate() const
91 {
93
94 UniLowerMatrix<MT,SO,DF> matrix;
95 randomize( matrix );
96 return matrix;
97 }
98 //**********************************************************************************************
99
100 //**********************************************************************************************
106 inline const UniLowerMatrix<MT,SO,DF> generate( size_t n ) const
107 {
109
110 UniLowerMatrix<MT,SO,DF> matrix( n );
111 randomize( matrix );
112 return matrix;
113 }
114 //**********************************************************************************************
115
116 //**********************************************************************************************
124 inline const UniLowerMatrix<MT,SO,DF> generate( size_t n, size_t nonzeros ) const
125 {
128
129 if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
130 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
131 }
132
133 UniLowerMatrix<MT,SO,DF> matrix( n );
134 randomize( matrix, nonzeros );
135
136 return matrix;
137 }
138 //**********************************************************************************************
139
140 //**********************************************************************************************
147 template< typename Arg > // Min/max argument type
148 inline const UniLowerMatrix<MT,SO,DF> generate( const Arg& min, const Arg& max ) const
149 {
151
152 UniLowerMatrix<MT,SO,DF> matrix;
153 randomize( matrix, min, max );
154 return matrix;
155 }
156 //**********************************************************************************************
157
158 //**********************************************************************************************
166 template< typename Arg > // Min/max argument type
167 inline const UniLowerMatrix<MT,SO,DF>
168 generate( size_t n, const Arg& min, const Arg& max ) const
169 {
171
172 UniLowerMatrix<MT,SO,DF> matrix( n );
173 randomize( matrix, min, max );
174 return matrix;
175 }
176 //**********************************************************************************************
177
178 //**********************************************************************************************
188 template< typename Arg > // Min/max argument type
189 inline const UniLowerMatrix<MT,SO,DF>
190 generate( size_t n, size_t nonzeros, const Arg& min, const Arg& max ) const
191 {
194
195 if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
196 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
197 }
198
199 UniLowerMatrix<MT,SO,DF> matrix( n );
200 randomize( matrix, nonzeros, min, max );
201
202 return matrix;
203 }
204 //**********************************************************************************************
205
206 //**********************************************************************************************
212 inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix ) const
213 {
214 randomize( matrix, typename IsDenseMatrix<MT>::Type() );
215 }
216 //**********************************************************************************************
217
218 //**********************************************************************************************
226 inline void randomize( UniLowerMatrix<MT,false,DF>& matrix, size_t nonzeros ) const
227 {
229
230 using ET = ElementType_t<MT>;
231
232 const size_t n( matrix.rows() );
233
234 if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
235 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
236 }
237
238 if( n == 0UL || n == 1UL ) return;
239
240 matrix.reset();
241 matrix.reserve( nonzeros );
242 matrix.finalize( 0UL );
243
244 std::vector<size_t> dist( n );
245
246 for( size_t nz=0UL; nz<nonzeros; ) {
247 const size_t index = rand<size_t>( 1UL, n-1UL );
248 if( dist[index] == index ) continue;
249 ++dist[index];
250 ++nz;
251 }
252
253 for( size_t i=1UL; i<n; ++i ) {
254 const Indices<size_t> indices( 0UL, i-1UL, dist[i] );
255 for( size_t j : indices ) {
256 matrix.append( i, j, rand<ET>() );
257 }
258 matrix.finalize( i );
259 }
260 }
261 //**********************************************************************************************
262
263 //**********************************************************************************************
271 inline void randomize( UniLowerMatrix<MT,true,DF>& matrix, size_t nonzeros ) const
272 {
274
275 using ET = ElementType_t<MT>;
276
277 const size_t n( matrix.rows() );
278
279 if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
280 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
281 }
282
283 if( n == 0UL || n == 1UL ) return;
284
285 matrix.reset();
286 matrix.reserve( nonzeros );
287
288 std::vector<size_t> dist( n-1UL );
289
290 for( size_t nz=0UL; nz<nonzeros; ) {
291 const size_t index = rand<size_t>( 0UL, n-2UL );
292 if( dist[index] == n - index - 1UL ) continue;
293 ++dist[index];
294 ++nz;
295 }
296
297 for( size_t j=0UL; j<n-1UL; ++j ) {
298 const Indices<size_t> indices( j+1UL, n-1UL, dist[j] );
299 for( size_t i : indices ) {
300 matrix.append( i, j, rand<ET>() );
301 }
302 matrix.finalize( j );
303 }
304
305 matrix.finalize( n-1UL );
306 }
307 //**********************************************************************************************
308
309 //**********************************************************************************************
317 template< typename Arg > // Min/max argument type
318 inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max ) const
319 {
320 randomize( matrix, min, max, typename IsDenseMatrix<MT>::Type() );
321 }
322 //**********************************************************************************************
323
324 //**********************************************************************************************
334 template< typename Arg > // Min/max argument type
335 inline void randomize( UniLowerMatrix<MT,false,DF>& matrix,
336 size_t nonzeros, const Arg& min, const Arg& max ) const
337 {
339
340 using ET = ElementType_t<MT>;
341
342 const size_t n( matrix.rows() );
343
344 if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
345 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
346 }
347
348 if( n == 0UL || n == 1UL ) return;
349
350 matrix.reset();
351 matrix.reserve( nonzeros );
352 matrix.finalize( 0UL );
353
354 std::vector<size_t> dist( n );
355
356 for( size_t nz=0UL; nz<nonzeros; ) {
357 const size_t index = rand<size_t>( 1UL, n-1UL );
358 if( dist[index] == index ) continue;
359 ++dist[index];
360 ++nz;
361 }
362
363 for( size_t i=1UL; i<n; ++i ) {
364 const Indices<size_t> indices( 0UL, i-1UL, dist[i] );
365 for( size_t j : indices ) {
366 matrix.append( i, j, rand<ET>( min, max ) );
367 }
368 matrix.finalize( i );
369 }
370 }
371 //**********************************************************************************************
372
373 //**********************************************************************************************
383 template< typename Arg > // Min/max argument type
384 inline void randomize( UniLowerMatrix<MT,true,DF>& matrix,
385 size_t nonzeros, const Arg& min, const Arg& max ) const
386 {
388
389 using ET = ElementType_t<MT>;
390
391 const size_t n( matrix.rows() );
392
393 if( nonzeros > UniLowerMatrix<MT,SO,DF>::maxNonZeros( n ) ) {
394 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
395 }
396
397 if( n == 0UL || n == 1UL ) return;
398
399 matrix.reset();
400 matrix.reserve( nonzeros );
401
402 std::vector<size_t> dist( n-1UL );
403
404 for( size_t nz=0UL; nz<nonzeros; ) {
405 const size_t index = rand<size_t>( 0UL, n-2UL );
406 if( dist[index] == n - index - 1UL ) continue;
407 ++dist[index];
408 ++nz;
409 }
410
411 for( size_t j=0UL; j<n-1UL; ++j ) {
412 const Indices<size_t> indices( j+1UL, n-1UL, dist[j] );
413 for( size_t i : indices ) {
414 matrix.append( i, j, rand<ET>( min, max ) );
415 }
416 matrix.finalize( j );
417 }
418
419 matrix.finalize( n-1UL );
420 }
421 //**********************************************************************************************
422
423 private:
424 //**********************************************************************************************
430 inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, TrueType ) const
431 {
433
434 using ET = ElementType_t<MT>;
435
436 const size_t n( matrix.rows() );
437
438 for( size_t i=1UL; i<n; ++i ) {
439 for( size_t j=0UL; j<i; ++j ) {
440 matrix(i,j) = rand<ET>();
441 }
442 }
443 }
444 //**********************************************************************************************
445
446 //**********************************************************************************************
452 inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix, FalseType ) const
453 {
455
456 const size_t n( matrix.rows() );
457
458 if( n == 0UL || n == 1UL ) return;
459
460 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
461
462 randomize( matrix, nonzeros );
463 }
464 //**********************************************************************************************
465
466 //**********************************************************************************************
474 template< typename Arg > // Min/max argument type
475 inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix,
476 const Arg& min, const Arg& max, TrueType ) const
477 {
479
480 using ET = ElementType_t<MT>;
481
482 const size_t n( matrix.rows() );
483
484 for( size_t i=1UL; i<n; ++i ) {
485 for( size_t j=0UL; j<i; ++j ) {
486 matrix(i,j) = rand<ET>( min, max );
487 }
488 }
489 }
490 //**********************************************************************************************
491
492 //**********************************************************************************************
500 template< typename Arg > // Min/max argument type
501 inline void randomize( UniLowerMatrix<MT,SO,DF>& matrix,
502 const Arg& min, const Arg& max, FalseType ) const
503 {
505
506 const size_t n( matrix.rows() );
507
508 if( n == 0UL || n == 1UL ) return;
509
510 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.2*n*n ) ) );
511
512 randomize( matrix, nonzeros, min, max );
513 }
514 //**********************************************************************************************
515};
517//*************************************************************************************************
518
519
520
521
522//=================================================================================================
523//
524// MAKE FUNCTIONS
525//
526//=================================================================================================
527
528//*************************************************************************************************
535template< typename MT // Type of the adapted matrix
536 , bool SO // Storage order of the adapted matrix
537 , bool DF > // Density flag
538void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix )
539{
540 reset( matrix );
541
542 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
543}
545//*************************************************************************************************
546
547
548//*************************************************************************************************
557template< typename MT // Type of the adapted matrix
558 , bool SO // Storage order of the adapted matrix
559 , bool DF // Density flag
560 , typename Arg > // Min/max argument type
561void makeSymmetric( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
562{
563 MAYBE_UNUSED( min, max );
564
565 makeSymmetric( matrix );
566}
568//*************************************************************************************************
569
570
571//*************************************************************************************************
578template< typename MT // Type of the adapted matrix
579 , bool SO // Storage order of the adapted matrix
580 , bool DF > // Density flag
581void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix )
582{
583 reset( matrix );
584
585 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
586}
588//*************************************************************************************************
589
590
591//*************************************************************************************************
600template< typename MT // Type of the adapted matrix
601 , bool SO // Storage order of the adapted matrix
602 , bool DF // Density flag
603 , typename Arg > // Min/max argument type
604void makeHermitian( UniLowerMatrix<MT,SO,DF>& matrix, const Arg& min, const Arg& max )
605{
606 MAYBE_UNUSED( min, max );
607
608 makeHermitian( matrix );
609}
611//*************************************************************************************************
612
613
614//*************************************************************************************************
621template< typename MT // Type of the adapted matrix
622 , bool SO // Storage order of the adapted matrix
623 , bool DF > // Density flag
624void makePositiveDefinite( UniLowerMatrix<MT,SO,DF>& matrix )
625{
626 makeHermitian( matrix );
627}
629//*************************************************************************************************
630
631} // namespace blaze
632
633#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.
Header file for the MAYBE_UNUSED function template.
Constraint on the data type.
Header file for all basic SparseMatrix functionality.
Header file for the complete UniUpperMatrix implementation.
Header file for the implementation of a lower matrix adaptor.
Header file for the implementation of a strictly lower triangular matrix adaptor.
Header file for the implementation of a lower unitriangular 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
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
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
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
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
#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.