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/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 SVECSERIALEXPR
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
76 template< typename VT // Type of the sparse vector
77  , bool TF > // Transpose flag
78 class SVecSerialExpr
79  : public VecSerialExpr< SparseVector< SVecSerialExpr<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 SVecSerialExpr( const VT& sv ) noexcept
108  : sv_( sv ) // Sparse vector of the serial 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  //**Conversion operator*************************************************************************
174  inline operator 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****************************************************************************
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 SVecSerialExpr& 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 SVecSerialExpr& 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 SVecSerialExpr& 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 SVecSerialExpr& 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*****************************************************
327  template< typename VT2 > // Type of the target dense vector
328  friend inline void subAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
329  {
331 
332  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
333 
334  subAssign( ~lhs, rhs.sv_ );
335  }
337  //**********************************************************************************************
338 
339  //**Subtraction assignment to sparse vectors****************************************************
352  template< typename VT2 > // Type of the target sparse vector
353  friend inline void subAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
354  {
356 
357  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
358 
359  subAssign( ~lhs, rhs.sv_ );
360  }
362  //**********************************************************************************************
363 
364  //**Multiplication assignment to dense vectors**************************************************
377  template< typename VT2 > // Type of the target dense vector
378  friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
379  {
381 
382  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
383 
384  multAssign( ~lhs, rhs.sv_ );
385  }
387  //**********************************************************************************************
388 
389  //**Multiplication assignment to sparse vectors*************************************************
402  template< typename VT2 > // Type of the target sparse vector
403  friend inline void multAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
404  {
406 
407  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
408 
409  multAssign( ~lhs, rhs.sv_ );
410  }
412  //**********************************************************************************************
413 
414  //**SMP assignment to dense vectors*************************************************************
426  template< typename VT2 > // Type of the target dense vector
427  friend inline void smpAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
428  {
430 
431  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
432 
433  assign( ~lhs, rhs.sv_ );
434  }
436  //**********************************************************************************************
437 
438  //**SMP assignment to sparse vectors************************************************************
450  template< typename VT2 > // Type of the target sparse vector
451  friend inline void smpAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
452  {
454 
455  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
456 
457  assign( ~lhs, rhs.sv_ );
458  }
460  //**********************************************************************************************
461 
462  //**SMP addition assignment to dense vectors****************************************************
475  template< typename VT2 > // Type of the target dense vector
476  friend inline void smpAddAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
477  {
479 
480  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
481 
482  addAssign( ~lhs, rhs.sv_ );
483  }
485  //**********************************************************************************************
486 
487  //**SMP addition assignment to sparse vectors***************************************************
500  template< typename VT2 > // Type of the target sparse vector
501  friend inline void smpAddAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
502  {
504 
505  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
506 
507  addAssign( ~lhs, rhs.sv_ );
508  }
510  //**********************************************************************************************
511 
512  //**SMP subtraction assignment to dense vectors*************************************************
525  template< typename VT2 > // Type of the target dense vector
526  friend inline void smpSubAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
527  {
529 
530  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
531 
532  subAssign( ~lhs, rhs.sv_ );
533  }
535  //**********************************************************************************************
536 
537  //**SMP subtraction assignment to sparse vectors************************************************
550  template< typename VT2 > // Type of the target sparse vector
551  friend inline void smpSubAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
552  {
554 
555  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
556 
557  subAssign( ~lhs, rhs.sv_ );
558  }
560  //**********************************************************************************************
561 
562  //**SMP multiplication assignment to dense vectors**********************************************
575  template< typename VT2 > // Type of the target dense vector
576  friend inline void smpMultAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
577  {
579 
580  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
581 
582  multAssign( ~lhs, rhs.sv_ );
583  }
585  //**********************************************************************************************
586 
587  //**SMP multiplication assignment to sparse vectors*********************************************
600  template< typename VT2 > // Type of the target sparse vector
601  friend inline void smpMultAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
602  {
604 
605  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
606 
607  multAssign( ~lhs, rhs.sv_ );
608  }
610  //**********************************************************************************************
611 
612  //**Compile time checks*************************************************************************
617  //**********************************************************************************************
618 };
619 //*************************************************************************************************
620 
621 
622 
623 
624 //=================================================================================================
625 //
626 // GLOBAL FUNCTIONS
627 //
628 //=================================================================================================
629 
630 //*************************************************************************************************
647 template< typename VT // Type of the dense vector
648  , bool TF > // Transpose flag
649 inline decltype(auto) serial( const SparseVector<VT,TF>& sv )
650 {
652 
653  using ReturnType = const SVecSerialExpr<VT,TF>;
654  return ReturnType( ~sv );
655 }
656 //*************************************************************************************************
657 
658 
659 
660 
661 //=================================================================================================
662 //
663 // GLOBAL RESTRUCTURING FUNCTIONS
664 //
665 //=================================================================================================
666 
667 //*************************************************************************************************
678 template< typename VT // Type of the sparse vector
679  , bool TF > // Transpose flag
680 inline decltype(auto) serial( const SVecSerialExpr<VT,TF>& sv )
681 {
682  return sv;
683 }
685 //*************************************************************************************************
686 
687 
688 
689 
690 //=================================================================================================
691 //
692 // SIZE SPECIALIZATIONS
693 //
694 //=================================================================================================
695 
696 //*************************************************************************************************
698 template< typename VT, bool TF >
699 struct Size< SVecSerialExpr<VT,TF> >
700  : public Size<VT>
701 {};
703 //*************************************************************************************************
704 
705 } // namespace blaze
706 
707 #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:91
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:94
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:107
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecSerialExpr.h:198
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:87
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:154
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecSerialExpr.h:88
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
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:118
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:131
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
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecSerialExpr.h:164
#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:208
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSerialExpr.h:86
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.
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
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:819
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecSerialExpr.h:186
Operand sv_
Sparse vector of the serial evaluation expression.
Definition: SVecSerialExpr.h:215
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:3082
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecSerialExpr.h:85
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
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSerialExpr.h:144
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.