All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DVecSVecAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECSVECADDEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_DVECSVECADDEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
62 #include <blaze/util/Assert.h>
65 #include <blaze/util/SelectType.h>
66 #include <blaze/util/Types.h>
67 
68 
69 namespace blaze {
70 
71 //=================================================================================================
72 //
73 // CLASS DVECSVECADDEXPR
74 //
75 //=================================================================================================
76 
77 //*************************************************************************************************
84 template< typename VT1 // Type of the left-hand side dense vector
85  , typename VT2 // Type of the right-hand side sparse vector
86  , bool TF > // Transpose flag
87 class DVecSVecAddExpr : public DenseVector< DVecSVecAddExpr<VT1,VT2,TF>, TF >
88  , private VecVecAddExpr
89  , private Computation
90 {
91  private:
92  //**Type definitions****************************************************************************
93  typedef typename VT1::ResultType RT1;
94  typedef typename VT2::ResultType RT2;
95  typedef typename VT1::ReturnType RN1;
96  typedef typename VT2::ReturnType RN2;
97  typedef typename VT1::CompositeType CT1;
98  typedef typename VT2::CompositeType CT2;
99  typedef typename VT1::TransposeType TT1;
100  typedef typename VT2::TransposeType TT2;
101  //**********************************************************************************************
102 
103  //**Return type evaluation**********************************************************************
105 
110  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
111 
114  //**********************************************************************************************
115 
116  public:
117  //**Type definitions****************************************************************************
122 
125 
127  typedef const ResultType CompositeType;
128 
130  typedef typename SelectType< IsExpression<VT1>::value, const VT1, const VT1& >::Type LeftOperand;
131 
133  typedef typename SelectType< IsExpression<VT2>::value, const VT2, const VT2& >::Type RightOperand;
134  //**********************************************************************************************
135 
136  //**Compilation flags***************************************************************************
138  enum { vectorizable = 0 };
139 
141  enum { smpAssignable = 0 };
142  //**********************************************************************************************
143 
144  //**Constructor*********************************************************************************
150  explicit inline DVecSVecAddExpr( const VT1& lhs, const VT2& rhs )
151  : lhs_( lhs ) // Left-hand side dense vector of the addition expression
152  , rhs_( rhs ) // Right-hand side sparse vector of the addition expression
153  {
154  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
155  }
156  //**********************************************************************************************
157 
158  //**Subscript operator**************************************************************************
164  inline ReturnType operator[]( size_t index ) const {
165  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
166  return lhs_[index] + rhs_[index];
167  }
168  //**********************************************************************************************
169 
170  //**Size function*******************************************************************************
175  inline size_t size() const {
176  return lhs_.size();
177  }
178  //**********************************************************************************************
179 
180  //**Left operand access*************************************************************************
185  inline LeftOperand leftOperand() const {
186  return lhs_;
187  }
188  //**********************************************************************************************
189 
190  //**Right operand access************************************************************************
195  inline RightOperand rightOperand() const {
196  return rhs_;
197  }
198  //**********************************************************************************************
199 
200  //**********************************************************************************************
206  template< typename T >
207  inline bool canAlias( const T* alias ) const {
208  return ( IsComputation<VT1>::value && lhs_.canAlias( alias ) ) ||
209  ( rhs_.canAlias( alias ) );
210  }
211  //**********************************************************************************************
212 
213  //**********************************************************************************************
219  template< typename T >
220  inline bool isAliased( const T* alias ) const {
221  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
222  }
223  //**********************************************************************************************
224 
225  private:
226  //**Member variables****************************************************************************
229  //**********************************************************************************************
230 
231  //**Assignment to dense vectors*****************************************************************
243  template< typename VT > // Type of the target dense vector
244  friend inline void assign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
245  {
247 
248  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
249 
250  if( !IsComputation<VT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
251  addAssign( ~lhs, rhs.rhs_ );
252  }
253  else {
254  assign ( ~lhs, rhs.lhs_ );
255  addAssign( ~lhs, rhs.rhs_ );
256  }
257  }
259  //**********************************************************************************************
260 
261  //**Assignment to sparse vectors****************************************************************
273  template< typename VT > // Type of the target sparse vector
274  friend inline void assign( SparseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
275  {
277 
281 
282  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
283 
284  const ResultType tmp( rhs );
285  assign( ~lhs, tmp );
286  }
288  //**********************************************************************************************
289 
290  //**Addition assignment to dense vectors********************************************************
302  template< typename VT > // Type of the target dense vector
303  friend inline void addAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
304  {
306 
307  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
308 
309  addAssign( ~lhs, rhs.lhs_ );
310  addAssign( ~lhs, rhs.rhs_ );
311  }
313  //**********************************************************************************************
314 
315  //**Addition assignment to sparse vectors*******************************************************
316  // No special implementation for the addition assignment to sparse vectors.
317  //**********************************************************************************************
318 
319  //**Subtraction assignment to dense vectors*****************************************************
331  template< typename VT > // Type of the target dense vector
332  friend inline void subAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
333  {
335 
336  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
337 
338  subAssign( ~lhs, rhs.lhs_ );
339  subAssign( ~lhs, rhs.rhs_ );
340  }
342  //**********************************************************************************************
343 
344  //**Subtraction assignment to sparse vectors****************************************************
345  // No special implementation for the subtraction assignment to sparse vectors.
346  //**********************************************************************************************
347 
348  //**Multiplication assignment to dense vectors**************************************************
360  template< typename VT > // Type of the target dense vector
361  friend inline void multAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
362  {
364 
368 
369  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
370 
371  const ResultType tmp( rhs );
372  multAssign( ~lhs, tmp );
373  }
375  //**********************************************************************************************
376 
377  //**Multiplication assignment to sparse vectors*************************************************
378  // No special implementation for the multiplication assignment to sparse vectors.
379  //**********************************************************************************************
380 
381  //**Compile time checks*************************************************************************
388  //**********************************************************************************************
389 };
390 //*************************************************************************************************
391 
392 
393 
394 
395 //=================================================================================================
396 //
397 // GLOBAL BINARY ARITHMETIC OPERATORS
398 //
399 //=================================================================================================
400 
401 //*************************************************************************************************
427 template< typename T1 // Type of the left-hand side dense vector
428  , typename T2 // Type of the right-hand side sparse vector
429  , bool TF > // Transpose flag
430 inline const DVecSVecAddExpr<T1,T2,TF>
432 {
434 
435  if( (~lhs).size() != (~rhs).size() )
436  throw std::invalid_argument( "Vector sizes do not match" );
437 
438  return DVecSVecAddExpr<T1,T2,TF>( ~lhs, ~rhs );
439 }
440 //*************************************************************************************************
441 
442 
443 //*************************************************************************************************
469 template< typename T1 // Type of the left-hand side sparse vector
470  , typename T2 // Type of the right-hand side dense vector
471  , bool TF > // Transpose flag
472 inline const DVecSVecAddExpr<T2,T1,TF>
474 {
476 
477  if( (~lhs).size() != (~rhs).size() )
478  throw std::invalid_argument( "Vector sizes do not match" );
479 
480  return DVecSVecAddExpr<T2,T1,TF>( ~rhs, ~lhs );
481 }
482 //*************************************************************************************************
483 
484 
485 
486 
487 //=================================================================================================
488 //
489 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
490 //
491 //=================================================================================================
492 
493 //*************************************************************************************************
506 template< typename T1 // Type of the dense vector of the left-hand side expression
507  , typename T2 // Type of the sparse vector of the left-hand side expression
508  , bool TF // Transpose flag of the left-hand side expression
509  , typename T3 > // Type of the right-hand side dense vector
510 inline const typename AddExprTrait< DVecSVecAddExpr<T1,T2,TF>, T3 >::Type
511  operator+( const DVecSVecAddExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
512 {
514 
515  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
516 }
518 //*************************************************************************************************
519 
520 
521 //*************************************************************************************************
534 template< typename T1 // Type of the dense vector of the left-hand side expression
535  , typename T2 // Type of the sparse vector of the left-hand side expression
536  , bool TF // Transpose flag of the left-hand side expression
537  , typename T3 > // Type of the right-hand side dense vector
538 inline const typename SubExprTrait< DVecSVecAddExpr<T1,T2,TF>, T3 >::Type
539  operator-( const DVecSVecAddExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
540 {
542 
543  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
544 }
546 //*************************************************************************************************
547 
548 
549 
550 
551 //=================================================================================================
552 //
553 // EXPRESSION TRAIT SPECIALIZATIONS
554 //
555 //=================================================================================================
556 
557 //*************************************************************************************************
559 template< typename VT1, typename VT2, typename VT3 >
560 struct DVecDVecAddExprTrait< DVecSVecAddExpr<VT1,VT2,false>, VT3 >
561 {
562  public:
563  //**********************************************************************************************
565  typedef typename SelectType< IsDenseVector<VT1>::value && IsColumnVector<VT1>::value &&
566  IsSparseVector<VT2>::value && IsColumnVector<VT2>::value &&
567  IsDenseVector<VT3>::value && IsColumnVector<VT3>::value
568  , typename DVecSVecAddExprTrait< typename DVecDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
569  , INVALID_TYPE >::Type Type;
571  //**********************************************************************************************
572 };
574 //*************************************************************************************************
575 
576 
577 //*************************************************************************************************
579 template< typename VT1, typename VT2, typename VT3 >
580 struct TDVecTDVecAddExprTrait< DVecSVecAddExpr<VT1,VT2,true>, VT3 >
581 {
582  public:
583  //**********************************************************************************************
585  typedef typename SelectType< IsDenseVector<VT1>::value && IsRowVector<VT1>::value &&
586  IsSparseVector<VT2>::value && IsRowVector<VT2>::value &&
587  IsDenseVector<VT3>::value && IsRowVector<VT3>::value
588  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
589  , INVALID_TYPE >::Type Type;
591  //**********************************************************************************************
592 };
594 //*************************************************************************************************
595 
596 
597 //*************************************************************************************************
599 template< typename VT1, typename VT2, typename VT3 >
600 struct DVecDVecSubExprTrait< DVecSVecAddExpr<VT1,VT2,false>, VT3 >
601 {
602  public:
603  //**********************************************************************************************
605  typedef typename SelectType< IsDenseVector<VT1>::value && IsColumnVector<VT1>::value &&
606  IsSparseVector<VT2>::value && IsColumnVector<VT2>::value &&
607  IsDenseVector<VT3>::value && IsColumnVector<VT3>::value
608  , typename DVecSVecAddExprTrait< typename DVecDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
609  , INVALID_TYPE >::Type Type;
611  //**********************************************************************************************
612 };
614 //*************************************************************************************************
615 
616 
617 //*************************************************************************************************
619 template< typename VT1, typename VT2, typename VT3 >
620 struct TDVecTDVecSubExprTrait< DVecSVecAddExpr<VT1,VT2,true>, VT3 >
621 {
622  public:
623  //**********************************************************************************************
625  typedef typename SelectType< IsDenseVector<VT1>::value && IsRowVector<VT1>::value &&
626  IsSparseVector<VT2>::value && IsRowVector<VT2>::value &&
627  IsDenseVector<VT3>::value && IsRowVector<VT3>::value
628  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
629  , INVALID_TYPE >::Type Type;
631  //**********************************************************************************************
632 };
634 //*************************************************************************************************
635 
636 
637 //*************************************************************************************************
639 template< typename VT1, typename VT2, bool TF >
640 struct SubvectorExprTrait< DVecSVecAddExpr<VT1,VT2,TF> >
641 {
642  public:
643  //**********************************************************************************************
644  typedef typename AddExprTrait< typename SubvectorExprTrait<const VT1>::Type
645  , typename SubvectorExprTrait<const VT2>::Type >::Type Type;
646  //**********************************************************************************************
647 };
649 //*************************************************************************************************
650 
651 } // namespace blaze
652 
653 #endif
VT2::ResultType RT2
Result type of the right-hand side sparse vector expression.
Definition: DVecSVecAddExpr.h:94
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: DVecSVecAddExpr.h:220
Compile time check whether the given type is a computational expression template.This type trait clas...
Definition: IsComputation.h:89
RightOperand rhs_
Right-hand side sparse vector of the addition expression.
Definition: DVecSVecAddExpr.h:228
Evaluation of the return type of an addition expression.Via this type trait it is possible to evaluat...
Definition: AddExprTrait.h:103
Compile time check whether the given type is a temporary vector or matrix type.This type trait class ...
Definition: IsTemporary.h:87
VT2::ReturnType RN2
Return type of the right-hand side sparse vector expression.
Definition: DVecSVecAddExpr.h:96
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: DVecSVecAddExpr.h:120
VT1::ResultType RT1
Result type of the left-hand side dense vector expression.
Definition: DVecSVecAddExpr.h:93
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:196
VT1::CompositeType CT1
Composite type of the left-hand side dense vector expression.
Definition: DVecSVecAddExpr.h:97
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2375
Header file for the IsRowVector type trait.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:248
Header file for the DenseVector base class.
Header file for the AddExprTrait class template.
Header file for the Computation base class.
Expression object for dense vector-sparse vector additions.The DVecSVecAddExpr class represents the c...
Definition: DVecSVecAddExpr.h:87
RightOperand rightOperand() const
Returns the right-hand side sparse vector operand.
Definition: DVecSVecAddExpr.h:195
Constraint on the data type.
VT1::TransposeType TT1
Transpose type of the left-hand side dense vector expression.
Definition: DVecSVecAddExpr.h:99
LeftOperand leftOperand() const
Returns the left-hand side dense vector operand.
Definition: DVecSVecAddExpr.h:185
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:250
VT2::TransposeType TT2
Transpose type of the right-hand side sparse vector expression.
Definition: DVecSVecAddExpr.h:100
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
Header file for the IsTemporary type trait class.
Header file for the VecVecAddExpr base class.
DVecSVecAddExpr< VT1, VT2, TF > This
Type of this DVecSVecAddExpr instance.
Definition: DVecSVecAddExpr.h:118
ResultType::ElementType ElementType
Resulting element type.
Definition: DVecSVecAddExpr.h:121
const ResultType CompositeType
Data type for composite expression templates.
Definition: DVecSVecAddExpr.h:127
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:179
const DenseIterator< Type > operator+(const DenseIterator< Type > &it, ptrdiff_t inc)
Addition between a DenseIterator and an integral value.
Definition: DenseIterator.h:556
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:79
Constraint on the data type.
LeftOperand lhs_
Left-hand side dense vector of the addition expression.
Definition: DVecSVecAddExpr.h:227
#define BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:78
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2372
Constraint on the data type.
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: DVecSVecAddExpr.h:207
VT2::CompositeType CT2
Composite type of the right-hand side sparse vector expression.
Definition: DVecSVecAddExpr.h:98
void multAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the multiplication assignment of a matrix to a matrix.
Definition: Matrix.h:269
Header file for the SelectType class template.
Header file for all forward declarations for expression class templates.
Constraint on the data type.
Base class for all vector/vector addition expression templates.The VecVecAddExpr class serves as a ta...
Definition: VecVecAddExpr.h:65
Header file for the IsSparseVector type trait.
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2374
AddExprTrait< RN1, RN2 >::Type ExprReturnType
Expression return type for the subscript operator.
Definition: DVecSVecAddExpr.h:113
Header file for run time assertion macros.
Base template for the AddTrait class.
Definition: AddTrait.h:141
Header file for the addition trait.
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:209
const DenseIterator< Type > operator-(const DenseIterator< Type > &it, ptrdiff_t inc)
Subtraction between a DenseIterator and an integral value.
Definition: DenseIterator.h:585
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:239
SelectType< IsExpression< VT1 >::value, const VT1, const VT1 & >::Type LeftOperand
Composite type of the left-hand side dense vector expression.
Definition: DVecSVecAddExpr.h:130
DVecSVecAddExpr(const VT1 &lhs, const VT2 &rhs)
Constructor for the DVecSVecAddExpr class.
Definition: DVecSVecAddExpr.h:150
const SelectType< returnExpr, ExprReturnType, ElementType >::Type ReturnType
Return type for expression template evaluations.
Definition: DVecSVecAddExpr.h:124
size_t size() const
Returns the current size/dimension of the vector.
Definition: DVecSVecAddExpr.h:175
Header file for the IsDenseVector type trait.
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type...
Definition: DenseVector.h:79
AddTrait< RT1, RT2 >::Type ResultType
Result type for expression template evaluations.
Definition: DVecSVecAddExpr.h:119
Header file for the IsComputation type trait class.
Base class for all compute expression templates.The Computation class serves as a tag for all computa...
Definition: Computation.h:59
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: DVecSVecAddExpr.h:164
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
#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
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2370
Header file for basic type definitions.
Header file for the SubvectorExprTrait class template.
SelectType< IsExpression< VT2 >::value, const VT2, const VT2 & >::Type RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: DVecSVecAddExpr.h:133
VT1::ReturnType RN1
Return type of the left-hand side dense vector expression.
Definition: DVecSVecAddExpr.h:95
Header file for the IsColumnVector type trait.
Header file for the SubExprTrait class template.
#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:238
#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.