Blaze 3.9
DynamicMatrix.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_DYNAMICMATRIX_H_
36#define _BLAZE_MATH_DYNAMICMATRIX_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
53#include <blaze/util/Assert.h>
54#include <blaze/util/Random.h>
55
56
57namespace blaze {
58
59//=================================================================================================
60//
61// RAND SPECIALIZATION
62//
63//=================================================================================================
64
65//*************************************************************************************************
72template< typename Type // Data type of the matrix
73 , bool SO // Storage order
74 , typename Tag > // Type tag
75class Rand< DynamicMatrix<Type,SO,Tag> >
76{
77 public:
78 //**********************************************************************************************
85 inline const DynamicMatrix<Type,SO,Tag>
86 generate( size_t m, size_t n ) const
87 {
88 DynamicMatrix<Type,SO,Tag> matrix( m, n );
89 randomize( matrix );
90 return matrix;
91 }
92 //**********************************************************************************************
93
94 //**********************************************************************************************
103 template< typename Arg > // Min/max argument type
104 inline const DynamicMatrix<Type,SO,Tag>
105 generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
106 {
107 DynamicMatrix<Type,SO,Tag> matrix( m, n );
108 randomize( matrix, min, max );
109 return matrix;
110 }
111 //**********************************************************************************************
112
113 //**********************************************************************************************
119 inline void randomize( DynamicMatrix<Type,SO,Tag>& matrix ) const
120 {
121 using blaze::randomize;
122
123 const size_t m( matrix.rows() );
124 const size_t n( matrix.columns() );
125
126 for( size_t i=0UL; i<m; ++i ) {
127 for( size_t j=0UL; j<n; ++j ) {
128 randomize( matrix(i,j) );
129 }
130 }
131 }
132 //**********************************************************************************************
133
134 //**********************************************************************************************
142 template< typename Arg > // Min/max argument type
143 inline void randomize( DynamicMatrix<Type,SO,Tag>& matrix,
144 const Arg& min, const Arg& max ) const
145 {
146 using blaze::randomize;
147
148 const size_t m( matrix.rows() );
149 const size_t n( matrix.columns() );
150
151 for( size_t i=0UL; i<m; ++i ) {
152 for( size_t j=0UL; j<n; ++j ) {
153 randomize( matrix(i,j), min, max );
154 }
155 }
156 }
157 //**********************************************************************************************
158};
160//*************************************************************************************************
161
162
163
164
165//=================================================================================================
166//
167// MAKE FUNCTIONS
168//
169//=================================================================================================
170
171//*************************************************************************************************
179template< typename Type // Data type of the matrix
180 , bool SO // Storage order
181 , typename Tag > // Type tag
182void makeSymmetric( DynamicMatrix<Type,SO,Tag>& matrix )
183{
184 using blaze::randomize;
185
186 if( !isSquare( *matrix ) ) {
187 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
188 }
189
190 const size_t n( matrix.rows() );
191
192 for( size_t i=0UL; i<n; ++i ) {
193 for( size_t j=0UL; j<i; ++j ) {
194 randomize( matrix(i,j) );
195 matrix(j,i) = matrix(i,j);
196 }
197 randomize( matrix(i,i) );
198 }
199
200 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
201}
203//*************************************************************************************************
204
205
206//*************************************************************************************************
216template< typename Type // Data type of the matrix
217 , bool SO // Storage order
218 , typename Tag // Type tag
219 , typename Arg > // Min/max argument type
220void makeSymmetric( DynamicMatrix<Type,SO,Tag>& matrix, const Arg& min, const Arg& max )
221{
222 using blaze::randomize;
223
224 if( !isSquare( *matrix ) ) {
225 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
226 }
227
228 const size_t n( matrix.rows() );
229
230 for( size_t i=0UL; i<n; ++i ) {
231 for( size_t j=0UL; j<i; ++j ) {
232 randomize( matrix(i,j), min, max );
233 matrix(j,i) = matrix(i,j);
234 }
235 randomize( matrix(i,i), min, max );
236 }
237
238 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
239}
241//*************************************************************************************************
242
243
244//*************************************************************************************************
252template< typename Type // Data type of the matrix
253 , bool SO // Storage order
254 , typename Tag > // Type tag
255void makeHermitian( DynamicMatrix<Type,SO,Tag>& matrix )
256{
257 using blaze::randomize;
258
260
261 using BT = UnderlyingBuiltin_t<Type>;
262
263 if( !isSquare( *matrix ) ) {
264 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
265 }
266
267 const size_t n( matrix.rows() );
268
269 for( size_t i=0UL; i<n; ++i ) {
270 for( size_t j=0UL; j<i; ++j ) {
271 randomize( matrix(i,j) );
272 matrix(j,i) = conj( matrix(i,j) );
273 }
274 matrix(i,i) = rand<BT>();
275 }
276
277 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
278}
280//*************************************************************************************************
281
282
283//*************************************************************************************************
293template< typename Type // Data type of the matrix
294 , bool SO // Storage order
295 , typename Tag // Type tag
296 , typename Arg > // Min/max argument type
297void makeHermitian( DynamicMatrix<Type,SO,Tag>& matrix, const Arg& min, const Arg& max )
298{
299 using blaze::randomize;
300
302
303 using BT = UnderlyingBuiltin_t<Type>;
304
305 if( !isSquare( *matrix ) ) {
306 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
307 }
308
309 const size_t n( matrix.rows() );
310
311 for( size_t i=0UL; i<n; ++i ) {
312 for( size_t j=0UL; j<i; ++j ) {
313 randomize( matrix(i,j), min, max );
314 matrix(j,i) = conj( matrix(i,j) );
315 }
316 matrix(i,i) = rand<BT>( real( min ), real( max ) );
317 }
318
319 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
320}
322//*************************************************************************************************
323
324
325//*************************************************************************************************
333template< typename Type // Data type of the matrix
334 , bool SO // Storage order
335 , typename Tag > // Type tag
336void makePositiveDefinite( DynamicMatrix<Type,SO,Tag>& matrix )
337{
338 using blaze::randomize;
339
341
342 if( !isSquare( *matrix ) ) {
343 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
344 }
345
346 const size_t n( matrix.rows() );
347
348 randomize( matrix );
349 matrix *= ctrans( matrix );
350
351 for( size_t i=0UL; i<n; ++i ) {
352 matrix(i,i) += Type(n);
353 }
354
355 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
356}
358//*************************************************************************************************
359
360} // namespace blaze
361
362#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 DynamicVector implementation.
Header file for the complete IdentityMatrix implementation.
Constraint on the data type.
Header file for the UnderlyingBuiltin type trait.
Header file for the complete ZeroMatrix implementation.
Header file for the implementation of a dynamic MxN 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
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.