Blaze  3.6
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>
49 #include <blaze/math/shims/Real.h>
51 #include <blaze/math/ZeroMatrix.h>
52 #include <blaze/util/Assert.h>
54 #include <blaze/util/Random.h>
55 
56 
57 namespace blaze {
58 
59 //=================================================================================================
60 //
61 // RAND SPECIALIZATION
62 //
63 //=================================================================================================
64 
65 //*************************************************************************************************
72 template< typename Type // Data type of the matrix
73  , bool SO > // Storage order
74 class Rand< DynamicMatrix<Type,SO> >
75 {
76  public:
77  //**Generate functions**************************************************************************
80  inline const DynamicMatrix<Type,SO> generate( size_t m, size_t n ) const;
81 
82  template< typename Arg >
83  inline const DynamicMatrix<Type,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
85  //**********************************************************************************************
86 
87  //**Randomize functions*************************************************************************
90  inline void randomize( DynamicMatrix<Type,SO>& matrix ) const;
91 
92  template< typename Arg >
93  inline void randomize( DynamicMatrix<Type,SO>& matrix, const Arg& min, const Arg& max ) const;
95  //**********************************************************************************************
96 };
98 //*************************************************************************************************
99 
100 
101 //*************************************************************************************************
109 template< typename Type // Data type of the matrix
110  , bool SO > // Storage order
111 inline const DynamicMatrix<Type,SO>
112  Rand< DynamicMatrix<Type,SO> >::generate( size_t m, size_t n ) const
113 {
114  DynamicMatrix<Type,SO> matrix( m, n );
115  randomize( matrix );
116  return matrix;
117 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
132 template< typename Type // Data type of the matrix
133  , bool SO > // Storage order
134 template< typename Arg > // Min/max argument type
135 inline const DynamicMatrix<Type,SO>
136  Rand< DynamicMatrix<Type,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
137 {
138  DynamicMatrix<Type,SO> matrix( m, n );
139  randomize( matrix, min, max );
140  return matrix;
141 }
143 //*************************************************************************************************
144 
145 
146 //*************************************************************************************************
153 template< typename Type // Data type of the matrix
154  , bool SO > // Storage order
155 inline void Rand< DynamicMatrix<Type,SO> >::randomize( DynamicMatrix<Type,SO>& matrix ) const
156 {
157  using blaze::randomize;
158 
159  const size_t m( matrix.rows() );
160  const size_t n( matrix.columns() );
161 
162  for( size_t i=0UL; i<m; ++i ) {
163  for( size_t j=0UL; j<n; ++j ) {
164  randomize( matrix(i,j) );
165  }
166  }
167 }
169 //*************************************************************************************************
170 
171 
172 //*************************************************************************************************
181 template< typename Type // Data type of the matrix
182  , bool SO > // Storage order
183 template< typename Arg > // Min/max argument type
184 inline void Rand< DynamicMatrix<Type,SO> >::randomize( DynamicMatrix<Type,SO>& matrix,
185  const Arg& min, const Arg& max ) const
186 {
187  using blaze::randomize;
188 
189  const size_t m( matrix.rows() );
190  const size_t n( matrix.columns() );
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  , bool SO > // Storage order
220 void makeSymmetric( DynamicMatrix<Type,SO>& matrix )
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) );
233  matrix(j,i) = matrix(i,j);
234  }
235  randomize( matrix(i,i) );
236  }
237 
238  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
239 }
241 //*************************************************************************************************
242 
243 
244 //*************************************************************************************************
254 template< typename Type // Data type of the matrix
255  , bool SO // Storage order
256  , typename Arg > // Min/max argument type
257 void makeSymmetric( DynamicMatrix<Type,SO>& matrix, const Arg& min, const Arg& max )
258 {
259  using blaze::randomize;
260 
261  if( !isSquare( ~matrix ) ) {
262  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
263  }
264 
265  const size_t n( matrix.rows() );
266 
267  for( size_t i=0UL; i<n; ++i ) {
268  for( size_t j=0UL; j<i; ++j ) {
269  randomize( matrix(i,j), min, max );
270  matrix(j,i) = matrix(i,j);
271  }
272  randomize( matrix(i,i), min, max );
273  }
274 
275  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
276 }
278 //*************************************************************************************************
279 
280 
281 //*************************************************************************************************
289 template< typename Type // Data type of the matrix
290  , bool SO > // Storage order
291 void makeHermitian( DynamicMatrix<Type,SO>& matrix )
292 {
293  using blaze::randomize;
294 
296 
297  using BT = UnderlyingBuiltin_t<Type>;
298 
299  if( !isSquare( ~matrix ) ) {
300  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
301  }
302 
303  const size_t n( matrix.rows() );
304 
305  for( size_t i=0UL; i<n; ++i ) {
306  for( size_t j=0UL; j<i; ++j ) {
307  randomize( matrix(i,j) );
308  matrix(j,i) = conj( matrix(i,j) );
309  }
310  matrix(i,i) = rand<BT>();
311  }
312 
313  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
314 }
316 //*************************************************************************************************
317 
318 
319 //*************************************************************************************************
329 template< typename Type // Data type of the matrix
330  , bool SO // Storage order
331  , typename Arg > // Min/max argument type
332 void makeHermitian( DynamicMatrix<Type,SO>& matrix, const Arg& min, const Arg& max )
333 {
334  using blaze::randomize;
335 
337 
338  using BT = UnderlyingBuiltin_t<Type>;
339 
340  if( !isSquare( ~matrix ) ) {
341  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
342  }
343 
344  const size_t n( matrix.rows() );
345 
346  for( size_t i=0UL; i<n; ++i ) {
347  for( size_t j=0UL; j<i; ++j ) {
348  randomize( matrix(i,j), min, max );
349  matrix(j,i) = conj( matrix(i,j) );
350  }
351  matrix(i,i) = rand<BT>( real( min ), real( max ) );
352  }
353 
354  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
355 }
357 //*************************************************************************************************
358 
359 
360 //*************************************************************************************************
368 template< typename Type // Data type of the matrix
369  , bool SO > // Storage order
370 void makePositiveDefinite( DynamicMatrix<Type,SO>& matrix )
371 {
372  using blaze::randomize;
373 
375 
376  if( !isSquare( ~matrix ) ) {
377  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
378  }
379 
380  const size_t n( matrix.rows() );
381 
382  randomize( matrix );
383  matrix *= ctrans( matrix );
384 
385  for( size_t i=0UL; i<n; ++i ) {
386  matrix(i,i) += Type(n);
387  }
388 
389  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
390 }
392 //*************************************************************************************************
393 
394 } // namespace blaze
395 
396 #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
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:1389
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:1361
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:1162
Header file for the exception macros of the math module.
Header file for the implementation of a dynamic MxN 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:1198
Header file for the complete DynamicVector implementation.
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
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:1406
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:1328
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:1324
bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:951
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
Header file for the complete ZeroMatrix implementation.