Blaze  3.6
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>
53 #include <blaze/util/Assert.h>
55 #include <blaze/util/mpl/If.h>
56 #include <blaze/util/Types.h>
57 
58 
59 namespace blaze {
60 
61 //=================================================================================================
62 //
63 // CLASS SVECSERIALEXPR
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
74 template< typename VT // Type of the sparse vector
75  , bool TF > // Transpose flag
76 class SVecSerialExpr
77  : public VecSerialExpr< SparseVector< SVecSerialExpr<VT,TF>, TF > >
78  , private Computation
79 {
80  public:
81  //**Type definitions****************************************************************************
88 
90  using CompositeType = const ResultType;
91 
93  using Operand = If_t< IsExpression_v<VT>, const VT, const VT& >;
94  //**********************************************************************************************
95 
96  //**Compilation flags***************************************************************************
98  static constexpr 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 } // namespace blaze
687 
688 #endif
Header file for auxiliary alias declarations.
Header file for basic type definitions.
Header file for the SparseVector base class.
typename If< Condition, T1, T2 >::Type If_t
Auxiliary alias template for the If class template.The If_t alias template provides a convenient shor...
Definition: If.h:109
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSerialExpr.h:90
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.The ResultType_t alias declaration provides ...
Definition: Aliases.h:390
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::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.The ReturnType_t alias declaration provides ...
Definition: Aliases.h:410
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSerialExpr.h:153
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
#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
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
Header file for the exception macros of the math module.
Constraint on the data type.
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SVecSerialExpr.h:98
Expression object for the forced serial evaluation of sparse vectors.The SVecSerialExpr class represe...
Definition: Forward.h:158
ReturnType_t< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecSerialExpr.h:87
Header file for the VecSerialExpr base class.
TransposeType_t< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSerialExpr.h:85
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.The TransposeType_t alias declaration pro...
Definition: Aliases.h:470
Header file for run time assertion macros.
auto smpAddAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP addition assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:131
#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
Header file for all forward declarations for expression class templates.
auto smpAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:100
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:808
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
If_t< IsExpression_v< VT >, const VT, const VT & > Operand
Composite data type of the sparse vector expression.
Definition: SVecSerialExpr.h:93
Header file for the IsComputation type trait class.
auto smpSubAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP subtraction assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:162
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:146
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSerialExpr.h:143
ResultType_t< VT > ResultType
Result type for expression template evaluations.
Definition: SVecSerialExpr.h:84
ElementType_t< VT > ElementType
Resulting element type.
Definition: SVecSerialExpr.h:86
#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,...
Definition: Assert.h:101
auto smpMultAssign(Vector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs) -> EnableIf_t< IsDenseVector_v< VT1 > >
Default implementation of the SMP multiplication assignment of a vector to a dense vector.
Definition: DenseVector.h:191
Header file for the IsExpression type trait class.
Header file for the function trace functionality.