SVecTransExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECTRANSEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECTRANSEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <iterator>
44 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
58 #include <blaze/util/Assert.h>
59 #include <blaze/util/EnableIf.h>
61 #include <blaze/util/InvalidType.h>
62 #include <blaze/util/mpl/If.h>
63 #include <blaze/util/Types.h>
66 
67 
68 namespace blaze {
69 
70 //=================================================================================================
71 //
72 // CLASS SVECTRANSEXPR
73 //
74 //=================================================================================================
75 
76 //*************************************************************************************************
83 template< typename VT // Type of the sparse vector
84  , bool TF > // Transpose flag
85 class SVecTransExpr
86  : public VecTransExpr< SparseVector< SVecTransExpr<VT,TF>, TF > >
87  , private If< IsComputation<VT>, Computation, Transformation >::Type
88 {
89  private:
90  //**Type definitions****************************************************************************
92  //**********************************************************************************************
93 
94  //**Serial evaluation strategy******************************************************************
96 
102  enum : bool { useAssign = RequiresEvaluation<VT>::value };
103 
105  template< typename VT2 >
107  struct UseAssign {
108  enum : bool { value = useAssign };
109  };
111  //**********************************************************************************************
112 
113  //**Parallel evaluation strategy****************************************************************
115 
120  template< typename VT2 >
121  struct UseSMPAssign {
122  enum : bool { value = VT2::smpAssignable && useAssign };
123  };
125  //**********************************************************************************************
126 
127  //**********************************************************************************************
129 
133  template< typename VT2 >
134  struct GetConstIterator {
136  struct Success { using Type = typename VT2::ConstIterator; };
137  struct Failure { using Type = INVALID_TYPE; };
138  using Type = typename If_< HasConstIterator<VT2>, Success, Failure >::Type;
139  };
141  //**********************************************************************************************
142 
143  public:
144  //**Type definitions****************************************************************************
150 
153 
155  using ConstIterator = typename GetConstIterator<VT>::Type;
156 
158  using Operand = If_< IsExpression<VT>, const VT, const VT& >;
159  //**********************************************************************************************
160 
161  //**Compilation flags***************************************************************************
163  enum : bool { smpAssignable = VT::smpAssignable };
164  //**********************************************************************************************
165 
166  //**Constructor*********************************************************************************
171  explicit inline SVecTransExpr( const VT& sv ) noexcept
172  : sv_( sv ) // Sparse vector of the transposition expression
173  {}
174  //**********************************************************************************************
175 
176  //**Subscript operator**************************************************************************
182  inline ReturnType operator[]( size_t index ) const {
183  BLAZE_INTERNAL_ASSERT( index < sv_.size(), "Invalid vector access index" );
184  return sv_[index];
185  }
186  //**********************************************************************************************
187 
188  //**At function*********************************************************************************
195  inline ReturnType at( size_t index ) const {
196  if( index >= sv_.size() ) {
197  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
198  }
199  return (*this)[index];
200  }
201  //**********************************************************************************************
202 
203  //**Begin function******************************************************************************
208  inline ConstIterator begin() const {
209  return ConstIterator( sv_.begin() );
210  }
211  //**********************************************************************************************
212 
213  //**End function********************************************************************************
218  inline ConstIterator end() const {
219  return ConstIterator( sv_.end() );
220  }
221  //**********************************************************************************************
222 
223  //**Size function*******************************************************************************
228  inline size_t size() const noexcept {
229  return sv_.size();
230  }
231  //**********************************************************************************************
232 
233  //**NonZeros function***************************************************************************
238  inline size_t nonZeros() const {
239  return sv_.nonZeros();
240  }
241  //**********************************************************************************************
242 
243  //**Find function*******************************************************************************
249  inline ConstIterator find( size_t index ) const {
251  return ConstIterator( sv_.find( index ) );
252  }
253  //**********************************************************************************************
254 
255  //**LowerBound function*************************************************************************
261  inline ConstIterator lowerBound( size_t index ) const {
263  return ConstIterator( sv_.lowerBound( index ) );
264  }
265  //**********************************************************************************************
266 
267  //**UpperBound function*************************************************************************
273  inline ConstIterator upperBound( size_t index ) const {
275  return ConstIterator( sv_.upperBound( index ) );
276  }
277  //**********************************************************************************************
278 
279  //**Operand access******************************************************************************
284  inline Operand operand() const noexcept {
285  return sv_;
286  }
287  //**********************************************************************************************
288 
289  //**********************************************************************************************
295  template< typename T >
296  inline bool canAlias( const T* alias ) const noexcept {
297  return sv_.canAlias( alias );
298  }
299  //**********************************************************************************************
300 
301  //**********************************************************************************************
307  template< typename T >
308  inline bool isAliased( const T* alias ) const noexcept {
309  return sv_.isAliased( alias );
310  }
311  //**********************************************************************************************
312 
313  //**********************************************************************************************
318  inline bool canSMPAssign() const noexcept {
319  return sv_.canSMPAssign();
320  }
321  //**********************************************************************************************
322 
323  private:
324  //**Member variables****************************************************************************
326  //**********************************************************************************************
327 
328  //**Assignment to dense vectors*****************************************************************
342  template< typename VT2 > // Type of the target dense vector
343  friend inline EnableIf_< UseAssign<VT2> >
344  assign( DenseVector<VT2,TF>& lhs, const SVecTransExpr& rhs )
345  {
347 
348  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
349 
350  DVecTransposer<VT2,!TF> tmp( ~lhs );
351  assign( tmp, rhs.sv_ );
352  }
354  //**********************************************************************************************
355 
356  //**Assignment to sparse vectors****************************************************************
370  template< typename VT2 > // Type of the target sparse vector
371  friend inline EnableIf_< UseAssign<VT2> >
372  assign( SparseVector<VT2,TF>& lhs, const SVecTransExpr& rhs )
373  {
375 
376  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
377 
378  SVecTransposer<VT2,!TF> tmp( ~lhs );
379  assign( tmp, rhs.sv_ );
380  }
382  //**********************************************************************************************
383 
384  //**Addition assignment to dense vectors********************************************************
398  template< typename VT2 > // Type of the target dense vector
399  friend inline EnableIf_< UseAssign<VT2> >
400  addAssign( DenseVector<VT2,TF>& lhs, const SVecTransExpr& rhs )
401  {
403 
404  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
405 
406  DVecTransposer<VT2,!TF> tmp( ~lhs );
407  addAssign( tmp, rhs.sv_ );
408  }
410  //**********************************************************************************************
411 
412  //**Addition assignment to sparse vectors*******************************************************
413  // No special implementation for the addition assignment to sparse vectors.
414  //**********************************************************************************************
415 
416  //**Subtraction assignment to dense vectors*****************************************************
430  template< typename VT2 > // Type of the target dense vector
431  friend inline EnableIf_< UseAssign<VT2> >
432  subAssign( DenseVector<VT2,TF>& lhs, const SVecTransExpr& rhs )
433  {
435 
436  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
437 
438  DVecTransposer<VT2,!TF> tmp( ~lhs );
439  subAssign( tmp, rhs.sv_ );
440  }
442  //**********************************************************************************************
443 
444  //**Subtraction assignment to sparse vectors****************************************************
445  // No special implementation for the subtraction assignment to sparse vectors.
446  //**********************************************************************************************
447 
448  //**Multiplication assignment to dense vectors**************************************************
462  template< typename VT2 > // Type of the target dense vector
463  friend inline EnableIf_< UseAssign<VT2> >
464  multAssign( DenseVector<VT2,TF>& lhs, const SVecTransExpr& rhs )
465  {
467 
468  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
469 
470  DVecTransposer<VT2,!TF> tmp( ~lhs );
471  multAssign( tmp, rhs.sv_ );
472  }
474  //**********************************************************************************************
475 
476  //**Multiplication assignment to sparse vectors*************************************************
477  // No special implementation for the multiplication assignment to sparse vectors.
478  //**********************************************************************************************
479 
480  //**SMP assignment to dense vectors*************************************************************
494  template< typename VT2 > // Type of the target dense vector
495  friend inline EnableIf_< UseSMPAssign<VT2> >
496  smpAssign( DenseVector<VT2,TF>& lhs, const SVecTransExpr& rhs )
497  {
499 
500  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
501 
502  DVecTransposer<VT2,!TF> tmp( ~lhs );
503  smpAssign( tmp, rhs.sv_ );
504  }
506  //**********************************************************************************************
507 
508  //**SMP assignment to sparse vectors************************************************************
522  template< typename VT2 > // Type of the target sparse vector
523  friend inline EnableIf_< UseSMPAssign<VT2> >
524  smpAssign( SparseVector<VT2,TF>& lhs, const SVecTransExpr& rhs )
525  {
527 
528  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
529 
530  SVecTransposer<VT2,!TF> tmp( ~lhs );
531  smpAssign( tmp, rhs.sv_ );
532  }
534  //**********************************************************************************************
535 
536  //**SMP addition assignment to dense vectors****************************************************
550  template< typename VT2 > // Type of the target dense vector
551  friend inline EnableIf_< UseSMPAssign<VT2> >
553  {
555 
556  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
557 
558  DVecTransposer<VT2,!TF> tmp( ~lhs );
559  smpAddAssign( tmp, rhs.sv_ );
560  }
562  //**********************************************************************************************
563 
564  //**SMP addition assignment to sparse vectors***************************************************
565  // No special implementation for the SMP addition assignment to sparse vectors.
566  //**********************************************************************************************
567 
568  //**SMP subtraction assignment to dense vectors*************************************************
582  template< typename VT2 > // Type of the target dense vector
583  friend inline EnableIf_< UseSMPAssign<VT2> >
585  {
587 
588  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
589 
590  DVecTransposer<VT2,!TF> tmp( ~lhs );
591  smpSubAssign( tmp, rhs.sv_ );
592  }
594  //**********************************************************************************************
595 
596  //**SMP subtraction assignment to sparse vectors************************************************
597  // No special implementation for the SMP subtraction assignment to sparse vectors.
598  //**********************************************************************************************
599 
600  //**SMP multiplication assignment to dense vectors**********************************************
615  template< typename VT2 > // Type of the target dense vector
616  friend inline EnableIf_< UseSMPAssign<VT2> >
618  {
620 
621  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
622 
623  DVecTransposer<VT2,!TF> tmp( ~lhs );
624  smpMultAssign( tmp, rhs.sv_ );
625  }
627  //**********************************************************************************************
628 
629  //**SMP multiplication assignment to sparse vectors*********************************************
630  // No special implementation for the SMP multiplication assignment to sparse vectors.
631  //**********************************************************************************************
632 
633  //**Compile time checks*************************************************************************
637  //**********************************************************************************************
638 };
639 //*************************************************************************************************
640 
641 
642 
643 
644 //=================================================================================================
645 //
646 // GLOBAL OPERATORS
647 //
648 //=================================================================================================
649 
650 //*************************************************************************************************
669 template< typename VT // Type of the sparse vector
670  , bool TF > // Transpose flag
671 inline decltype(auto) trans( const SparseVector<VT,TF>& sv )
672 {
674 
675  using ReturnType = const SVecTransExpr<VT,!TF>;
676  return ReturnType( ~sv );
677 }
678 //*************************************************************************************************
679 
680 
681 
682 
683 //=================================================================================================
684 //
685 // GLOBAL RESTRUCTURING FUNCTIONS
686 //
687 //=================================================================================================
688 
689 //*************************************************************************************************
709 template< typename VT // Type of the sparse vector
710  , bool TF > // Transpose flag
711 inline decltype(auto) trans( const SVecTransExpr<VT,TF>& sv )
712 {
714 
715  return sv.operand();
716 }
718 //*************************************************************************************************
719 
720 
721 //*************************************************************************************************
733 template< typename VT // Type of the left-hand side sparse vector
734  , typename ST // Type of the right-hand side scalar value
735  , bool TF > // Transpose flag
736 inline decltype(auto) trans( const SVecScalarMultExpr<VT,ST,TF>& sv )
737 {
739 
740  return trans( sv.leftOperand() ) * sv.rightOperand();
741 }
743 //*************************************************************************************************
744 
745 
746 
747 
748 //=================================================================================================
749 //
750 // SIZE SPECIALIZATIONS
751 //
752 //=================================================================================================
753 
754 //*************************************************************************************************
756 template< typename VT, bool TF >
757 struct Size< SVecTransExpr<VT,TF> >
758  : public Size<VT>
759 {};
761 //*************************************************************************************************
762 
763 } // namespace blaze
764 
765 #endif
RightOperand rightOperand() const noexcept
Returns the right-hand side scalar operand.
Definition: SVecScalarMultExpr.h:438
ConstIterator lowerBound(size_t index) const
Returns an iterator to the first index not less then the given index.
Definition: SVecTransExpr.h:261
Header file for auxiliary alias declarations.
Operand sv_
Sparse vector of the transposition expression.
Definition: SVecTransExpr.h:325
Header file for basic type definitions.
Header file for the SparseVector base class.
bool canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SVecTransExpr.h:318
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
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecTransExpr.h:182
TransposeType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecTransExpr.h:146
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
#define BLAZE_CREATE_HAS_TYPE_MEMBER_TYPE_TRAIT(TYPE_TRAIT_NAME, MEMBER_NAME)
Macro for the creation of a type trait for compile time checks for member types.This macro creates th...
Definition: HasMember.h:182
Expression object for sparse vector transpositions.The SVecTransExpr class represents the compile tim...
Definition: Forward.h:146
Header file for the Computation base class.
ResultType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecTransExpr.h:147
Header file for the RequiresEvaluation type trait.
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:343
ConstIterator upperBound(size_t index) const
Returns an iterator to the first index greater then the given index.
Definition: SVecTransExpr.h:273
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
Expression object for the transposition of a dense vector.The DVecTransposer class is a wrapper objec...
Definition: DVecTransposer.h:77
typename IfTrue< Condition, T1, T2 >::Type IfTrue_
Auxiliary alias declaration for the IfTrue class template.The IfTrue_ alias declaration provides a co...
Definition: If.h:109
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:363
Compile time check to query the requirement to evaluate an expression.Via this type trait it is possi...
Definition: RequiresEvaluation.h:72
typename T::CompositeType CompositeType_
Alias declaration for nested CompositeType type definitions.The CompositeType_ alias declaration prov...
Definition: Aliases.h:83
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the If class template.
Header file for the dense vector transposer.
Header file for the Transformation base class.
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
CompositeType_< VT > CT
Composite type of the sparse vector expression.
Definition: SVecTransExpr.h:91
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3087
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
Expression object for the transposition of a sparse vector.The SVecTransposer class is a wrapper obje...
Definition: Forward.h:147
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
ConstIterator begin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransExpr.h:208
Header file for the exception macros of the math module.
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecTransExpr.h:284
Constraint on the data type.
Header file for all forward declarations for expression class templates.
Constraint on the data type.
Header file for the EnableIf class template.
typename GetConstIterator< VT >::Type ConstIterator
Iterator over the elements of the dense vector.
Definition: SVecTransExpr.h:155
ConstIterator find(size_t index) const
Searches for a specific vector element.
Definition: SVecTransExpr.h:249
Header file for the VecTransExpr base class.
Header file for run time assertion macros.
SVecTransExpr(const VT &sv) noexcept
Constructor for the SVecTransExpr class.
Definition: SVecTransExpr.h:171
If_< IsExpression< VT >, const VT, const VT &> Operand
Composite data type of the sparse vector expression.
Definition: SVecTransExpr.h:158
Utility type for generic codes.
IfTrue_< useAssign, const ResultType, const SVecTransExpr &> CompositeType
Data type for composite expression templates.
Definition: SVecTransExpr.h:152
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
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecTransExpr.h:148
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecTransExpr.h:238
Header file for the HasMember type traits.
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.In case the given data type T requires an intermediate evaluation within ...
Definition: RequiresEvaluation.h:81
Header file for the RemoveReference type trait.
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:224
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:3082
decltype(auto) trans(const DenseMatrix< MT, SO > &dm)
Calculation of the transpose of the given dense matrix.
Definition: DMatTransExpr.h:790
Header file for the sparse vector transposer.
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecTransExpr.h:228
Expression object for sparse vector-scalar multiplications.The SVecScalarMultExpr class represents th...
Definition: Forward.h:139
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
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:423
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecTransExpr.h:195
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecTransExpr.h:308
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecTransExpr.h:149
Header file for the Size type trait.
#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
LeftOperand leftOperand() const noexcept
Returns the left-hand side sparse vector operand.
Definition: SVecScalarMultExpr.h:428
Header file for the IsExpression type trait class.
Header file for the function trace functionality.
ConstIterator end() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransExpr.h:218
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecTransExpr.h:296