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>
45 #include <blaze/util/Assert.h>
47 #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  //**ConstIterator class definition**************************************************************
136  {
137  public:
138  //**Type definitions*************************************************************************
141 
143  typedef typename boost::remove_reference<Operand>::type::ConstIterator IteratorType;
144 
145  typedef std::forward_iterator_tag IteratorCategory;
146  typedef Element ValueType;
150 
151  // STL iterator requirements
157  //*******************************************************************************************
158 
159  //**Constructor******************************************************************************
163  : it_( it ) // Iterator over the elements of the sparse vector expression
164  {}
165  //*******************************************************************************************
166 
167  //**Prefix increment operator****************************************************************
173  ++it_;
174  return *this;
175  }
176  //*******************************************************************************************
177 
178  //**Element access operator******************************************************************
183  inline const Element operator*() const {
184  using std::abs;
185  return Element( abs( it_->value() ), it_->index() );
186  }
187  //*******************************************************************************************
188 
189  //**Element access operator******************************************************************
194  inline const ConstIterator* operator->() const {
195  return this;
196  }
197  //*******************************************************************************************
198 
199  //**Value function***************************************************************************
204  inline ReturnType value() const {
205  using std::abs;
206  return abs( it_->value() );
207  }
208  //*******************************************************************************************
209 
210  //**Index function***************************************************************************
215  inline size_t index() const {
216  return it_->index();
217  }
218  //*******************************************************************************************
219 
220  //**Equality operator************************************************************************
226  inline bool operator==( const ConstIterator& rhs ) const {
227  return it_ == rhs.it_;
228  }
229  //*******************************************************************************************
230 
231  //**Inequality operator**********************************************************************
237  inline bool operator!=( const ConstIterator& rhs ) const {
238  return it_ != rhs.it_;
239  }
240  //*******************************************************************************************
241 
242  //**Subtraction operator*********************************************************************
248  inline DifferenceType operator-( const ConstIterator& rhs ) const {
249  return it_ - rhs.it_;
250  }
251  //*******************************************************************************************
252 
253  private:
254  //**Member variables*************************************************************************
256  //*******************************************************************************************
257  };
258  //**********************************************************************************************
259 
260  //**Constructor*********************************************************************************
265  explicit inline SVecAbsExpr( const VT& sv )
266  : sv_( sv ) // Sparse vector of the absolute value expression
267  {}
268  //**********************************************************************************************
269 
270  //**Subscript operator**************************************************************************
276  inline ReturnType operator[]( size_t index ) const {
277  using std::abs;
278  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
279  return abs( sv_[index] );
280  }
281  //**********************************************************************************************
282 
283  //**Begin function******************************************************************************
288  inline ConstIterator begin() const {
289  return ConstIterator( sv_.begin() );
290  }
291  //**********************************************************************************************
292 
293  //**End function********************************************************************************
298  inline ConstIterator end() const {
299  return ConstIterator( sv_.end() );
300  }
301  //**********************************************************************************************
302 
303  //**Size function*******************************************************************************
308  inline size_t size() const {
309  return sv_.size();
310  }
311  //**********************************************************************************************
312 
313  //**NonZeros function***************************************************************************
318  inline size_t nonZeros() const {
319  return sv_.nonZeros();
320  }
321  //**********************************************************************************************
322 
323  //**Operand access******************************************************************************
328  inline Operand operand() const {
329  return sv_;
330  }
331  //**********************************************************************************************
332 
333  //**********************************************************************************************
339  template< typename T >
340  inline bool canAlias( const T* alias ) const {
341  return sv_.canAlias( alias );
342  }
343  //**********************************************************************************************
344 
345  //**********************************************************************************************
351  template< typename T >
352  inline bool isAliased( const T* alias ) const {
353  return sv_.isAliased( alias );
354  }
355  //**********************************************************************************************
356 
357  private:
358  //**Member variables****************************************************************************
360  //**********************************************************************************************
361 
362  //**Assignment to dense vectors*****************************************************************
376  template< typename VT2 > // Type of the target dense vector
377  friend inline typename EnableIf< UseAssign<VT2> >::Type
378  assign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
379  {
381 
382  using std::abs;
383 
384  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
385 
386  assign( ~lhs, rhs.sv_ );
387 
388  const size_t size( rhs.size() );
389  for( size_t i=0UL; i<size; ++i ) {
390  (~lhs)[i] = abs( (~lhs)[i] );
391  }
392  }
394  //**********************************************************************************************
395 
396  //**Assignment to sparse vectors****************************************************************
410  template< typename VT2 > // Type of the target sparse vector
411  friend inline typename EnableIf< UseAssign<VT2> >::Type
412  assign( SparseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
413  {
415 
416  using std::abs;
417 
418  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
419 
420  typedef typename VT2::Iterator Iterator;
421 
422  assign( ~lhs, rhs.sv_ );
423 
424  const Iterator end( (~lhs).end() );
425  for( Iterator element=(~lhs).begin(); element!=end; ++element ) {
426  element->value() = abs( element->value() );
427  }
428  }
430  //**********************************************************************************************
431 
432  //**Addition assignment to dense vectors********************************************************
446  template< typename VT2 > // Type of the target dense vector
447  friend inline typename EnableIf< UseAssign<VT2> >::Type
448  addAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
449  {
451 
454  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
455 
456  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
457 
458  const ResultType tmp( rhs );
459  addAssign( ~lhs, tmp );
460  }
462  //**********************************************************************************************
463 
464  //**Addition assignment to sparse vectors*******************************************************
465  // No special implementation for the addition assignment to sparse vectors.
466  //**********************************************************************************************
467 
468  //**Subtraction assignment to dense vectors*****************************************************
482  template< typename VT2 > // Type of the target dense vector
483  friend inline typename EnableIf< UseAssign<VT2> >::Type
484  subAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
485  {
487 
490  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
491 
492  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
493 
494  const ResultType tmp( rhs );
495  subAssign( ~lhs, tmp );
496  }
498  //**********************************************************************************************
499 
500  //**Subtraction assignment to sparse vectors****************************************************
501  // No special implementation for the subtraction assignment to sparse vectors.
502  //**********************************************************************************************
503 
504  //**Multiplication assignment to dense vectors**************************************************
518  template< typename VT2 > // Type of the target dense vector
519  friend inline typename EnableIf< UseAssign<VT2> >::Type
520  multAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
521  {
523 
526  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
527 
528  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
529 
530  const ResultType tmp( rhs );
531  multAssign( ~lhs, tmp );
532  }
534  //**********************************************************************************************
535 
536  //**Multiplication assignment to sparse vectors*************************************************
537  // No special implementation for the multiplication assignment to sparse vectors.
538  //**********************************************************************************************
539 
540  //**Compile time checks*************************************************************************
545  //**********************************************************************************************
546 };
547 //*************************************************************************************************
548 
549 
550 
551 
552 //=================================================================================================
553 //
554 // GLOBAL OPERATORS
555 //
556 //=================================================================================================
557 
558 //*************************************************************************************************
575 template< typename VT // Type of the sparse vector
576  , bool TF > // Transpose flag
577 inline const SVecAbsExpr<VT,TF> abs( const SparseVector<VT,TF>& sv )
578 {
580 
581  return SVecAbsExpr<VT,TF>( ~sv );
582 }
583 //*************************************************************************************************
584 
585 
586 
587 
588 //=================================================================================================
589 //
590 // GLOBAL RESTRUCTURING OPERATORS
591 //
592 //=================================================================================================
593 
594 //*************************************************************************************************
605 template< typename VT // Type of the sparse vector
606  , bool TF > // Transpose flag
607 inline const SVecAbsExpr<VT,TF>& abs( const SVecAbsExpr<VT,TF>& sv )
608 {
610 
611  return sv;
612 }
614 //*************************************************************************************************
615 
616 } // namespace blaze
617 
618 #endif