All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DVecSVecAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECSVECADDEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DVECSVECADDEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
48 #include <blaze/util/Assert.h>
50 #include <blaze/util/SelectType.h>
51 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // CLASS DVECSVECADDEXPR
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
69 template< typename VT1 // Type of the left-hand side dense vector
70  , typename VT2 // Type of the right-hand side sparse vector
71  , bool TF > // Transpose flag
72 class DVecSVecAddExpr : public DenseVector< DVecSVecAddExpr<VT1,VT2,TF>, TF >
73  , private Expression
74  , private Computation
75 {
76  private:
77  //**Type definitions****************************************************************************
78  typedef typename VT1::ResultType RT1;
79  typedef typename VT2::ResultType RT2;
80  typedef typename VT1::ReturnType RN1;
81  typedef typename VT2::ReturnType RN2;
82  typedef typename VT1::CompositeType CT1;
83  typedef typename VT2::CompositeType CT2;
84  typedef typename VT1::TransposeType TT1;
85  typedef typename VT2::TransposeType TT2;
86  //**********************************************************************************************
87 
88  //**Return type evaluation**********************************************************************
90 
95  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
96 
99  //**********************************************************************************************
100 
101  public:
102  //**Type definitions****************************************************************************
105  typedef typename ResultType::TransposeType TransposeType;
106  typedef typename ResultType::ElementType ElementType;
107 
110 
112  typedef const ResultType CompositeType;
113 
115  typedef typename SelectType< IsExpression<VT1>::value, const VT1, const VT1& >::Type LeftOperand;
116 
118  typedef typename SelectType< IsExpression<VT2>::value, const VT2, const VT2& >::Type RightOperand;
119  //**********************************************************************************************
120 
121  //**Compilation flags***************************************************************************
123  enum { vectorizable = 0 };
124 
126  enum { canAlias = ( IsComputation<VT1>::value && CanAlias<VT1>::value ) };
127  //**********************************************************************************************
128 
129  //**Constructor*********************************************************************************
135  explicit inline DVecSVecAddExpr( const VT1& lhs, const VT2& rhs )
136  : lhs_( lhs ) // Left-hand side dense vector of the addition expression
137  , rhs_( rhs ) // Right-hand side sparse vector of the addition expression
138  {
139  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
140  }
141  //**********************************************************************************************
142 
143  //**Subscript operator**************************************************************************
149  inline ReturnType operator[]( size_t index ) const {
150  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
151  return lhs_[index] + rhs_[index];
152  }
153  //**********************************************************************************************
154 
155  //**Size function*******************************************************************************
160  inline size_t size() const {
161  return lhs_.size();
162  }
163  //**********************************************************************************************
164 
165  //**Left operand access*************************************************************************
170  inline LeftOperand leftOperand() const {
171  return lhs_;
172  }
173  //**********************************************************************************************
174 
175  //**Right operand access************************************************************************
180  inline RightOperand rightOperand() const {
181  return rhs_;
182  }
183  //**********************************************************************************************
184 
185  //**********************************************************************************************
191  template< typename T >
192  inline bool isAliased( const T* alias ) const {
193  return ( IsComputation<VT1>::value && CanAlias<VT1>::value && lhs_.isAliased( alias ) );
194  }
195  //**********************************************************************************************
196 
197  private:
198  //**Member variables****************************************************************************
201  //**********************************************************************************************
202 
203  //**Assignment to dense vectors*****************************************************************
215  template< typename VT > // Type of the target dense vector
216  friend inline void assign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
217  {
218  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
219 
220  assign ( ~lhs, rhs.lhs_ );
221  addAssign( ~lhs, rhs.rhs_ );
222  }
224  //**********************************************************************************************
225 
226  //**Assignment to sparse vectors****************************************************************
238  template< typename VT > // Type of the target sparse vector
239  friend inline void assign( SparseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
240  {
243  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
244 
245  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
246 
247  const ResultType tmp( rhs );
248  assign( ~lhs, tmp );
249  }
251  //**********************************************************************************************
252 
253  //**Addition assignment to dense vectors********************************************************
265  template< typename VT > // Type of the target dense vector
266  friend inline void addAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
267  {
268  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
269 
270  addAssign( ~lhs, rhs.lhs_ );
271  addAssign( ~lhs, rhs.rhs_ );
272  }
274  //**********************************************************************************************
275 
276  //**Addition assignment to sparse vectors*******************************************************
277  // No special implementation for the addition assignment to sparse vectors.
278  //**********************************************************************************************
279 
280  //**Subtraction assignment to dense vectors*****************************************************
292  template< typename VT > // Type of the target dense vector
293  friend inline void subAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
294  {
295  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
296 
297  subAssign( ~lhs, rhs.lhs_ );
298  subAssign( ~lhs, rhs.rhs_ );
299  }
301  //**********************************************************************************************
302 
303  //**Subtraction assignment to sparse vectors****************************************************
304  // No special implementation for the subtraction assignment to sparse vectors.
305  //**********************************************************************************************
306 
307  //**Multiplication assignment to dense vectors**************************************************
319  template< typename VT > // Type of the target dense vector
320  friend inline void multAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
321  {
324  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
325 
326  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
327 
328  const ResultType tmp( rhs );
329  multAssign( ~lhs, tmp );
330  }
332  //**********************************************************************************************
333 
334  //**Multiplication assignment to sparse vectors*************************************************
335  // No special implementation for the multiplication assignment to sparse vectors.
336  //**********************************************************************************************
337 
338  //**Compile time checks*************************************************************************
345  //**********************************************************************************************
346 };
347 //*************************************************************************************************
348 
349 
350 
351 
352 //=================================================================================================
353 //
354 // GLOBAL BINARY ARITHMETIC OPERATORS
355 //
356 //=================================================================================================
357 
358 //*************************************************************************************************
384 template< typename T1 // Type of the left-hand side dense vector
385  , typename T2 // Type of the right-hand side sparse vector
386  , bool TF > // Transpose flag
387 inline const DVecSVecAddExpr<T1,T2,TF>
389 {
390  if( (~lhs).size() != (~rhs).size() )
391  throw std::invalid_argument( "Vector sizes do not match" );
392 
393  return DVecSVecAddExpr<T1,T2,TF>( ~lhs, ~rhs );
394 }
395 //*************************************************************************************************
396 
397 
398 //*************************************************************************************************
424 template< typename T1 // Type of the left-hand side sparse vector
425  , typename T2 // Type of the right-hand side dense vector
426  , bool TF > // Transpose flag
427 inline const DVecSVecAddExpr<T2,T1,TF>
429 {
430  if( (~lhs).size() != (~rhs).size() )
431  throw std::invalid_argument( "Vector sizes do not match" );
432 
433  return DVecSVecAddExpr<T2,T1,TF>( ~rhs, ~lhs );
434 }
435 //*************************************************************************************************
436 
437 
438 
439 
440 //=================================================================================================
441 //
442 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
443 //
444 //=================================================================================================
445 
446 //*************************************************************************************************
459 template< typename T1 // Type of the dense vector of the left-hand side expression
460  , typename T2 // Type of the sparse vector of the left-hand side expression
461  , bool TF // Transpose flag of the left-hand side expression
462  , typename T3 > // Type of the right-hand side dense vector
463 inline const typename AddExprTrait< DVecSVecAddExpr<T1,T2,TF>, T3 >::Type
464  operator+( const DVecSVecAddExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
465 {
466  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
467 }
469 //*************************************************************************************************
470 
471 
472 //*************************************************************************************************
485 template< typename T1 // Type of the dense vector of the left-hand side expression
486  , typename T2 // Type of the sparse vector of the left-hand side expression
487  , bool TF // Transpose flag of the left-hand side expression
488  , typename T3 > // Type of the right-hand side dense vector
489 inline const typename SubExprTrait< DVecSVecAddExpr<T1,T2,TF>, T3 >::Type
490  operator-( const DVecSVecAddExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
491 {
492  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
493 }
495 //*************************************************************************************************
496 
497 
498 
499 
500 //=================================================================================================
501 //
502 // EXPRESSION TRAIT SPECIALIZATIONS
503 //
504 //=================================================================================================
505 
506 //*************************************************************************************************
508 template< typename VT1, typename VT2, typename VT3 >
509 struct DVecDVecAddExprTrait< DVecSVecAddExpr<VT1,VT2,false>, VT3 >
510 {
511  public:
512  //**********************************************************************************************
514  typedef typename SelectType< IsDenseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
515  IsSparseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
516  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
517  , typename DVecSVecAddExprTrait< typename DVecDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
518  , INVALID_TYPE >::Type Type;
520  //**********************************************************************************************
521 };
523 //*************************************************************************************************
524 
525 
526 //*************************************************************************************************
528 template< typename VT1, typename VT2, typename VT3 >
529 struct TDVecTDVecAddExprTrait< DVecSVecAddExpr<VT1,VT2,true>, VT3 >
530 {
531  public:
532  //**********************************************************************************************
534  typedef typename SelectType< IsDenseVector<VT1>::value && IsTransposeVector<VT1>::value &&
535  IsSparseVector<VT2>::value && IsTransposeVector<VT2>::value &&
536  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
537  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
538  , INVALID_TYPE >::Type Type;
540  //**********************************************************************************************
541 };
543 //*************************************************************************************************
544 
545 
546 //*************************************************************************************************
548 template< typename VT1, typename VT2, typename VT3 >
549 struct DVecDVecSubExprTrait< DVecSVecAddExpr<VT1,VT2,false>, VT3 >
550 {
551  public:
552  //**********************************************************************************************
554  typedef typename SelectType< IsDenseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
555  IsSparseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
556  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
557  , typename DVecSVecAddExprTrait< typename DVecDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
558  , INVALID_TYPE >::Type Type;
560  //**********************************************************************************************
561 };
563 //*************************************************************************************************
564 
565 
566 //*************************************************************************************************
568 template< typename VT1, typename VT2, typename VT3 >
569 struct TDVecTDVecSubExprTrait< DVecSVecAddExpr<VT1,VT2,true>, VT3 >
570 {
571  public:
572  //**********************************************************************************************
574  typedef typename SelectType< IsDenseVector<VT1>::value && IsTransposeVector<VT1>::value &&
575  IsSparseVector<VT2>::value && IsTransposeVector<VT2>::value &&
576  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
577  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
578  , INVALID_TYPE >::Type Type;
580  //**********************************************************************************************
581 };
583 //*************************************************************************************************
584 
585 } // namespace blaze
586 
587 #endif