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 
44 #include <blaze/math/DenseMatrix.h>
46 #include <blaze/math/Exception.h>
48 #include <blaze/math/shims/Real.h>
50 #include <blaze/system/Precision.h>
51 #include <blaze/util/Assert.h>
53 #include <blaze/util/Random.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // RAND SPECIALIZATION
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename Type // Data type of the matrix
72  , bool SO > // Storage order
73 class Rand< DynamicMatrix<Type,SO> >
74 {
75  public:
76  //**Generate functions**************************************************************************
79  inline const DynamicMatrix<Type,SO> generate( size_t m, size_t n ) const;
80 
81  template< typename Arg >
82  inline const DynamicMatrix<Type,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
84  //**********************************************************************************************
85 
86  //**Randomize functions*************************************************************************
89  inline void randomize( DynamicMatrix<Type,SO>& matrix ) const;
90 
91  template< typename Arg >
92  inline void randomize( DynamicMatrix<Type,SO>& matrix, const Arg& min, const Arg& max ) const;
94  //**********************************************************************************************
95 };
97 //*************************************************************************************************
98 
99 
100 //*************************************************************************************************
108 template< typename Type // Data type of the matrix
109  , bool SO > // Storage order
110 inline const DynamicMatrix<Type,SO>
111  Rand< DynamicMatrix<Type,SO> >::generate( size_t m, size_t n ) const
112 {
113  DynamicMatrix<Type,SO> matrix( m, n );
114  randomize( matrix );
115  return matrix;
116 }
118 //*************************************************************************************************
119 
120 
121 //*************************************************************************************************
131 template< typename Type // Data type of the matrix
132  , bool SO > // Storage order
133 template< typename Arg > // Min/max argument type
134 inline const DynamicMatrix<Type,SO>
135  Rand< DynamicMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
136 {
137  DynamicMatrix<Type,SO> matrix( m, n );
138  randomize( matrix, min, max );
139  return matrix;
140 }
142 //*************************************************************************************************
143 
144 
145 //*************************************************************************************************
152 template< typename Type // Data type of the matrix
153  , bool SO > // Storage order
154 inline void Rand< DynamicMatrix<Type,SO> >::randomize( DynamicMatrix<Type,SO>& matrix ) const
155 {
156  using blaze::randomize;
157 
158  const size_t m( matrix.rows() );
159  const size_t n( matrix.columns() );
160 
161  for( size_t i=0UL; i<m; ++i ) {
162  for( size_t j=0UL; j<n; ++j ) {
163  randomize( matrix(i,j) );
164  }
165  }
166 }
168 //*************************************************************************************************
169 
170 
171 //*************************************************************************************************
180 template< typename Type // Data type of the matrix
181  , bool SO > // Storage order
182 template< typename Arg > // Min/max argument type
183 inline void Rand< DynamicMatrix<Type,SO> >::randomize( DynamicMatrix<Type,SO>& matrix,
184  const Arg& min, const Arg& max ) const
185 {
186  using blaze::randomize;
187 
188  const size_t m( matrix.rows() );
189  const size_t n( matrix.columns() );
190 
191  for( size_t i=0UL; i<m; ++i ) {
192  for( size_t j=0UL; j<n; ++j ) {
193  randomize( matrix(i,j), min, max );
194  }
195  }
196 }
198 //*************************************************************************************************
199 
200 
201 
202 
203 //=================================================================================================
204 //
205 // MAKE FUNCTIONS
206 //
207 //=================================================================================================
208 
209 //*************************************************************************************************
217 template< typename Type // Data type of the matrix
218  , bool SO > // Storage order
219 void makeSymmetric( DynamicMatrix<Type,SO>& matrix )
220 {
221  using blaze::randomize;
222 
223  if( !isSquare( ~matrix ) ) {
224  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
225  }
226 
227  const size_t n( matrix.rows() );
228 
229  for( size_t i=0UL; i<n; ++i ) {
230  for( size_t j=0UL; j<i; ++j ) {
231  randomize( matrix(i,j) );
232  matrix(j,i) = matrix(i,j);
233  }
234  randomize( matrix(i,i) );
235  }
236 
237  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
238 }
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
253 template< typename Type // Data type of the matrix
254  , bool SO // Storage order
255  , typename Arg > // Min/max argument type
256 void makeSymmetric( DynamicMatrix<Type,SO>& matrix, const Arg& min, const Arg& max )
257 {
258  using blaze::randomize;
259 
260  if( !isSquare( ~matrix ) ) {
261  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
262  }
263 
264  const size_t n( matrix.rows() );
265 
266  for( size_t i=0UL; i<n; ++i ) {
267  for( size_t j=0UL; j<i; ++j ) {
268  randomize( matrix(i,j), min, max );
269  matrix(j,i) = matrix(i,j);
270  }
271  randomize( matrix(i,i), min, max );
272  }
273 
274  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
275 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
288 template< typename Type // Data type of the matrix
289  , bool SO > // Storage order
290 void makeHermitian( DynamicMatrix<Type,SO>& matrix )
291 {
292  using blaze::randomize;
293 
295 
296  typedef UnderlyingBuiltin_<Type> BT;
297 
298  if( !isSquare( ~matrix ) ) {
299  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
300  }
301 
302  const size_t n( matrix.rows() );
303 
304  for( size_t i=0UL; i<n; ++i ) {
305  for( size_t j=0UL; j<i; ++j ) {
306  randomize( matrix(i,j) );
307  matrix(j,i) = conj( matrix(i,j) );
308  }
309  matrix(i,i) = rand<BT>();
310  }
311 
312  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
313 }
315 //*************************************************************************************************
316 
317 
318 //*************************************************************************************************
328 template< typename Type // Data type of the matrix
329  , bool SO // Storage order
330  , typename Arg > // Min/max argument type
331 void makeHermitian( DynamicMatrix<Type,SO>& matrix, const Arg& min, const Arg& max )
332 {
333  using blaze::randomize;
334 
336 
337  typedef UnderlyingBuiltin_<Type> BT;
338 
339  if( !isSquare( ~matrix ) ) {
340  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
341  }
342 
343  const size_t n( matrix.rows() );
344 
345  for( size_t i=0UL; i<n; ++i ) {
346  for( size_t j=0UL; j<i; ++j ) {
347  randomize( matrix(i,j), min, max );
348  matrix(j,i) = conj( matrix(i,j) );
349  }
350  matrix(i,i) = rand<BT>( real( min ), real( max ) );
351  }
352 
353  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
354 }
356 //*************************************************************************************************
357 
358 
359 //*************************************************************************************************
367 template< typename Type // Data type of the matrix
368  , bool SO > // Storage order
369 void makePositiveDefinite( DynamicMatrix<Type,SO>& matrix )
370 {
371  using blaze::randomize;
372 
374 
375  if( !isSquare( ~matrix ) ) {
376  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
377  }
378 
379  const size_t n( matrix.rows() );
380 
381  randomize( matrix );
382  matrix *= ctrans( matrix );
383 
384  for( size_t i=0UL; i<n; ++i ) {
385  matrix(i,i) += Type(n);
386  }
387 
388  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
389 }
391 //*************************************************************************************************
392 
393 } // namespace blaze
394 
395 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
const DMatForEachExpr< MT, Conj, SO > conj(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the complex conjugate of each single element of dm.
Definition: DMatForEachExpr.h:1158
Constraint on the data type.
const CTransExprTrait_< MT > ctrans(const DenseMatrix< MT, SO > &dm)
Returns the conjugate transpose matrix of dm.
Definition: DMatForEachExpr.h:1195
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:926
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:689
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
Implementation of a random number generator.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
Header file for the floating point precision of the Blaze library.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:289
Header file for the UnderlyingBuiltin type trait.
Header file for the exception macros of the math module.
Header file for the implementation of a dynamic MxN matrix.
Header file for the complete DynamicVector implementation.
const DMatForEachExpr< MT, Real, SO > real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatForEachExpr.h:1223
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
T generate() const
Generation of a random value in the range .
Definition: Random.h:249
Header file for all basic DenseMatrix functionality.
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:759
Header file for the real shim.
BLAZE_ALWAYS_INLINE bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:609
#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