HybridMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_HYBRIDMATRIX_H_
36 #define _BLAZE_MATH_HYBRIDMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
44 #include <blaze/math/DenseMatrix.h>
45 #include <blaze/math/Exception.h>
49 #include <blaze/math/shims/Real.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  , size_t M // Number of rows
74  , size_t N // Number of columns
75  , bool SO > // Storage order
76 class Rand< HybridMatrix<Type,M,N,SO> >
77 {
78  public:
79  //**Generate functions**************************************************************************
82  inline const HybridMatrix<Type,M,N,SO> generate( size_t m, size_t n ) const;
83 
84  template< typename Arg >
85  inline const HybridMatrix<Type,M,N,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
87  //**********************************************************************************************
88 
89  //**Randomize functions*************************************************************************
92  inline void randomize( HybridMatrix<Type,M,N,SO>& matrix ) const;
93 
94  template< typename Arg >
95  inline void randomize( HybridMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max ) const;
97  //**********************************************************************************************
98 };
100 //*************************************************************************************************
101 
102 
103 //*************************************************************************************************
109 template< typename Type // Data type of the matrix
110  , size_t M // Number of rows
111  , size_t N // Number of columns
112  , bool SO > // Storage order
113 inline const HybridMatrix<Type,M,N,SO>
114  Rand< HybridMatrix<Type,M,N,SO> >::generate( size_t m, size_t n ) const
115 {
116  HybridMatrix<Type,M,N,SO> matrix( m, n );
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 HybridMatrix<Type,M,N,SO>
138  Rand< HybridMatrix<Type,M,N,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
139 {
140  HybridMatrix<Type,M,N,SO> matrix( m, n );
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< HybridMatrix<Type,M,N,SO> >::randomize( HybridMatrix<Type,M,N,SO>& matrix ) const
160 {
161  using blaze::randomize;
162 
163  const size_t m( matrix.rows() );
164  const size_t n( matrix.columns() );
165 
166  for( size_t i=0UL; i<m; ++i ) {
167  for( size_t j=0UL; j<n; ++j ) {
168  randomize( matrix(i,j) );
169  }
170  }
171 }
173 //*************************************************************************************************
174 
175 
176 //*************************************************************************************************
185 template< typename Type // Data type of the matrix
186  , size_t M // Number of rows
187  , size_t N // Number of columns
188  , bool SO > // Storage order
189 template< typename Arg > // Min/max argument type
190 inline void Rand< HybridMatrix<Type,M,N,SO> >::randomize( HybridMatrix<Type,M,N,SO>& matrix,
191  const Arg& min, const Arg& max ) const
192 {
193  using blaze::randomize;
194 
195  const size_t m( matrix.rows() );
196  const size_t n( matrix.columns() );
197 
198  for( size_t i=0UL; i<m; ++i ) {
199  for( size_t j=0UL; j<n; ++j ) {
200  randomize( matrix(i,j), min, max );
201  }
202  }
203 }
205 //*************************************************************************************************
206 
207 
208 
209 
210 //=================================================================================================
211 //
212 // MAKE FUNCTIONS
213 //
214 //=================================================================================================
215 
216 //*************************************************************************************************
224 template< typename Type // Data type of the matrix
225  , size_t M // Number of rows
226  , size_t N // Number of columns
227  , bool SO > // Storage order
228 void makeSymmetric( HybridMatrix<Type,M,N,SO>& matrix )
229 {
230  using blaze::randomize;
231 
232  if( !isSquare( ~matrix ) ) {
233  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
234  }
235 
236  const size_t n( matrix.rows() );
237 
238  for( size_t i=0UL; i<n; ++i ) {
239  for( size_t j=0UL; j<i; ++j ) {
240  randomize( matrix(i,j) );
241  matrix(j,i) = matrix(i,j);
242  }
243  randomize( matrix(i,i) );
244  }
245 
246  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
247 }
249 //*************************************************************************************************
250 
251 
252 //*************************************************************************************************
262 template< typename Type // Data type of the matrix
263  , size_t M // Number of rows
264  , size_t N // Number of columns
265  , bool SO // Storage order
266  , typename Arg > // Min/max argument type
267 void makeSymmetric( HybridMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max )
268 {
269  using blaze::randomize;
270 
271  if( !isSquare( ~matrix ) ) {
272  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
273  }
274 
275  const size_t n( matrix.rows() );
276 
277  for( size_t i=0UL; i<n; ++i ) {
278  for( size_t j=0UL; j<i; ++j ) {
279  randomize( matrix(i,j), min, max );
280  matrix(j,i) = matrix(i,j);
281  }
282  randomize( matrix(i,i), min, max );
283  }
284 
285  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
286 }
288 //*************************************************************************************************
289 
290 
291 //*************************************************************************************************
299 template< typename Type // Data type of the matrix
300  , size_t M // Number of rows
301  , size_t N // Number of columns
302  , bool SO > // Storage order
303 void makeHermitian( HybridMatrix<Type,M,N,SO>& matrix )
304 {
305  using blaze::randomize;
306 
308 
309  using BT = UnderlyingBuiltin_<Type>;
310 
311  if( !isSquare( ~matrix ) ) {
312  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
313  }
314 
315  const size_t n( matrix.rows() );
316 
317  for( size_t i=0UL; i<n; ++i ) {
318  for( size_t j=0UL; j<i; ++j ) {
319  randomize( matrix(i,j) );
320  matrix(j,i) = conj( matrix(i,j) );
321  }
322  matrix(i,i) = rand<BT>();
323  }
324 
325  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
326 }
328 //*************************************************************************************************
329 
330 
331 //*************************************************************************************************
341 template< typename Type // Data type of the matrix
342  , size_t M // Number of rows
343  , size_t N // Number of columns
344  , bool SO // Storage order
345  , typename Arg > // Min/max argument type
346 void makeHermitian( HybridMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max )
347 {
348  using blaze::randomize;
349 
351 
352  using BT = UnderlyingBuiltin_<Type>;
353 
354  if( !isSquare( ~matrix ) ) {
355  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
356  }
357 
358  const size_t n( matrix.rows() );
359 
360  for( size_t i=0UL; i<n; ++i ) {
361  for( size_t j=0UL; j<i; ++j ) {
362  randomize( matrix(i,j), min, max );
363  matrix(j,i) = conj( matrix(i,j) );
364  }
365  matrix(i,i) = rand<BT>( real( min ), real( max ) );
366  }
367 
368  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
369 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
382 template< typename Type // Data type of the matrix
383  , size_t M // Number of rows
384  , size_t N // Number of columns
385  , bool SO > // Storage order
386 void makePositiveDefinite( HybridMatrix<Type,M,N,SO>& matrix )
387 {
388  using blaze::randomize;
389 
391 
392  if( !isSquare( ~matrix ) ) {
393  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
394  }
395 
396  const size_t n( matrix.rows() );
397 
398  randomize( matrix );
399  matrix *= ctrans( matrix );
400 
401  for( size_t i=0UL; i<n; ++i ) {
402  matrix(i,i) += Type(n);
403  }
404 
405  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
406 }
408 //*************************************************************************************************
409 
410 } // namespace blaze
411 
412 #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.
Header file for the complete HybridVector implementation.
decltype(auto) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1387
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1903
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:1950
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:1359
Header file for the UnderlyingBuiltin type trait.
Header file for the exception macros of the math module.
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:919
Header file for the implementation of a fixed-size matrix.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:841
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 StaticMatrix implementation.
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:1321
BLAZE_ALWAYS_INLINE bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:908
#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