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>
43 #include <blaze/util/Assert.h>
45 #include <blaze/util/EnableIf.h>
47 #include <blaze/util/SelectType.h>
48 #include <blaze/util/Types.h>
49 
50 
51 namespace blaze {
52 
53 //=================================================================================================
54 //
55 // CLASS DMATABSEXPR
56 //
57 //=================================================================================================
58 
59 //*************************************************************************************************
66 template< typename MT // Type of the dense matrix
67  , bool SO > // Storage order
68 class DMatAbsExpr : public DenseMatrix< DMatAbsExpr<MT,SO>, SO >
69  , private MatAbsExpr
70  , private Computation
71 {
72  private:
73  //**Type definitions****************************************************************************
74  typedef typename MT::ResultType RT;
75  typedef typename MT::ReturnType RN;
76  typedef typename MT::CompositeType CT;
77  //**********************************************************************************************
78 
79  //**Return type evaluation**********************************************************************
81 
86  enum { returnExpr = !IsTemporary<RN>::value };
87 
90  //**********************************************************************************************
91 
92  //**Evaluation strategy*************************************************************************
94 
100  enum { useAssign = RequiresEvaluation<MT>::value };
101 
103 
104  template< typename MT2 >
105  struct UseAssign {
106  enum { value = useAssign };
107  };
109  //**********************************************************************************************
110 
111  public:
112  //**Type definitions****************************************************************************
114  typedef typename MT::ResultType ResultType;
115  typedef typename MT::OppositeType OppositeType;
116  typedef typename MT::TransposeType TransposeType;
117  typedef typename MT::ElementType ElementType;
118 
121 
124 
126  typedef typename SelectType< IsExpression<MT>::value, const MT, const MT& >::Type Operand;
127  //**********************************************************************************************
128 
129  //**Compilation flags***************************************************************************
131  enum { vectorizable = 0 };
132  //**********************************************************************************************
133 
134  //**Constructor*********************************************************************************
139  explicit inline DMatAbsExpr( const MT& dm )
140  : dm_( dm ) // Dense matrix of the absolute value expression
141  {}
142  //**********************************************************************************************
143 
144  //**Access operator*****************************************************************************
151  inline ReturnType operator()( size_t i, size_t j ) const {
152  using std::abs;
153  BLAZE_INTERNAL_ASSERT( i < dm_.rows() , "Invalid row access index" );
154  BLAZE_INTERNAL_ASSERT( j < dm_.columns(), "Invalid column access index" );
155  return abs( dm_(i,j) );
156  }
157  //**********************************************************************************************
158 
159  //**Rows function*******************************************************************************
164  inline size_t rows() const {
165  return dm_.rows();
166  }
167  //**********************************************************************************************
168 
169  //**Columns function****************************************************************************
174  inline size_t columns() const {
175  return dm_.columns();
176  }
177  //**********************************************************************************************
178 
179  //**Operand access******************************************************************************
184  inline Operand operand() const {
185  return dm_;
186  }
187  //**********************************************************************************************
188 
189  //**********************************************************************************************
195  template< typename T >
196  inline bool canAlias( const T* alias ) const {
197  return dm_.canAlias( alias );
198  }
199  //**********************************************************************************************
200 
201  //**********************************************************************************************
207  template< typename T >
208  inline bool isAliased( const T* alias ) const {
209  return dm_.isAliased( alias );
210  }
211  //**********************************************************************************************
212 
213  private:
214  //**Member variables****************************************************************************
216  //**********************************************************************************************
217 
218  //**Assignment to dense matrices****************************************************************
232  template< typename MT2 > // Type of the target dense matrix
233  friend inline typename EnableIf< UseAssign<MT2> >::Type
234  assign( DenseMatrix<MT2,false>& lhs, const DMatAbsExpr& rhs )
235  {
237 
238  using std::abs;
239 
240  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
241  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
242 
243  assign( ~lhs, rhs.dm_ );
244 
245  const size_t m( rhs.rows() );
246  const size_t n( rhs.columns() );
247 
248  for( size_t i=0UL; i<m; ++i ) {
249  for( size_t j=0UL; j<n; ++j ) {
250  (~lhs)(i,j) = abs( (~lhs)(i,j) );
251  }
252  }
253  }
255  //**********************************************************************************************
256 
257  //**Assignment to dense matrices****************************************************************
271  template< typename MT2 > // Type of the target dense matrix
272  friend inline typename EnableIf< UseAssign<MT2> >::Type
273  assign( DenseMatrix<MT2,true>& lhs, const DMatAbsExpr& rhs )
274  {
276 
277  using std::abs;
278 
279  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
280  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
281 
282  assign( ~lhs, rhs.dm_ );
283 
284  const size_t m( rhs.rows() );
285  const size_t n( rhs.columns() );
286 
287  for( size_t j=0UL; j<n; ++j ) {
288  for( size_t i=0UL; i<m; ++i ) {
289  (~lhs)(i,j) = abs( (~lhs)(i,j) );
290  }
291  }
292  }
294  //**********************************************************************************************
295 
296  //**Assignment to sparse matrices***************************************************************
310  template< typename MT2 // Type of the target sparse matrix
311  , bool SO2 > // Storage order or the target sparse matrix
312  friend inline typename EnableIf< UseAssign<MT2> >::Type
313  assign( SparseMatrix<MT2,SO2>& lhs, const DMatAbsExpr& rhs )
314  {
316 
317  typedef typename SelectType< SO == SO2, ResultType, OppositeType >::Type TmpType;
318 
324  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
325 
326  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
327  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
328 
329  const TmpType tmp( rhs );
330  assign( ~lhs, tmp );
331  }
333  //**********************************************************************************************
334 
335  //**Addition assignment to dense matrices*******************************************************
349  template< typename MT2 // Type of the target dense matrix
350  , bool SO2 > // Storage order of the target dense matrix
351  friend inline typename EnableIf< UseAssign<MT2> >::Type
352  addAssign( DenseMatrix<MT2,SO2>& lhs, const DMatAbsExpr& rhs )
353  {
355 
358  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
359 
360  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
361  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
362 
363  const ResultType tmp( rhs );
364  addAssign( ~lhs, tmp );
365  }
367  //**********************************************************************************************
368 
369  //**Addition assignment to sparse matrices******************************************************
370  // No special implementation for the addition assignment to sparse matrices.
371  //**********************************************************************************************
372 
373  //**Subtraction assignment to dense matrices****************************************************
387  template< typename MT2 // Type of the target dense matrix
388  , bool SO2 > // Storage order of the target dense matrix
389  friend inline typename EnableIf< UseAssign<MT2> >::Type
390  subAssign( DenseMatrix<MT2,SO2>& lhs, const DMatAbsExpr& rhs )
391  {
393 
396  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
397 
398  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
399  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
400 
401  const ResultType tmp( rhs );
402  subAssign( ~lhs, tmp );
403  }
405  //**********************************************************************************************
406 
407  //**Subtraction assignment to sparse matrices***************************************************
408  // No special implementation for the subtraction assignment to sparse matrices.
409  //**********************************************************************************************
410 
411  //**Multiplication assignment to dense matrices*************************************************
412  // No special implementation for the multiplication assignment to dense matrices.
413  //**********************************************************************************************
414 
415  //**Multiplication assignment to sparse matrices************************************************
416  // No special implementation for the multiplication assignment to sparse matrices.
417  //**********************************************************************************************
418 
419  //**Compile time checks*************************************************************************
424  //**********************************************************************************************
425 };
426 //*************************************************************************************************
427 
428 
429 
430 
431 //=================================================================================================
432 //
433 // GLOBAL OPERATORS
434 //
435 //=================================================================================================
436 
437 //*************************************************************************************************
454 template< typename MT // Type of the dense matrix
455  , bool SO > // Storage order
456 inline const DMatAbsExpr<MT,SO> abs( const DenseMatrix<MT,SO>& dm )
457 {
459 
460  return DMatAbsExpr<MT,SO>( ~dm );
461 }
462 //*************************************************************************************************
463 
464 
465 
466 
467 //=================================================================================================
468 //
469 // GLOBAL RESTRUCTURING OPERATORS
470 //
471 //=================================================================================================
472 
473 //*************************************************************************************************
484 template< typename MT // Type of the dense matrix
485  , bool SO > // Storage order
486 inline const DMatAbsExpr<MT,SO>& abs( const DMatAbsExpr<MT,SO>& dm )
487 {
489 
490  return dm;
491 }
493 //*************************************************************************************************
494 
495 
496 
497 
498 //=================================================================================================
499 //
500 // EXPRESSION TRAIT SPECIALIZATIONS
501 //
502 //=================================================================================================
503 
504 //*************************************************************************************************
506 template< typename MT, bool SO >
507 struct RowExprTrait< DMatAbsExpr<MT,SO> >
508 {
509  public:
510  //**********************************************************************************************
511  typedef typename AbsExprTrait< typename RowExprTrait<const MT>::Type >::Type Type;
512  //**********************************************************************************************
513 };
515 //*************************************************************************************************
516 
517 
518 //*************************************************************************************************
520 template< typename MT, bool SO >
521 struct ColumnExprTrait< DMatAbsExpr<MT,SO> >
522 {
523  public:
524  //**********************************************************************************************
525  typedef typename AbsExprTrait< typename ColumnExprTrait<const MT>::Type >::Type Type;
526  //**********************************************************************************************
527 };
529 //*************************************************************************************************
530 
531 } // namespace blaze
532 
533 #endif