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>
49 #include <blaze/math/shims/Real.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 AF // Alignment flag
73  , bool PF // Padding flag
74  , bool SO > // Storage order
75 class Rand< CustomMatrix<Type,AF,PF,SO> >
76 {
77  public:
78  //**Randomize functions*************************************************************************
81  inline void randomize( CustomMatrix<Type,AF,PF,SO>& matrix ) const;
82 
83  template< typename Arg >
84  inline void randomize( CustomMatrix<Type,AF,PF,SO>& matrix, const Arg& min, const Arg& max ) const;
86  //**********************************************************************************************
87 };
89 //*************************************************************************************************
90 
91 
92 //*************************************************************************************************
99 template< typename Type // Data type of the matrix
100  , bool AF // Alignment flag
101  , bool PF // Padding flag
102  , bool SO > // Storage order
103 inline void Rand< CustomMatrix<Type,AF,PF,SO> >::randomize( CustomMatrix<Type,AF,PF,SO>& matrix ) const
104 {
105  using blaze::randomize;
106 
107  const size_t m( matrix.rows() );
108  const size_t n( matrix.columns() );
109 
110  for( size_t i=0UL; i<m; ++i ) {
111  for( size_t j=0UL; j<n; ++j ) {
112  randomize( matrix(i,j) );
113  }
114  }
115 }
117 //*************************************************************************************************
118 
119 
120 //*************************************************************************************************
129 template< typename Type // Data type of the matrix
130  , bool AF // Alignment flag
131  , bool PF // Padding flag
132  , bool SO > // Storage order
133 template< typename Arg > // Min/max argument type
134 inline void Rand< CustomMatrix<Type,AF,PF,SO> >::randomize( CustomMatrix<Type,AF,PF,SO>& matrix,
135  const Arg& min, const Arg& max ) const
136 {
137  using blaze::randomize;
138 
139  const size_t m( matrix.rows() );
140  const size_t n( matrix.columns() );
141 
142  for( size_t i=0UL; i<m; ++i ) {
143  for( size_t j=0UL; j<n; ++j ) {
144  randomize( matrix(i,j), min, max );
145  }
146  }
147 }
149 //*************************************************************************************************
150 
151 
152 
153 
154 //=================================================================================================
155 //
156 // MAKE FUNCTIONS
157 //
158 //=================================================================================================
159 
160 //*************************************************************************************************
168 template< typename Type // Data type of the matrix
169  , bool AF // Alignment flag
170  , bool PF // Padding flag
171  , bool SO > // Storage order
172 void makeSymmetric( CustomMatrix<Type,AF,PF,SO>& matrix )
173 {
174  using blaze::randomize;
175 
176  if( !isSquare( ~matrix ) ) {
177  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
178  }
179 
180  const size_t n( matrix.rows() );
181 
182  for( size_t i=0UL; i<n; ++i ) {
183  for( size_t j=0UL; j<i; ++j ) {
184  randomize( matrix(i,j) );
185  matrix(j,i) = matrix(i,j);
186  }
187  randomize( matrix(i,i) );
188  }
189 
190  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
191 }
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
206 template< typename Type // Data type of the matrix
207  , bool AF // Alignment flag
208  , bool PF // Padding flag
209  , bool SO // Storage order
210  , typename Arg > // Min/max argument type
211 void makeSymmetric( CustomMatrix<Type,AF,PF,SO>& matrix, const Arg& min, const Arg& max )
212 {
213  using blaze::randomize;
214 
215  if( !isSquare( ~matrix ) ) {
216  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
217  }
218 
219  const size_t n( matrix.rows() );
220 
221  for( size_t i=0UL; i<n; ++i ) {
222  for( size_t j=0UL; j<i; ++j ) {
223  randomize( matrix(i,j), min, max );
224  matrix(j,i) = matrix(i,j);
225  }
226  randomize( matrix(i,i), min, max );
227  }
228 
229  BLAZE_INTERNAL_ASSERT( isSymmetric( matrix ), "Non-symmetric matrix detected" );
230 }
232 //*************************************************************************************************
233 
234 
235 //*************************************************************************************************
243 template< typename Type // Data type of the matrix
244  , bool AF // Alignment flag
245  , bool PF // Padding flag
246  , bool SO > // Storage order
247 void makeHermitian( CustomMatrix<Type,AF,PF,SO>& matrix )
248 {
249  using blaze::randomize;
250 
252 
253  typedef UnderlyingBuiltin_<Type> BT;
254 
255  if( !isSquare( ~matrix ) ) {
256  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
257  }
258 
259  const size_t n( matrix.rows() );
260 
261  for( size_t i=0UL; i<n; ++i ) {
262  for( size_t j=0UL; j<i; ++j ) {
263  randomize( matrix(i,j) );
264  matrix(j,i) = conj( matrix(i,j) );
265  }
266  matrix(i,i) = rand<BT>();
267  }
268 
269  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
270 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
285 template< typename Type // Data type of the matrix
286  , bool AF // Alignment flag
287  , bool PF // Padding flag
288  , bool SO // Storage order
289  , typename Arg > // Min/max argument type
290 void makeHermitian( CustomMatrix<Type,AF,PF,SO>& matrix, const Arg& min, const Arg& max )
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), min, max );
307  matrix(j,i) = conj( matrix(i,j) );
308  }
309  matrix(i,i) = rand<BT>( real( min ), real( max ) );
310  }
311 
312  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-Hermitian matrix detected" );
313 }
315 //*************************************************************************************************
316 
317 
318 //*************************************************************************************************
326 template< typename Type // Data type of the matrix
327  , bool AF // Alignment flag
328  , bool PF // Padding flag
329  , bool SO > // Storage order
330 void makePositiveDefinite( CustomMatrix<Type,AF,PF,SO>& matrix )
331 {
332  using blaze::randomize;
333 
335 
336  if( !isSquare( ~matrix ) ) {
337  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
338  }
339 
340  const size_t n( matrix.rows() );
341 
342  randomize( matrix );
343  matrix *= ctrans( matrix );
344 
345  for( size_t i=0UL; i<n; ++i ) {
346  matrix(i,i) += Type(n);
347  }
348 
349  BLAZE_INTERNAL_ASSERT( isHermitian( matrix ), "Non-symmetric matrix detected" );
350 }
352 //*************************************************************************************************
353 
354 } // namespace blaze
355 
356 #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 implementation of a customizable matrix.
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
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