Blaze 3.9
CustomMatrix.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_CUSTOMMATRIX_H_
36#define _BLAZE_MATH_CUSTOMMATRIX_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
55#include <blaze/util/Assert.h>
56#include <blaze/util/Random.h>
57
58
59namespace blaze {
60
61//=================================================================================================
62//
63// RAND SPECIALIZATION
64//
65//=================================================================================================
66
67//*************************************************************************************************
74template< typename Type // Data type of the matrix
75 , AlignmentFlag AF // Alignment flag
76 , PaddingFlag PF // Padding flag
77 , bool SO // Storage order
78 , typename Tag // Type tag
79 , typename RT > // Result type
80class Rand< CustomMatrix<Type,AF,PF,SO,Tag,RT> >
81{
82 public:
83 //*************************************************************************************************
89 inline void randomize( CustomMatrix<Type,AF,PF,SO,Tag,RT>& matrix ) const
90 {
91 using blaze::randomize;
92
93 const size_t m( matrix.rows() );
94 const size_t n( matrix.columns() );
95
96 for( size_t i=0UL; i<m; ++i ) {
97 for( size_t j=0UL; j<n; ++j ) {
98 randomize( matrix(i,j) );
99 }
100 }
101 }
102 //*************************************************************************************************
103
104 //*************************************************************************************************
112 template< typename Arg > // Min/max argument type
113 inline void randomize( CustomMatrix<Type,AF,PF,SO,Tag,RT>& matrix,
114 const Arg& min, const Arg& max ) const
115 {
116 using blaze::randomize;
117
118 const size_t m( matrix.rows() );
119 const size_t n( matrix.columns() );
120
121 for( size_t i=0UL; i<m; ++i ) {
122 for( size_t j=0UL; j<n; ++j ) {
123 randomize( matrix(i,j), min, max );
124 }
125 }
126 }
127 //*************************************************************************************************
128};
130//*************************************************************************************************
131
132
133
134
135//=================================================================================================
136//
137// MAKE FUNCTIONS
138//
139//=================================================================================================
140
141//*************************************************************************************************
149template< typename Type // Data type of the matrix
150 , AlignmentFlag AF // Alignment flag
151 , PaddingFlag PF // Padding flag
152 , bool SO // Storage order
153 , typename Tag // Type tag
154 , typename RT > // Result type
155void makeSymmetric( CustomMatrix<Type,AF,PF,SO,Tag,RT>& matrix )
156{
157 using blaze::randomize;
158
159 if( !isSquare( *matrix ) ) {
160 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
161 }
162
163 const size_t n( matrix.rows() );
164
165 for( size_t i=0UL; i<n; ++i ) {
166 for( size_t j=0UL; j<i; ++j ) {
167 randomize( matrix(i,j) );
168 matrix(j,i) = matrix(i,j);
169 }
170 randomize( matrix(i,i) );
171 }
172
173 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
174}
176//*************************************************************************************************
177
178
179//*************************************************************************************************
189template< typename Type // Data type of the matrix
190 , AlignmentFlag AF // Alignment flag
191 , PaddingFlag PF // Padding flag
192 , bool SO // Storage order
193 , typename Tag // Type tag
194 , typename RT // Result type
195 , typename Arg > // Min/max argument type
196void makeSymmetric( CustomMatrix<Type,AF,PF,SO,Tag,RT>& matrix, const Arg& min, const Arg& max )
197{
198 using blaze::randomize;
199
200 if( !isSquare( *matrix ) ) {
201 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
202 }
203
204 const size_t n( matrix.rows() );
205
206 for( size_t i=0UL; i<n; ++i ) {
207 for( size_t j=0UL; j<i; ++j ) {
208 randomize( matrix(i,j), min, max );
209 matrix(j,i) = matrix(i,j);
210 }
211 randomize( matrix(i,i), min, max );
212 }
213
214 BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
215}
217//*************************************************************************************************
218
219
220//*************************************************************************************************
228template< typename Type // Data type of the matrix
229 , AlignmentFlag AF // Alignment flag
230 , PaddingFlag PF // Padding flag
231 , bool SO // Storage order
232 , typename Tag // Type tag
233 , typename RT > // Result type
234void makeHermitian( CustomMatrix<Type,AF,PF,SO,Tag,RT>& matrix )
235{
236 using blaze::randomize;
237
239
240 using BT = UnderlyingBuiltin_t<Type>;
241
242 if( !isSquare( *matrix ) ) {
243 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
244 }
245
246 const size_t n( matrix.rows() );
247
248 for( size_t i=0UL; i<n; ++i ) {
249 for( size_t j=0UL; j<i; ++j ) {
250 randomize( matrix(i,j) );
251 matrix(j,i) = conj( matrix(i,j) );
252 }
253 matrix(i,i) = rand<BT>();
254 }
255
256 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
257}
259//*************************************************************************************************
260
261
262//*************************************************************************************************
272template< typename Type // Data type of the matrix
273 , AlignmentFlag AF // Alignment flag
274 , PaddingFlag PF // Padding flag
275 , bool SO // Storage order
276 , typename Tag // Type tag
277 , typename RT // Result type
278 , typename Arg > // Min/max argument type
279void makeHermitian( CustomMatrix<Type,AF,PF,SO,Tag,RT>& matrix, const Arg& min, const Arg& max )
280{
281 using blaze::randomize;
282
284
285 using BT = UnderlyingBuiltin_t<Type>;
286
287 if( !isSquare( *matrix ) ) {
288 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
289 }
290
291 const size_t n( matrix.rows() );
292
293 for( size_t i=0UL; i<n; ++i ) {
294 for( size_t j=0UL; j<i; ++j ) {
295 randomize( matrix(i,j), min, max );
296 matrix(j,i) = conj( matrix(i,j) );
297 }
298 matrix(i,i) = rand<BT>( real( min ), real( max ) );
299 }
300
301 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
302}
304//*************************************************************************************************
305
306
307//*************************************************************************************************
315template< typename Type // Data type of the matrix
316 , AlignmentFlag AF // Alignment flag
317 , PaddingFlag PF // Padding flag
318 , bool SO // Storage order
319 , typename Tag // Type tag
320 , typename RT > // Result type
321void makePositiveDefinite( CustomMatrix<Type,AF,PF,SO,Tag,RT>& matrix )
322{
323 using blaze::randomize;
324
326
327 if( !isSquare( *matrix ) ) {
328 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
329 }
330
331 const size_t n( matrix.rows() );
332
333 randomize( matrix );
334 matrix *= ctrans( matrix );
335
336 for( size_t i=0UL; i<n; ++i ) {
337 matrix(i,i) += Type(n);
338 }
339
340 BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
341}
343//*************************************************************************************************
344
345} // namespace blaze
346
347#endif
Header file for the alignment flag enumeration.
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 customizable matrix.
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
#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.