CustomMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_CUSTOMMATRIX_H_
36 #define _BLAZE_MATH_CUSTOMMATRIX_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
45 #include <blaze/math/DenseMatrix.h>
47 #include <blaze/math/Exception.h>
50 #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  , bool AF // Alignment flag
74  , bool PF // Padding flag
75  , bool SO > // Storage order
76 class Rand< CustomMatrix<Type,AF,PF,SO> >
77 {
78  public:
79  //**Randomize functions*************************************************************************
82  inline void randomize( CustomMatrix<Type,AF,PF,SO>& matrix ) const;
83 
84  template< typename Arg >
85  inline void randomize( CustomMatrix<Type,AF,PF,SO>& matrix, const Arg& min, const Arg& max ) const;
87  //**********************************************************************************************
88 };
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
100 template< typename Type // Data type of the matrix
101  , bool AF // Alignment flag
102  , bool PF // Padding flag
103  , bool SO > // Storage order
104 inline void Rand< CustomMatrix<Type,AF,PF,SO> >::randomize( CustomMatrix<Type,AF,PF,SO>& matrix ) const
105 {
106  using blaze::randomize;
107 
108  const size_t m( matrix.rows() );
109  const size_t n( matrix.columns() );
110 
111  for( size_t i=0UL; i<m; ++i ) {
112  for( size_t j=0UL; j<n; ++j ) {
113  randomize( matrix(i,j) );
114  }
115  }
116 }
118 //*************************************************************************************************
119 
120 
121 //*************************************************************************************************
130 template< typename Type // Data type of the matrix
131  , bool AF // Alignment flag
132  , bool PF // Padding flag
133  , bool SO > // Storage order
134 template< typename Arg > // Min/max argument type
135 inline void Rand< CustomMatrix<Type,AF,PF,SO> >::randomize( CustomMatrix<Type,AF,PF,SO>& matrix,
136  const Arg& min, const Arg& max ) const
137 {
138  using blaze::randomize;
139 
140  const size_t m( matrix.rows() );
141  const size_t n( matrix.columns() );
142 
143  for( size_t i=0UL; i<m; ++i ) {
144  for( size_t j=0UL; j<n; ++j ) {
145  randomize( matrix(i,j), min, max );
146  }
147  }
148 }
150 //*************************************************************************************************
151 
152 
153 
154 
155 //=================================================================================================
156 //
157 // MAKE FUNCTIONS
158 //
159 //=================================================================================================
160 
161 //*************************************************************************************************
169 template< typename Type // Data type of the matrix
170  , bool AF // Alignment flag
171  , bool PF // Padding flag
172  , bool SO > // Storage order
173 void makeSymmetric( CustomMatrix<Type,AF,PF,SO>& matrix )
174 {
175  using blaze::randomize;
176 
177  if( !isSquare( ~matrix ) ) {
178  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
179  }
180 
181  const size_t n( matrix.rows() );
182 
183  for( size_t i=0UL; i<n; ++i ) {
184  for( size_t j=0UL; j<i; ++j ) {
185  randomize( matrix(i,j) );
186  matrix(j,i) = matrix(i,j);
187  }
188  randomize( matrix(i,i) );
189  }
190 
191  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
192 }
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
207 template< typename Type // Data type of the matrix
208  , bool AF // Alignment flag
209  , bool PF // Padding flag
210  , bool SO // Storage order
211  , typename Arg > // Min/max argument type
212 void makeSymmetric( CustomMatrix<Type,AF,PF,SO>& matrix, const Arg& min, const Arg& max )
213 {
214  using blaze::randomize;
215 
216  if( !isSquare( ~matrix ) ) {
217  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
218  }
219 
220  const size_t n( matrix.rows() );
221 
222  for( size_t i=0UL; i<n; ++i ) {
223  for( size_t j=0UL; j<i; ++j ) {
224  randomize( matrix(i,j), min, max );
225  matrix(j,i) = matrix(i,j);
226  }
227  randomize( matrix(i,i), min, max );
228  }
229 
230  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
231 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
244 template< typename Type // Data type of the matrix
245  , bool AF // Alignment flag
246  , bool PF // Padding flag
247  , bool SO > // Storage order
248 void makeHermitian( CustomMatrix<Type,AF,PF,SO>& matrix )
249 {
250  using blaze::randomize;
251 
253 
254  using BT = UnderlyingBuiltin_<Type>;
255 
256  if( !isSquare( ~matrix ) ) {
257  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
258  }
259 
260  const size_t n( matrix.rows() );
261 
262  for( size_t i=0UL; i<n; ++i ) {
263  for( size_t j=0UL; j<i; ++j ) {
264  randomize( matrix(i,j) );
265  matrix(j,i) = conj( matrix(i,j) );
266  }
267  matrix(i,i) = rand<BT>();
268  }
269 
270  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
271 }
273 //*************************************************************************************************
274 
275 
276 //*************************************************************************************************
286 template< typename Type // Data type of the matrix
287  , bool AF // Alignment flag
288  , bool PF // Padding flag
289  , bool SO // Storage order
290  , typename Arg > // Min/max argument type
291 void makeHermitian( CustomMatrix<Type,AF,PF,SO>& matrix, const Arg& min, const Arg& max )
292 {
293  using blaze::randomize;
294 
296 
297  using BT = UnderlyingBuiltin_<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), min, max );
308  matrix(j,i) = conj( matrix(i,j) );
309  }
310  matrix(i,i) = rand<BT>( real( min ), real( max ) );
311  }
312 
313  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
314 }
316 //*************************************************************************************************
317 
318 
319 //*************************************************************************************************
327 template< typename Type // Data type of the matrix
328  , bool AF // Alignment flag
329  , bool PF // Padding flag
330  , bool SO > // Storage order
331 void makePositiveDefinite( CustomMatrix<Type,AF,PF,SO>& matrix )
332 {
333  using blaze::randomize;
334 
336 
337  if( !isSquare( ~matrix ) ) {
338  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
339  }
340 
341  const size_t n( matrix.rows() );
342 
343  randomize( matrix );
344  matrix *= ctrans( matrix );
345 
346  for( size_t i=0UL; i<n; ++i ) {
347  matrix(i,i) += Type(n);
348  }
349 
350  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
351 }
353 //*************************************************************************************************
354 
355 } // namespace blaze
356 
357 #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: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
Header file for the implementation of a customizable matrix.
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 implementation of a dynamic MxN matrix.
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:919
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:841
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: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