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/mpl/If.h>
57 #include <blaze/util/Types.h>
58 
59 
60 namespace blaze {
61 
62 //=================================================================================================
63 //
64 // CLASS SVECEVALEXPR
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
75 template< typename VT // Type of the sparse vector
76  , bool TF > // Transpose flag
77 class SVecEvalExpr
78  : public VecEvalExpr< SparseVector< SVecEvalExpr<VT,TF>, TF > >
79  , private Computation
80 {
81  public:
82  //**Type definitions****************************************************************************
88 
90  using CompositeType = const ResultType;
91 
93  using Operand = If_< IsExpression<VT>, const VT, const VT& >;
94  //**********************************************************************************************
95 
96  //**Compilation flags***************************************************************************
98  enum : bool { smpAssignable = VT::smpAssignable };
99  //**********************************************************************************************
100 
101  //**Constructor*********************************************************************************
106  explicit inline SVecEvalExpr( const VT& sv ) noexcept
107  : sv_( sv ) // Sparse vector of the evaluation expression
108  {}
109  //**********************************************************************************************
110 
111  //**Subscript operator**************************************************************************
117  inline ReturnType operator[]( size_t index ) const {
118  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
119  return sv_[index];
120  }
121  //**********************************************************************************************
122 
123  //**At function*********************************************************************************
130  inline ReturnType at( size_t index ) const {
131  if( index >= sv_.size() ) {
132  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
133  }
134  return (*this)[index];
135  }
136  //**********************************************************************************************
137 
138  //**Size function*******************************************************************************
143  inline size_t size() const noexcept {
144  return sv_.size();
145  }
146  //**********************************************************************************************
147 
148  //**NonZeros function***************************************************************************
153  inline size_t nonZeros() const {
154  return sv_.nonZeros();
155  }
156  //**********************************************************************************************
157 
158  //**Operand access******************************************************************************
163  inline Operand operand() const noexcept {
164  return sv_;
165  }
166  //**********************************************************************************************
167 
168  //**********************************************************************************************
174  template< typename T >
175  inline bool canAlias( const T* alias ) const noexcept {
176  return sv_.canAlias( alias );
177  }
178  //**********************************************************************************************
179 
180  //**********************************************************************************************
186  template< typename T >
187  inline bool isAliased( const T* alias ) const noexcept {
188  return sv_.isAliased( alias );
189  }
190  //**********************************************************************************************
191 
192  //**********************************************************************************************
197  inline bool canSMPAssign() const noexcept {
198  return sv_.canSMPAssign();
199  }
200  //**********************************************************************************************
201 
202  private:
203  //**Member variables****************************************************************************
205  //**********************************************************************************************
206 
207  //**Assignment to dense vectors*****************************************************************
219  template< typename VT2 > // Type of the target dense vector
220  friend inline void assign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
221  {
223 
224  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
225 
226  assign( ~lhs, rhs.sv_ );
227  }
229  //**********************************************************************************************
230 
231  //**Assignment to sparse vectors****************************************************************
243  template< typename VT2 > // Type of the target sparse vector
244  friend inline void assign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
245  {
247 
248  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
249 
250  assign( ~lhs, rhs.sv_ );
251  }
253  //**********************************************************************************************
254 
255  //**Addition assignment to dense vectors********************************************************
267  template< typename VT2 > // Type of the target dense vector
268  friend inline void addAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
269  {
271 
272  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
273 
274  addAssign( ~lhs, rhs.sv_ );
275  }
277  //**********************************************************************************************
278 
279  //**Addition assignment to sparse vectors*******************************************************
291  template< typename VT2 > // Type of the target sparse vector
292  friend inline void addAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
293  {
295 
296  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
297 
298  addAssign( ~lhs, rhs.sv_ );
299  }
301  //**********************************************************************************************
302 
303  //**Subtraction assignment to dense vectors*****************************************************
315  template< typename VT2 > // Type of the target dense vector
316  friend inline void subAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
317  {
319 
320  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
321 
322  subAssign( ~lhs, rhs.sv_ );
323  }
325  //**********************************************************************************************
326 
327  //**Subtraction assignment to sparse vectors****************************************************
339  template< typename VT2 > // Type of the target sparse vector
340  friend inline void subAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
341  {
343 
344  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
345 
346  subAssign( ~lhs, rhs.sv_ );
347  }
349  //**********************************************************************************************
350 
351  //**Multiplication assignment to dense vectors**************************************************
363  template< typename VT2 > // Type of the target dense vector
364  friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
365  {
367 
368  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
369 
370  multAssign( ~lhs, rhs.sv_ );
371  }
373  //**********************************************************************************************
374 
375  //**Multiplication assignment to sparse vectors*************************************************
387  template< typename VT2 > // Type of the target sparse vector
388  friend inline void multAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
389  {
391 
392  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
393 
394  multAssign( ~lhs, rhs.sv_ );
395  }
397  //**********************************************************************************************
398 
399  //**SMP assignment to dense vectors*************************************************************
411  template< typename VT2 > // Type of the target dense vector
412  friend inline void smpAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
413  {
415 
416  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
417 
418  smpAssign( ~lhs, rhs.sv_ );
419  }
421  //**********************************************************************************************
422 
423  //**SMP assignment to sparse vectors************************************************************
435  template< typename VT2 > // Type of the target sparse vector
436  friend inline void smpAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
437  {
439 
440  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
441 
442  smpAssign( ~lhs, rhs.sv_ );
443  }
445  //**********************************************************************************************
446 
447  //**SMP addition assignment to dense vectors****************************************************
459  template< typename VT2 > // Type of the target dense vector
460  friend inline void smpAddAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
461  {
463 
464  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
465 
466  smpAddAssign( ~lhs, rhs.sv_ );
467  }
469  //**********************************************************************************************
470 
471  //**SMP addition assignment to sparse vectors***************************************************
483  template< typename VT2 > // Type of the target sparse vector
484  friend inline void smpAddAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
485  {
487 
488  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
489 
490  smpAddAssign( ~lhs, rhs.sv_ );
491  }
493  //**********************************************************************************************
494 
495  //**SMP subtraction assignment to dense vectors*************************************************
507  template< typename VT2 > // Type of the target dense vector
508  friend inline void smpSubAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
509  {
511 
512  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
513 
514  smpSubAssign( ~lhs, rhs.sv_ );
515  }
517  //**********************************************************************************************
518 
519  //**SMP subtraction assignment to sparse vectors************************************************
531  template< typename VT2 > // Type of the target sparse vector
532  friend inline void smpSubAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
533  {
535 
536  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
537 
538  smpSubAssign( ~lhs, rhs.sv_ );
539  }
541  //**********************************************************************************************
542 
543  //**SMP multiplication assignment to dense vectors**********************************************
555  template< typename VT2 > // Type of the target dense vector
556  friend inline void smpMultAssign( DenseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
557  {
559 
560  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
561 
562  smpMultAssign( ~lhs, rhs.sv_ );
563  }
565  //**********************************************************************************************
566 
567  //**SMP multiplication assignment to sparse vectors*********************************************
579  template< typename VT2 > // Type of the target sparse vector
580  friend inline void smpMultAssign( SparseVector<VT2,TF>& lhs, const SVecEvalExpr& rhs )
581  {
583 
584  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
585 
586  smpMultAssign( ~lhs, rhs.sv_ );
587  }
589  //**********************************************************************************************
590 
591  //**Compile time checks*************************************************************************
596  //**********************************************************************************************
597 };
598 //*************************************************************************************************
599 
600 
601 
602 
603 //=================================================================================================
604 //
605 // GLOBAL FUNCTIONS
606 //
607 //=================================================================================================
608 
609 //*************************************************************************************************
626 template< typename VT // Type of the dense vector
627  , bool TF > // Transpose flag
628 inline decltype(auto) eval( const SparseVector<VT,TF>& sv )
629 {
631 
632  using ReturnType = const SVecEvalExpr<VT,TF>;
633  return ReturnType( ~sv );
634 }
635 //*************************************************************************************************
636 
637 
638 
639 
640 //=================================================================================================
641 //
642 // GLOBAL RESTRUCTURING FUNCTIONS
643 //
644 //=================================================================================================
645 
646 //*************************************************************************************************
657 template< typename VT // Type of the sparse vector
658  , bool TF > // Transpose flag
659 inline decltype(auto) eval( const SVecEvalExpr<VT,TF>& sv )
660 {
661  return sv;
662 }
664 //*************************************************************************************************
665 
666 
667 
668 
669 //=================================================================================================
670 //
671 // SIZE SPECIALIZATIONS
672 //
673 //=================================================================================================
674 
675 //*************************************************************************************************
677 template< typename VT, bool TF >
678 struct Size< SVecEvalExpr<VT,TF>, 0UL >
679  : public Size<VT,0UL>
680 {};
682 //*************************************************************************************************
683 
684 } // namespace blaze
685 
686 #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:93
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:85
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:84
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:204
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecEvalExpr.h:143
Header file for the Computation base class.
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecEvalExpr.h:130
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecEvalExpr.h:90
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:106
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
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:76
#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:175
decltype(auto) eval(const DenseMatrix< MT, SO > &dm)
Forces the evaluation of the given dense matrix expression dm.
Definition: DMatEvalExpr.h:794
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:197
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecEvalExpr.h:86
Header file for run time assertion macros.
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:153
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecEvalExpr.h:117
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecEvalExpr.h:187
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecEvalExpr.h:163
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecEvalExpr.h:87
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:3080
Header file for the IsComputation type trait class.
Compile time evaluation of the size of vectors and matrices.The Size type trait evaluates the size of...
Definition: Size.h:80
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.