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 <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 SVECSERIALEXPR
76 //
77 //=================================================================================================
78 
79 //*************************************************************************************************
86 template< typename VT // Type of the sparse vector
87  , bool TF > // Transpose flag
88 class SVecSerialExpr : public SparseVector< SVecSerialExpr<VT,TF>, TF >
89  , private VecSerialExpr
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 SVecSerialExpr( const VT& sv ) noexcept
118  : sv_( sv ) // Sparse vector of the serial 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  //**Conversion operator*************************************************************************
184  inline operator Operand() const noexcept {
185  return sv_;
186  }
187  //**********************************************************************************************
188 
189  //**********************************************************************************************
195  template< typename T >
196  inline bool canAlias( const T* alias ) const noexcept {
197  return sv_.canAlias( alias );
198  }
199  //**********************************************************************************************
200 
201  //**********************************************************************************************
207  template< typename T >
208  inline bool isAliased( const T* alias ) const noexcept {
209  return sv_.isAliased( alias );
210  }
211  //**********************************************************************************************
212 
213  //**********************************************************************************************
218  inline bool canSMPAssign() const noexcept {
219  return sv_.canSMPAssign();
220  }
221  //**********************************************************************************************
222 
223  private:
224  //**Member variables****************************************************************************
225  Operand sv_;
226  //**********************************************************************************************
227 
228  //**Assignment to dense vectors*****************************************************************
240  template< typename VT2 > // Type of the target dense vector
241  friend inline void assign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
242  {
244 
245  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
246 
247  assign( ~lhs, rhs.sv_ );
248  }
250  //**********************************************************************************************
251 
252  //**Assignment to sparse vectors****************************************************************
264  template< typename VT2 > // Type of the target sparse vector
265  friend inline void assign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
266  {
268 
269  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
270 
271  assign( ~lhs, rhs.sv_ );
272  }
274  //**********************************************************************************************
275 
276  //**Addition assignment to dense vectors********************************************************
288  template< typename VT2 > // Type of the target dense vector
289  friend inline void addAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
290  {
292 
293  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
294 
295  addAssign( ~lhs, rhs.sv_ );
296  }
298  //**********************************************************************************************
299 
300  //**Addition assignment to sparse vectors*******************************************************
312  template< typename VT2 > // Type of the target sparse vector
313  friend inline void addAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
314  {
316 
317  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
318 
319  addAssign( ~lhs, rhs.sv_ );
320  }
322  //**********************************************************************************************
323 
324  //**Subtraction assignment to dense vectors*****************************************************
337  template< typename VT2 > // Type of the target dense vector
338  friend inline void subAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
339  {
341 
342  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
343 
344  subAssign( ~lhs, rhs.sv_ );
345  }
347  //**********************************************************************************************
348 
349  //**Subtraction assignment to sparse vectors****************************************************
362  template< typename VT2 > // Type of the target sparse vector
363  friend inline void subAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
364  {
366 
367  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
368 
369  subAssign( ~lhs, rhs.sv_ );
370  }
372  //**********************************************************************************************
373 
374  //**Multiplication assignment to dense vectors**************************************************
387  template< typename VT2 > // Type of the target dense vector
388  friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
389  {
391 
392  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
393 
394  multAssign( ~lhs, rhs.sv_ );
395  }
397  //**********************************************************************************************
398 
399  //**Multiplication assignment to sparse vectors*************************************************
412  template< typename VT2 > // Type of the target sparse vector
413  friend inline void multAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
414  {
416 
417  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
418 
419  multAssign( ~lhs, rhs.sv_ );
420  }
422  //**********************************************************************************************
423 
424  //**SMP assignment to dense vectors*************************************************************
436  template< typename VT2 > // Type of the target dense vector
437  friend inline void smpAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
438  {
440 
441  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
442 
443  assign( ~lhs, rhs.sv_ );
444  }
446  //**********************************************************************************************
447 
448  //**SMP assignment to sparse vectors************************************************************
460  template< typename VT2 > // Type of the target sparse vector
461  friend inline void smpAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
462  {
464 
465  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
466 
467  assign( ~lhs, rhs.sv_ );
468  }
470  //**********************************************************************************************
471 
472  //**SMP addition assignment to dense vectors****************************************************
485  template< typename VT2 > // Type of the target dense vector
486  friend inline void smpAddAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
487  {
489 
490  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
491 
492  addAssign( ~lhs, rhs.sv_ );
493  }
495  //**********************************************************************************************
496 
497  //**SMP addition assignment to sparse vectors***************************************************
510  template< typename VT2 > // Type of the target sparse vector
511  friend inline void smpAddAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
512  {
514 
515  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
516 
517  addAssign( ~lhs, rhs.sv_ );
518  }
520  //**********************************************************************************************
521 
522  //**SMP subtraction assignment to dense vectors*************************************************
535  template< typename VT2 > // Type of the target dense vector
536  friend inline void smpSubAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
537  {
539 
540  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
541 
542  subAssign( ~lhs, rhs.sv_ );
543  }
545  //**********************************************************************************************
546 
547  //**SMP subtraction assignment to sparse vectors************************************************
560  template< typename VT2 > // Type of the target sparse vector
561  friend inline void smpSubAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
562  {
564 
565  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
566 
567  subAssign( ~lhs, rhs.sv_ );
568  }
570  //**********************************************************************************************
571 
572  //**SMP multiplication assignment to dense vectors**********************************************
585  template< typename VT2 > // Type of the target dense vector
586  friend inline void smpMultAssign( DenseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
587  {
589 
590  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
591 
592  multAssign( ~lhs, rhs.sv_ );
593  }
595  //**********************************************************************************************
596 
597  //**SMP multiplication assignment to sparse vectors*********************************************
610  template< typename VT2 > // Type of the target sparse vector
611  friend inline void smpMultAssign( SparseVector<VT2,TF>& lhs, const SVecSerialExpr& rhs )
612  {
614 
615  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
616 
617  multAssign( ~lhs, rhs.sv_ );
618  }
620  //**********************************************************************************************
621 
622  //**Compile time checks*************************************************************************
627  //**********************************************************************************************
628 };
629 //*************************************************************************************************
630 
631 
632 
633 
634 //=================================================================================================
635 //
636 // GLOBAL FUNCTIONS
637 //
638 //=================================================================================================
639 
640 //*************************************************************************************************
657 template< typename VT // Type of the dense vector
658  , bool TF > // Transpose flag
660 {
662 
663  return SVecSerialExpr<VT,TF>( ~sv );
664 }
665 //*************************************************************************************************
666 
667 
668 
669 
670 //=================================================================================================
671 //
672 // GLOBAL RESTRUCTURING FUNCTIONS
673 //
674 //=================================================================================================
675 
676 //*************************************************************************************************
687 template< typename VT // Type of the sparse vector
688  , bool TF > // Transpose flag
689 inline const SVecSerialExpr<VT,TF> serial( const SVecSerialExpr<VT,TF>& sv )
690 {
691  return sv;
692 }
694 //*************************************************************************************************
695 
696 
697 
698 
699 //=================================================================================================
700 //
701 // SIZE SPECIALIZATIONS
702 //
703 //=================================================================================================
704 
705 //*************************************************************************************************
707 template< typename VT, bool TF >
708 struct Size< SVecSerialExpr<VT,TF> > : public Size<VT>
709 {};
711 //*************************************************************************************************
712 
713 
714 
715 
716 //=================================================================================================
717 //
718 // EXPRESSION TRAIT SPECIALIZATIONS
719 //
720 //=================================================================================================
721 
722 //*************************************************************************************************
724 template< typename VT >
725 struct SVecSerialExprTrait< SVecSerialExpr<VT,false> >
726 {
727  public:
728  //**********************************************************************************************
729  using Type = If_< And< IsSparseVector<VT>, IsColumnVector<VT> >
730  , SVecSerialExpr<VT,false>
731  , INVALID_TYPE >;
732  //**********************************************************************************************
733 };
735 //*************************************************************************************************
736 
737 
738 //*************************************************************************************************
740 template< typename VT >
741 struct TSVecSerialExprTrait< SVecSerialExpr<VT,true> >
742 {
743  public:
744  //**********************************************************************************************
745  using Type = If_< And< IsSparseVector<VT>, IsRowVector<VT> >
746  , SVecSerialExpr<VT,true>
747  , INVALID_TYPE >;
748  //**********************************************************************************************
749 };
751 //*************************************************************************************************
752 
753 
754 //*************************************************************************************************
756 template< typename VT, bool TF, bool AF >
757 struct SubvectorExprTrait< SVecSerialExpr<VT,TF>, AF >
758 {
759  public:
760  //**********************************************************************************************
761  using Type = SerialExprTrait_< SubvectorExprTrait_<const VT,AF> >;
762  //**********************************************************************************************
763 };
765 //*************************************************************************************************
766 
767 } // namespace blaze
768 
769 #endif
Header file for auxiliary alias declarations.
SVecSerialExpr< VT, TF > This
Type of this SVecSerialExpr instance.
Definition: SVecSerialExpr.h:94
Header file for basic type definitions.
Header file for the SparseVector base class.
If_< IsExpression< VT >, const VT, const VT & > Operand
Composite data type of the sparse vector expression.
Definition: SVecSerialExpr.h:104
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
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecSerialExpr.h:174
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
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecSerialExpr.h:141
Header file for the And class template.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSerialExpr.h:128
const DMatSerialExpr< MT, SO > serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:723
Header file for the Computation base class.
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSerialExpr.h:154
SVecSerialExpr(const VT &sv) noexcept
Constructor for the SVecSerialExpr class.
Definition: SVecSerialExpr.h:117
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
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
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSerialExpr.h:101
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the If class template.
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecSerialExpr.h:208
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
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
Header file for the SerialExprTrait class template.
Constraint on the data type.
Header file for the SVecSerialExprTrait class template.
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:119
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecSerialExpr.h:98
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSerialExpr.h:164
Header file for the VecSerialExpr base class.
Header file for the IsSparseVector type trait.
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecSerialExpr.h:196
Header file for run time assertion macros.
Utility type for generic codes.
ResultType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecSerialExpr.h:95
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 TSVecSerialExprTrait class template.
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecSerialExpr.h:97
Operand sv_
Sparse vector of the serial evaluation expression.
Definition: SVecSerialExpr.h:225
TransposeType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSerialExpr.h:96
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
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 IsColumnVector type trait.
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
bool canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SVecSerialExpr.h:218
#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.