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>
47 #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  //**********************************************************************************************
125 
126  //**Constructor*********************************************************************************
132  explicit inline DVecSVecAddExpr( const VT1& lhs, const VT2& rhs )
133  : lhs_( lhs ) // Left-hand side dense vector of the addition expression
134  , rhs_( rhs ) // Right-hand side sparse vector of the addition expression
135  {
136  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
137  }
138  //**********************************************************************************************
139 
140  //**Subscript operator**************************************************************************
146  inline ReturnType operator[]( size_t index ) const {
147  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
148  return lhs_[index] + rhs_[index];
149  }
150  //**********************************************************************************************
151 
152  //**Size function*******************************************************************************
157  inline size_t size() const {
158  return lhs_.size();
159  }
160  //**********************************************************************************************
161 
162  //**Left operand access*************************************************************************
167  inline LeftOperand leftOperand() const {
168  return lhs_;
169  }
170  //**********************************************************************************************
171 
172  //**Right operand access************************************************************************
177  inline RightOperand rightOperand() const {
178  return rhs_;
179  }
180  //**********************************************************************************************
181 
182  //**********************************************************************************************
188  template< typename T >
189  inline bool canAlias( const T* alias ) const {
190  return ( IsComputation<VT1>::value && lhs_.canAlias( alias ) ) ||
191  ( rhs_.canAlias( alias ) );
192  }
193  //**********************************************************************************************
194 
195  //**********************************************************************************************
201  template< typename T >
202  inline bool isAliased( const T* alias ) const {
203  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
204  }
205  //**********************************************************************************************
206 
207  private:
208  //**Member variables****************************************************************************
211  //**********************************************************************************************
212 
213  //**Assignment to dense vectors*****************************************************************
225  template< typename VT > // Type of the target dense vector
226  friend inline void assign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
227  {
229 
230  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
231 
232  if( !IsComputation<VT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
233  addAssign( ~lhs, rhs.rhs_ );
234  }
235  else {
236  assign ( ~lhs, rhs.lhs_ );
237  addAssign( ~lhs, rhs.rhs_ );
238  }
239  }
241  //**********************************************************************************************
242 
243  //**Assignment to sparse vectors****************************************************************
255  template< typename VT > // Type of the target sparse vector
256  friend inline void assign( SparseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
257  {
259 
262  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
263 
264  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
265 
266  const ResultType tmp( rhs );
267  assign( ~lhs, tmp );
268  }
270  //**********************************************************************************************
271 
272  //**Addition assignment to dense vectors********************************************************
284  template< typename VT > // Type of the target dense vector
285  friend inline void addAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
286  {
288 
289  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
290 
291  addAssign( ~lhs, rhs.lhs_ );
292  addAssign( ~lhs, rhs.rhs_ );
293  }
295  //**********************************************************************************************
296 
297  //**Addition assignment to sparse vectors*******************************************************
298  // No special implementation for the addition assignment to sparse vectors.
299  //**********************************************************************************************
300 
301  //**Subtraction assignment to dense vectors*****************************************************
313  template< typename VT > // Type of the target dense vector
314  friend inline void subAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
315  {
317 
318  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
319 
320  subAssign( ~lhs, rhs.lhs_ );
321  subAssign( ~lhs, rhs.rhs_ );
322  }
324  //**********************************************************************************************
325 
326  //**Subtraction assignment to sparse vectors****************************************************
327  // No special implementation for the subtraction assignment to sparse vectors.
328  //**********************************************************************************************
329 
330  //**Multiplication assignment to dense vectors**************************************************
342  template< typename VT > // Type of the target dense vector
343  friend inline void multAssign( DenseVector<VT,TF>& lhs, const DVecSVecAddExpr& rhs )
344  {
346 
349  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
350 
351  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
352 
353  const ResultType tmp( rhs );
354  multAssign( ~lhs, tmp );
355  }
357  //**********************************************************************************************
358 
359  //**Multiplication assignment to sparse vectors*************************************************
360  // No special implementation for the multiplication assignment to sparse vectors.
361  //**********************************************************************************************
362 
363  //**Compile time checks*************************************************************************
370  //**********************************************************************************************
371 };
372 //*************************************************************************************************
373 
374 
375 
376 
377 //=================================================================================================
378 //
379 // GLOBAL BINARY ARITHMETIC OPERATORS
380 //
381 //=================================================================================================
382 
383 //*************************************************************************************************
409 template< typename T1 // Type of the left-hand side dense vector
410  , typename T2 // Type of the right-hand side sparse vector
411  , bool TF > // Transpose flag
412 inline const DVecSVecAddExpr<T1,T2,TF>
414 {
416 
417  if( (~lhs).size() != (~rhs).size() )
418  throw std::invalid_argument( "Vector sizes do not match" );
419 
420  return DVecSVecAddExpr<T1,T2,TF>( ~lhs, ~rhs );
421 }
422 //*************************************************************************************************
423 
424 
425 //*************************************************************************************************
451 template< typename T1 // Type of the left-hand side sparse vector
452  , typename T2 // Type of the right-hand side dense vector
453  , bool TF > // Transpose flag
454 inline const DVecSVecAddExpr<T2,T1,TF>
456 {
458 
459  if( (~lhs).size() != (~rhs).size() )
460  throw std::invalid_argument( "Vector sizes do not match" );
461 
462  return DVecSVecAddExpr<T2,T1,TF>( ~rhs, ~lhs );
463 }
464 //*************************************************************************************************
465 
466 
467 
468 
469 //=================================================================================================
470 //
471 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
472 //
473 //=================================================================================================
474 
475 //*************************************************************************************************
488 template< typename T1 // Type of the dense vector of the left-hand side expression
489  , typename T2 // Type of the sparse vector of the left-hand side expression
490  , bool TF // Transpose flag of the left-hand side expression
491  , typename T3 > // Type of the right-hand side dense vector
492 inline const typename AddExprTrait< DVecSVecAddExpr<T1,T2,TF>, T3 >::Type
493  operator+( const DVecSVecAddExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
494 {
496 
497  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
498 }
500 //*************************************************************************************************
501 
502 
503 //*************************************************************************************************
516 template< typename T1 // Type of the dense vector of the left-hand side expression
517  , typename T2 // Type of the sparse vector of the left-hand side expression
518  , bool TF // Transpose flag of the left-hand side expression
519  , typename T3 > // Type of the right-hand side dense vector
520 inline const typename SubExprTrait< DVecSVecAddExpr<T1,T2,TF>, T3 >::Type
521  operator-( const DVecSVecAddExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
522 {
524 
525  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
526 }
528 //*************************************************************************************************
529 
530 
531 
532 
533 //=================================================================================================
534 //
535 // EXPRESSION TRAIT SPECIALIZATIONS
536 //
537 //=================================================================================================
538 
539 //*************************************************************************************************
541 template< typename VT1, typename VT2, typename VT3 >
542 struct DVecDVecAddExprTrait< DVecSVecAddExpr<VT1,VT2,false>, VT3 >
543 {
544  public:
545  //**********************************************************************************************
547  typedef typename SelectType< IsDenseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
548  IsSparseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
549  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
550  , typename DVecSVecAddExprTrait< typename DVecDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
551  , INVALID_TYPE >::Type Type;
553  //**********************************************************************************************
554 };
556 //*************************************************************************************************
557 
558 
559 //*************************************************************************************************
561 template< typename VT1, typename VT2, typename VT3 >
562 struct TDVecTDVecAddExprTrait< DVecSVecAddExpr<VT1,VT2,true>, VT3 >
563 {
564  public:
565  //**********************************************************************************************
567  typedef typename SelectType< IsDenseVector<VT1>::value && IsTransposeVector<VT1>::value &&
568  IsSparseVector<VT2>::value && IsTransposeVector<VT2>::value &&
569  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
570  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
571  , INVALID_TYPE >::Type Type;
573  //**********************************************************************************************
574 };
576 //*************************************************************************************************
577 
578 
579 //*************************************************************************************************
581 template< typename VT1, typename VT2, typename VT3 >
582 struct DVecDVecSubExprTrait< DVecSVecAddExpr<VT1,VT2,false>, VT3 >
583 {
584  public:
585  //**********************************************************************************************
587  typedef typename SelectType< IsDenseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
588  IsSparseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
589  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
590  , typename DVecSVecAddExprTrait< typename DVecDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
591  , INVALID_TYPE >::Type Type;
593  //**********************************************************************************************
594 };
596 //*************************************************************************************************
597 
598 
599 //*************************************************************************************************
601 template< typename VT1, typename VT2, typename VT3 >
602 struct TDVecTDVecSubExprTrait< DVecSVecAddExpr<VT1,VT2,true>, VT3 >
603 {
604  public:
605  //**********************************************************************************************
607  typedef typename SelectType< IsDenseVector<VT1>::value && IsTransposeVector<VT1>::value &&
608  IsSparseVector<VT2>::value && IsTransposeVector<VT2>::value &&
609  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
610  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
611  , INVALID_TYPE >::Type Type;
613  //**********************************************************************************************
614 };
616 //*************************************************************************************************
617 
618 } // namespace blaze
619 
620 #endif