SVecEvalExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECEVALEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECEVALEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
46 #include <blaze/math/Exception.h>
61 #include <blaze/util/Assert.h>
63 #include <blaze/util/InvalidType.h>
64 #include <blaze/util/mpl/And.h>
65 #include <blaze/util/mpl/If.h>
66 #include <blaze/util/Types.h>
67 
68 
69 namespace blaze {
70 
71 //=================================================================================================
72 //
73 // CLASS SVECEVALEXPR
74 //
75 //=================================================================================================
76 
77 //*************************************************************************************************
84 template< typename VT // Type of the sparse vector
85  , bool TF > // Transpose flag
86 class SVecEvalExpr : public SparseVector< SVecEvalExpr<VT,TF>, TF >
87  , private VecEvalExpr
88  , private Computation
89 {
90  public:
91  //**Type definitions****************************************************************************
97 
99  typedef const ResultType CompositeType;
100 
102  typedef If_< IsExpression<VT>, const VT, const VT& > Operand;
103  //**********************************************************************************************
104 
105  //**Compilation flags***************************************************************************
107  enum : bool { smpAssignable = VT::smpAssignable };
108  //**********************************************************************************************
109 
110  //**Constructor*********************************************************************************
115  explicit inline SVecEvalExpr( const VT& sv ) noexcept
116  : sv_( sv ) // Sparse vector of the evaluation expression
117  {}
118  //**********************************************************************************************
119 
120  //**Subscript operator**************************************************************************
126  inline ReturnType operator[]( size_t index ) const {
127  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
128  return sv_[index];
129  }
130  //**********************************************************************************************
131 
132  //**At function*********************************************************************************
139  inline ReturnType at( size_t index ) const {
140  if( index >= sv_.size() ) {
141  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
142  }
143  return (*this)[index];
144  }
145  //**********************************************************************************************
146 
147  //**Size function*******************************************************************************
152  inline size_t size() const noexcept {
153  return sv_.size();
154  }
155  //**********************************************************************************************
156 
157  //**NonZeros function***************************************************************************
162  inline size_t nonZeros() const {
163  return sv_.nonZeros();
164  }
165  //**********************************************************************************************
166 
167  //**Operand access******************************************************************************
172  inline Operand operand() const noexcept {
173  return sv_;
174  }
175  //**********************************************************************************************
176 
177  //**********************************************************************************************
183  template< typename T >
184  inline bool canAlias( const T* alias ) const noexcept {
185  return sv_.canAlias( alias );
186  }
187  //**********************************************************************************************
188 
189  //**********************************************************************************************
195  template< typename T >
196  inline bool isAliased( const T* alias ) const noexcept {
197  return sv_.isAliased( alias );
198  }
199  //**********************************************************************************************
200 
201  //**********************************************************************************************
206  inline bool canSMPAssign() const noexcept {
207  return sv_.canSMPAssign();
208  }
209  //**********************************************************************************************
210 
211  private:
212  //**Member variables****************************************************************************
213  Operand sv_;
214  //**********************************************************************************************
215 
216  //**Assignment to dense vectors*****************************************************************
228  template< typename VT2 > // Type of the target dense vector
229  friend inline void assign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
230  {
232 
233  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
234 
235  assign( ~lhs, rhs.sv_ );
236  }
238  //**********************************************************************************************
239 
240  //**Assignment to sparse vectors****************************************************************
252  template< typename VT2 > // Type of the target sparse vector
253  friend inline void assign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
254  {
256 
257  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
258 
259  assign( ~lhs, rhs.sv_ );
260  }
262  //**********************************************************************************************
263 
264  //**Addition assignment to dense vectors********************************************************
276  template< typename VT2 > // Type of the target dense vector
277  friend inline void addAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
278  {
280 
281  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
282 
283  addAssign( ~lhs, rhs.sv_ );
284  }
286  //**********************************************************************************************
287 
288  //**Addition assignment to sparse vectors*******************************************************
300  template< typename VT2 > // Type of the target sparse vector
301  friend inline void addAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
302  {
304 
305  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
306 
307  addAssign( ~lhs, rhs.sv_ );
308  }
310  //**********************************************************************************************
311 
312  //**Subtraction assignment to dense vectors*****************************************************
324  template< typename VT2 > // Type of the target dense vector
325  friend inline void subAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
326  {
328 
329  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
330 
331  subAssign( ~lhs, rhs.sv_ );
332  }
334  //**********************************************************************************************
335 
336  //**Subtraction assignment to sparse vectors****************************************************
348  template< typename VT2 > // Type of the target sparse vector
349  friend inline void subAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
350  {
352 
353  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
354 
355  subAssign( ~lhs, rhs.sv_ );
356  }
358  //**********************************************************************************************
359 
360  //**Multiplication assignment to dense vectors**************************************************
372  template< typename VT2 > // Type of the target dense vector
373  friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
374  {
376 
377  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
378 
379  multAssign( ~lhs, rhs.sv_ );
380  }
382  //**********************************************************************************************
383 
384  //**Multiplication assignment to sparse vectors*************************************************
396  template< typename VT2 > // Type of the target sparse vector
397  friend inline void multAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
398  {
400 
401  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
402 
403  multAssign( ~lhs, rhs.sv_ );
404  }
406  //**********************************************************************************************
407 
408  //**SMP assignment to dense vectors*************************************************************
420  template< typename VT2 > // Type of the target dense vector
421  friend inline void smpAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
422  {
424 
425  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
426 
427  smpAssign( ~lhs, rhs.sv_ );
428  }
430  //**********************************************************************************************
431 
432  //**SMP assignment to sparse vectors************************************************************
444  template< typename VT2 > // Type of the target sparse vector
445  friend inline void smpAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
446  {
448 
449  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
450 
451  smpAssign( ~lhs, rhs.sv_ );
452  }
454  //**********************************************************************************************
455 
456  //**SMP addition assignment to dense vectors****************************************************
468  template< typename VT2 > // Type of the target dense vector
469  friend inline void smpAddAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
470  {
472 
473  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
474 
475  smpAddAssign( ~lhs, rhs.sv_ );
476  }
478  //**********************************************************************************************
479 
480  //**SMP addition assignment to sparse vectors***************************************************
492  template< typename VT2 > // Type of the target sparse vector
493  friend inline void smpAddAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
494  {
496 
497  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
498 
499  smpAddAssign( ~lhs, rhs.sv_ );
500  }
502  //**********************************************************************************************
503 
504  //**SMP subtraction assignment to dense vectors*************************************************
516  template< typename VT2 > // Type of the target dense vector
517  friend inline void smpSubAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
518  {
520 
521  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
522 
523  smpSubAssign( ~lhs, rhs.sv_ );
524  }
526  //**********************************************************************************************
527 
528  //**SMP subtraction assignment to sparse vectors************************************************
540  template< typename VT2 > // Type of the target sparse vector
541  friend inline void smpSubAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
542  {
544 
545  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
546 
547  smpSubAssign( ~lhs, rhs.sv_ );
548  }
550  //**********************************************************************************************
551 
552  //**SMP multiplication assignment to dense vectors**********************************************
564  template< typename VT2 > // Type of the target dense vector
565  friend inline void smpMultAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
566  {
568 
569  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
570 
571  smpMultAssign( ~lhs, rhs.sv_ );
572  }
574  //**********************************************************************************************
575 
576  //**SMP multiplication assignment to sparse vectors*********************************************
588  template< typename VT2 > // Type of the target sparse vector
589  friend inline void smpMultAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
590  {
592 
593  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
594 
595  smpMultAssign( ~lhs, rhs.sv_ );
596  }
598  //**********************************************************************************************
599 
600  //**Compile time checks*************************************************************************
605  //**********************************************************************************************
606 };
607 //*************************************************************************************************
608 
609 
610 
611 
612 //=================================================================================================
613 //
614 // GLOBAL FUNCTIONS
615 //
616 //=================================================================================================
617 
618 //*************************************************************************************************
635 template< typename VT // Type of the dense vector
636  , bool TF > // Transpose flag
638 {
640 
641  return SVecEvalExpr<VT,TF>( ~sv );
642 }
643 //*************************************************************************************************
644 
645 
646 
647 
648 //=================================================================================================
649 //
650 // GLOBAL RESTRUCTURING FUNCTIONS
651 //
652 //=================================================================================================
653 
654 //*************************************************************************************************
665 template< typename VT // Type of the sparse vector
666  , bool TF > // Transpose flag
667 inline const SVecEvalExpr<VT,TF> eval( const SVecEvalExpr<VT,TF>& sv )
668 {
669  return sv;
670 }
672 //*************************************************************************************************
673 
674 
675 
676 
677 //=================================================================================================
678 //
679 // SIZE SPECIALIZATIONS
680 //
681 //=================================================================================================
682 
683 //*************************************************************************************************
685 template< typename VT, bool TF >
686 struct Size< SVecEvalExpr<VT,TF> > : public Size<VT>
687 {};
689 //*************************************************************************************************
690 
691 
692 
693 
694 //=================================================================================================
695 //
696 // EXPRESSION TRAIT SPECIALIZATIONS
697 //
698 //=================================================================================================
699 
700 //*************************************************************************************************
702 template< typename VT >
703 struct SVecEvalExprTrait< SVecEvalExpr<VT,false> >
704 {
705  public:
706  //**********************************************************************************************
709  , INVALID_TYPE >;
710  //**********************************************************************************************
711 };
713 //*************************************************************************************************
714 
715 
716 //*************************************************************************************************
718 template< typename VT >
719 struct TSVecEvalExprTrait< SVecEvalExpr<VT,true> >
720 {
721  public:
722  //**********************************************************************************************
725  , INVALID_TYPE >;
726  //**********************************************************************************************
727 };
729 //*************************************************************************************************
730 
731 
732 //*************************************************************************************************
734 template< typename VT, bool TF, bool AF >
735 struct SubvectorExprTrait< SVecEvalExpr<VT,TF>, AF >
736 {
737  public:
738  //**********************************************************************************************
740  //**********************************************************************************************
741 };
743 //*************************************************************************************************
744 
745 } // namespace blaze
746 
747 #endif
Compile time check for row vector types.This type trait tests whether or not the given template argum...
Definition: IsRowVector.h:80
Header file for auxiliary alias declarations.
Evaluation of the expression type type of a subvector operation.Via this type trait it is possible to...
Definition: SubvectorExprTrait.h:79
Header file for basic type definitions.
Header file for the SparseVector base class.
EnableIf_< IsDenseMatrix< MT1 > > smpSubAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP subtraction assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:160
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecEvalExpr.h:96
Header file for the IsRowVector type trait.
EnableIf_< IsDenseVector< VT1 > > smpMultAssign(Vector< 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:193
Expression object for the forced evaluation of sparse vectors.The SVecEvalExpr class represents the c...
Definition: Forward.h:126
Header file for the And class template.
Operand sv_
Sparse vector of the evaluation expression.
Definition: SVecEvalExpr.h:213
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecEvalExpr.h:152
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecEvalExpr.h:99
Header file for the Computation base class.
typename EvalExprTrait< T >::Type EvalExprTrait_
Auxiliary alias declaration for the EvalExprTrait class template.The EvalExprTrait_ alias declaration...
Definition: EvalExprTrait.h:142
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecEvalExpr.h:139
Header file for the VecEvalExpr base class.
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:323
EnableIf_< IsDenseMatrix< MT1 > > smpAddAssign(Matrix< 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:129
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecEvalExpr.h:94
Evaluation of the expression type of a sparse vector evaluation operation.Via this type trait it is p...
Definition: TSVecEvalExprTrait.h:74
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:343
SVecEvalExpr< VT, TF > This
Type of this SVecEvalExpr instance.
Definition: SVecEvalExpr.h:92
SVecEvalExpr(const VT &sv) noexcept
Constructor for the SVecEvalExpr class.
Definition: SVecEvalExpr.h:115
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the If class template.
EnableIf_< IsDenseMatrix< MT1 > > smpAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:98
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.This macro encapsulates the default way of Bl...
Definition: Exception.h:331
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
#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:61
Constraint on the data type.
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecEvalExpr.h:184
Compile time check for column vector types.This type trait tests whether or not the given template ar...
Definition: IsColumnVector.h:80
Header file for the exception macros of the math module.
Header file for all forward declarations for expression class templates.
Constraint on the data type.
bool canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SVecEvalExpr.h:206
Header file for the IsSparseVector type trait.
Header file for run time assertion macros.
Utility type for generic codes.
Header file for the EvalExprTrait class template.
typename If< T1, T2, T3 >::Type If_
Auxiliary alias declaration for the If class template.The If_ alias declaration provides a convenient...
Definition: If.h:160
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:93
Header file for the TSVecEvalExprTrait class template.
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecEvalExpr.h:162
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecEvalExpr.h:126
Evaluation of the expression type of a sparse vector evaluation operation.Via this type trait it is p...
Definition: SVecEvalExprTrait.h:74
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecEvalExpr.h:196
const DMatEvalExpr< MT, SO > eval(const DenseMatrix< MT, SO > &dm)
Forces the evaluation of the given dense matrix expression dm.
Definition: DMatEvalExpr.h:703
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecEvalExpr.h:172
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecEvalExpr.h:95
If_< IsExpression< VT >, const VT, const VT &> Operand
Composite data type of the sparse vector expression.
Definition: SVecEvalExpr.h:102
Header file for the IsComputation type trait class.
Compile time evaluation of the size of a vector.The Size type trait evaluates the size of the given v...
Definition: Size.h:75
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:120
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecEvalExpr.h:93
Header file for the SubvectorExprTrait class template.
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:403
Header file for the SVecEvalExprTrait class template.
Header file for the IsColumnVector type trait.
Header file for the Size type trait.
#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:63
#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 function trace functionality.