All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SMatAbsExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATABSEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SMATABSEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cmath>
31 #include <iterator>
32 #include <boost/type_traits/remove_reference.hpp>
46 #include <blaze/util/Assert.h>
48 #include <blaze/util/EnableIf.h>
49 #include <blaze/util/SelectType.h>
50 #include <blaze/util/Types.h>
51 
52 
53 namespace blaze {
54 
55 //=================================================================================================
56 //
57 // CLASS SMATABSEXPR
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
68 template< typename MT // Type of the sparse matrix
69  , bool SO > // Storage order
70 class SMatAbsExpr : public SparseMatrix< SMatAbsExpr<MT,SO>, SO >
71  , private Expression
72  , private Computation
73 {
74  private:
75  //**Type definitions****************************************************************************
76  typedef typename MT::ResultType RT;
77  typedef typename MT::ReturnType RN;
78  typedef typename MT::CompositeType CT;
79  //**********************************************************************************************
80 
81  //**Return type evaluation**********************************************************************
83 
88  enum { returnExpr = !IsTemporary<RN>::value };
89 
92  //**********************************************************************************************
93 
94  //**Evaluation strategy*************************************************************************
96 
102  enum { useAssign = RequiresEvaluation<MT>::value };
103 
105 
106  template< typename MT2 >
107  struct UseAssign {
108  enum { value = useAssign };
109  };
111  //**********************************************************************************************
112 
113  public:
114  //**Type definitions****************************************************************************
116  typedef typename MT::ResultType ResultType;
117  typedef typename MT::OppositeType OppositeType;
118  typedef typename MT::TransposeType TransposeType;
119  typedef typename MT::ElementType ElementType;
120 
123 
126 
128  typedef typename SelectType< IsExpression<MT>::value, const MT, const MT& >::Type Operand;
129  //**********************************************************************************************
130 
131  //**Compilation flags***************************************************************************
134  //**********************************************************************************************
135 
136  //**ConstIterator class definition**************************************************************
140  {
141  public:
142  //**Type definitions*************************************************************************
145 
147  typedef typename boost::remove_reference<Operand>::type::ConstIterator IteratorType;
148 
149  typedef std::forward_iterator_tag IteratorCategory;
150  typedef Element ValueType;
154 
155  // STL iterator requirements
161  //*******************************************************************************************
162 
163  //**Constructor******************************************************************************
167  : it_( it ) // Iterator over the elements of the sparse matrix expression
168  {}
169  //*******************************************************************************************
170 
171  //**Prefix increment operator****************************************************************
177  ++it_;
178  return *this;
179  }
180  //*******************************************************************************************
181 
182  //**Element access operator******************************************************************
187  inline const Element operator*() const {
188  using std::abs;
189  return Element( abs( it_->value() ), it_->index() );
190  }
191  //*******************************************************************************************
192 
193  //**Element access operator******************************************************************
198  inline const ConstIterator* operator->() const {
199  return this;
200  }
201  //*******************************************************************************************
202 
203  //**Value function***************************************************************************
208  inline ReturnType value() const {
209  using std::abs;
210  return abs( it_->value() );
211  }
212  //*******************************************************************************************
213 
214  //**Index function***************************************************************************
219  inline size_t index() const {
220  return it_->index();
221  }
222  //*******************************************************************************************
223 
224  //**Equality operator************************************************************************
230  inline bool operator==( const ConstIterator& rhs ) const {
231  return it_ == rhs.it_;
232  }
233  //*******************************************************************************************
234 
235  //**Inequality operator**********************************************************************
241  inline bool operator!=( const ConstIterator& rhs ) const {
242  return it_ != rhs.it_;
243  }
244  //*******************************************************************************************
245 
246  //**Subtraction operator*********************************************************************
252  inline DifferenceType operator-( const ConstIterator& rhs ) const {
253  return it_ - rhs.it_;
254  }
255  //*******************************************************************************************
256 
257  private:
258  //**Member variables*************************************************************************
260  //*******************************************************************************************
261  };
262  //**********************************************************************************************
263 
264  //**Constructor*********************************************************************************
269  explicit inline SMatAbsExpr( const MT& sm )
270  : sm_( sm ) // Sparse matrix of the absolute value expression
271  {}
272  //**********************************************************************************************
273 
274  //**Access operator*****************************************************************************
281  inline ReturnType operator()( size_t i, size_t j ) const {
282  using std::abs;
283  BLAZE_INTERNAL_ASSERT( i < sm_.rows() , "Invalid row access index" );
284  BLAZE_INTERNAL_ASSERT( j < sm_.columns(), "Invalid column access index" );
285  return abs( sm_(i,j) );
286  }
287  //**********************************************************************************************
288 
289  //**Begin function******************************************************************************
295  inline ConstIterator begin( size_t i ) const {
296  return ConstIterator( sm_.begin(i) );
297  }
298  //**********************************************************************************************
299 
300  //**End function********************************************************************************
306  inline ConstIterator end( size_t i ) const {
307  return ConstIterator( sm_.end(i) );
308  }
309  //**********************************************************************************************
310 
311  //**Rows function*******************************************************************************
316  inline size_t rows() const {
317  return sm_.rows();
318  }
319  //**********************************************************************************************
320 
321  //**Columns function****************************************************************************
326  inline size_t columns() const {
327  return sm_.columns();
328  }
329  //**********************************************************************************************
330 
331  //**NonZeros function***************************************************************************
336  inline size_t nonZeros() const {
337  return sm_.nonZeros();
338  }
339  //**********************************************************************************************
340 
341  //**NonZeros function***************************************************************************
347  inline size_t nonZeros( size_t i ) const {
348  return sm_.nonZeros(i);
349  }
350  //**********************************************************************************************
351 
352  //**Operand access******************************************************************************
357  inline Operand operand() const {
358  return sm_;
359  }
360  //**********************************************************************************************
361 
362  //**********************************************************************************************
368  template< typename T >
369  inline bool isAliased( const T* alias ) const {
371  !RequiresEvaluation<MT>::value && sm_.isAliased( alias );
372  }
373  //**********************************************************************************************
374 
375  private:
376  //**Member variables****************************************************************************
378  //**********************************************************************************************
379 
380  //**Assignment to dense matrices****************************************************************
394  template< typename MT2 // Type of the target dense matrix
395  , bool SO2 > // Storage order of the target dense matrix
396  friend inline typename EnableIf< UseAssign<MT2> >::Type
397  assign( DenseMatrix<MT2,SO2>& lhs, const SMatAbsExpr& rhs )
398  {
399  using std::abs;
400 
401  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
402  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
403 
404  assign( ~lhs, rhs.sm_ );
405 
406  const size_t m( rhs.rows() );
407  const size_t n( rhs.columns() );
408 
409  for( size_t i=0UL; i<m; ++i ) {
410  for( size_t j=0UL; j<n; ++j ) {
411  (~lhs)(i,j) = abs( (~lhs)(i,j) );
412  }
413  }
414  }
416  //**********************************************************************************************
417 
418  //**Assignment to row-major sparse matrices*****************************************************
432  template< typename MT2 > // Type of the target sparse matrix
433  friend inline typename EnableIf< UseAssign<MT2> >::Type
434  assign( SparseMatrix<MT2,false>& lhs, const SMatAbsExpr& rhs )
435  {
436  using std::abs;
437 
438  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
439  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
440 
441  typedef typename MT2::Iterator Iterator;
442 
443  assign( ~lhs, rhs.sm_ );
444 
445  const size_t m( rhs.rows() );
446 
447  for( size_t i=0UL; i<m; ++i ) {
448  const Iterator end( (~lhs).end(i) );
449  for( Iterator element=(~lhs).begin(i); element!=end; ++element ) {
450  element->value() = abs( element->value() );
451  }
452  }
453  }
455  //**********************************************************************************************
456 
457  //**Assignment to column-major sparse matrices**************************************************
471  template< typename MT2 > // Type of the target sparse matrix
472  friend inline typename EnableIf< UseAssign<MT2> >::Type
473  assign( SparseMatrix<MT2,true>& lhs, const SMatAbsExpr& rhs )
474  {
475  using std::abs;
476 
477  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
478  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
479 
480  typedef typename MT2::Iterator Iterator;
481 
482  assign( ~lhs, rhs.sm_ );
483 
484  const size_t n( rhs.columns() );
485 
486  for( size_t j=0UL; j<n; ++j ) {
487  const Iterator end( (~lhs).end(j) );
488  for( Iterator element=(~lhs).begin(j); element!=end; ++element ) {
489  element->value() = abs( element->value() );
490  }
491  }
492  }
494  //**********************************************************************************************
495 
496  //**Addition assignment to dense matrices*******************************************************
510  template< typename MT2 // Type of the target dense matrix
511  , bool SO2 > // Storage order of the target dense matrix
512  friend inline typename EnableIf< UseAssign<MT2> >::Type
513  addAssign( DenseMatrix<MT2,SO2>& lhs, const SMatAbsExpr& rhs )
514  {
516  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
517 
518  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
519  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
520 
521  const ResultType tmp( rhs );
522  addAssign( ~lhs, tmp );
523  }
525  //**********************************************************************************************
526 
527  //**Addition assignment to sparse matrices******************************************************
528  // No special implementation for the addition assignment to sparse matrices.
529  //**********************************************************************************************
530 
531  //**Subtraction assignment to dense matrices****************************************************
545  template< typename MT2 // Type of the target dense matrix
546  , bool SO2 > // Storage order of the target sparse matrix
547  friend inline typename EnableIf< UseAssign<MT2> >::Type
548  subAssign( DenseMatrix<MT2,SO2>& lhs, const SMatAbsExpr& rhs )
549  {
551  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
552 
553  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
554  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
555 
556  const ResultType tmp( rhs );
557  subAssign( ~lhs, tmp );
558  }
560  //**********************************************************************************************
561 
562  //**Subtraction assignment to sparse matrices***************************************************
563  // No special implementation for the subtraction assignment to sparse matrices.
564  //**********************************************************************************************
565 
566  //**Multiplication assignment to dense matrices*************************************************
567  // No special implementation for the multiplication assignment to dense matrices.
568  //**********************************************************************************************
569 
570  //**Multiplication assignment to sparse matrices************************************************
571  // No special implementation for the multiplication assignment to sparse matrices.
572  //**********************************************************************************************
573 
574  //**Compile time checks*************************************************************************
579  //**********************************************************************************************
580 };
581 //*************************************************************************************************
582 
583 
584 
585 
586 //=================================================================================================
587 //
588 // GLOBAL OPERATORS
589 //
590 //=================================================================================================
591 
592 //*************************************************************************************************
609 template< typename MT // Type of the sparse matrix
610  , bool SO > // Storage order
611 inline const SMatAbsExpr<MT,SO> abs( const SparseMatrix<MT,SO>& sm )
612 {
613  return SMatAbsExpr<MT,SO>( ~sm );
614 }
615 //*************************************************************************************************
616 
617 
618 
619 
620 //=================================================================================================
621 //
622 // GLOBAL RESTRUCTURING OPERATORS
623 //
624 //=================================================================================================
625 
626 //*************************************************************************************************
637 template< typename MT // Type of the sparse matrix
638  , bool TF > // Transpose flag
639 inline const SMatAbsExpr<MT,TF>& abs( const SMatAbsExpr<MT,TF>& sm )
640 {
641  return sm;
642 }
644 //*************************************************************************************************
645 
646 } // namespace blaze
647 
648 #endif