Blaze 3.9
HybridMatrix.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_HYBRIDMATRIX_H_
36#define _BLAZE_MATH_HYBRIDMATRIX_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
54#include <blaze/util/Assert.h>
55#include <blaze/util/Random.h>
56
57
58namespace blaze {
59
60//=================================================================================================
61//
62// RAND SPECIALIZATION
63//
64//=================================================================================================
65
66//*************************************************************************************************
73template< typename Type // Data type of the matrix
74 , size_t M // Number of rows
75 , size_t N // Number of columns
76 , bool SO // Storage order
77 , AlignmentFlag AF // Alignment flag
78 , PaddingFlag PF // Padding flag
79 , typename Tag > // Type tag
80class Rand< HybridMatrix<Type,M,N,SO,AF,PF,Tag> >
81{
82 public:
83 //*************************************************************************************************
88 inline const HybridMatrix<Type,M,N,SO,AF,PF,Tag>
89 generate( size_t m, size_t n ) const
90 {
91 HybridMatrix<Type,M,N,SO,AF,PF,Tag> matrix( m, n );
92 randomize( matrix );
93 return matrix;
94 }
95 //*************************************************************************************************
96
97 //*************************************************************************************************
104 template< typename Arg > // Min/max argument type
105 inline const HybridMatrix<Type,M,N,SO,AF,PF,Tag>
106 generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
107 {
108 HybridMatrix<Type,M,N,SO,AF,PF,Tag> matrix( m, n );
109 randomize( matrix, min, max );
110 return matrix;
111 }
112 //*************************************************************************************************
113
114 //*************************************************************************************************
120 inline void randomize( HybridMatrix<Type,M,N,SO,AF,PF,Tag>& matrix ) const
121 {
122 using blaze::randomize;
123
124 const size_t m( matrix.rows() );
125 const size_t n( matrix.columns() );
126
127 for( size_t i=0UL; i<m; ++i ) {
128 for( size_t j=0UL; j<n; ++j ) {
129 randomize( matrix(i,j) );
130 }
131 }
132 }
133 //*************************************************************************************************
134
135 //*************************************************************************************************
143 template< typename Arg > // Min/max argument type
144 inline void randomize( HybridMatrix<Type,M,N,SO,AF,PF,Tag>& matrix,
145 const Arg& min, const Arg& max ) const
146 {
147 using blaze::randomize;
148
149 const size_t m( matrix.rows() );
150 const size_t n( matrix.columns() );
151
152 for( size_t i=0UL; i<m; ++i ) {
153 for( size_t j=0UL; j<n; ++j ) {
154 randomize( matrix(i,j), min, max );
155 }
156 }
157 }
158 //*************************************************************************************************
159};
161//*************************************************************************************************
162
163
164
165
166//=================================================================================================
167//
168// MAKE FUNCTIONS
169//
170//=================================================================================================
171
172//*************************************************************************************************
180template< typename Type // Data type of the matrix
181 , size_t M // Number of rows
182 , size_t N // Number of columns
183 , bool SO // Storage order
184 , AlignmentFlag AF // Alignment flag
185 , PaddingFlag PF // Padding flag
186 , typename Tag > // Type tag
187void makeSymmetric( HybridMatrix<Type,M,N,SO,AF,PF,Tag>& matrix )
188{
189 using blaze::randomize;
190
191 if( !isSquare( *matrix ) ) {
192 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
193 }
194
195 const size_t n( matrix.rows() );
196
197 for( size_t i=0UL; i<n; ++i ) {
198 for( size_t j=0UL; j<i; ++j ) {
199 randomize( matrix(i,j) );
200 matrix(j,i) = matrix(i,j);
201 }
202 randomize( matrix(i,i) );
203 }
204
205 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
206}
208//*************************************************************************************************
209
210
211//*************************************************************************************************
221template< typename Type // Data type of the matrix
222 , size_t M // Number of rows
223 , size_t N // Number of columns
224 , bool SO // Storage order
225 , AlignmentFlag AF // Alignment flag
226 , PaddingFlag PF // Padding flag
227 , typename Tag // Type tag
228 , typename Arg > // Min/max argument type
229void makeSymmetric( HybridMatrix<Type,M,N,SO,AF,PF,Tag>& matrix, const Arg& min, const Arg& max )
230{
231 using blaze::randomize;
232
233 if( !isSquare( *matrix ) ) {
234 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
235 }
236
237 const size_t n( matrix.rows() );
238
239 for( size_t i=0UL; i<n; ++i ) {
240 for( size_t j=0UL; j<i; ++j ) {
241 randomize( matrix(i,j), min, max );
242 matrix(j,i) = matrix(i,j);
243 }
244 randomize( matrix(i,i), min, max );
245 }
246
247 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
248}
250//*************************************************************************************************
251
252
253//*************************************************************************************************
261template< typename Type // Data type of the matrix
262 , size_t M // Number of rows
263 , size_t N // Number of columns
264 , bool SO // Storage order
265 , AlignmentFlag AF // Alignment flag
266 , PaddingFlag PF // Padding flag
267 , typename Tag > // Type tag
268void makeHermitian( HybridMatrix<Type,M,N,SO,AF,PF,Tag>& matrix )
269{
270 using blaze::randomize;
271
273
274 using BT = UnderlyingBuiltin_t<Type>;
275
276 if( !isSquare( *matrix ) ) {
277 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
278 }
279
280 const size_t n( matrix.rows() );
281
282 for( size_t i=0UL; i<n; ++i ) {
283 for( size_t j=0UL; j<i; ++j ) {
284 randomize( matrix(i,j) );
285 matrix(j,i) = conj( matrix(i,j) );
286 }
287 matrix(i,i) = rand<BT>();
288 }
289
290 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
291}
293//*************************************************************************************************
294
295
296//*************************************************************************************************
306template< typename Type // Data type of the matrix
307 , size_t M // Number of rows
308 , size_t N // Number of columns
309 , bool SO // Storage order
310 , AlignmentFlag AF // Alignment flag
311 , PaddingFlag PF // Padding flag
312 , typename Tag // Type tag
313 , typename Arg > // Min/max argument type
314void makeHermitian( HybridMatrix<Type,M,N,SO,AF,PF,Tag>& matrix, const Arg& min, const Arg& max )
315{
316 using blaze::randomize;
317
319
320 using BT = UnderlyingBuiltin_t<Type>;
321
322 if( !isSquare( *matrix ) ) {
323 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
324 }
325
326 const size_t n( matrix.rows() );
327
328 for( size_t i=0UL; i<n; ++i ) {
329 for( size_t j=0UL; j<i; ++j ) {
330 randomize( matrix(i,j), min, max );
331 matrix(j,i) = conj( matrix(i,j) );
332 }
333 matrix(i,i) = rand<BT>( real( min ), real( max ) );
334 }
335
336 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
337}
339//*************************************************************************************************
340
341
342//*************************************************************************************************
350template< typename Type // Data type of the matrix
351 , size_t M // Number of rows
352 , size_t N // Number of columns
353 , bool SO // Storage order
354 , AlignmentFlag AF // Alignment flag
355 , PaddingFlag PF // Padding flag
356 , typename Tag > // Type tag
357void makePositiveDefinite( HybridMatrix<Type,M,N,SO,AF,PF,Tag>& matrix )
358{
359 using blaze::randomize;
360
362
363 if( !isSquare( *matrix ) ) {
364 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
365 }
366
367 const size_t n( matrix.rows() );
368
369 randomize( matrix );
370 matrix *= ctrans( matrix );
371
372 for( size_t i=0UL; i<n; ++i ) {
373 matrix(i,i) += Type(n);
374 }
375
376 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
377}
379//*************************************************************************************************
380
381} // namespace blaze
382
383#endif
Header file for run time assertion macros.
Header file for the conjugate shim.
Header file for all basic DenseMatrix functionality.
Header file for the complete HybridVector implementation.
Header file for the complete IdentityMatrix implementation.
Constraint on the data type.
Header file for the complete StaticMatrix implementation.
Header file for the UnderlyingBuiltin type trait.
Header file for the complete ZeroMatrix implementation.
Header file for the implementation of a fixed-size matrix.
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) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1529
decltype(auto) conj(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the complex conjugate of each single element of dm.
Definition: DMatMapExpr.h:1464
decltype(auto) ctrans(const DenseMatrix< MT, SO > &dm)
Returns the conjugate transpose matrix of dm.
Definition: DMatMapExpr.h:1501
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_SCALAR_TYPE(T)
Constraint on the data type.
Definition: Scalar.h:61
PaddingFlag
Padding flag for (un-)padded vectors and matrices.
Definition: PaddingFlag.h:77
AlignmentFlag
Alignment flag for (un-)aligned vectors and matrices.
Definition: AlignmentFlag.h:63
bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:1383
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
#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 real shim.
Implementation of a random number generator.