All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DVecSVecSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECSVECSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DVECSVECSUBEXPR_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 DVECSVECSUBEXPR
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 DVecSVecSubExpr : public DenseVector< DVecSVecSubExpr<VT1,VT2,TF>, TF >
73  , private VecVecSubExpr
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 DVecSVecSubExpr( const VT1& lhs, const VT2& rhs )
133  : lhs_( lhs ) // Left-hand side dense vector of the subtraction expression
134  , rhs_( rhs ) // Right-hand side sparse vector of the subtraction 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 DVecSVecSubExpr& 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  subAssign( ~lhs, rhs.rhs_ );
234  }
235  else {
236  assign ( ~lhs, rhs.lhs_ );
237  subAssign( ~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 DVecSVecSubExpr& 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 DVecSVecSubExpr& rhs )
286  {
288 
289  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
290 
291  addAssign( ~lhs, rhs.lhs_ );
292  subAssign( ~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 DVecSVecSubExpr& rhs )
315  {
317 
318  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
319 
320  subAssign( ~lhs, rhs.lhs_ );
321  addAssign( ~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 DVecSVecSubExpr& 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 DVecSVecSubExpr<T1,T2,TF>
414 {
416 
417  if( (~lhs).size() != (~rhs).size() )
418  throw std::invalid_argument( "Vector sizes do not match" );
419 
420  return DVecSVecSubExpr<T1,T2,TF>( ~lhs, ~rhs );
421 }
422 //*************************************************************************************************
423 
424 
425 
426 
427 //=================================================================================================
428 //
429 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
430 //
431 //=================================================================================================
432 
433 //*************************************************************************************************
446 template< typename T1 // Type of the dense vector of the left-hand side expression
447  , typename T2 // Type of the sparse vector of the left-hand side expression
448  , bool TF // Transpose flag of the left-hand side expression
449  , typename T3 > // Type of the right-hand side dense vector
450 inline const typename AddExprTrait< DVecSVecSubExpr<T1,T2,TF>, T3 >::Type
451  operator+( const DVecSVecSubExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
452 {
454 
455  return ( lhs.leftOperand() + (~rhs) ) - lhs.rightOperand();
456 }
458 //*************************************************************************************************
459 
460 
461 //*************************************************************************************************
474 template< typename T1 // Type of the dense vector of the left-hand side expression
475  , typename T2 // Type of the sparse vector of the left-hand side expression
476  , bool TF // Transpose flag of the left-hand side expression
477  , typename T3 > // Type of the right-hand side dense vector
478 inline const typename SubExprTrait< DVecSVecSubExpr<T1,T2,TF>, T3 >::Type
479  operator-( const DVecSVecSubExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
480 {
482 
483  return ( lhs.leftOperand() - (~rhs) ) - lhs.rightOperand();
484 }
486 //*************************************************************************************************
487 
488 
489 
490 
491 //=================================================================================================
492 //
493 // EXPRESSION TRAIT SPECIALIZATIONS
494 //
495 //=================================================================================================
496 
497 //*************************************************************************************************
499 template< typename VT1, typename VT2, typename VT3 >
500 struct DVecDVecAddExprTrait< DVecSVecSubExpr<VT1,VT2,false>, VT3 >
501 {
502  public:
503  //**********************************************************************************************
505  typedef typename SelectType< IsDenseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
506  IsSparseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
507  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
508  , typename DVecSVecSubExprTrait< typename DVecDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
509  , INVALID_TYPE >::Type Type;
511  //**********************************************************************************************
512 };
514 //*************************************************************************************************
515 
516 
517 //*************************************************************************************************
519 template< typename VT1, typename VT2, typename VT3 >
520 struct TDVecTDVecAddExprTrait< DVecSVecSubExpr<VT1,VT2,true>, VT3 >
521 {
522  public:
523  //**********************************************************************************************
525  typedef typename SelectType< IsDenseVector<VT1>::value && IsTransposeVector<VT1>::value &&
526  IsSparseVector<VT2>::value && IsTransposeVector<VT2>::value &&
527  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
528  , typename TDVecTSVecSubExprTrait< typename TDVecTDVecAddExprTrait<VT1,VT3>::Type, VT2 >::Type
529  , INVALID_TYPE >::Type Type;
531  //**********************************************************************************************
532 };
534 //*************************************************************************************************
535 
536 
537 //*************************************************************************************************
539 template< typename VT1, typename VT2, typename VT3 >
540 struct DVecDVecSubExprTrait< DVecSVecSubExpr<VT1,VT2,false>, VT3 >
541 {
542  public:
543  //**********************************************************************************************
545  typedef typename SelectType< IsDenseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
546  IsSparseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
547  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
548  , typename DVecSVecSubExprTrait< typename DVecDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
549  , INVALID_TYPE >::Type Type;
551  //**********************************************************************************************
552 };
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
559 template< typename VT1, typename VT2, typename VT3 >
560 struct TDVecTDVecSubExprTrait< DVecSVecSubExpr<VT1,VT2,true>, VT3 >
561 {
562  public:
563  //**********************************************************************************************
565  typedef typename SelectType< IsDenseVector<VT1>::value && IsTransposeVector<VT1>::value &&
566  IsSparseVector<VT2>::value && IsTransposeVector<VT2>::value &&
567  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
568  , typename TDVecTSVecSubExprTrait< typename TDVecTDVecSubExprTrait<VT1,VT3>::Type, VT2 >::Type
569  , INVALID_TYPE >::Type Type;
571  //**********************************************************************************************
572 };
574 //*************************************************************************************************
575 
576 } // namespace blaze
577 
578 #endif