All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVecAbsExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECABSEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SVECABSEXPR_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 SVECABSEXPR
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
68 template< typename VT // Type of the sparse vector
69  , bool TF > // Transpose flag
70 class SVecAbsExpr : public SparseVector< SVecAbsExpr<VT,TF>, TF >
71  , private Expression
72  , private Computation
73 {
74  private:
75  //**Type definitions****************************************************************************
76  typedef typename VT::ResultType RT;
77  typedef typename VT::ReturnType RN;
78  typedef typename VT::CompositeType CT;
79  typedef typename VT::TransposeType TT;
80  typedef typename VT::ElementType ET;
81  //**********************************************************************************************
82 
83  //**Return type evaluation**********************************************************************
85 
90  enum { returnExpr = !IsTemporary<RN>::value };
91 
94  //**********************************************************************************************
95 
96  //**Evaluation strategy*************************************************************************
98 
104  enum { useAssign = RequiresEvaluation<VT>::value };
105 
107 
108  template< typename VT2 >
109  struct UseAssign {
110  enum { value = useAssign };
111  };
113  //**********************************************************************************************
114 
115  public:
116  //**Type definitions****************************************************************************
118  typedef RT ResultType;
119  typedef TT TransposeType;
120  typedef ET ElementType;
121 
124 
127 
129  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type Operand;
130  //**********************************************************************************************
131 
132  //**Compilation flags***************************************************************************
135  //**********************************************************************************************
136 
137  //**ConstIterator class definition**************************************************************
141  {
142  public:
143  //**Type definitions*************************************************************************
146 
148  typedef typename boost::remove_reference<Operand>::type::ConstIterator IteratorType;
149 
150  typedef std::forward_iterator_tag IteratorCategory;
151  typedef Element ValueType;
155 
156  // STL iterator requirements
162  //*******************************************************************************************
163 
164  //**Constructor******************************************************************************
168  : it_( it ) // Iterator over the elements of the sparse vector expression
169  {}
170  //*******************************************************************************************
171 
172  //**Prefix increment operator****************************************************************
178  ++it_;
179  return *this;
180  }
181  //*******************************************************************************************
182 
183  //**Element access operator******************************************************************
188  inline const Element operator*() const {
189  using std::abs;
190  return Element( abs( it_->value() ), it_->index() );
191  }
192  //*******************************************************************************************
193 
194  //**Element access operator******************************************************************
199  inline const ConstIterator* operator->() const {
200  return this;
201  }
202  //*******************************************************************************************
203 
204  //**Value function***************************************************************************
209  inline ReturnType value() const {
210  using std::abs;
211  return abs( it_->value() );
212  }
213  //*******************************************************************************************
214 
215  //**Index function***************************************************************************
220  inline size_t index() const {
221  return it_->index();
222  }
223  //*******************************************************************************************
224 
225  //**Equality operator************************************************************************
231  inline bool operator==( const ConstIterator& rhs ) const {
232  return it_ == rhs.it_;
233  }
234  //*******************************************************************************************
235 
236  //**Inequality operator**********************************************************************
242  inline bool operator!=( const ConstIterator& rhs ) const {
243  return it_ != rhs.it_;
244  }
245  //*******************************************************************************************
246 
247  //**Subtraction operator*********************************************************************
253  inline DifferenceType operator-( const ConstIterator& rhs ) const {
254  return it_ - rhs.it_;
255  }
256  //*******************************************************************************************
257 
258  private:
259  //**Member variables*************************************************************************
261  //*******************************************************************************************
262  };
263  //**********************************************************************************************
264 
265  //**Constructor*********************************************************************************
270  explicit inline SVecAbsExpr( const VT& sv )
271  : sv_( sv ) // Sparse vector of the absolute value expression
272  {}
273  //**********************************************************************************************
274 
275  //**Subscript operator**************************************************************************
281  inline ReturnType operator[]( size_t index ) const {
282  using std::abs;
283  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
284  return abs( sv_[index] );
285  }
286  //**********************************************************************************************
287 
288  //**Begin function******************************************************************************
293  inline ConstIterator begin() const {
294  return ConstIterator( sv_.begin() );
295  }
296  //**********************************************************************************************
297 
298  //**End function********************************************************************************
303  inline ConstIterator end() const {
304  return ConstIterator( sv_.end() );
305  }
306  //**********************************************************************************************
307 
308  //**Size function*******************************************************************************
313  inline size_t size() const {
314  return sv_.size();
315  }
316  //**********************************************************************************************
317 
318  //**NonZeros function***************************************************************************
323  inline size_t nonZeros() const {
324  return sv_.nonZeros();
325  }
326  //**********************************************************************************************
327 
328  //**Operand access******************************************************************************
333  inline Operand operand() const {
334  return sv_;
335  }
336  //**********************************************************************************************
337 
338  //**********************************************************************************************
344  template< typename T >
345  inline bool isAliased( const T* alias ) const {
347  !RequiresEvaluation<VT>::value && sv_.isAliased( alias );
348  }
349  //**********************************************************************************************
350 
351  private:
352  //**Member variables****************************************************************************
354  //**********************************************************************************************
355 
356  //**Assignment to dense vectors*****************************************************************
370  template< typename VT2 > // Type of the target dense vector
371  friend inline typename EnableIf< UseAssign<VT2> >::Type
372  assign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
373  {
374  using std::abs;
375 
376  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
377 
378  assign( ~lhs, rhs.sv_ );
379 
380  const size_t size( rhs.size() );
381  for( size_t i=0UL; i<size; ++i ) {
382  (~lhs)[i] = abs( (~lhs)[i] );
383  }
384  }
386  //**********************************************************************************************
387 
388  //**Assignment to sparse vectors****************************************************************
402  template< typename VT2 > // Type of the target sparse vector
403  friend inline typename EnableIf< UseAssign<VT2> >::Type
404  assign( SparseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
405  {
406  using std::abs;
407 
408  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
409 
410  typedef typename VT2::Iterator Iterator;
411 
412  assign( ~lhs, rhs.sv_ );
413 
414  const Iterator end( (~lhs).end() );
415  for( Iterator element=(~lhs).begin(); element!=end; ++element ) {
416  element->value() = abs( element->value() );
417  }
418  }
420  //**********************************************************************************************
421 
422  //**Addition assignment to dense vectors********************************************************
436  template< typename VT2 > // Type of the target dense vector
437  friend inline typename EnableIf< UseAssign<VT2> >::Type
438  addAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
439  {
442  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
443 
444  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
445 
446  const ResultType tmp( rhs );
447  addAssign( ~lhs, tmp );
448  }
450  //**********************************************************************************************
451 
452  //**Addition assignment to sparse vectors*******************************************************
453  // No special implementation for the addition assignment to sparse vectors.
454  //**********************************************************************************************
455 
456  //**Subtraction assignment to dense vectors*****************************************************
470  template< typename VT2 > // Type of the target dense vector
471  friend inline typename EnableIf< UseAssign<VT2> >::Type
472  subAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
473  {
476  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
477 
478  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
479 
480  const ResultType tmp( rhs );
481  subAssign( ~lhs, tmp );
482  }
484  //**********************************************************************************************
485 
486  //**Subtraction assignment to sparse vectors****************************************************
487  // No special implementation for the subtraction assignment to sparse vectors.
488  //**********************************************************************************************
489 
490  //**Multiplication assignment to dense vectors**************************************************
504  template< typename VT2 > // Type of the target dense vector
505  friend inline typename EnableIf< UseAssign<VT2> >::Type
506  multAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
507  {
510  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
511 
512  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
513 
514  const ResultType tmp( rhs );
515  multAssign( ~lhs, tmp );
516  }
518  //**********************************************************************************************
519 
520  //**Multiplication assignment to sparse vectors*************************************************
521  // No special implementation for the multiplication assignment to sparse vectors.
522  //**********************************************************************************************
523 
524  //**Compile time checks*************************************************************************
529  //**********************************************************************************************
530 };
531 //*************************************************************************************************
532 
533 
534 
535 
536 //=================================================================================================
537 //
538 // GLOBAL OPERATORS
539 //
540 //=================================================================================================
541 
542 //*************************************************************************************************
559 template< typename VT // Type of the sparse vector
560  , bool TF > // Transpose flag
561 inline const SVecAbsExpr<VT,TF> abs( const SparseVector<VT,TF>& sv )
562 {
563  return SVecAbsExpr<VT,TF>( ~sv );
564 }
565 //*************************************************************************************************
566 
567 
568 
569 
570 //=================================================================================================
571 //
572 // GLOBAL RESTRUCTURING OPERATORS
573 //
574 //=================================================================================================
575 
576 //*************************************************************************************************
587 template< typename VT // Type of the sparse vector
588  , bool TF > // Transpose flag
589 inline const SVecAbsExpr<VT,TF>& abs( const SVecAbsExpr<VT,TF>& sv )
590 {
591  return sv;
592 }
594 //*************************************************************************************************
595 
596 } // namespace blaze
597 
598 #endif