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 <cmath>
44 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
62 #include <blaze/util/Assert.h>
64 #include <blaze/util/InvalidType.h>
66 #include <blaze/util/mpl/And.h>
67 #include <blaze/util/mpl/If.h>
68 #include <blaze/util/Types.h>
69 
70 
71 namespace blaze {
72 
73 //=================================================================================================
74 //
75 // CLASS SVECEVALEXPR
76 //
77 //=================================================================================================
78 
79 //*************************************************************************************************
86 template< typename VT // Type of the sparse vector
87  , bool TF > // Transpose flag
88 class SVecEvalExpr : public SparseVector< SVecEvalExpr<VT,TF>, TF >
89  , private VecEvalExpr
90  , private Computation
91 {
92  public:
93  //**Type definitions****************************************************************************
99 
101  typedef const ResultType CompositeType;
102 
104  typedef If_< IsExpression<VT>, const VT, const VT& > Operand;
105  //**********************************************************************************************
106 
107  //**Compilation flags***************************************************************************
109  enum : bool { smpAssignable = VT::smpAssignable };
110  //**********************************************************************************************
111 
112  //**Constructor*********************************************************************************
117  explicit inline SVecEvalExpr( const VT& sv ) noexcept
118  : sv_( sv ) // Sparse vector of the evaluation expression
119  {}
120  //**********************************************************************************************
121 
122  //**Subscript operator**************************************************************************
128  inline ReturnType operator[]( size_t index ) const {
129  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
130  return sv_[index];
131  }
132  //**********************************************************************************************
133 
134  //**At function*********************************************************************************
141  inline ReturnType at( size_t index ) const {
142  if( index >= sv_.size() ) {
143  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
144  }
145  return (*this)[index];
146  }
147  //**********************************************************************************************
148 
149  //**Size function*******************************************************************************
154  inline size_t size() const noexcept {
155  return sv_.size();
156  }
157  //**********************************************************************************************
158 
159  //**NonZeros function***************************************************************************
164  inline size_t nonZeros() const {
165  return sv_.nonZeros();
166  }
167  //**********************************************************************************************
168 
169  //**Operand access******************************************************************************
174  inline Operand operand() const noexcept {
175  return sv_;
176  }
177  //**********************************************************************************************
178 
179  //**********************************************************************************************
185  template< typename T >
186  inline bool canAlias( const T* alias ) const noexcept {
187  return sv_.canAlias( alias );
188  }
189  //**********************************************************************************************
190 
191  //**********************************************************************************************
197  template< typename T >
198  inline bool isAliased( const T* alias ) const noexcept {
199  return sv_.isAliased( alias );
200  }
201  //**********************************************************************************************
202 
203  //**********************************************************************************************
208  inline bool canSMPAssign() const noexcept {
209  return sv_.canSMPAssign();
210  }
211  //**********************************************************************************************
212 
213  private:
214  //**Member variables****************************************************************************
215  Operand sv_;
216  //**********************************************************************************************
217 
218  //**Assignment to dense vectors*****************************************************************
230  template< typename VT2 > // Type of the target dense vector
231  friend inline void assign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
232  {
234 
235  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
236 
237  assign( ~lhs, rhs.sv_ );
238  }
240  //**********************************************************************************************
241 
242  //**Assignment to sparse vectors****************************************************************
254  template< typename VT2 > // Type of the target sparse vector
255  friend inline void assign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
256  {
258 
259  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
260 
261  assign( ~lhs, rhs.sv_ );
262  }
264  //**********************************************************************************************
265 
266  //**Addition assignment to dense vectors********************************************************
278  template< typename VT2 > // Type of the target dense vector
279  friend inline void addAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
280  {
282 
283  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
284 
285  addAssign( ~lhs, rhs.sv_ );
286  }
288  //**********************************************************************************************
289 
290  //**Addition assignment to sparse vectors*******************************************************
302  template< typename VT2 > // Type of the target sparse vector
303  friend inline void addAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
304  {
306 
307  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
308 
309  addAssign( ~lhs, rhs.sv_ );
310  }
312  //**********************************************************************************************
313 
314  //**Subtraction assignment to dense vectors*****************************************************
326  template< typename VT2 > // Type of the target dense vector
327  friend inline void subAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
328  {
330 
331  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
332 
333  subAssign( ~lhs, rhs.sv_ );
334  }
336  //**********************************************************************************************
337 
338  //**Subtraction assignment to sparse vectors****************************************************
350  template< typename VT2 > // Type of the target sparse vector
351  friend inline void subAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
352  {
354 
355  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
356 
357  subAssign( ~lhs, rhs.sv_ );
358  }
360  //**********************************************************************************************
361 
362  //**Multiplication assignment to dense vectors**************************************************
374  template< typename VT2 > // Type of the target dense vector
375  friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
376  {
378 
379  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
380 
381  multAssign( ~lhs, rhs.sv_ );
382  }
384  //**********************************************************************************************
385 
386  //**Multiplication assignment to sparse vectors*************************************************
398  template< typename VT2 > // Type of the target sparse vector
399  friend inline void multAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
400  {
402 
403  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
404 
405  multAssign( ~lhs, rhs.sv_ );
406  }
408  //**********************************************************************************************
409 
410  //**SMP assignment to dense vectors*************************************************************
422  template< typename VT2 > // Type of the target dense vector
423  friend inline void smpAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
424  {
426 
427  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
428 
429  smpAssign( ~lhs, rhs.sv_ );
430  }
432  //**********************************************************************************************
433 
434  //**SMP assignment to sparse vectors************************************************************
446  template< typename VT2 > // Type of the target sparse vector
447  friend inline void smpAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
448  {
450 
451  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
452 
453  smpAssign( ~lhs, rhs.sv_ );
454  }
456  //**********************************************************************************************
457 
458  //**SMP addition assignment to dense vectors****************************************************
470  template< typename VT2 > // Type of the target dense vector
471  friend inline void smpAddAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
472  {
474 
475  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
476 
477  smpAddAssign( ~lhs, rhs.sv_ );
478  }
480  //**********************************************************************************************
481 
482  //**SMP addition assignment to sparse vectors***************************************************
494  template< typename VT2 > // Type of the target sparse vector
495  friend inline void smpAddAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
496  {
498 
499  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
500 
501  smpAddAssign( ~lhs, rhs.sv_ );
502  }
504  //**********************************************************************************************
505 
506  //**SMP subtraction assignment to dense vectors*************************************************
518  template< typename VT2 > // Type of the target dense vector
519  friend inline void smpSubAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
520  {
522 
523  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
524 
525  smpSubAssign( ~lhs, rhs.sv_ );
526  }
528  //**********************************************************************************************
529 
530  //**SMP subtraction assignment to sparse vectors************************************************
542  template< typename VT2 > // Type of the target sparse vector
543  friend inline void smpSubAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
544  {
546 
547  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
548 
549  smpSubAssign( ~lhs, rhs.sv_ );
550  }
552  //**********************************************************************************************
553 
554  //**SMP multiplication assignment to dense vectors**********************************************
566  template< typename VT2 > // Type of the target dense vector
567  friend inline void smpMultAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
568  {
570 
571  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
572 
573  smpMultAssign( ~lhs, rhs.sv_ );
574  }
576  //**********************************************************************************************
577 
578  //**SMP multiplication assignment to sparse vectors*********************************************
590  template< typename VT2 > // Type of the target sparse vector
591  friend inline void smpMultAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
592  {
594 
595  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
596 
597  smpMultAssign( ~lhs, rhs.sv_ );
598  }
600  //**********************************************************************************************
601 
602  //**Compile time checks*************************************************************************
607  //**********************************************************************************************
608 };
609 //*************************************************************************************************
610 
611 
612 
613 
614 //=================================================================================================
615 //
616 // GLOBAL FUNCTIONS
617 //
618 //=================================================================================================
619 
620 //*************************************************************************************************
637 template< typename VT // Type of the dense vector
638  , bool TF > // Transpose flag
640 {
642 
643  return SVecEvalExpr<VT,TF>( ~sv );
644 }
645 //*************************************************************************************************
646 
647 
648 
649 
650 //=================================================================================================
651 //
652 // GLOBAL RESTRUCTURING FUNCTIONS
653 //
654 //=================================================================================================
655 
656 //*************************************************************************************************
667 template< typename VT // Type of the sparse vector
668  , bool TF > // Transpose flag
669 inline const SVecEvalExpr<VT,TF> eval( const SVecEvalExpr<VT,TF>& sv )
670 {
671  return sv;
672 }
674 //*************************************************************************************************
675 
676 
677 
678 
679 //=================================================================================================
680 //
681 // SIZE SPECIALIZATIONS
682 //
683 //=================================================================================================
684 
685 //*************************************************************************************************
687 template< typename VT, bool TF >
688 struct Size< SVecEvalExpr<VT,TF> > : public Size<VT>
689 {};
691 //*************************************************************************************************
692 
693 
694 
695 
696 //=================================================================================================
697 //
698 // EXPRESSION TRAIT SPECIALIZATIONS
699 //
700 //=================================================================================================
701 
702 //*************************************************************************************************
704 template< typename VT >
705 struct SVecEvalExprTrait< SVecEvalExpr<VT,false> >
706 {
707  public:
708  //**********************************************************************************************
709  using Type = If_< And< IsSparseVector<VT>, IsColumnVector<VT> >
710  , SVecEvalExpr<VT,false>
711  , INVALID_TYPE >;
712  //**********************************************************************************************
713 };
715 //*************************************************************************************************
716 
717 
718 //*************************************************************************************************
720 template< typename VT >
721 struct TSVecEvalExprTrait< SVecEvalExpr<VT,true> >
722 {
723  public:
724  //**********************************************************************************************
725  using Type = If_< And< IsSparseVector<VT>, IsRowVector<VT> >
726  , SVecEvalExpr<VT,true>
727  , INVALID_TYPE >;
728  //**********************************************************************************************
729 };
731 //*************************************************************************************************
732 
733 
734 //*************************************************************************************************
736 template< typename VT, bool TF, bool AF >
737 struct SubvectorExprTrait< SVecEvalExpr<VT,TF>, AF >
738 {
739  public:
740  //**********************************************************************************************
741  using Type = EvalExprTrait_< SubvectorExprTrait_<const VT,AF> >;
742  //**********************************************************************************************
743 };
745 //*************************************************************************************************
746 
747 } // namespace blaze
748 
749 #endif
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecEvalExpr.h:198
Header file for auxiliary alias declarations.
bool canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SVecEvalExpr.h:208
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:98
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:115
Header file for the And class template.
Operand sv_
Sparse vector of the evaluation expression.
Definition: SVecEvalExpr.h:215
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecEvalExpr.h:101
Header file for the Computation base class.
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:96
Constraint on the data type.
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:94
SVecEvalExpr(const VT &sv) noexcept
Constructor for the SVecEvalExpr class.
Definition: SVecEvalExpr.h:117
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecEvalExpr.h:174
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
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecEvalExpr.h:141
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecEvalExpr.h:164
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.
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecEvalExpr.h:154
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 canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecEvalExpr.h:186
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
Header file for the TSVecEvalExprTrait class template.
const DMatEvalExpr< MT, SO > eval(const DenseMatrix< MT, SO > &dm)
Forces the evaluation of the given dense matrix expression dm.
Definition: DMatEvalExpr.h:705
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecEvalExpr.h:97
If_< IsExpression< VT >, const VT, const VT & > Operand
Composite data type of the sparse vector expression.
Definition: SVecEvalExpr.h:104
Header file for the IsComputation type trait class.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:110
#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
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecEvalExpr.h:95
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.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecEvalExpr.h:128
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 FunctionTrace class.