Blaze  3.6
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/math/ZeroMatrix.h>
53 #include <blaze/util/Assert.h>
55 #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< HybridMatrix<Type,M,N,SO> >
78 {
79  public:
80  //**Generate functions**************************************************************************
83  inline const HybridMatrix<Type,M,N,SO> generate( size_t m, size_t n ) const;
84 
85  template< typename Arg >
86  inline const HybridMatrix<Type,M,N,SO> generate( size_t m, size_t n, const Arg& min, const Arg& max ) const;
88  //**********************************************************************************************
89 
90  //**Randomize functions*************************************************************************
93  inline void randomize( HybridMatrix<Type,M,N,SO>& matrix ) const;
94 
95  template< typename Arg >
96  inline void randomize( HybridMatrix<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 HybridMatrix<Type,M,N,SO>
115  Rand< HybridMatrix<Type,M,N,SO> >::generate( size_t m, size_t n ) const
116 {
117  HybridMatrix<Type,M,N,SO> matrix( m, n );
118  randomize( matrix );
119  return matrix;
120 }
122 //*************************************************************************************************
123 
124 
125 //*************************************************************************************************
133 template< typename Type // Data type of the matrix
134  , size_t M // Number of rows
135  , size_t N // Number of columns
136  , bool SO > // Storage order
137 template< typename Arg > // Min/max argument type
138 inline const HybridMatrix<Type,M,N,SO>
139  Rand< HybridMatrix<Type,M,N,SO> >::generate( size_t m, size_t n, const Arg& min, const Arg& max ) const
140 {
141  HybridMatrix<Type,M,N,SO> matrix( m, n );
142  randomize( matrix, min, max );
143  return matrix;
144 }
146 //*************************************************************************************************
147 
148 
149 //*************************************************************************************************
156 template< typename Type // Data type of the matrix
157  , size_t M // Number of rows
158  , size_t N // Number of columns
159  , bool SO > // Storage order
160 inline void Rand< HybridMatrix<Type,M,N,SO> >::randomize( HybridMatrix<Type,M,N,SO>& matrix ) const
161 {
162  using blaze::randomize;
163 
164  const size_t m( matrix.rows() );
165  const size_t n( matrix.columns() );
166 
167  for( size_t i=0UL; i<m; ++i ) {
168  for( size_t j=0UL; j<n; ++j ) {
169  randomize( matrix(i,j) );
170  }
171  }
172 }
174 //*************************************************************************************************
175 
176 
177 //*************************************************************************************************
186 template< typename Type // Data type of the matrix
187  , size_t M // Number of rows
188  , size_t N // Number of columns
189  , bool SO > // Storage order
190 template< typename Arg > // Min/max argument type
191 inline void Rand< HybridMatrix<Type,M,N,SO> >::randomize( HybridMatrix<Type,M,N,SO>& matrix,
192  const Arg& min, const Arg& max ) const
193 {
194  using blaze::randomize;
195 
196  const size_t m( matrix.rows() );
197  const size_t n( matrix.columns() );
198 
199  for( size_t i=0UL; i<m; ++i ) {
200  for( size_t j=0UL; j<n; ++j ) {
201  randomize( matrix(i,j), min, max );
202  }
203  }
204 }
206 //*************************************************************************************************
207 
208 
209 
210 
211 //=================================================================================================
212 //
213 // MAKE FUNCTIONS
214 //
215 //=================================================================================================
216 
217 //*************************************************************************************************
225 template< typename Type // Data type of the matrix
226  , size_t M // Number of rows
227  , size_t N // Number of columns
228  , bool SO > // Storage order
229 void makeSymmetric( HybridMatrix<Type,M,N,SO>& matrix )
230 {
231  using blaze::randomize;
232 
233  if( !isSquare( ~matrix ) ) {
234  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
235  }
236 
237  const size_t n( matrix.rows() );
238 
239  for( size_t i=0UL; i<n; ++i ) {
240  for( size_t j=0UL; j<i; ++j ) {
241  randomize( matrix(i,j) );
242  matrix(j,i) = matrix(i,j);
243  }
244  randomize( matrix(i,i) );
245  }
246 
247  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
248 }
250 //*************************************************************************************************
251 
252 
253 //*************************************************************************************************
263 template< typename Type // Data type of the matrix
264  , size_t M // Number of rows
265  , size_t N // Number of columns
266  , bool SO // Storage order
267  , typename Arg > // Min/max argument type
268 void makeSymmetric( HybridMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max )
269 {
270  using blaze::randomize;
271 
272  if( !isSquare( ~matrix ) ) {
273  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
274  }
275 
276  const size_t n( matrix.rows() );
277 
278  for( size_t i=0UL; i<n; ++i ) {
279  for( size_t j=0UL; j<i; ++j ) {
280  randomize( matrix(i,j), min, max );
281  matrix(j,i) = matrix(i,j);
282  }
283  randomize( matrix(i,i), min, max );
284  }
285 
286  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
287 }
289 //*************************************************************************************************
290 
291 
292 //*************************************************************************************************
300 template< typename Type // Data type of the matrix
301  , size_t M // Number of rows
302  , size_t N // Number of columns
303  , bool SO > // Storage order
304 void makeHermitian( HybridMatrix<Type,M,N,SO>& matrix )
305 {
306  using blaze::randomize;
307 
309 
310  using BT = UnderlyingBuiltin_t<Type>;
311 
312  if( !isSquare( ~matrix ) ) {
313  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
314  }
315 
316  const size_t n( matrix.rows() );
317 
318  for( size_t i=0UL; i<n; ++i ) {
319  for( size_t j=0UL; j<i; ++j ) {
320  randomize( matrix(i,j) );
321  matrix(j,i) = conj( matrix(i,j) );
322  }
323  matrix(i,i) = rand<BT>();
324  }
325 
326  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
327 }
329 //*************************************************************************************************
330 
331 
332 //*************************************************************************************************
342 template< typename Type // Data type of the matrix
343  , size_t M // Number of rows
344  , size_t N // Number of columns
345  , bool SO // Storage order
346  , typename Arg > // Min/max argument type
347 void makeHermitian( HybridMatrix<Type,M,N,SO>& matrix, const Arg& min, const Arg& max )
348 {
349  using blaze::randomize;
350 
352 
353  using BT = UnderlyingBuiltin_t<Type>;
354 
355  if( !isSquare( ~matrix ) ) {
356  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
357  }
358 
359  const size_t n( matrix.rows() );
360 
361  for( size_t i=0UL; i<n; ++i ) {
362  for( size_t j=0UL; j<i; ++j ) {
363  randomize( matrix(i,j), min, max );
364  matrix(j,i) = conj( matrix(i,j) );
365  }
366  matrix(i,i) = rand<BT>( real( min ), real( max ) );
367  }
368 
369  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
370 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
383 template< typename Type // Data type of the matrix
384  , size_t M // Number of rows
385  , size_t N // Number of columns
386  , bool SO > // Storage order
387 void makePositiveDefinite( HybridMatrix<Type,M,N,SO>& matrix )
388 {
389  using blaze::randomize;
390 
392 
393  if( !isSquare( ~matrix ) ) {
394  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
395  }
396 
397  const size_t n( matrix.rows() );
398 
399  randomize( matrix );
400  matrix *= ctrans( matrix );
401 
402  for( size_t i=0UL; i<n; ++i ) {
403  matrix(i,i) += Type(n);
404  }
405 
406  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
407 }
409 //*************************************************************************************************
410 
411 } // namespace blaze
412 
413 #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: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.
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 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
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: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 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: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.