SVecSerialExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECSERIALEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECSERIALEXPR_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 SVECSERIALEXPR
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
75 template< typename VT // Type of the sparse vector
76  , bool TF > // Transpose flag
77 class SVecSerialExpr
78  : public VecSerialExpr< SparseVector< SVecSerialExpr<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 SVecSerialExpr( const VT& sv ) noexcept
107  : sv_( sv ) // Sparse vector of the serial 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  //**Conversion operator*************************************************************************
173  inline operator Operand() const noexcept {
174  return sv_;
175  }
176  //**********************************************************************************************
177 
178  //**********************************************************************************************
184  template< typename T >
185  inline bool canAlias( const T* alias ) const noexcept {
186  return sv_.canAlias( alias );
187  }
188  //**********************************************************************************************
189 
190  //**********************************************************************************************
196  template< typename T >
197  inline bool isAliased( const T* alias ) const noexcept {
198  return sv_.isAliased( alias );
199  }
200  //**********************************************************************************************
201 
202  //**********************************************************************************************
207  inline bool canSMPAssign() const noexcept {
208  return sv_.canSMPAssign();
209  }
210  //**********************************************************************************************
211 
212  private:
213  //**Member variables****************************************************************************
215  //**********************************************************************************************
216 
217  //**Assignment to dense vectors*****************************************************************
229  template< typename VT2 > // Type of the target dense vector
230  friend inline void assign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
231  {
233 
234  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
235 
236  assign( ~lhs, rhs.sv_ );
237  }
239  //**********************************************************************************************
240 
241  //**Assignment to sparse vectors****************************************************************
253  template< typename VT2 > // Type of the target sparse vector
254  friend inline void assign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
255  {
257 
258  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
259 
260  assign( ~lhs, rhs.sv_ );
261  }
263  //**********************************************************************************************
264 
265  //**Addition assignment to dense vectors********************************************************
277  template< typename VT2 > // Type of the target dense vector
278  friend inline void addAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
279  {
281 
282  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
283 
284  addAssign( ~lhs, rhs.sv_ );
285  }
287  //**********************************************************************************************
288 
289  //**Addition assignment to sparse vectors*******************************************************
301  template< typename VT2 > // Type of the target sparse vector
302  friend inline void addAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
303  {
305 
306  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
307 
308  addAssign( ~lhs, rhs.sv_ );
309  }
311  //**********************************************************************************************
312 
313  //**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 SVecSerialExpr& 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****************************************************
351  template< typename VT2 > // Type of the target sparse vector
352  friend inline void subAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
353  {
355 
356  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
357 
358  subAssign( ~lhs, rhs.sv_ );
359  }
361  //**********************************************************************************************
362 
363  //**Multiplication assignment to dense vectors**************************************************
376  template< typename VT2 > // Type of the target dense vector
377  friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
378  {
380 
381  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
382 
383  multAssign( ~lhs, rhs.sv_ );
384  }
386  //**********************************************************************************************
387 
388  //**Multiplication assignment to sparse vectors*************************************************
401  template< typename VT2 > // Type of the target sparse vector
402  friend inline void multAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
403  {
405 
406  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
407 
408  multAssign( ~lhs, rhs.sv_ );
409  }
411  //**********************************************************************************************
412 
413  //**SMP assignment to dense vectors*************************************************************
425  template< typename VT2 > // Type of the target dense vector
426  friend inline void smpAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
427  {
429 
430  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
431 
432  assign( ~lhs, rhs.sv_ );
433  }
435  //**********************************************************************************************
436 
437  //**SMP assignment to sparse vectors************************************************************
449  template< typename VT2 > // Type of the target sparse vector
450  friend inline void smpAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
451  {
453 
454  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
455 
456  assign( ~lhs, rhs.sv_ );
457  }
459  //**********************************************************************************************
460 
461  //**SMP addition assignment to dense vectors****************************************************
474  template< typename VT2 > // Type of the target dense vector
475  friend inline void smpAddAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
476  {
478 
479  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
480 
481  addAssign( ~lhs, rhs.sv_ );
482  }
484  //**********************************************************************************************
485 
486  //**SMP addition assignment to sparse vectors***************************************************
499  template< typename VT2 > // Type of the target sparse vector
500  friend inline void smpAddAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
501  {
503 
504  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
505 
506  addAssign( ~lhs, rhs.sv_ );
507  }
509  //**********************************************************************************************
510 
511  //**SMP subtraction assignment to dense vectors*************************************************
524  template< typename VT2 > // Type of the target dense vector
525  friend inline void smpSubAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
526  {
528 
529  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
530 
531  subAssign( ~lhs, rhs.sv_ );
532  }
534  //**********************************************************************************************
535 
536  //**SMP subtraction assignment to sparse vectors************************************************
549  template< typename VT2 > // Type of the target sparse vector
550  friend inline void smpSubAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
551  {
553 
554  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
555 
556  subAssign( ~lhs, rhs.sv_ );
557  }
559  //**********************************************************************************************
560 
561  //**SMP multiplication assignment to dense vectors**********************************************
574  template< typename VT2 > // Type of the target dense vector
575  friend inline void smpMultAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
576  {
578 
579  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
580 
581  multAssign( ~lhs, rhs.sv_ );
582  }
584  //**********************************************************************************************
585 
586  //**SMP multiplication assignment to sparse vectors*********************************************
599  template< typename VT2 > // Type of the target sparse vector
600  friend inline void smpMultAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
601  {
603 
604  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
605 
606  multAssign( ~lhs, rhs.sv_ );
607  }
609  //**********************************************************************************************
610 
611  //**Compile time checks*************************************************************************
616  //**********************************************************************************************
617 };
618 //*************************************************************************************************
619 
620 
621 
622 
623 //=================================================================================================
624 //
625 // GLOBAL FUNCTIONS
626 //
627 //=================================================================================================
628 
629 //*************************************************************************************************
646 template< typename VT // Type of the dense vector
647  , bool TF > // Transpose flag
648 inline decltype(auto) serial( const SparseVector<VT,TF>& sv )
649 {
651 
652  using ReturnType = const SVecSerialExpr<VT,TF>;
653  return ReturnType( ~sv );
654 }
655 //*************************************************************************************************
656 
657 
658 
659 
660 //=================================================================================================
661 //
662 // GLOBAL RESTRUCTURING FUNCTIONS
663 //
664 //=================================================================================================
665 
666 //*************************************************************************************************
677 template< typename VT // Type of the sparse vector
678  , bool TF > // Transpose flag
679 inline decltype(auto) serial( const SVecSerialExpr<VT,TF>& sv )
680 {
681  return sv;
682 }
684 //*************************************************************************************************
685 
686 
687 
688 
689 //=================================================================================================
690 //
691 // SIZE SPECIALIZATIONS
692 //
693 //=================================================================================================
694 
695 //*************************************************************************************************
697 template< typename VT, bool TF >
698 struct Size< SVecSerialExpr<VT,TF>, 0UL >
699  : public Size<VT,0UL>
700 {};
702 //*************************************************************************************************
703 
704 } // namespace blaze
705 
706 #endif
Header file for auxiliary alias declarations.
Header file for basic type definitions.
Header file for the SparseVector base class.
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSerialExpr.h:90
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
If_< IsExpression< VT >, const VT, const VT &> Operand
Composite data type of the sparse vector expression.
Definition: SVecSerialExpr.h:93
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
Header file for the Computation base class.
SVecSerialExpr(const VT &sv) noexcept
Constructor for the SVecSerialExpr class.
Definition: SVecSerialExpr.h:106
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecSerialExpr.h:197
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
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecSerialExpr.h:86
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:363
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSerialExpr.h:153
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecSerialExpr.h:87
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the If class template.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSerialExpr.h:117
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
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecSerialExpr.h:130
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
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecSerialExpr.h:163
#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 canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SVecSerialExpr.h:207
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSerialExpr.h:85
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.
Expression object for the forced serial evaluation of sparse vectors.The SVecSerialExpr class represe...
Definition: Forward.h:140
Header file for the VecSerialExpr base class.
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
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:816
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecSerialExpr.h:185
Operand sv_
Sparse vector of the serial evaluation expression.
Definition: SVecSerialExpr.h:214
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:3080
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecSerialExpr.h:84
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
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSerialExpr.h:143
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.