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>
54 #include <blaze/util/Assert.h>
56 #include <blaze/util/InvalidType.h>
57 #include <blaze/util/mpl/If.h>
58 #include <blaze/util/Types.h>
59 
60 
61 namespace blaze {
62 
63 //=================================================================================================
64 //
65 // CLASS SVECEVALEXPR
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
76 template< typename VT // Type of the sparse vector
77  , bool TF > // Transpose flag
78 class SVecEvalExpr
79  : public VecEvalExpr< SparseVector< SVecEvalExpr<VT,TF>, TF > >
80  , private Computation
81 {
82  public:
83  //**Type definitions****************************************************************************
89 
91  using CompositeType = const ResultType;
92 
94  using Operand = If_< IsExpression<VT>, const VT, const VT& >;
95  //**********************************************************************************************
96 
97  //**Compilation flags***************************************************************************
99  enum : bool { smpAssignable = VT::smpAssignable };
100  //**********************************************************************************************
101 
102  //**Constructor*********************************************************************************
107  explicit inline SVecEvalExpr( const VT& sv ) noexcept
108  : sv_( sv ) // Sparse vector of the evaluation expression
109  {}
110  //**********************************************************************************************
111 
112  //**Subscript operator**************************************************************************
118  inline ReturnType operator[]( size_t index ) const {
119  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
120  return sv_[index];
121  }
122  //**********************************************************************************************
123 
124  //**At function*********************************************************************************
131  inline ReturnType at( size_t index ) const {
132  if( index >= sv_.size() ) {
133  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
134  }
135  return (*this)[index];
136  }
137  //**********************************************************************************************
138 
139  //**Size function*******************************************************************************
144  inline size_t size() const noexcept {
145  return sv_.size();
146  }
147  //**********************************************************************************************
148 
149  //**NonZeros function***************************************************************************
154  inline size_t nonZeros() const {
155  return sv_.nonZeros();
156  }
157  //**********************************************************************************************
158 
159  //**Operand access******************************************************************************
164  inline Operand operand() const noexcept {
165  return sv_;
166  }
167  //**********************************************************************************************
168 
169  //**********************************************************************************************
175  template< typename T >
176  inline bool canAlias( const T* alias ) const noexcept {
177  return sv_.canAlias( alias );
178  }
179  //**********************************************************************************************
180 
181  //**********************************************************************************************
187  template< typename T >
188  inline bool isAliased( const T* alias ) const noexcept {
189  return sv_.isAliased( alias );
190  }
191  //**********************************************************************************************
192 
193  //**********************************************************************************************
198  inline bool canSMPAssign() const noexcept {
199  return sv_.canSMPAssign();
200  }
201  //**********************************************************************************************
202 
203  private:
204  //**Member variables****************************************************************************
206  //**********************************************************************************************
207 
208  //**Assignment to dense vectors*****************************************************************
220  template< typename VT2 > // Type of the target dense vector
221  friend inline void assign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
222  {
224 
225  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
226 
227  assign( ~lhs, rhs.sv_ );
228  }
230  //**********************************************************************************************
231 
232  //**Assignment to sparse vectors****************************************************************
244  template< typename VT2 > // Type of the target sparse vector
245  friend inline void assign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
246  {
248 
249  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
250 
251  assign( ~lhs, rhs.sv_ );
252  }
254  //**********************************************************************************************
255 
256  //**Addition assignment to dense vectors********************************************************
268  template< typename VT2 > // Type of the target dense vector
269  friend inline void addAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
270  {
272 
273  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
274 
275  addAssign( ~lhs, rhs.sv_ );
276  }
278  //**********************************************************************************************
279 
280  //**Addition assignment to sparse vectors*******************************************************
292  template< typename VT2 > // Type of the target sparse vector
293  friend inline void addAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
294  {
296 
297  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
298 
299  addAssign( ~lhs, rhs.sv_ );
300  }
302  //**********************************************************************************************
303 
304  //**Subtraction assignment to dense vectors*****************************************************
316  template< typename VT2 > // Type of the target dense vector
317  friend inline void subAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
318  {
320 
321  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
322 
323  subAssign( ~lhs, rhs.sv_ );
324  }
326  //**********************************************************************************************
327 
328  //**Subtraction assignment to sparse vectors****************************************************
340  template< typename VT2 > // Type of the target sparse vector
341  friend inline void subAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
342  {
344 
345  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
346 
347  subAssign( ~lhs, rhs.sv_ );
348  }
350  //**********************************************************************************************
351 
352  //**Multiplication assignment to dense vectors**************************************************
364  template< typename VT2 > // Type of the target dense vector
365  friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
366  {
368 
369  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
370 
371  multAssign( ~lhs, rhs.sv_ );
372  }
374  //**********************************************************************************************
375 
376  //**Multiplication assignment to sparse vectors*************************************************
388  template< typename VT2 > // Type of the target sparse vector
389  friend inline void multAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
390  {
392 
393  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
394 
395  multAssign( ~lhs, rhs.sv_ );
396  }
398  //**********************************************************************************************
399 
400  //**SMP assignment to dense vectors*************************************************************
412  template< typename VT2 > // Type of the target dense vector
413  friend inline void smpAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
414  {
416 
417  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
418 
419  smpAssign( ~lhs, rhs.sv_ );
420  }
422  //**********************************************************************************************
423 
424  //**SMP assignment to sparse vectors************************************************************
436  template< typename VT2 > // Type of the target sparse vector
437  friend inline void smpAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
438  {
440 
441  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
442 
443  smpAssign( ~lhs, rhs.sv_ );
444  }
446  //**********************************************************************************************
447 
448  //**SMP addition assignment to dense vectors****************************************************
460  template< typename VT2 > // Type of the target dense vector
461  friend inline void smpAddAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
462  {
464 
465  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
466 
467  smpAddAssign( ~lhs, rhs.sv_ );
468  }
470  //**********************************************************************************************
471 
472  //**SMP addition assignment to sparse vectors***************************************************
484  template< typename VT2 > // Type of the target sparse vector
485  friend inline void smpAddAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
486  {
488 
489  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
490 
491  smpAddAssign( ~lhs, rhs.sv_ );
492  }
494  //**********************************************************************************************
495 
496  //**SMP subtraction assignment to dense vectors*************************************************
508  template< typename VT2 > // Type of the target dense vector
509  friend inline void smpSubAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
510  {
512 
513  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
514 
515  smpSubAssign( ~lhs, rhs.sv_ );
516  }
518  //**********************************************************************************************
519 
520  //**SMP subtraction assignment to sparse vectors************************************************
532  template< typename VT2 > // Type of the target sparse vector
533  friend inline void smpSubAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
534  {
536 
537  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
538 
539  smpSubAssign( ~lhs, rhs.sv_ );
540  }
542  //**********************************************************************************************
543 
544  //**SMP multiplication assignment to dense vectors**********************************************
556  template< typename VT2 > // Type of the target dense vector
557  friend inline void smpMultAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
558  {
560 
561  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
562 
563  smpMultAssign( ~lhs, rhs.sv_ );
564  }
566  //**********************************************************************************************
567 
568  //**SMP multiplication assignment to sparse vectors*********************************************
580  template< typename VT2 > // Type of the target sparse vector
581  friend inline void smpMultAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
582  {
584 
585  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
586 
587  smpMultAssign( ~lhs, rhs.sv_ );
588  }
590  //**********************************************************************************************
591 
592  //**Compile time checks*************************************************************************
597  //**********************************************************************************************
598 };
599 //*************************************************************************************************
600 
601 
602 
603 
604 //=================================================================================================
605 //
606 // GLOBAL FUNCTIONS
607 //
608 //=================================================================================================
609 
610 //*************************************************************************************************
627 template< typename VT // Type of the dense vector
628  , bool TF > // Transpose flag
629 inline decltype(auto) eval( const SparseVector<VT,TF>& sv )
630 {
632 
633  using ReturnType = const SVecEvalExpr<VT,TF>;
634  return ReturnType( ~sv );
635 }
636 //*************************************************************************************************
637 
638 
639 
640 
641 //=================================================================================================
642 //
643 // GLOBAL RESTRUCTURING FUNCTIONS
644 //
645 //=================================================================================================
646 
647 //*************************************************************************************************
658 template< typename VT // Type of the sparse vector
659  , bool TF > // Transpose flag
660 inline decltype(auto) eval( const SVecEvalExpr<VT,TF>& sv )
661 {
662  return sv;
663 }
665 //*************************************************************************************************
666 
667 
668 
669 
670 //=================================================================================================
671 //
672 // SIZE SPECIALIZATIONS
673 //
674 //=================================================================================================
675 
676 //*************************************************************************************************
678 template< typename VT, bool TF >
679 struct Size< SVecEvalExpr<VT,TF> >
680  : public Size<VT>
681 {};
683 //*************************************************************************************************
684 
685 } // namespace blaze
686 
687 #endif
Header file for auxiliary alias declarations.
If_< IsExpression< VT >, const VT, const VT &> Operand
Composite data type of the sparse vector expression.
Definition: SVecEvalExpr.h:94
Header file for basic type definitions.
Header file for the SparseVector base class.
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecEvalExpr.h:86
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:164
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecEvalExpr.h:85
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:136
Operand sv_
Sparse vector of the evaluation expression.
Definition: SVecEvalExpr.h:205
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecEvalExpr.h:144
Header file for the Computation base class.
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecEvalExpr.h:131
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecEvalExpr.h:91
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:343
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:133
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:363
SVecEvalExpr(const VT &sv) noexcept
Constructor for the SVecEvalExpr class.
Definition: SVecEvalExpr.h:107
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:102
#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:176
decltype(auto) eval(const DenseMatrix< MT, SO > &dm)
Forces the evaluation of the given dense matrix expression dm.
Definition: DMatEvalExpr.h:797
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:198
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecEvalExpr.h:87
Header file for run time assertion macros.
Utility type for generic codes.
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:154
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:94
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecEvalExpr.h:154
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecEvalExpr.h:118
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecEvalExpr.h:188
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecEvalExpr.h:164
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecEvalExpr.h:88
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:3082
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:74
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:130
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:423
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.