StaticMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_STATICMATRIX_H_
36 #define _BLAZE_MATH_STATICMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
44 #include <blaze/math/DenseMatrix.h>
48 #include <blaze/math/shims/Real.h>
51 #include <blaze/math/ZeroMatrix.h>
52 #include <blaze/util/Assert.h>
54 #include <blaze/util/Random.h>
56 
57 
58 namespace blaze {
59 
60 //=================================================================================================
61 //
62 // RAND SPECIALIZATION
63 //
64 //=================================================================================================
65 
66 //*************************************************************************************************
73 template< 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 class Rand< StaticMatrix<Type,M,N,SO> >
78 {
79  public:
80  //**Generate functions**************************************************************************
83  inline const StaticMatrix<Type,M,N,SO> generate() const;
84 
85  template< typename Arg >
86  inline const StaticMatrix<Type,M,N,SO> generate( const Arg& min, const Arg& max ) const;
88  //**********************************************************************************************
89 
90  //**Randomize functions*************************************************************************
93  inline void randomize( StaticMatrix<Type,M,N,SO>& matrix ) const;
94 
95  template< typename Arg >
96  inline void randomize( StaticMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max ) const;
98  //**********************************************************************************************
99 };
101 //*************************************************************************************************
102 
103 
104 //*************************************************************************************************
110 template< typename Type // Data type of the matrix
111  , size_t M // Number of rows
112  , size_t N // Number of columns
113  , bool SO > // Storage order
114 inline const StaticMatrix<Type,M,N,SO> Rand< StaticMatrix<Type,M,N,SO> >::generate() const
115 {
116  StaticMatrix<Type,M,N,SO> matrix;
117  randomize( matrix );
118  return matrix;
119 }
121 //*************************************************************************************************
122 
123 
124 //*************************************************************************************************
132 template< typename Type // Data type of the matrix
133  , size_t M // Number of rows
134  , size_t N // Number of columns
135  , bool SO > // Storage order
136 template< typename Arg > // Min/max argument type
137 inline const StaticMatrix<Type,M,N,SO>
138  Rand< StaticMatrix<Type,M,N,SO> >::generate( const Arg& min, const Arg& max ) const
139 {
140  StaticMatrix<Type,M,N,SO> matrix;
141  randomize( matrix, min, max );
142  return matrix;
143 }
145 //*************************************************************************************************
146 
147 
148 //*************************************************************************************************
155 template< typename Type // Data type of the matrix
156  , size_t M // Number of rows
157  , size_t N // Number of columns
158  , bool SO > // Storage order
159 inline void Rand< StaticMatrix<Type,M,N,SO> >::randomize( StaticMatrix<Type,M,N,SO>& matrix ) const
160 {
161  using blaze::randomize;
162 
163  for( size_t i=0UL; i<M; ++i ) {
164  for( size_t j=0UL; j<N; ++j ) {
165  randomize( matrix(i,j) );
166  }
167  }
168 }
170 //*************************************************************************************************
171 
172 
173 //*************************************************************************************************
182 template< typename Type // Data type of the matrix
183  , size_t M // Number of rows
184  , size_t N // Number of columns
185  , bool SO > // Storage order
186 template< typename Arg > // Min/max argument type
187 inline void Rand< StaticMatrix<Type,M,N,SO> >::randomize( StaticMatrix<Type,M,N,SO>& matrix,
188  const Arg& min, const Arg& max ) const
189 {
190  using blaze::randomize;
191 
192  for( size_t i=0UL; i<M; ++i ) {
193  for( size_t j=0UL; j<N; ++j ) {
194  randomize( matrix(i,j), min, max );
195  }
196  }
197 }
199 //*************************************************************************************************
200 
201 
202 
203 
204 //=================================================================================================
205 //
206 // MAKE FUNCTIONS
207 //
208 //=================================================================================================
209 
210 //*************************************************************************************************
218 template< typename Type // Data type of the matrix
219  , size_t M // Number of rows
220  , size_t N // Number of columns
221  , bool SO > // Storage order
222 void makeSymmetric( StaticMatrix<Type,M,N,SO>& matrix )
223 {
224  using blaze::randomize;
225 
226  BLAZE_STATIC_ASSERT( M == N );
227 
228  for( size_t i=0UL; i<N; ++i ) {
229  for( size_t j=0UL; j<i; ++j ) {
230  randomize( matrix(i,j) );
231  matrix(j,i) = matrix(i,j);
232  }
233  randomize( matrix(i,i) );
234  }
235 
236  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
237 }
239 //*************************************************************************************************
240 
241 
242 //*************************************************************************************************
252 template< typename Type // Data type of the matrix
253  , size_t M // Number of rows
254  , size_t N // Number of columns
255  , bool SO // Storage order
256  , typename Arg > // Min/max argument type
257 void makeSymmetric( StaticMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max )
258 {
259  using blaze::randomize;
260 
261  BLAZE_STATIC_ASSERT( M == N );
262 
263  for( size_t i=0UL; i<N; ++i ) {
264  for( size_t j=0UL; j<i; ++j ) {
265  randomize( matrix(i,j), min, max );
266  matrix(j,i) = matrix(i,j);
267  }
268  randomize( matrix(i,i), min, max );
269  }
270 
271  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
272 }
274 //*************************************************************************************************
275 
276 
277 //*************************************************************************************************
285 template< typename Type // Data type of the matrix
286  , size_t M // Number of rows
287  , size_t N // Number of columns
288  , bool SO > // Storage order
289 void makeHermitian( StaticMatrix<Type,M,N,SO>& matrix )
290 {
291  using blaze::randomize;
292 
293  BLAZE_STATIC_ASSERT( M == N );
295 
296  using BT = UnderlyingBuiltin_t<Type>;
297 
298  for( size_t i=0UL; i<N; ++i ) {
299  for( size_t j=0UL; j<i; ++j ) {
300  randomize( matrix(i,j) );
301  matrix(j,i) = conj( matrix(i,j) );
302  }
303  matrix(i,i) = rand<BT>();
304  }
305 
306  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
307 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
322 template< typename Type // Data type of the matrix
323  , size_t M // Number of rows
324  , size_t N // Number of columns
325  , bool SO // Storage order
326  , typename Arg > // Min/max argument type
327 void makeHermitian( StaticMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max )
328 {
329  using blaze::randomize;
330 
331  BLAZE_STATIC_ASSERT( M == N );
333 
334  using BT = UnderlyingBuiltin_t<Type>;
335 
336  for( size_t i=0UL; i<N; ++i ) {
337  for( size_t j=0UL; j<i; ++j ) {
338  randomize( matrix(i,j), min, max );
339  matrix(j,i) = conj( matrix(i,j) );
340  }
341  matrix(i,i) = rand<BT>( real( min ), real( max ) );
342  }
343 
344  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
345 }
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
358 template< typename Type // Data type of the matrix
359  , size_t M // Number of rows
360  , size_t N // Number of columns
361  , bool SO > // Storage order
362 void makePositiveDefinite( StaticMatrix<Type,M,N,SO>& matrix )
363 {
364  using blaze::randomize;
365 
366  BLAZE_STATIC_ASSERT( M == N );
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 the complete HybridMatrix implementation.
Constraint on the data type.
decltype(auto) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1392
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
Implementation of a random number generator.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
decltype(auto) ctrans(const DenseMatrix< MT, SO > &dm)
Returns the conjugate transpose matrix of dm.
Definition: DMatMapExpr.h:1364
Compile time assertion.
Header file for the UnderlyingBuiltin type trait.
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:1147
Header file for the implementation of a fixed-size matrix.
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:1179
Header file for the conjugate shim.
Header file for run time assertion macros.
#define BLAZE_CONSTRAINT_MUST_BE_NUMERIC_TYPE(T)
Constraint on the data type.In case the given data type T is not a numeric (integral or floating poin...
Definition: Numeric.h:61
Header file for the complete StaticVector implementation.
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:617
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:539
T generate() const
Generation of a random value in the range .
Definition: Random.h:252
Header file for all basic DenseMatrix functionality.
Header file for the complete IdentityMatrix implementation.
Header file for the real shim.
decltype(auto) conj(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the complex conjugate of each single element of dm.
Definition: DMatMapExpr.h:1326
#define BLAZE_STATIC_ASSERT(expr)
Compile time assertion macro.In case of an invalid compile time expression, a compilation error is cr...
Definition: StaticAssert.h:112
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the complete ZeroMatrix implementation.