All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVecAbsExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECABSEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECABSEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <iterator>
64 #include <blaze/util/Assert.h>
66 #include <blaze/util/EnableIf.h>
67 #include <blaze/util/InvalidType.h>
69 #include <blaze/util/SelectType.h>
70 #include <blaze/util/Types.h>
72 
73 
74 namespace blaze {
75 
76 //=================================================================================================
77 //
78 // CLASS SVECABSEXPR
79 //
80 //=================================================================================================
81 
82 //*************************************************************************************************
89 template< typename VT // Type of the sparse vector
90  , bool TF > // Transpose flag
91 class SVecAbsExpr : public SparseVector< SVecAbsExpr<VT,TF>, TF >
92  , private VecAbsExpr
93  , private Computation
94 {
95  private:
96  //**Type definitions****************************************************************************
97  typedef typename VT::ResultType RT;
98  typedef typename VT::ReturnType RN;
99  typedef typename VT::CompositeType CT;
100  typedef typename VT::TransposeType TT;
101  typedef typename VT::ElementType ET;
102  //**********************************************************************************************
103 
104  //**Return type evaluation**********************************************************************
106 
111  enum { returnExpr = !IsTemporary<RN>::value };
112 
115  //**********************************************************************************************
116 
117  //**Evaluation strategy*************************************************************************
119 
125  enum { useAssign = RequiresEvaluation<VT>::value };
126 
128  template< typename VT2 >
130  struct UseAssign {
131  enum { value = useAssign };
132  };
134  //**********************************************************************************************
135 
136  public:
137  //**Type definitions****************************************************************************
139  typedef RT ResultType;
140  typedef TT TransposeType;
141  typedef ET ElementType;
142 
145 
148 
150  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type Operand;
151  //**********************************************************************************************
152 
153  //**Compilation flags***************************************************************************
155  enum { smpAssignable = VT::smpAssignable };
156  //**********************************************************************************************
157 
158  //**ConstIterator class definition**************************************************************
162  {
163  public:
164  //**Type definitions*************************************************************************
167 
170 
171  typedef std::forward_iterator_tag IteratorCategory;
172  typedef Element ValueType;
176 
177  // STL iterator requirements
183  //*******************************************************************************************
184 
185  //**Constructor******************************************************************************
189  : it_( it ) // Iterator over the elements of the sparse vector expression
190  {}
191  //*******************************************************************************************
192 
193  //**Prefix increment operator****************************************************************
199  ++it_;
200  return *this;
201  }
202  //*******************************************************************************************
203 
204  //**Element access operator******************************************************************
209  inline const Element operator*() const {
210  using std::abs;
211  return Element( abs( it_->value() ), it_->index() );
212  }
213  //*******************************************************************************************
214 
215  //**Element access operator******************************************************************
220  inline const ConstIterator* operator->() const {
221  return this;
222  }
223  //*******************************************************************************************
224 
225  //**Value function***************************************************************************
230  inline ReturnType value() const {
231  using std::abs;
232  return abs( it_->value() );
233  }
234  //*******************************************************************************************
235 
236  //**Index function***************************************************************************
241  inline size_t index() const {
242  return it_->index();
243  }
244  //*******************************************************************************************
245 
246  //**Equality operator************************************************************************
252  inline bool operator==( const ConstIterator& rhs ) const {
253  return it_ == rhs.it_;
254  }
255  //*******************************************************************************************
256 
257  //**Inequality operator**********************************************************************
263  inline bool operator!=( const ConstIterator& rhs ) const {
264  return it_ != rhs.it_;
265  }
266  //*******************************************************************************************
267 
268  //**Subtraction operator*********************************************************************
274  inline DifferenceType operator-( const ConstIterator& rhs ) const {
275  return it_ - rhs.it_;
276  }
277  //*******************************************************************************************
278 
279  private:
280  //**Member variables*************************************************************************
282  //*******************************************************************************************
283  };
284  //**********************************************************************************************
285 
286  //**Constructor*********************************************************************************
291  explicit inline SVecAbsExpr( const VT& sv )
292  : sv_( sv ) // Sparse vector of the absolute value expression
293  {}
294  //**********************************************************************************************
295 
296  //**Subscript operator**************************************************************************
302  inline ReturnType operator[]( size_t index ) const {
303  using std::abs;
304  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
305  return abs( sv_[index] );
306  }
307  //**********************************************************************************************
308 
309  //**Begin function******************************************************************************
314  inline ConstIterator begin() const {
315  return ConstIterator( sv_.begin() );
316  }
317  //**********************************************************************************************
318 
319  //**End function********************************************************************************
324  inline ConstIterator end() const {
325  return ConstIterator( sv_.end() );
326  }
327  //**********************************************************************************************
328 
329  //**Size function*******************************************************************************
334  inline size_t size() const {
335  return sv_.size();
336  }
337  //**********************************************************************************************
338 
339  //**NonZeros function***************************************************************************
344  inline size_t nonZeros() const {
345  return sv_.nonZeros();
346  }
347  //**********************************************************************************************
348 
349  //**Operand access******************************************************************************
354  inline Operand operand() const {
355  return sv_;
356  }
357  //**********************************************************************************************
358 
359  //**********************************************************************************************
365  template< typename T >
366  inline bool canAlias( const T* alias ) const {
367  return sv_.canAlias( alias );
368  }
369  //**********************************************************************************************
370 
371  //**********************************************************************************************
377  template< typename T >
378  inline bool isAliased( const T* alias ) const {
379  return sv_.isAliased( alias );
380  }
381  //**********************************************************************************************
382 
383  //**********************************************************************************************
388  inline bool canSMPAssign() const {
389  return sv_.canSMPAssign();
390  }
391  //**********************************************************************************************
392 
393  private:
394  //**Member variables****************************************************************************
396  //**********************************************************************************************
397 
398  //**Assignment to dense vectors*****************************************************************
412  template< typename VT2 > // Type of the target dense vector
413  friend inline typename EnableIf< UseAssign<VT2> >::Type
414  assign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
415  {
417 
418  using std::abs;
419 
420  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
421 
422  assign ( ~lhs, rhs.sv_ );
423  smpAssign( ~lhs, abs( ~lhs ) );
424  }
426  //**********************************************************************************************
427 
428  //**Assignment to sparse vectors****************************************************************
442  template< typename VT2 > // Type of the target sparse vector
443  friend inline typename EnableIf< UseAssign<VT2> >::Type
444  assign( SparseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
445  {
447 
448  using std::abs;
449 
450  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
451 
452  typedef typename VT2::Iterator Iterator;
453 
454  assign( ~lhs, rhs.sv_ );
455 
456  const Iterator end( (~lhs).end() );
457  for( Iterator element=(~lhs).begin(); element!=end; ++element ) {
458  element->value() = abs( element->value() );
459  }
460  }
462  //**********************************************************************************************
463 
464  //**Addition assignment to dense vectors********************************************************
478  template< typename VT2 > // Type of the target dense vector
479  friend inline typename EnableIf< UseAssign<VT2> >::Type
480  addAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
481  {
483 
487 
488  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
489 
490  const ResultType tmp( rhs );
491  smpAddAssign( ~lhs, tmp );
492  }
494  //**********************************************************************************************
495 
496  //**Addition assignment to sparse vectors*******************************************************
497  // No special implementation for the addition assignment to sparse vectors.
498  //**********************************************************************************************
499 
500  //**Subtraction assignment to dense vectors*****************************************************
514  template< typename VT2 > // Type of the target dense vector
515  friend inline typename EnableIf< UseAssign<VT2> >::Type
516  subAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
517  {
519 
523 
524  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
525 
526  const ResultType tmp( rhs );
527  smpSubAssign( ~lhs, tmp );
528  }
530  //**********************************************************************************************
531 
532  //**Subtraction assignment to sparse vectors****************************************************
533  // No special implementation for the subtraction assignment to sparse vectors.
534  //**********************************************************************************************
535 
536  //**Multiplication assignment to dense vectors**************************************************
550  template< typename VT2 > // Type of the target dense vector
551  friend inline typename EnableIf< UseAssign<VT2> >::Type
552  multAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
553  {
555 
559 
560  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
561 
562  const ResultType tmp( rhs );
563  smpMultAssign( ~lhs, tmp );
564  }
566  //**********************************************************************************************
567 
568  //**Multiplication assignment to sparse vectors*************************************************
569  // No special implementation for the multiplication assignment to sparse vectors.
570  //**********************************************************************************************
571 
572  //**Compile time checks*************************************************************************
577  //**********************************************************************************************
578 };
579 //*************************************************************************************************
580 
581 
582 
583 
584 //=================================================================================================
585 //
586 // GLOBAL FUNCTIONS
587 //
588 //=================================================================================================
589 
590 //*************************************************************************************************
607 template< typename VT // Type of the sparse vector
608  , bool TF > // Transpose flag
609 inline const SVecAbsExpr<VT,TF> abs( const SparseVector<VT,TF>& sv )
610 {
612 
613  return SVecAbsExpr<VT,TF>( ~sv );
614 }
615 //*************************************************************************************************
616 
617 
618 
619 
620 //=================================================================================================
621 //
622 // GLOBAL RESTRUCTURING FUNCTIONS
623 //
624 //=================================================================================================
625 
626 //*************************************************************************************************
637 template< typename VT // Type of the sparse vector
638  , bool TF > // Transpose flag
639 inline const SVecAbsExpr<VT,TF>& abs( const SVecAbsExpr<VT,TF>& sv )
640 {
642 
643  return sv;
644 }
646 //*************************************************************************************************
647 
648 
649 
650 
651 //=================================================================================================
652 //
653 // EXPRESSION TRAIT SPECIALIZATIONS
654 //
655 //=================================================================================================
656 
657 //*************************************************************************************************
659 template< typename VT >
660 struct SVecAbsExprTrait< SVecAbsExpr<VT,false> >
661 {
662  public:
663  //**********************************************************************************************
664  typedef typename SelectType< IsSparseVector<VT>::value && IsColumnVector<VT>::value
665  , SVecAbsExpr<VT,false>
666  , INVALID_TYPE >::Type Type;
667  //**********************************************************************************************
668 };
670 //*************************************************************************************************
671 
672 
673 //*************************************************************************************************
675 template< typename VT >
676 struct TSVecAbsExprTrait< SVecAbsExpr<VT,true> >
677 {
678  public:
679  //**********************************************************************************************
680  typedef typename SelectType< IsSparseVector<VT>::value && IsRowVector<VT>::value
681  , SVecAbsExpr<VT,true>
682  , INVALID_TYPE >::Type Type;
683  //**********************************************************************************************
684 };
686 //*************************************************************************************************
687 
688 
689 //*************************************************************************************************
691 template< typename VT, bool TF, bool AF >
692 struct SubvectorExprTrait< SVecAbsExpr<VT,TF>, AF >
693 {
694  public:
695  //**********************************************************************************************
696  typedef typename AbsExprTrait< typename SubvectorExprTrait<const VT,AF>::Type >::Type Type;
697  //**********************************************************************************************
698 };
700 //*************************************************************************************************
701 
702 } // namespace blaze
703 
704 #endif
Pointer difference type of the Blaze library.
ptrdiff_t DifferenceType
Difference between two iterators.
Definition: SVecAbsExpr.h:175
Header file for the TSVecAbsExprTrait class template.
Compile time check whether the given type is a temporary vector or matrix type.This type trait class ...
Definition: IsTemporary.h:87
VT::ReturnType RN
Return type of the sparse vector expression.
Definition: SVecAbsExpr.h:98
Header file for the SparseVector base class.
void smpSubAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP subtraction assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:151
void smpMultAssign(DenseVector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs)
Default implementation of the SMP multiplication assignment of a vector to a dense vector...
Definition: DenseVector.h:178
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2384
SelectType< useAssign, const ResultType, const SVecAbsExpr & >::Type CompositeType
Data type for composite expression templates.
Definition: SVecAbsExpr.h:147
Header file for the IsRowVector type trait.
IteratorCategory iterator_category
The iterator category.
Definition: SVecAbsExpr.h:178
const DMatAbsExpr< MT, SO > abs(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the absolute values of each single element of dm.
Definition: DMatAbsExpr.h:764
ReturnType value() const
Access to the current value of the sparse element.
Definition: SVecAbsExpr.h:230
Header file for the Computation base class.
Header file for the SVecAbsExprTrait class template.
Header file for the RequiresEvaluation type trait.
ValueType * PointerType
Pointer return type.
Definition: SVecAbsExpr.h:173
DifferenceType operator-(const ConstIterator &rhs) const
Calculating the number of elements between two expression iterators.
Definition: SVecAbsExpr.h:274
VT::ElementType ET
Element type of the sparse vector expression.
Definition: SVecAbsExpr.h:101
Expression object for the sparse vector abs() function.The SVecAbsExpr class represents the compile t...
Definition: Forward.h:106
const SelectType< returnExpr, ExprReturnType, ElementType >::Type ReturnType
Return type for expression template evaluations.
Definition: SVecAbsExpr.h:144
ConstIterator begin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecAbsExpr.h:314
Constraint on the data type.
RT ResultType
Result type for expression template evaluations.
Definition: SVecAbsExpr.h:139
ValueType value_type
Type of the underlying pointers.
Definition: SVecAbsExpr.h:179
void smpAddAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP addition assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:121
Compile time check to query the requirement to evaluate an expression.Via this type trait it is possi...
Definition: RequiresEvaluation.h:90
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:251
ValueType & ReferenceType
Reference return type.
Definition: SVecAbsExpr.h:174
Header file for the ValueIndexPair class.
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
bool operator!=(const ConstIterator &rhs) const
Inequality comparison between two ConstIterator objects.
Definition: SVecAbsExpr.h:263
Header file for the IsTemporary type trait class.
IteratorType it_
Iterator over the elements of the sparse vector expression.
Definition: SVecAbsExpr.h:281
Header file for the dense vector SMP implementation.
ConstIterator(IteratorType it)
Constructor for the ConstIterator class.
Definition: SVecAbsExpr.h:188
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecAbsExpr.h:344
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2388
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:179
size_t index() const
Access to the current index of the sparse element.
Definition: SVecAbsExpr.h:241
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
ET ElementType
Resulting element type.
Definition: SVecAbsExpr.h:141
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type...
Definition: SparseVector.h:79
Header file for the VecAbsExpr base class.
Constraint on the data type.
Element ValueType
Type of the underlying pointers.
Definition: SVecAbsExpr.h:172
#define BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:78
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2381
DifferenceType difference_type
Difference between two iterators.
Definition: SVecAbsExpr.h:182
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2382
void multAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the multiplication assignment of a matrix to a matrix.
Definition: Matrix.h:269
ValueIndexPair< ElementType > Element
Element type of the sparse vector expression.
Definition: SVecAbsExpr.h:166
Header file for the SelectType class template.
Header file for all forward declarations for expression class templates.
Constraint on the data type.
ReferenceType reference
Reference return type.
Definition: SVecAbsExpr.h:181
AbsExprTrait< RN >::Type ExprReturnType
Expression return type for the subscript operator.
Definition: SVecAbsExpr.h:114
Header file for the EnableIf class template.
VT::TransposeType TT
Transpose type of the sparse vector expression.
Definition: SVecAbsExpr.h:100
RemoveReference< Operand >::Type::ConstIterator IteratorType
Iterator type of the sparse vector expression.
Definition: SVecAbsExpr.h:169
void smpAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:91
ConstIterator end() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecAbsExpr.h:324
Operand sv_
Sparse vector of the absolute value expression.
Definition: SVecAbsExpr.h:395
Header file for the IsSparseVector type trait.
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2383
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
Iterator over the elements of the sparse vector absolute value expression.
Definition: SVecAbsExpr.h:161
SVecAbsExpr< VT, TF > This
Type of this SVecAbsExpr instance.
Definition: SVecAbsExpr.h:138
Header file for run time assertion macros.
Utility type for generic codes.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecAbsExpr.h:302
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:209
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: SVecAbsExpr.h:378
VT::ResultType RT
Result type of the sparse vector expression.
Definition: SVecAbsExpr.h:97
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:239
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2387
std::forward_iterator_tag IteratorCategory
The iterator category.
Definition: SVecAbsExpr.h:171
Header file for the RemoveReference type trait.
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: SVecAbsExpr.h:366
VT::CompositeType CT
Composite type of the sparse vector expression.
Definition: SVecAbsExpr.h:99
PointerType pointer
Pointer return type.
Definition: SVecAbsExpr.h:180
Index-value-pair for sparse vectors and matrices.The ValueIndexPair class represents a single index-v...
Definition: ValueIndexPair.h:69
Header file for the IsComputation type trait class.
bool canSMPAssign() const
Returns whether the expression can be used in SMP assignments.
Definition: SVecAbsExpr.h:388
TT TransposeType
Transpose type for expression template evaluations.
Definition: SVecAbsExpr.h:140
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:157
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2379
Header file for basic type definitions.
Header file for the SubvectorExprTrait class template.
const Element operator*() const
Direct access to the sparse vector element at the current iterator position.
Definition: SVecAbsExpr.h:209
Header file for the AbsExprTrait class template.
SelectType< IsExpression< VT >::value, const VT, const VT & >::Type Operand
Composite data type of the sparse vector expression.
Definition: SVecAbsExpr.h:150
Header file for the IsColumnVector type trait.
Evaluation of the return type of an absolute value expression.Via this type trait it is possible to e...
Definition: AbsExprTrait.h:86
const ConstIterator * operator->() const
Direct access to the sparse vector element at the current iterator position.
Definition: SVecAbsExpr.h:220
ConstIterator & operator++()
Pre-increment operator.
Definition: SVecAbsExpr.h:198
bool operator==(const ConstIterator &rhs) const
Equality comparison between two ConstIterator objects.
Definition: SVecAbsExpr.h:252
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.In case the given data type T is not a dense or sparse vector type and in...
Definition: TransposeFlag.h:238
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the IsExpression type trait class.
Header file for the FunctionTrace class.
size_t size() const
Returns the current size/dimension of the vector.
Definition: SVecAbsExpr.h:334
Operand operand() const
Returns the sparse vector operand.
Definition: SVecAbsExpr.h:354
SVecAbsExpr(const VT &sv)
Constructor for the SVecAbsExpr class.
Definition: SVecAbsExpr.h:291