All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatAbsExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATABSEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DMATABSEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cmath>
42 #include <blaze/util/Assert.h>
44 #include <blaze/util/EnableIf.h>
45 #include <blaze/util/SelectType.h>
46 #include <blaze/util/Types.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // CLASS DMATABSEXPR
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
64 template< typename MT // Type of the dense matrix
65  , bool SO > // Storage order
66 class DMatAbsExpr : public DenseMatrix< DMatAbsExpr<MT,SO>, SO >
67  , private Expression
68  , private Computation
69 {
70  private:
71  //**Type definitions****************************************************************************
72  typedef typename MT::ResultType RT;
73  typedef typename MT::ReturnType RN;
74  typedef typename MT::CompositeType CT;
75  //**********************************************************************************************
76 
77  //**Return type evaluation**********************************************************************
79 
84  enum { returnExpr = !IsTemporary<RN>::value };
85 
88  //**********************************************************************************************
89 
90  //**Evaluation strategy*************************************************************************
92 
98  enum { useAssign = RequiresEvaluation<MT>::value };
99 
101 
102  template< typename MT2 >
103  struct UseAssign {
104  enum { value = useAssign };
105  };
107  //**********************************************************************************************
108 
109  public:
110  //**Type definitions****************************************************************************
112  typedef typename MT::ResultType ResultType;
113  typedef typename MT::OppositeType OppositeType;
114  typedef typename MT::TransposeType TransposeType;
115  typedef typename MT::ElementType ElementType;
116 
119 
122 
124  typedef typename SelectType< IsExpression<MT>::value, const MT, const MT& >::Type Operand;
125  //**********************************************************************************************
126 
127  //**Compilation flags***************************************************************************
129  enum { vectorizable = 0 };
130 
132  enum { canAlias = CanAlias<MT>::value };
133  //**********************************************************************************************
134 
135  //**Constructor*********************************************************************************
140  explicit inline DMatAbsExpr( const MT& dm )
141  : dm_( dm ) // Dense matrix of the absolute value expression
142  {}
143  //**********************************************************************************************
144 
145  //**Access operator*****************************************************************************
152  inline ReturnType operator()( size_t i, size_t j ) const {
153  using std::abs;
154  BLAZE_INTERNAL_ASSERT( i < dm_.rows() , "Invalid row access index" );
155  BLAZE_INTERNAL_ASSERT( j < dm_.columns(), "Invalid column access index" );
156  return abs( dm_(i,j) );
157  }
158  //**********************************************************************************************
159 
160  //**Rows function*******************************************************************************
165  inline size_t rows() const {
166  return dm_.rows();
167  }
168  //**********************************************************************************************
169 
170  //**Columns function****************************************************************************
175  inline size_t columns() const {
176  return dm_.columns();
177  }
178  //**********************************************************************************************
179 
180  //**Operand access******************************************************************************
185  inline Operand operand() const {
186  return dm_;
187  }
188  //**********************************************************************************************
189 
190  //**********************************************************************************************
196  template< typename T >
197  inline bool isAliased( const T* alias ) const {
198  return CanAlias<MT>::value && dm_.isAliased( alias );
199  }
200  //**********************************************************************************************
201 
202  private:
203  //**Member variables****************************************************************************
205  //**********************************************************************************************
206 
207  //**Assignment to dense matrices****************************************************************
221  template< typename MT2 > // Type of the target dense matrix
222  friend inline typename EnableIf< UseAssign<MT2> >::Type
223  assign( DenseMatrix<MT2,false>& lhs, const DMatAbsExpr& rhs )
224  {
225  using std::abs;
226 
227  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
228  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
229 
230  assign( ~lhs, rhs.dm_ );
231 
232  const size_t m( rhs.rows() );
233  const size_t n( rhs.columns() );
234 
235  for( size_t i=0UL; i<m; ++i ) {
236  for( size_t j=0UL; j<n; ++j ) {
237  (~lhs)(i,j) = abs( (~lhs)(i,j) );
238  }
239  }
240  }
242  //**********************************************************************************************
243 
244  //**Assignment to dense matrices****************************************************************
258  template< typename MT2 > // Type of the target dense matrix
259  friend inline typename EnableIf< UseAssign<MT2> >::Type
260  assign( DenseMatrix<MT2,true>& lhs, const DMatAbsExpr& rhs )
261  {
262  using std::abs;
263 
264  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
265  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
266 
267  assign( ~lhs, rhs.dm_ );
268 
269  const size_t m( rhs.rows() );
270  const size_t n( rhs.columns() );
271 
272  for( size_t j=0UL; j<n; ++j ) {
273  for( size_t i=0UL; i<m; ++i ) {
274  (~lhs)(i,j) = abs( (~lhs)(i,j) );
275  }
276  }
277  }
279  //**********************************************************************************************
280 
281  //**Assignment to sparse matrices***************************************************************
295  template< typename MT2 // Type of the target sparse matrix
296  , bool SO2 > // Storage order or the target sparse matrix
297  friend inline typename EnableIf< UseAssign<MT2> >::Type
298  assign( SparseMatrix<MT2,SO2>& lhs, const DMatAbsExpr& rhs )
299  {
300  typedef typename SelectType< SO == SO2, ResultType, OppositeType >::Type TmpType;
301 
307  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
308 
309  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
310  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
311 
312  const TmpType tmp( rhs );
313  assign( ~lhs, tmp );
314  }
316  //**********************************************************************************************
317 
318  //**Addition assignment to dense matrices*******************************************************
332  template< typename MT2 // Type of the target dense matrix
333  , bool SO2 > // Storage order of the target dense matrix
334  friend inline typename EnableIf< UseAssign<MT2> >::Type
335  addAssign( DenseMatrix<MT2,SO2>& lhs, const DMatAbsExpr& rhs )
336  {
339  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
340 
341  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
342  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
343 
344  const ResultType tmp( rhs );
345  addAssign( ~lhs, tmp );
346  }
348  //**********************************************************************************************
349 
350  //**Addition assignment to sparse matrices******************************************************
351  // No special implementation for the addition assignment to sparse matrices.
352  //**********************************************************************************************
353 
354  //**Subtraction assignment to dense matrices****************************************************
368  template< typename MT2 // Type of the target dense matrix
369  , bool SO2 > // Storage order of the target dense matrix
370  friend inline typename EnableIf< UseAssign<MT2> >::Type
371  subAssign( DenseMatrix<MT2,SO2>& lhs, const DMatAbsExpr& rhs )
372  {
375  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
376 
377  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
378  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
379 
380  const ResultType tmp( rhs );
381  subAssign( ~lhs, tmp );
382  }
384  //**********************************************************************************************
385 
386  //**Subtraction assignment to sparse matrices***************************************************
387  // No special implementation for the subtraction assignment to sparse matrices.
388  //**********************************************************************************************
389 
390  //**Multiplication assignment to dense matrices*************************************************
391  // No special implementation for the multiplication assignment to dense matrices.
392  //**********************************************************************************************
393 
394  //**Multiplication assignment to sparse matrices************************************************
395  // No special implementation for the multiplication assignment to sparse matrices.
396  //**********************************************************************************************
397 
398  //**Compile time checks*************************************************************************
403  //**********************************************************************************************
404 };
405 //*************************************************************************************************
406 
407 
408 
409 
410 //=================================================================================================
411 //
412 // GLOBAL OPERATORS
413 //
414 //=================================================================================================
415 
416 //*************************************************************************************************
433 template< typename MT // Type of the dense matrix
434  , bool SO > // Storage order
435 inline const DMatAbsExpr<MT,SO> abs( const DenseMatrix<MT,SO>& dm )
436 {
437  return DMatAbsExpr<MT,SO>( ~dm );
438 }
439 //*************************************************************************************************
440 
441 
442 
443 
444 //=================================================================================================
445 //
446 // GLOBAL RESTRUCTURING OPERATORS
447 //
448 //=================================================================================================
449 
450 //*************************************************************************************************
461 template< typename MT // Type of the dense matrix
462  , bool TF > // Transpose flag
463 inline const DMatAbsExpr<MT,TF>& abs( const DMatAbsExpr<MT,TF>& dm )
464 {
465  return dm;
466 }
468 //*************************************************************************************************
469 
470 } // namespace blaze
471 
472 #endif