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>
63 #include <blaze/util/Assert.h>
65 #include <blaze/util/EnableIf.h>
66 #include <blaze/util/InvalidType.h>
68 #include <blaze/util/SelectType.h>
69 #include <blaze/util/Types.h>
71 
72 
73 namespace blaze {
74 
75 //=================================================================================================
76 //
77 // CLASS SVECABSEXPR
78 //
79 //=================================================================================================
80 
81 //*************************************************************************************************
88 template< typename VT // Type of the sparse vector
89  , bool TF > // Transpose flag
90 class SVecAbsExpr : public SparseVector< SVecAbsExpr<VT,TF>, TF >
91  , private VecAbsExpr
92  , private Computation
93 {
94  private:
95  //**Type definitions****************************************************************************
96  typedef typename VT::ResultType RT;
97  typedef typename VT::ReturnType RN;
98  typedef typename VT::CompositeType CT;
99  typedef typename VT::TransposeType TT;
100  typedef typename VT::ElementType ET;
101  //**********************************************************************************************
102 
103  //**Return type evaluation**********************************************************************
105 
110  enum { returnExpr = !IsTemporary<RN>::value };
111 
114  //**********************************************************************************************
115 
116  //**Evaluation strategy*************************************************************************
118 
124  enum { useAssign = RequiresEvaluation<VT>::value };
125 
127  template< typename VT2 >
129  struct UseAssign {
130  enum { value = useAssign };
131  };
133  //**********************************************************************************************
134 
135  public:
136  //**Type definitions****************************************************************************
138  typedef RT ResultType;
139  typedef TT TransposeType;
140  typedef ET ElementType;
141 
144 
147 
149  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type Operand;
150  //**********************************************************************************************
151 
152  //**ConstIterator class definition**************************************************************
156  {
157  public:
158  //**Type definitions*************************************************************************
161 
164 
165  typedef std::forward_iterator_tag IteratorCategory;
166  typedef Element ValueType;
170 
171  // STL iterator requirements
177  //*******************************************************************************************
178 
179  //**Constructor******************************************************************************
183  : it_( it ) // Iterator over the elements of the sparse vector expression
184  {}
185  //*******************************************************************************************
186 
187  //**Prefix increment operator****************************************************************
193  ++it_;
194  return *this;
195  }
196  //*******************************************************************************************
197 
198  //**Element access operator******************************************************************
203  inline const Element operator*() const {
204  using std::abs;
205  return Element( abs( it_->value() ), it_->index() );
206  }
207  //*******************************************************************************************
208 
209  //**Element access operator******************************************************************
214  inline const ConstIterator* operator->() const {
215  return this;
216  }
217  //*******************************************************************************************
218 
219  //**Value function***************************************************************************
224  inline ReturnType value() const {
225  using std::abs;
226  return abs( it_->value() );
227  }
228  //*******************************************************************************************
229 
230  //**Index function***************************************************************************
235  inline size_t index() const {
236  return it_->index();
237  }
238  //*******************************************************************************************
239 
240  //**Equality operator************************************************************************
246  inline bool operator==( const ConstIterator& rhs ) const {
247  return it_ == rhs.it_;
248  }
249  //*******************************************************************************************
250 
251  //**Inequality operator**********************************************************************
257  inline bool operator!=( const ConstIterator& rhs ) const {
258  return it_ != rhs.it_;
259  }
260  //*******************************************************************************************
261 
262  //**Subtraction operator*********************************************************************
268  inline DifferenceType operator-( const ConstIterator& rhs ) const {
269  return it_ - rhs.it_;
270  }
271  //*******************************************************************************************
272 
273  private:
274  //**Member variables*************************************************************************
276  //*******************************************************************************************
277  };
278  //**********************************************************************************************
279 
280  //**Constructor*********************************************************************************
285  explicit inline SVecAbsExpr( const VT& sv )
286  : sv_( sv ) // Sparse vector of the absolute value expression
287  {}
288  //**********************************************************************************************
289 
290  //**Subscript operator**************************************************************************
296  inline ReturnType operator[]( size_t index ) const {
297  using std::abs;
298  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
299  return abs( sv_[index] );
300  }
301  //**********************************************************************************************
302 
303  //**Begin function******************************************************************************
308  inline ConstIterator begin() const {
309  return ConstIterator( sv_.begin() );
310  }
311  //**********************************************************************************************
312 
313  //**End function********************************************************************************
318  inline ConstIterator end() const {
319  return ConstIterator( sv_.end() );
320  }
321  //**********************************************************************************************
322 
323  //**Size function*******************************************************************************
328  inline size_t size() const {
329  return sv_.size();
330  }
331  //**********************************************************************************************
332 
333  //**NonZeros function***************************************************************************
338  inline size_t nonZeros() const {
339  return sv_.nonZeros();
340  }
341  //**********************************************************************************************
342 
343  //**Operand access******************************************************************************
348  inline Operand operand() const {
349  return sv_;
350  }
351  //**********************************************************************************************
352 
353  //**********************************************************************************************
359  template< typename T >
360  inline bool canAlias( const T* alias ) const {
361  return sv_.canAlias( alias );
362  }
363  //**********************************************************************************************
364 
365  //**********************************************************************************************
371  template< typename T >
372  inline bool isAliased( const T* alias ) const {
373  return sv_.isAliased( alias );
374  }
375  //**********************************************************************************************
376 
377  private:
378  //**Member variables****************************************************************************
380  //**********************************************************************************************
381 
382  //**Assignment to dense vectors*****************************************************************
396  template< typename VT2 > // Type of the target dense vector
397  friend inline typename EnableIf< UseAssign<VT2> >::Type
398  assign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
399  {
401 
402  using std::abs;
403 
404  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
405 
406  assign( ~lhs, rhs.sv_ );
407 
408  const size_t size( rhs.size() );
409  for( size_t i=0UL; i<size; ++i ) {
410  (~lhs)[i] = abs( (~lhs)[i] );
411  }
412  }
414  //**********************************************************************************************
415 
416  //**Assignment to sparse vectors****************************************************************
430  template< typename VT2 > // Type of the target sparse vector
431  friend inline typename EnableIf< UseAssign<VT2> >::Type
432  assign( SparseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
433  {
435 
436  using std::abs;
437 
438  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
439 
440  typedef typename VT2::Iterator Iterator;
441 
442  assign( ~lhs, rhs.sv_ );
443 
444  const Iterator end( (~lhs).end() );
445  for( Iterator element=(~lhs).begin(); element!=end; ++element ) {
446  element->value() = abs( element->value() );
447  }
448  }
450  //**********************************************************************************************
451 
452  //**Addition assignment to dense vectors********************************************************
466  template< typename VT2 > // Type of the target dense vector
467  friend inline typename EnableIf< UseAssign<VT2> >::Type
468  addAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
469  {
471 
475 
476  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
477 
478  const ResultType tmp( rhs );
479  addAssign( ~lhs, tmp );
480  }
482  //**********************************************************************************************
483 
484  //**Addition assignment to sparse vectors*******************************************************
485  // No special implementation for the addition assignment to sparse vectors.
486  //**********************************************************************************************
487 
488  //**Subtraction assignment to dense vectors*****************************************************
502  template< typename VT2 > // Type of the target dense vector
503  friend inline typename EnableIf< UseAssign<VT2> >::Type
504  subAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
505  {
507 
511 
512  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
513 
514  const ResultType tmp( rhs );
515  subAssign( ~lhs, tmp );
516  }
518  //**********************************************************************************************
519 
520  //**Subtraction assignment to sparse vectors****************************************************
521  // No special implementation for the subtraction assignment to sparse vectors.
522  //**********************************************************************************************
523 
524  //**Multiplication assignment to dense vectors**************************************************
538  template< typename VT2 > // Type of the target dense vector
539  friend inline typename EnableIf< UseAssign<VT2> >::Type
540  multAssign( DenseVector<VT2,TF>& lhs, const SVecAbsExpr& rhs )
541  {
543 
547 
548  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
549 
550  const ResultType tmp( rhs );
551  multAssign( ~lhs, tmp );
552  }
554  //**********************************************************************************************
555 
556  //**Multiplication assignment to sparse vectors*************************************************
557  // No special implementation for the multiplication assignment to sparse vectors.
558  //**********************************************************************************************
559 
560  //**Compile time checks*************************************************************************
565  //**********************************************************************************************
566 };
567 //*************************************************************************************************
568 
569 
570 
571 
572 //=================================================================================================
573 //
574 // GLOBAL FUNCTIONS
575 //
576 //=================================================================================================
577 
578 //*************************************************************************************************
595 template< typename VT // Type of the sparse vector
596  , bool TF > // Transpose flag
597 inline const SVecAbsExpr<VT,TF> abs( const SparseVector<VT,TF>& sv )
598 {
600 
601  return SVecAbsExpr<VT,TF>( ~sv );
602 }
603 //*************************************************************************************************
604 
605 
606 
607 
608 //=================================================================================================
609 //
610 // GLOBAL RESTRUCTURING FUNCTIONS
611 //
612 //=================================================================================================
613 
614 //*************************************************************************************************
625 template< typename VT // Type of the sparse vector
626  , bool TF > // Transpose flag
627 inline const SVecAbsExpr<VT,TF>& abs( const SVecAbsExpr<VT,TF>& sv )
628 {
630 
631  return sv;
632 }
634 //*************************************************************************************************
635 
636 
637 
638 
639 //=================================================================================================
640 //
641 // EXPRESSION TRAIT SPECIALIZATIONS
642 //
643 //=================================================================================================
644 
645 //*************************************************************************************************
647 template< typename VT >
648 struct SVecAbsExprTrait< SVecAbsExpr<VT,false> >
649 {
650  public:
651  //**********************************************************************************************
652  typedef typename SelectType< IsSparseVector<VT>::value && IsColumnVector<VT>::value
653  , SVecAbsExpr<VT,false>
654  , INVALID_TYPE >::Type Type;
655  //**********************************************************************************************
656 };
658 //*************************************************************************************************
659 
660 
661 //*************************************************************************************************
663 template< typename VT >
664 struct TSVecAbsExprTrait< SVecAbsExpr<VT,true> >
665 {
666  public:
667  //**********************************************************************************************
668  typedef typename SelectType< IsSparseVector<VT>::value && IsRowVector<VT>::value
669  , SVecAbsExpr<VT,true>
670  , INVALID_TYPE >::Type Type;
671  //**********************************************************************************************
672 };
674 //*************************************************************************************************
675 
676 
677 //*************************************************************************************************
679 template< typename VT, bool TF >
680 struct SubvectorExprTrait< SVecAbsExpr<VT,TF> >
681 {
682  public:
683  //**********************************************************************************************
684  typedef typename AbsExprTrait< typename SubvectorExprTrait<const VT>::Type >::Type Type;
685  //**********************************************************************************************
686 };
688 //*************************************************************************************************
689 
690 } // namespace blaze
691 
692 #endif
Pointer difference type of the Blaze library.
ptrdiff_t DifferenceType
Difference between two iterators.
Definition: SVecAbsExpr.h:169
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:97
Header file for the SparseVector base class.
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2375
SelectType< useAssign, const ResultType, const SVecAbsExpr & >::Type CompositeType
Data type for composite expression templates.
Definition: SVecAbsExpr.h:146
Header file for the IsRowVector type trait.
IteratorCategory iterator_category
The iterator category.
Definition: SVecAbsExpr.h:172
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:739
ReturnType value() const
Access to the current value of the sparse element.
Definition: SVecAbsExpr.h:224
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:167
DifferenceType operator-(const ConstIterator &rhs) const
Calculating the number of elements between two expression iterators.
Definition: SVecAbsExpr.h:268
VT::ElementType ET
Element type of the sparse vector expression.
Definition: SVecAbsExpr.h:100
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:143
ConstIterator begin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecAbsExpr.h:308
Constraint on the data type.
RT ResultType
Result type for expression template evaluations.
Definition: SVecAbsExpr.h:138
ValueType value_type
Type of the underlying pointers.
Definition: SVecAbsExpr.h:173
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:250
ValueType & ReferenceType
Reference return type.
Definition: SVecAbsExpr.h:168
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:257
Header file for the IsTemporary type trait class.
IteratorType it_
Iterator over the elements of the sparse vector expression.
Definition: SVecAbsExpr.h:275
ConstIterator(IteratorType it)
Constructor for the ConstIterator class.
Definition: SVecAbsExpr.h:182
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecAbsExpr.h:338
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2379
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:235
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:140
#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:166
#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:2372
DifferenceType difference_type
Difference between two iterators.
Definition: SVecAbsExpr.h:176
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2373
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:160
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:175
AbsExprTrait< RN >::Type ExprReturnType
Expression return type for the subscript operator.
Definition: SVecAbsExpr.h:113
Header file for the EnableIf class template.
VT::TransposeType TT
Transpose type of the sparse vector expression.
Definition: SVecAbsExpr.h:99
RemoveReference< Operand >::Type::ConstIterator IteratorType
Iterator type of the sparse vector expression.
Definition: SVecAbsExpr.h:163
ConstIterator end() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecAbsExpr.h:318
Operand sv_
Sparse vector of the absolute value expression.
Definition: SVecAbsExpr.h:379
Header file for the IsSparseVector type trait.
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2374
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:155
SVecAbsExpr< VT, TF > This
Type of this SVecAbsExpr instance.
Definition: SVecAbsExpr.h:137
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:296
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:372
VT::ResultType RT
Result type of the sparse vector expression.
Definition: SVecAbsExpr.h:96
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:2378
std::forward_iterator_tag IteratorCategory
The iterator category.
Definition: SVecAbsExpr.h:165
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:360
VT::CompositeType CT
Composite type of the sparse vector expression.
Definition: SVecAbsExpr.h:98
PointerType pointer
Pointer return type.
Definition: SVecAbsExpr.h:174
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.
TT TransposeType
Transpose type for expression template evaluations.
Definition: SVecAbsExpr.h:139
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:2370
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:203
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:149
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:214
ConstIterator & operator++()
Pre-increment operator.
Definition: SVecAbsExpr.h:192
bool operator==(const ConstIterator &rhs) const
Equality comparison between two ConstIterator objects.
Definition: SVecAbsExpr.h:246
#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:328
Operand operand() const
Returns the sparse vector operand.
Definition: SVecAbsExpr.h:348
SVecAbsExpr(const VT &sv)
Constructor for the SVecAbsExpr class.
Definition: SVecAbsExpr.h:285